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

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() { \
1.1.1.58! root     1577:                                        if(params[0][0] == 'R' || params[0][0] == 'r') { \
1.1.1.33  root     1578:                                                break_point_ptr = &rd_break_point; \
1.1.1.58! root     1579:                                        } else if(params[0][0] == 'W' || params[0][0] == 'w') { \
1.1.1.33  root     1580:                                                break_point_ptr = &wr_break_point; \
1.1.1.58! root     1581:                                        } else if(params[0][0] == 'I' || params[0][0] == 'i') { \
1.1.1.33  root     1582:                                                break_point_ptr = &in_break_point; \
1.1.1.58! root     1583:                                        } else if(params[0][0] == 'O' || params[0][0] == 'o') { \
1.1.1.33  root     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) {
1.1.1.58! root     1591:                                        UINT32 seg = 0;
        !          1592:                                        if(params[0][0] == 'R' || params[0][0] == 'r' || params[0][0] == 'W' || params[0][0] == 'w') {
        !          1593:                                                seg = debugger_get_seg(params[1], data_seg);
        !          1594:                                        } else {
        !          1595:                                                seg = debugger_get_seg(params[1], SREG(CS));
        !          1596:                                        }
1.1.1.33  root     1597:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1598:                                        bool found = false;
                   1599:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1600:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1601:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1602:                                                        break_point_ptr->table[i].seg = seg;
                   1603:                                                        break_point_ptr->table[i].ofs = ofs;
                   1604:                                                        break_point_ptr->table[i].status = 1;
                   1605:                                                        found = true;
                   1606:                                                }
                   1607:                                        }
                   1608:                                        if(!found) {
                   1609:                                                telnet_printf("too many break points\n");
                   1610:                                        }
                   1611:                                } else {
                   1612:                                        telnet_printf("invalid parameter number\n");
                   1613:                                }
                   1614:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1615:                                break_point_t *break_point_ptr;
                   1616:                                GET_BREAK_POINT_PTR();
                   1617:                                if(num == 2) {
                   1618:                                        UINT32 addr = debugger_get_val(params[1]);
                   1619:                                        bool found = false;
                   1620:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1621:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1622:                                                        break_point_ptr->table[i].addr = addr;
                   1623:                                                        break_point_ptr->table[i].status = 1;
                   1624:                                                        found = true;
                   1625:                                                }
                   1626:                                        }
                   1627:                                        if(!found) {
                   1628:                                                telnet_printf("too many break points\n");
                   1629:                                        }
                   1630:                                } else {
                   1631:                                        telnet_printf("invalid parameter number\n");
                   1632:                                }
                   1633:                        } 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) {
                   1634:                                break_point_t *break_point_ptr;
                   1635:                                GET_BREAK_POINT_PTR();
                   1636:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1637:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1638:                                } else if(num >= 2) {
                   1639:                                        for(int i = 1; i < num; i++) {
                   1640:                                                int index = debugger_get_val(params[i]);
                   1641:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1642:                                                        telnet_printf("invalid index %x\n", index);
                   1643:                                                } else {
                   1644:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1645:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1646:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1647:                                                        break_point_ptr->table[index - 1].status = 0;
                   1648:                                                }
                   1649:                                        }
                   1650:                                } else {
                   1651:                                        telnet_printf("invalid parameter number\n");
                   1652:                                }
                   1653:                        } 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 ||
                   1654:                                  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) {
                   1655:                                break_point_t *break_point_ptr;
                   1656:                                GET_BREAK_POINT_PTR();
                   1657:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1658:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1659:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1660:                                                if(break_point_ptr->table[i].status != 0) {
                   1661:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1662:                                                }
                   1663:                                        }
                   1664:                                } else if(num >= 2) {
                   1665:                                        for(int i = 1; i < num; i++) {
                   1666:                                                int index = debugger_get_val(params[i]);
                   1667:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1668:                                                        telnet_printf("invalid index %x\n", index);
                   1669:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1670:                                                        telnet_printf("break point %x is null\n", index);
                   1671:                                                } else {
                   1672:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1673:                                                }
                   1674:                                        }
                   1675:                                } else {
                   1676:                                        telnet_printf("invalid parameter number\n");
                   1677:                                }
                   1678:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1679:                                break_point_t *break_point_ptr;
                   1680:                                GET_BREAK_POINT_PTR();
                   1681:                                if(num == 1) {
                   1682:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1683:                                                if(break_point_ptr->table[i].status) {
                   1684:                                                        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);
                   1685:                                                }
                   1686:                                        }
                   1687:                                } else {
                   1688:                                        telnet_printf("invalid parameter number\n");
                   1689:                                }
                   1690:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1691:                                break_point_t *break_point_ptr;
                   1692:                                GET_BREAK_POINT_PTR();
                   1693:                                if(num == 1) {
                   1694:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1695:                                                if(break_point_ptr->table[i].status) {
                   1696:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1697:                                                }
                   1698:                                        }
                   1699:                                } else {
                   1700:                                        telnet_printf("invalid parameter number\n");
                   1701:                                }
                   1702:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1703:                                if(num >= 2 && num <= 4) {
                   1704:                                        int int_num = debugger_get_val(params[1]);
                   1705:                                        UINT8 ah = 0, ah_registered = 0;
                   1706:                                        UINT8 al = 0, al_registered = 0;
                   1707:                                        if(num >= 3) {
                   1708:                                                ah = debugger_get_val(params[2]);
                   1709:                                                ah_registered = 1;
                   1710:                                        }
                   1711:                                        if(num == 4) {
                   1712:                                                al = debugger_get_val(params[3]);
                   1713:                                                al_registered = 1;
                   1714:                                        }
                   1715:                                        bool found = false;
                   1716:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1717:                                                if(int_break_point.table[i].status == 0 || (
                   1718:                                                   int_break_point.table[i].int_num == int_num &&
                   1719:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1720:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1721:                                                        int_break_point.table[i].int_num = int_num;
                   1722:                                                        int_break_point.table[i].ah = ah;
                   1723:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1724:                                                        int_break_point.table[i].al = al;
                   1725:                                                        int_break_point.table[i].al_registered = al_registered;
                   1726:                                                        int_break_point.table[i].status = 1;
                   1727:                                                        found = true;
                   1728:                                                }
                   1729:                                        }
                   1730:                                        if(!found) {
                   1731:                                                telnet_printf("too many break points\n");
                   1732:                                        }
                   1733:                                } else {
                   1734:                                        telnet_printf("invalid parameter number\n");
                   1735:                                }
                   1736:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1737:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1738:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1739:                                } else if(num >= 2) {
                   1740:                                        for(int i = 1; i < num; i++) {
                   1741:                                                int index = debugger_get_val(params[i]);
                   1742:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1743:                                                        telnet_printf("invalid index %x\n", index);
                   1744:                                                } else {
                   1745:                                                        int_break_point.table[index - 1].int_num = 0;
                   1746:                                                        int_break_point.table[index - 1].ah = 0;
                   1747:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1748:                                                        int_break_point.table[index - 1].al = 0;
                   1749:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1750:                                                        int_break_point.table[index - 1].status = 0;
                   1751:                                                }
                   1752:                                        }
                   1753:                                } else {
                   1754:                                        telnet_printf("invalid parameter number\n");
                   1755:                                }
                   1756:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1757:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1758:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1759:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1760:                                                if(int_break_point.table[i].status != 0) {
                   1761:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1762:                                                }
                   1763:                                        }
                   1764:                                } else if(num >= 2) {
                   1765:                                        for(int i = 1; i < num; i++) {
                   1766:                                                int index = debugger_get_val(params[i]);
                   1767:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1768:                                                        telnet_printf("invalid index %x\n", index);
                   1769:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1770:                                                        telnet_printf("break point %x is null\n", index);
                   1771:                                                } else {
                   1772:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1773:                                                }
                   1774:                                        }
                   1775:                                } else {
                   1776:                                        telnet_printf("invalid parameter number\n");
                   1777:                                }
                   1778:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1779:                                if(num == 1) {
                   1780:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1781:                                                if(int_break_point.table[i].status) {
                   1782:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1783:                                                        if(int_break_point.table[i].ah_registered) {
                   1784:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1785:                                                        }
                   1786:                                                        if(int_break_point.table[i].al_registered) {
                   1787:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1788:                                                        }
                   1789:                                                        telnet_printf("\n");
                   1790:                                                }
                   1791:                                        }
                   1792:                                } else {
                   1793:                                        telnet_printf("invalid parameter number\n");
                   1794:                                }
                   1795:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1796:                                if(num == 1 || num == 2) {
                   1797:                                        break_point_t break_point_stored;
                   1798:                                        bool break_points_stored = false;
                   1799:                                        
                   1800:                                        if(stricmp(params[0], "P") == 0) {
                   1801:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1802:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1803:                                                break_points_stored = true;
                   1804:                                                
                   1805:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1806:                                                break_point.table[0].status = 1;
                   1807:                                        } else if(num >= 2) {
                   1808:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1809:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1810:                                                break_points_stored = true;
                   1811:                                                
                   1812:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1813:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1814:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1815:                                                break_point.table[0].seg = seg;
                   1816:                                                break_point.table[0].ofs = ofs;
                   1817:                                                break_point.table[0].status = 1;
                   1818:                                        }
                   1819:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1820:                                        now_going = true;
                   1821:                                        now_suspended = false;
                   1822:                                        
                   1823:                                        telnet_command("\033[2l"); // key unlock
1.1.1.54  root     1824:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1825:                                                if(telnet_kbhit()) {
                   1826:                                                        break;
                   1827:                                                }
                   1828:                                                Sleep(10);
                   1829:                                        }
                   1830:                                        now_going = false;
                   1831:                                        telnet_command("\033[2h"); // key lock
                   1832:                                        
                   1833:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1834:                                                Sleep(100);
1.1.1.54  root     1835:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1836:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1837:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1838:                                                }
                   1839:                                        }
1.1.1.54  root     1840:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1841:                                                if(telnet_disconnected()) {
                   1842:                                                        break;
                   1843:                                                }
                   1844:                                                Sleep(10);
                   1845:                                        }
                   1846:                                        dasm_seg = SREG(CS);
                   1847:                                        dasm_ofs = m_eip;
                   1848:                                        
                   1849:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1850:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1851:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1852:                                        
                   1853:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1854:                                        debugger_regs_info(buffer);
                   1855:                                        telnet_printf("%s", buffer);
                   1856:                                        
                   1857:                                        if(break_point.hit) {
                   1858:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1859:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1860:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1861:                                                }
                   1862:                                        } else if(rd_break_point.hit) {
                   1863:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1864:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1865:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1866:                                                m_prev_cs, m_prev_eip);
                   1867:                                        } else if(wr_break_point.hit) {
                   1868:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1869:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1870:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1871:                                                m_prev_cs, m_prev_eip);
                   1872:                                        } else if(in_break_point.hit) {
                   1873:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1874:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1875:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1876:                                                m_prev_cs, m_prev_eip);
                   1877:                                        } else if(out_break_point.hit) {
                   1878:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1879:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1880:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1881:                                                m_prev_cs, m_prev_eip);
                   1882:                                        } else if(int_break_point.hit) {
                   1883:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1884:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1885:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1886:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1887:                                                }
                   1888:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1889:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1890:                                                }
                   1891:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1892:                                        } else {
                   1893:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1894:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1895:                                        }
                   1896:                                        if(break_points_stored) {
                   1897:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1898:                                        }
                   1899:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1900:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1901:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1902:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1903:                                } else {
                   1904:                                        telnet_printf("invalid parameter number\n");
                   1905:                                }
                   1906:                        } else if(stricmp(params[0], "T") == 0) {
                   1907:                                if(num == 1 || num == 2) {
                   1908:                                        int steps = 1;
                   1909:                                        if(num >= 2) {
                   1910:                                                steps = debugger_get_val(params[1]);
                   1911:                                        }
                   1912:                                        
                   1913:                                        telnet_command("\033[2l"); // key unlock
                   1914:                                        while(steps-- > 0) {
                   1915:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1916:                                                now_going = false;
                   1917:                                                now_suspended = false;
                   1918:                                                
1.1.1.54  root     1919:                                                while(!m_exit && !now_suspended) {
1.1.1.33  root     1920:                                                        if(telnet_disconnected()) {
                   1921:                                                                break;
                   1922:                                                        }
                   1923:                                                        Sleep(10);
                   1924:                                                }
                   1925:                                                dasm_seg = SREG(CS);
                   1926:                                                dasm_ofs = m_eip;
                   1927:                                                
                   1928:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1929:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1930:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1931:                                                
                   1932:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1933:                                                debugger_regs_info(buffer);
                   1934:                                                telnet_printf("%s", buffer);
                   1935:                                                
                   1936:                                                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()) {
                   1937:                                                        break;
                   1938:                                                }
                   1939:                                        }
                   1940:                                        telnet_command("\033[2h"); // key lock
                   1941:                                        
                   1942:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1943:                                                Sleep(100);
1.1.1.54  root     1944:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1945:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1946:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1947:                                                }
                   1948:                                        }
1.1.1.54  root     1949:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1950:                                                if(telnet_disconnected()) {
                   1951:                                                        break;
                   1952:                                                }
                   1953:                                                Sleep(10);
                   1954:                                        }
                   1955:                                        if(break_point.hit) {
                   1956:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1957:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1958:                                        } else if(rd_break_point.hit) {
                   1959:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1960:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1961:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1962:                                                m_prev_cs, m_prev_eip);
                   1963:                                        } else if(wr_break_point.hit) {
                   1964:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1965:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1966:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1967:                                                m_prev_cs, m_prev_eip);
                   1968:                                        } else if(in_break_point.hit) {
                   1969:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1970:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1971:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1972:                                                m_prev_cs, m_prev_eip);
                   1973:                                        } else if(out_break_point.hit) {
                   1974:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1975:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1976:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1977:                                                m_prev_cs, m_prev_eip);
                   1978:                                        } else if(int_break_point.hit) {
                   1979:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1980:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1981:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1982:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1983:                                                }
                   1984:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1985:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1986:                                                }
                   1987:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1988:                                        } else if(steps > 0) {
                   1989:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1990:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1991:                                        }
                   1992:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1993:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1994:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1995:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1996:                                } else {
                   1997:                                        telnet_printf("invalid parameter number\n");
                   1998:                                }
                   1999:                        } else if(stricmp(params[0], "Q") == 0) {
                   2000:                                break;
                   2001:                        } else if(stricmp(params[0], "X") == 0) {
                   2002:                                debugger_process_info(buffer);
                   2003:                                telnet_printf("%s", buffer);
                   2004:                        } else if(stricmp(params[0], ">") == 0) {
                   2005:                                if(num == 2) {
                   2006:                                        if(fp_debugger != NULL) {
                   2007:                                                fclose(fp_debugger);
                   2008:                                                fp_debugger = NULL;
                   2009:                                        }
                   2010:                                        fp_debugger = fopen(params[1], "w");
                   2011:                                } else {
                   2012:                                        telnet_printf("invalid parameter number\n");
                   2013:                                }
                   2014:                        } else if(stricmp(params[0], "<") == 0) {
                   2015:                                if(num == 2) {
                   2016:                                        if(fi_debugger != NULL) {
                   2017:                                                fclose(fi_debugger);
                   2018:                                                fi_debugger = NULL;
                   2019:                                        }
                   2020:                                        fi_debugger = fopen(params[1], "r");
                   2021:                                } else {
                   2022:                                        telnet_printf("invalid parameter number\n");
                   2023:                                }
                   2024:                        } else if(stricmp(params[0], "?") == 0) {
                   2025:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   2026:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   2027:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   2028:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   2029:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   2030:                                
                   2031:                                telnet_printf("R - show registers\n");
                   2032:                                telnet_printf("R <reg> <value> - edit register\n");
                   2033:                                telnet_printf("S <start> <end> <list> - search\n");
                   2034:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   2035:                                
                   2036:                                telnet_printf("H <value> <value> - hexadd\n");
                   2037:                                
                   2038:                                telnet_printf("BP <address> - set breakpoint\n");
                   2039:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   2040:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   2041:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   2042:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   2043:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   2044:                                
                   2045:                                telnet_printf("G - go (press enter key to break)\n");
                   2046:                                telnet_printf("G <address> - go and break at address\n");
                   2047:                                telnet_printf("P - trace one opcode (step over)\n");
                   2048:                                telnet_printf("T [<count>] - trace (step in)\n");
                   2049:                                telnet_printf("Q - quit\n");
                   2050:                                telnet_printf("X - show dos process info\n");
                   2051:                                
                   2052:                                telnet_printf("> <filename> - output logfile\n");
                   2053:                                telnet_printf("< <filename> - input commands from file\n");
                   2054:                                
                   2055:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   2056:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   2057:                        } else {
                   2058:                                telnet_printf("unknown command %s\n", params[0]);
                   2059:                        }
                   2060:                }
                   2061:        }
                   2062:        if(fp_debugger != NULL) {
                   2063:                fclose(fp_debugger);
                   2064:                fp_debugger = NULL;
                   2065:        }
                   2066:        if(fi_debugger != NULL) {
                   2067:                fclose(fi_debugger);
                   2068:                fi_debugger = NULL;
                   2069:        }
                   2070:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2071:        closesocket(cli_socket);
                   2072: }
                   2073: 
                   2074: const char *debugger_get_ttermpro_path()
                   2075: {
                   2076:        static char path[MAX_PATH] = {0};
                   2077:        
                   2078:        if(getenv("ProgramFiles")) {
                   2079:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2080:        }
                   2081:        return(path);
                   2082: }
                   2083: 
                   2084: const char *debugger_get_ttermpro_x86_path()
                   2085: {
                   2086:        static char path[MAX_PATH] = {0};
                   2087:        
                   2088:        if(getenv("ProgramFiles(x86)")) {
                   2089:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2090:        }
                   2091:        return(path);
                   2092: }
                   2093: 
                   2094: const char *debugger_get_putty_path()
                   2095: {
                   2096:        static char path[MAX_PATH] = {0};
                   2097:        
                   2098:        if(getenv("ProgramFiles")) {
                   2099:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2100:        }
                   2101:        return(path);
                   2102: }
                   2103: 
                   2104: const char *debugger_get_putty_x86_path()
                   2105: {
                   2106:        static char path[MAX_PATH] = {0};
                   2107:        
                   2108:        if(getenv("ProgramFiles(x86)")) {
                   2109:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2110:        }
                   2111:        return(path);
                   2112: }
                   2113: 
                   2114: const char *debugger_get_telnet_path()
                   2115: {
                   2116:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2117:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2118:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2119:        // and 64bit version of telnet.exe will be installed in System32.
                   2120:        static char path[MAX_PATH] = {0};
                   2121:        
                   2122:        if(getenv("windir") != NULL) {
                   2123:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2124:        }
                   2125:        return(path);
                   2126: }
                   2127: 
                   2128: DWORD WINAPI debugger_thread(LPVOID)
                   2129: {
                   2130:        WSADATA was_data;
                   2131:        struct sockaddr_in svr_addr;
                   2132:        struct sockaddr_in cli_addr;
                   2133:        int cli_addr_len = sizeof(cli_addr);
                   2134:        int port = 23;
                   2135:        int bind_stat = SOCKET_ERROR;
                   2136:        struct timeval timeout;
                   2137:        
                   2138:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2139:        
                   2140:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2141:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2142:                svr_addr.sin_family = AF_INET;
                   2143:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2144:                
1.1.1.54  root     2145:                while(!m_exit && port < 10000) {
1.1.1.33  root     2146:                        svr_addr.sin_port = htons(port);
                   2147:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2148:                                break;
                   2149:                        } else {
                   2150:                                port = (port == 23) ? 9000 : (port + 1);
                   2151:                        }
                   2152:                }
                   2153:                if(bind_stat == 0) {
                   2154:                        timeout.tv_sec = 1;
                   2155:                        timeout.tv_usec = 0;
1.1.1.45  root     2156:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2157:                        
                   2158:                        listen(svr_socket, 1);
                   2159:                        
                   2160:                        char command[MAX_PATH] = {0};
                   2161:                        STARTUPINFO si;
                   2162:                        PROCESS_INFORMATION pi;
                   2163:                        
                   2164:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2165:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2166:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2167:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2168:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2169:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2170:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2171:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2172:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2173:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2174:                        }
                   2175:                        if(command[0] != '\0') {
                   2176:                                memset(&si, 0, sizeof(STARTUPINFO));
                   2177:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
                   2178:                                CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
                   2179:                        }
                   2180:                        
1.1.1.54  root     2181:                        while(!m_exit) {
1.1.1.33  root     2182:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2183:                                        u_long val = 1;
                   2184:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2185:                                        debugger_main();
                   2186:                                }
                   2187:                        }
                   2188:                }
                   2189:        }
                   2190:        WSACleanup();
                   2191:        return(0);
                   2192: }
                   2193: #endif
                   2194: 
                   2195: /* ----------------------------------------------------------------------------
1.1       root     2196:        main
                   2197: ---------------------------------------------------------------------------- */
                   2198: 
1.1.1.28  root     2199: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2200: {
                   2201:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2202:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2203: #ifdef USE_SERVICE_THREAD
                   2204:                        EnterCriticalSection(&key_buf_crit_sect);
                   2205: #endif
1.1.1.51  root     2206:                        pcbios_clear_key_buffer();
1.1.1.35  root     2207: #ifdef USE_SERVICE_THREAD
                   2208:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2209: #endif
1.1.1.33  root     2210:                }
                   2211: //             key_code = key_recv = 0;
1.1.1.28  root     2212:                return TRUE;
                   2213:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2214:                return TRUE;
                   2215:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2216:                // this program will be terminated abnormally, do minimum end process
                   2217:                exit_handler();
                   2218:                exit(1);
                   2219:        }
                   2220:        return FALSE;
                   2221: }
                   2222: 
                   2223: void exit_handler()
                   2224: {
                   2225:        if(temp_file_created) {
                   2226:                DeleteFile(temp_file_path);
                   2227:                temp_file_created = false;
                   2228:        }
                   2229:        if(key_buf_char != NULL) {
                   2230:                key_buf_char->release();
                   2231:                delete key_buf_char;
                   2232:                key_buf_char = NULL;
                   2233:        }
                   2234:        if(key_buf_scan != NULL) {
                   2235:                key_buf_scan->release();
                   2236:                delete key_buf_scan;
                   2237:                key_buf_scan = NULL;
                   2238:        }
1.1.1.57  root     2239:        if(key_buf_data != NULL) {
                   2240:                key_buf_data->release();
                   2241:                delete key_buf_data;
                   2242:                key_buf_data = NULL;
                   2243:        }
1.1.1.32  root     2244: #ifdef SUPPORT_XMS
                   2245:        msdos_xms_release();
                   2246: #endif
1.1.1.28  root     2247:        hardware_release();
                   2248: }
                   2249: 
1.1.1.35  root     2250: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2251: DWORD WINAPI vram_thread(LPVOID)
                   2252: {
1.1.1.54  root     2253:        while(!m_exit) {
1.1.1.28  root     2254:                EnterCriticalSection(&vram_crit_sect);
                   2255:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2256:                        vram_flush_char();
                   2257:                }
                   2258:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2259:                        vram_flush_attr();
                   2260:                }
                   2261:                vram_last_length_char = vram_length_char;
                   2262:                vram_last_length_attr = vram_length_attr;
                   2263:                LeaveCriticalSection(&vram_crit_sect);
                   2264:                // this is about half the maximum keyboard repeat rate - any
                   2265:                // lower tends to be jerky, any higher misses updates
                   2266:                Sleep(15);
                   2267:        }
                   2268:        return 0;
                   2269: }
                   2270: #endif
                   2271: 
1.1.1.45  root     2272: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2273: {
                   2274:        UINT8 header[0x400];
                   2275:        
                   2276:        long position = ftell(fp);
                   2277:        fseek(fp, 0, SEEK_SET);
                   2278:        fread(header, sizeof(header), 1, fp);
                   2279:        fseek(fp, position, SEEK_SET);
                   2280:        
                   2281:        try {
                   2282:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2283:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2284:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2285:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2286:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2287:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2288:                
                   2289:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2290:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2291:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2292:                                return(sectionHeader->PointerToRawData);
                   2293:                        }
                   2294:                }
                   2295:        } catch(...) {
                   2296:        }
                   2297:        return(0);
                   2298: }
                   2299: 
1.1.1.10  root     2300: bool is_started_from_command_prompt()
                   2301: {
1.1.1.58! root     2302:        bool result = false;
1.1.1.18  root     2303:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
1.1.1.58! root     2304:        
1.1.1.18  root     2305:        if(hLibrary) {
                   2306:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2307:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2308:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
1.1.1.58! root     2309:                if(lpfnGetConsoleProcessList) { // Windows XP or later
1.1.1.18  root     2310:                        DWORD pl;
1.1.1.58! root     2311:                        result = (lpfnGetConsoleProcessList(&pl, 1) > 1);
1.1.1.18  root     2312:                        FreeLibrary(hLibrary);
1.1.1.58! root     2313:                        return(result);
1.1.1.18  root     2314:                }
                   2315:                FreeLibrary(hLibrary);
                   2316:        }
                   2317:        
                   2318:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2319:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2320:                DWORD dwParentProcessID = 0;
                   2321:                PROCESSENTRY32 pe32;
                   2322:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2323:                if(Process32First(hSnapshot, &pe32)) {
                   2324:                        do {
                   2325:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2326:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2327:                                        break;
                   2328:                                }
                   2329:                        } while(Process32Next(hSnapshot, &pe32));
                   2330:                }
                   2331:                CloseHandle(hSnapshot);
                   2332:                if(dwParentProcessID != 0) {
                   2333:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2334:                        if(hProcess != NULL) {
                   2335:                                HMODULE hMod;
                   2336:                                DWORD cbNeeded;
                   2337:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2338:                                        char module_name[MAX_PATH];
                   2339:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
1.1.1.58! root     2340:                                                result = (_strnicmp(module_name, "cmd.exe", 7) == 0);
1.1.1.18  root     2341:                                        }
                   2342:                                }
                   2343:                                CloseHandle(hProcess);
                   2344:                        }
                   2345:                }
                   2346:        }
1.1.1.58! root     2347:        return(result);
1.1.1.14  root     2348: }
                   2349: 
                   2350: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2351: {
1.1.1.24  root     2352:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2353:        OSVERSIONINFOEX osvi;
                   2354:        DWORDLONG dwlConditionMask = 0;
                   2355:        int op = VER_GREATER_EQUAL;
                   2356:        
                   2357:        // Initialize the OSVERSIONINFOEX structure.
                   2358:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2359:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2360:        osvi.dwMajorVersion = dwMajorVersion;
                   2361:        osvi.dwMinorVersion = dwMinorVersion;
                   2362:        osvi.wServicePackMajor = wServicePackMajor;
                   2363:        osvi.wServicePackMinor = wServicePackMinor;
                   2364:        
                   2365:         // Initialize the condition mask.
                   2366:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2367:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2368:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2369:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2370:        
                   2371:        // Perform the test.
                   2372:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2373: }
                   2374: 
1.1.1.58! root     2375: bool get_console_font_size(HANDLE hStdout, int *width, int *height)
        !          2376: {
        !          2377:        bool result = false;
        !          2378:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
        !          2379:        
        !          2380:        if(hLibrary) {
        !          2381:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
        !          2382:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
        !          2383:                if(lpfnGetCurrentConsoleFont) { // Windows XP or later
        !          2384:                        CONSOLE_FONT_INFO fi;
        !          2385:                        if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi) != 0) {
        !          2386:                                *width  = fi.dwFontSize.X;
        !          2387:                                *height = fi.dwFontSize.Y;
        !          2388:                                result = true;
        !          2389:                        }
        !          2390:                }
        !          2391:                FreeLibrary(hLibrary);
        !          2392:        }
        !          2393:        return(result);
        !          2394: }
        !          2395: 
1.1.1.56  root     2396: bool set_console_font_size(HANDLE hStdout, int width, int height)
                   2397: {
                   2398:        // http://d.hatena.ne.jp/aharisu/20090427/1240852598
                   2399:        bool result = false;
                   2400:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2401:        
                   2402:        if(hLibrary) {
                   2403:                typedef BOOL (WINAPI* GetConsoleFontInfoFunction)(HANDLE, BOOL, DWORD, PCONSOLE_FONT_INFO);
                   2404:                typedef DWORD (WINAPI* GetNumberOfConsoleFontsFunction)(VOID);
                   2405:                typedef BOOL (WINAPI* SetConsoleFontFunction)(HANDLE, DWORD);
                   2406:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
                   2407:                
                   2408:                GetConsoleFontInfoFunction lpfnGetConsoleFontInfo = reinterpret_cast<GetConsoleFontInfoFunction>(::GetProcAddress(hLibrary, "GetConsoleFontInfo"));
                   2409:                GetNumberOfConsoleFontsFunction lpfnGetNumberOfConsoleFonts = reinterpret_cast<GetNumberOfConsoleFontsFunction>(::GetProcAddress(hLibrary, "GetNumberOfConsoleFonts"));
                   2410:                SetConsoleFontFunction lpfnSetConsoleFont = reinterpret_cast<SetConsoleFontFunction>(::GetProcAddress(hLibrary, "SetConsoleFont"));
                   2411:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
                   2412:                
                   2413:                if(lpfnGetConsoleFontInfo && lpfnGetNumberOfConsoleFonts && lpfnSetConsoleFont) {
1.1.1.58! root     2414:                        static const int offsets[] = {0, +1, -1, +2, -2, +3, -3};
1.1.1.56  root     2415:                        DWORD dwFontNum = lpfnGetNumberOfConsoleFonts();
                   2416:                        CONSOLE_FONT_INFO* fonts = (CONSOLE_FONT_INFO*)malloc(sizeof(CONSOLE_FONT_INFO) * dwFontNum);
                   2417:                        lpfnGetConsoleFontInfo(hStdout, FALSE, dwFontNum, fonts);
                   2418:                        for(int i = 0; i < dwFontNum; i++) {
                   2419:                                fonts[i].dwFontSize = GetConsoleFontSize(hStdout, fonts[i].nFont);
                   2420:                        }
1.1.1.58! root     2421:                        for(int h = 0; h < 5; h++) { // 0, +1, -1, +2, -2
        !          2422:                                int height_tmp = height + offsets[h];
        !          2423:                                for(int w = 0; w < 7; w++) { // 0, +1, -1, +2, -2, +3, -3
        !          2424:                                        int width_tmp = width + offsets[w];
        !          2425:                                        for(int i = 0; i < dwFontNum; i++) {
        !          2426:                                                if(fonts[i].dwFontSize.X == width_tmp && fonts[i].dwFontSize.Y == height_tmp) {
        !          2427:                                                        lpfnSetConsoleFont(hStdout, fonts[i].nFont);
        !          2428:                                                        if(lpfnGetCurrentConsoleFont) { // Windows XP or later
        !          2429:                                                                CONSOLE_FONT_INFO fi;
        !          2430:                                                                if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi)) {
        !          2431:                                                                        if(fi.dwFontSize.X == width_tmp && fi.dwFontSize.Y == height_tmp) {
        !          2432:                                                                                result = true;
        !          2433:                                                                        }
        !          2434:                                                                }
        !          2435:                                                        } else {
        !          2436:                                                                result = true;
        !          2437:                                                        }
        !          2438:                                                }
        !          2439:                                                if(result) {
        !          2440:                                                        *(UINT16 *)(mem + 0x485) = height_tmp;
        !          2441:                                                        goto exit_loop;
        !          2442:                                                }
        !          2443:                                        }
1.1.1.57  root     2444:                                }
1.1.1.56  root     2445:                        }
1.1.1.58! root     2446: exit_loop:
        !          2447:                        free(fonts);
1.1.1.56  root     2448:                }
                   2449:                FreeLibrary(hLibrary);
                   2450:        }
                   2451:        return(result);
                   2452: }
                   2453: 
1.1.1.27  root     2454: void get_sio_port_numbers()
                   2455: {
                   2456:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2457:        HDEVINFO hDevInfo = 0;
                   2458:        HKEY hKey = 0;
                   2459:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2460:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2461:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2462:                                char chData[256];
                   2463:                                DWORD dwType = 0;
                   2464:                                DWORD dwSize = sizeof(chData);
                   2465:                                int port_number = 0;
                   2466:                                
                   2467:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2468:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2469:                                                port_number = atoi(chData + 3);
                   2470:                                        }
                   2471:                                }
                   2472:                                RegCloseKey(hKey);
                   2473:                                
1.1.1.29  root     2474:                                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     2475:                                        continue;
                   2476:                                }
                   2477:                                if(sio_port_number[0] == 0) {
                   2478:                                        sio_port_number[0] = port_number;
                   2479:                                } else if(sio_port_number[1] == 0) {
                   2480:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2481:                                } else if(sio_port_number[2] == 0) {
                   2482:                                        sio_port_number[2] = port_number;
                   2483:                                } else if(sio_port_number[3] == 0) {
                   2484:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2485:                                }
1.1.1.29  root     2486:                                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     2487:                                        break;
                   2488:                                }
                   2489:                        }
                   2490:                }
                   2491:        }
                   2492: }
                   2493: 
1.1.1.28  root     2494: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2495: 
1.1       root     2496: int main(int argc, char *argv[], char *envp[])
                   2497: {
1.1.1.9   root     2498:        int arg_offset = 0;
                   2499:        int standard_env = 0;
1.1.1.14  root     2500:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2501:        bool get_console_info_success = false;
1.1.1.56  root     2502:        bool get_console_font_success = false;
1.1.1.28  root     2503:        bool screen_size_changed = false;
                   2504:        
                   2505:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2506:        GetModuleFileName(NULL, path, MAX_PATH);
                   2507:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2508:        
1.1.1.27  root     2509:        char dummy_argv_0[] = "msdos.exe";
                   2510:        char dummy_argv_1[MAX_PATH];
                   2511:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2512:        char new_exec_file[MAX_PATH];
                   2513:        bool convert_cmd_file = false;
1.1.1.28  root     2514:        unsigned int code_page = 0;
1.1.1.27  root     2515:        
                   2516:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2517:                // check if command file is embedded to this execution file
                   2518:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2519:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2520:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2521:                if(offset != 0) {
1.1.1.30  root     2522:                        UINT8 buffer[16];
1.1.1.28  root     2523:                        fseek(fp, offset, SEEK_SET);
                   2524:                        fread(buffer, sizeof(buffer), 1, fp);
                   2525:                        
                   2526:                        // restore flags
                   2527:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2528:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2529:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2530:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2531:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2532:                        if((buffer[0] & 0x20) != 0) {
                   2533:                                get_sio_port_numbers();
                   2534:                        }
                   2535:                        if((buffer[0] & 0x40) != 0) {
                   2536:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2537:                                support_ems = true;
1.1.1.30  root     2538:                        }
1.1.1.27  root     2539: #ifdef SUPPORT_XMS
1.1.1.30  root     2540:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2541:                                support_xms = true;
                   2542:                        }
1.1.1.30  root     2543: #endif
1.1.1.28  root     2544:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2545:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2546:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2547:                        }
                   2548:                        if(buffer[5] != 0) {
1.1.1.30  root     2549:                                dos_major_version = buffer[5];
                   2550:                                dos_minor_version = buffer[6];
                   2551:                        }
                   2552:                        if(buffer[7] != 0) {
                   2553:                                win_major_version = buffer[7];
                   2554:                                win_minor_version = buffer[8];
1.1.1.28  root     2555:                        }
1.1.1.30  root     2556:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2557:                                SetConsoleCP(code_page);
                   2558:                                SetConsoleOutputCP(code_page);
                   2559:                        }
1.1.1.30  root     2560:                        int name_len = buffer[11];
                   2561:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2562:                        
                   2563:                        // restore command file name
                   2564:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2565:                        fread(dummy_argv_1, name_len, 1, fp);
                   2566:                        
                   2567:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2568:                                // if original command file exists, create a temporary file name
                   2569:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2570:                                        // create a temporary command file in the current director
                   2571:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2572:                                } else {
1.1.1.28  root     2573:                                        // create a temporary command file in the temporary folder
                   2574:                                        GetTempPath(MAX_PATH, path);
                   2575:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2576:                                                DeleteFile(dummy_argv_1);
                   2577:                                        } else {
                   2578:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2579:                                        }
1.1.1.27  root     2580:                                }
1.1.1.28  root     2581:                                // check the command file type
                   2582:                                fread(buffer, 2, 1, fp);
                   2583:                                fseek(fp, -2, SEEK_CUR);
                   2584:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2585:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2586:                                } else {
                   2587:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2588:                                }
                   2589:                        }
1.1.1.28  root     2590:                        
                   2591:                        // restore command file
                   2592:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2593:                        for(int i = 0; i < file_len; i++) {
                   2594:                                fputc(fgetc(fp), fo);
                   2595:                        }
                   2596:                        fclose(fo);
                   2597:                        
                   2598:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2599:                        temp_file_created = true;
                   2600:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2601:                        
                   2602:                        // adjust argc/argv
                   2603:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2604:                                dummy_argv[i + 1] = argv[i];
                   2605:                        }
                   2606:                        argc++;
                   2607:                        argv = dummy_argv;
1.1.1.27  root     2608:                }
                   2609:                fclose(fp);
                   2610:        }
1.1.1.9   root     2611:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2612:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2613:                        stay_busy = true;
                   2614:                        arg_offset++;
1.1.1.27  root     2615:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2616:                        if(argv[i][2] != '\0') {
                   2617:                                strcpy(new_exec_file, &argv[i][2]);
                   2618:                        } else {
                   2619:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2620:                        }
                   2621:                        convert_cmd_file = true;
                   2622:                        arg_offset++;
1.1.1.28  root     2623:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2624:                        if(IS_NUMERIC(argv[i][2])) {
                   2625:                                code_page = atoi(&argv[i][2]);
                   2626:                        } else {
                   2627:                                code_page = GetConsoleCP();
                   2628:                        }
                   2629:                        arg_offset++;
1.1.1.25  root     2630:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2631:                        no_windows = true;
                   2632:                        arg_offset++;
                   2633:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2634:                        standard_env = 1;
                   2635:                        arg_offset++;
1.1.1.14  root     2636:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2637:                        ignore_illegal_insn = true;
                   2638:                        arg_offset++;
                   2639:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2640:                        limit_max_memory = true;
                   2641:                        arg_offset++;
                   2642:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51  root     2643:                        int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
                   2644:                        if(result == 1) {
                   2645:                                buf_width = 0;
                   2646:                        } else if(result != 2) {
1.1.1.17  root     2647:                                buf_width = buf_height = 0;
                   2648:                        }
                   2649:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2650:                                buf_width = 80;
                   2651:                        }
                   2652:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2653:                                buf_height = 25;
                   2654:                        }
1.1.1.14  root     2655:                        arg_offset++;
1.1.1.25  root     2656:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2657:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2658:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2659:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2660:                                        sio_port_number[1] = atoi(p1 + 1);
                   2661:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2662:                                                sio_port_number[2] = atoi(p2 + 1);
                   2663:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2664:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2665:                                                }
                   2666:                                        }
1.1.1.25  root     2667:                                }
1.1.1.29  root     2668:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2669:                        }
1.1.1.29  root     2670:                        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     2671:                                get_sio_port_numbers();
1.1.1.25  root     2672:                        }
                   2673:                        arg_offset++;
1.1.1.9   root     2674:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2675:                        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     2676:                                dos_major_version = argv[i][2] - '0';
                   2677:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2678:                        }
                   2679:                        arg_offset++;
                   2680:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2681:                        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]))) {
                   2682:                                win_major_version = argv[i][2] - '0';
                   2683:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2684:                        }
                   2685:                        arg_offset++;
1.1.1.25  root     2686:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2687:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2688:                        support_ems = true;
                   2689: #ifdef SUPPORT_XMS
                   2690:                        support_xms = true;
                   2691: #endif
                   2692:                        arg_offset++;
1.1.1.9   root     2693:                } else {
                   2694:                        break;
                   2695:                }
                   2696:        }
                   2697:        if(argc < 2 + arg_offset) {
1.1       root     2698: #ifdef _WIN64
1.1.1.14  root     2699:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2700: #else
1.1.1.14  root     2701:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2702: #endif
1.1.1.25  root     2703:                fprintf(stderr,
1.1.1.28  root     2704:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2705:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2706:                        "\n"
                   2707:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2708: #ifdef _WIN64
1.1.1.27  root     2709:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2710: #else
1.1.1.27  root     2711:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2712: #endif
1.1.1.28  root     2713:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2714:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2715:                        "\t-e\tuse a reduced environment block\n"
                   2716:                        "\t-i\tignore invalid instructions\n"
                   2717:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2718:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2719:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2720:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2721:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2722: #ifdef SUPPORT_XMS
1.1.1.28  root     2723:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2724: #else
1.1.1.28  root     2725:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2726: #endif
                   2727:                );
1.1.1.10  root     2728:                
                   2729:                if(!is_started_from_command_prompt()) {
                   2730:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2731:                        while(!_kbhit()) {
                   2732:                                Sleep(10);
                   2733:                        }
                   2734:                }
1.1.1.20  root     2735: #ifdef _DEBUG
                   2736:                _CrtDumpMemoryLeaks();
                   2737: #endif
1.1       root     2738:                return(EXIT_FAILURE);
                   2739:        }
1.1.1.27  root     2740:        if(convert_cmd_file) {
                   2741:                retval = EXIT_FAILURE;
1.1.1.28  root     2742:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2743:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2744:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2745:                        
1.1.1.28  root     2746:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2747:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2748:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2749:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2750:                        } else {
1.1.1.28  root     2751:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2752:                                if(offset != 0) {
                   2753:                                        UINT8 buffer[14];
                   2754:                                        fseek(fp, offset, SEEK_SET);
                   2755:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2756:                                        memset(path, 0, sizeof(path));
                   2757:                                        fread(path, buffer[9], 1, fp);
                   2758:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2759:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2760:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2761:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2762:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2763:                                } else {
                   2764:                                        // read pe header of msdos.exe
                   2765:                                        UINT8 header[0x400];
                   2766:                                        fseek(fp, 0, SEEK_SET);
                   2767:                                        fread(header, sizeof(header), 1, fp);
                   2768:                                        
                   2769:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2770:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2771:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2772:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2773:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2774:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2775:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2776:                                        
                   2777:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2778:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2779:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2780:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2781:                                        if(dwExtraLastSectionBytes != 0) {
                   2782:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2783:                                                dwLastSectionSize += dwRemain;
                   2784:                                        }
                   2785:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2786:                                        
                   2787:                                        // store msdos.exe
                   2788:                                        fseek(fp, 0, SEEK_SET);
                   2789:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2790:                                                if((data = fgetc(fp)) != EOF) {
                   2791:                                                        fputc(data, fo);
                   2792:                                                } else {
                   2793:                                                        // we should not reach here :-(
                   2794:                                                        fputc(0, fo);
                   2795:                                                }
                   2796:                                        }
                   2797:                                        
                   2798:                                        // store options
                   2799:                                        UINT8 flags = 0;
                   2800:                                        if(stay_busy) {
                   2801:                                                flags |= 0x01;
                   2802:                                        }
                   2803:                                        if(no_windows) {
                   2804:                                                flags |= 0x02;
                   2805:                                        }
                   2806:                                        if(standard_env) {
                   2807:                                                flags |= 0x04;
                   2808:                                        }
                   2809:                                        if(ignore_illegal_insn) {
                   2810:                                                flags |= 0x08;
                   2811:                                        }
                   2812:                                        if(limit_max_memory) {
                   2813:                                                flags |= 0x10;
                   2814:                                        }
1.1.1.29  root     2815:                                        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     2816:                                                flags |= 0x20;
                   2817:                                        }
                   2818:                                        if(support_ems) {
                   2819:                                                flags |= 0x40;
                   2820:                                        }
1.1.1.30  root     2821: #ifdef SUPPORT_XMS
                   2822:                                        if(support_xms) {
                   2823:                                                flags |= 0x80;
                   2824:                                        }
                   2825: #endif
1.1.1.28  root     2826:                                        
                   2827:                                        fputc(flags, fo);
                   2828:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2829:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2830:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2831:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2832:                                        fputc(dos_major_version, fo);
                   2833:                                        fputc(dos_minor_version, fo);
                   2834:                                        fputc(win_major_version, fo);
                   2835:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2836:                                        fputc((code_page >> 0) & 0xff, fo);
                   2837:                                        fputc((code_page >> 8) & 0xff, fo);
                   2838:                                        
                   2839:                                        // store command file info
                   2840:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2841:                                        int name_len = strlen(name);
                   2842:                                        fseek(fs, 0, SEEK_END);
                   2843:                                        long file_size = ftell(fs);
                   2844:                                        
                   2845:                                        fputc(name_len, fo);
                   2846:                                        fputc((file_size >>  0) & 0xff, fo);
                   2847:                                        fputc((file_size >>  8) & 0xff, fo);
                   2848:                                        fputc((file_size >> 16) & 0xff, fo);
                   2849:                                        fputc((file_size >> 24) & 0xff, fo);
                   2850:                                        fwrite(name, name_len, 1, fo);
                   2851:                                        
                   2852:                                        // store command file
                   2853:                                        fseek(fs, 0, SEEK_SET);
                   2854:                                        for(int i = 0; i < file_size; i++) {
                   2855:                                                if((data = fgetc(fs)) != EOF) {
                   2856:                                                        fputc(data, fo);
                   2857:                                                } else {
                   2858:                                                        // we should not reach here :-(
                   2859:                                                        fputc(0, fo);
                   2860:                                                }
                   2861:                                        }
                   2862:                                        
                   2863:                                        // store padding data and update pe header
1.1.1.29  root     2864:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2865:                                        coffHeader->NumberOfSections++;
                   2866:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2867:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2868:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2869:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2870:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2871:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2872:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2873:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2874:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2875:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2876:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2877:                                                        if(i < 2) {
                   2878:                                                                fputc(padding[i & 15], fo);
                   2879:                                                        } else {
                   2880:                                                                fputc(padding[(i - 2) & 15], fo);
                   2881:                                                        }
1.1.1.28  root     2882:                                                }
                   2883:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2884:                                        }
                   2885:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2886:                                        
                   2887:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2888:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2889:                                        if(dwExtraNewSectionBytes != 0) {
                   2890:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2891:                                                dwNewSectionSize += dwRemain;
                   2892:                                        }
                   2893:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2894:                                        
                   2895:                                        fseek(fo, 0, SEEK_SET);
                   2896:                                        fwrite(header, sizeof(header), 1, fo);
                   2897:                                        
                   2898:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2899:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2900:                                }
                   2901:                        }
                   2902:                        if(fp != NULL) {
                   2903:                                fclose(fp);
                   2904:                        }
                   2905:                        if(fs != NULL) {
                   2906:                                fclose(fs);
                   2907:                        }
                   2908:                        if(fo != NULL) {
                   2909:                                fclose(fo);
                   2910:                        }
                   2911:                }
                   2912: #ifdef _DEBUG
                   2913:                _CrtDumpMemoryLeaks();
                   2914: #endif
                   2915:                return(retval);
                   2916:        }
1.1       root     2917:        
1.1.1.54  root     2918:        is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14  root     2919:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2920:        
1.1.1.23  root     2921:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2922:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2923:        CONSOLE_CURSOR_INFO ci;
1.1.1.58! root     2924:        int font_width = 10, font_height = 18; // default in english mode
1.1.1.23  root     2925:        
1.1.1.28  root     2926:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2927:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     2928:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1.1.58! root     2929:        get_console_font_success = get_console_font_size(hStdout, &font_width, &font_height);
1.1       root     2930:        
1.1.1.14  root     2931:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2932:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2933:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2934:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2935:                }
                   2936:        }
1.1.1.28  root     2937:        if(get_console_info_success) {
1.1.1.12  root     2938:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2939:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2940:                
1.1.1.28  root     2941:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2942:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2943:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2944:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2945:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2946:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2947:                                scr_width = 80;
                   2948:                                scr_height = 25;
                   2949:                        }
1.1.1.28  root     2950:                        screen_size_changed = true;
1.1.1.14  root     2951:                }
1.1.1.12  root     2952:        } else {
                   2953:                // for a proof (not a console)
                   2954:                scr_width = 80;
                   2955:                scr_height = 25;
                   2956:        }
1.1.1.14  root     2957:        scr_buf_size.X = scr_width;
                   2958:        scr_buf_size.Y = scr_height;
                   2959:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2960:        scr_top = csbi.srWindow.Top;
1.1       root     2961:        cursor_moved = false;
                   2962:        
1.1.1.54  root     2963:        SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
                   2964:        
1.1.1.35  root     2965: #ifdef USE_SERVICE_THREAD
                   2966:        InitializeCriticalSection(&input_crit_sect);
                   2967:        InitializeCriticalSection(&key_buf_crit_sect);
                   2968:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     2969:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     2970: #endif
1.1.1.50  root     2971:        
1.1.1.25  root     2972:        key_buf_char = new FIFO(256);
                   2973:        key_buf_scan = new FIFO(256);
1.1.1.57  root     2974:        key_buf_data = new FIFO(256);
1.1       root     2975:        
                   2976:        hardware_init();
                   2977:        
1.1.1.33  root     2978: #ifdef USE_DEBUGGER
                   2979:        debugger_init();
                   2980: #endif
                   2981:        
1.1.1.9   root     2982:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     2983:                retval = EXIT_FAILURE;
                   2984:        } else {
1.1.1.27  root     2985: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     2986:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   2987: #endif
                   2988:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   2989:                
1.1.1.28  root     2990:                if(screen_size_changed) {
1.1.1.24  root     2991:                        change_console_size(scr_width, scr_height);
                   2992:                }
1.1.1.8   root     2993:                TIMECAPS caps;
                   2994:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   2995:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     2996: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2997:                InitializeCriticalSection(&vram_crit_sect);
                   2998:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   2999: #endif
1.1.1.33  root     3000: #ifdef USE_DEBUGGER
                   3001:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   3002:                // wait until telnet client starts and connects to me
                   3003:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   3004:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   3005:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   3006:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   3007:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   3008:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   3009:                                Sleep(100);
                   3010:                        }
                   3011:                }
                   3012: #endif
1.1       root     3013:                hardware_run();
1.1.1.35  root     3014: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3015:                vram_flush();
                   3016:                DeleteCriticalSection(&vram_crit_sect);
                   3017: #endif
1.1.1.24  root     3018:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     3019:                
1.1.1.24  root     3020:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.56  root     3021:                if(get_console_font_success) {
                   3022:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.58! root     3023:                        set_console_font_size(hStdout, font_width, font_height);
1.1.1.56  root     3024:                }
1.1.1.28  root     3025:                if(get_console_info_success) {
1.1.1.23  root     3026:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3027:                        if(restore_console_on_exit) {
1.1.1.14  root     3028:                                // window can't be bigger than buffer,
                   3029:                                // buffer can't be smaller than window,
                   3030:                                // so make a tiny window,
                   3031:                                // set the required buffer,
                   3032:                                // then set the required window
                   3033:                                SMALL_RECT rect;
                   3034:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   3035:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     3036:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     3037:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     3038:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3039:                        }
1.1.1.14  root     3040:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   3041:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     3042:                }
1.1.1.24  root     3043:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   3044:                
1.1       root     3045:                msdos_finish();
1.1.1.14  root     3046:                
                   3047:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     3048:        }
1.1.1.35  root     3049:        if(temp_file_created) {
                   3050:                DeleteFile(temp_file_path);
                   3051:                temp_file_created = false;
                   3052:        }
1.1.1.10  root     3053:        hardware_finish();
                   3054:        
1.1.1.28  root     3055:        if(key_buf_char != NULL) {
                   3056:                key_buf_char->release();
                   3057:                delete key_buf_char;
                   3058:                key_buf_char = NULL;
                   3059:        }
                   3060:        if(key_buf_scan != NULL) {
                   3061:                key_buf_scan->release();
                   3062:                delete key_buf_scan;
                   3063:                key_buf_scan = NULL;
                   3064:        }
1.1.1.57  root     3065:        if(key_buf_data != NULL) {
                   3066:                key_buf_data->release();
                   3067:                delete key_buf_data;
                   3068:                key_buf_data = NULL;
                   3069:        }
1.1.1.35  root     3070: #ifdef USE_SERVICE_THREAD
                   3071:        DeleteCriticalSection(&input_crit_sect);
                   3072:        DeleteCriticalSection(&key_buf_crit_sect);
                   3073:        DeleteCriticalSection(&putch_crit_sect);
                   3074: #endif
1.1.1.20  root     3075: #ifdef _DEBUG
                   3076:        _CrtDumpMemoryLeaks();
                   3077: #endif
1.1       root     3078:        return(retval);
                   3079: }
                   3080: 
1.1.1.20  root     3081: /* ----------------------------------------------------------------------------
                   3082:        console
                   3083: ---------------------------------------------------------------------------- */
                   3084: 
1.1.1.14  root     3085: void change_console_size(int width, int height)
1.1.1.12  root     3086: {
1.1.1.23  root     3087:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3088:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3089:        SMALL_RECT rect;
                   3090:        COORD co;
                   3091:        
                   3092:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     3093:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   3094:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   3095:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   3096:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3097:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3098:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   3099:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   3100:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3101:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3102:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     3103:                }
                   3104:        }
1.1.1.14  root     3105:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     3106:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     3107:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     3108:                SetConsoleCursorPosition(hStdout, co);
                   3109:                cursor_moved = true;
                   3110:        }
1.1.1.14  root     3111:        
                   3112:        // window can't be bigger than buffer,
                   3113:        // buffer can't be smaller than window,
                   3114:        // so make a tiny window,
                   3115:        // set the required buffer,
                   3116:        // then set the required window
                   3117:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     3118:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     3119:        co.X = width;
                   3120:        co.Y = height;
1.1.1.12  root     3121:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     3122:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3123:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3124:        
                   3125:        scr_width = scr_buf_size.X = width;
                   3126:        scr_height = scr_buf_size.Y = height;
                   3127:        scr_top = 0;
                   3128:        
                   3129:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3130:        
                   3131:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     3132:        text_vram_end_address = text_vram_top_address + regen;
                   3133:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   3134:        
1.1.1.14  root     3135:        if(regen > 0x4000) {
                   3136:                regen = 0x8000;
                   3137:                vram_pages = 1;
                   3138:        } else if(regen > 0x2000) {
                   3139:                regen = 0x4000;
                   3140:                vram_pages = 2;
                   3141:        } else if(regen > 0x1000) {
                   3142:                regen = 0x2000;
                   3143:                vram_pages = 4;
                   3144:        } else {
                   3145:                regen = 0x1000;
                   3146:                vram_pages = 8;
                   3147:        }
1.1.1.15  root     3148:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3149:        *(UINT16 *)(mem + 0x44c) = regen;
                   3150:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3151:        
1.1.1.24  root     3152:        mouse.min_position.x = 0;
                   3153:        mouse.min_position.y = 0;
1.1.1.34  root     3154:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3155:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3156:        
1.1.1.15  root     3157:        restore_console_on_exit = true;
1.1.1.14  root     3158: }
                   3159: 
                   3160: void clear_scr_buffer(WORD attr)
                   3161: {
                   3162:        for(int y = 0; y < scr_height; y++) {
                   3163:                for(int x = 0; x < scr_width; x++) {
                   3164:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3165:                        SCR_BUF(y,x).Attributes = attr;
                   3166:                }
                   3167:        }
1.1.1.12  root     3168: }
                   3169: 
1.1.1.24  root     3170: bool update_console_input()
1.1       root     3171: {
1.1.1.35  root     3172: #ifdef USE_SERVICE_THREAD
                   3173:        EnterCriticalSection(&input_crit_sect);
                   3174: #endif
1.1.1.23  root     3175:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3176:        DWORD dwNumberOfEvents = 0;
1.1       root     3177:        DWORD dwRead;
                   3178:        INPUT_RECORD ir[16];
1.1.1.24  root     3179:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3180:        bool result = false;
1.1       root     3181:        
1.1.1.8   root     3182:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3183:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3184:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3185:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34  root     3186:                                        if(mouse.hidden == 0) {
                   3187:                                                // NOTE: if restore_console_on_exit, console is not scrolled
                   3188:                                                if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3189:                                                        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3190:                                                }
                   3191:                                                // FIXME: character size is always 8x8 ???
                   3192:                                                int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3193:                                                int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3194:                                                
                   3195:                                                if(mouse.position.x != x || mouse.position.y != y) {
                   3196:                                                        mouse.position.x = x;
                   3197:                                                        mouse.position.y = y;
                   3198:                                                        mouse.status |= 1;
1.1.1.43  root     3199:                                                        mouse.status_alt |= 1;
1.1.1.34  root     3200:                                                }
                   3201:                                        }
                   3202:                                        if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3203:                                                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   3204:                                                        static const DWORD bits[] = {
                   3205:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3206:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3207:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3208:                                                        };
                   3209:                                                        bool prev_status = mouse.buttons[i].status;
                   3210:                                                        mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   3211:                                                        
                   3212:                                                        if(!prev_status && mouse.buttons[i].status) {
                   3213:                                                                mouse.buttons[i].pressed_times++;
                   3214:                                                                mouse.buttons[i].pressed_position.x = mouse.position.x;
                   3215:                                                                mouse.buttons[i].pressed_position.y = mouse.position.x;
1.1.1.43  root     3216:                                                                if(i < 2) {
                   3217:                                                                        mouse.status_alt |= 2 << (i * 2);
                   3218:                                                                }
1.1.1.34  root     3219:                                                                mouse.status |= 2 << (i * 2);
                   3220:                                                        } else if(prev_status && !mouse.buttons[i].status) {
                   3221:                                                                mouse.buttons[i].released_times++;
                   3222:                                                                mouse.buttons[i].released_position.x = mouse.position.x;
                   3223:                                                                mouse.buttons[i].released_position.y = mouse.position.x;
1.1.1.43  root     3224:                                                                if(i < 2) {
                   3225:                                                                        mouse.status_alt |= 4 << (i * 2);
                   3226:                                                                }
1.1.1.34  root     3227:                                                                mouse.status |= 4 << (i * 2);
1.1.1.14  root     3228:                                                        }
                   3229:                                                }
                   3230:                                        }
1.1.1.24  root     3231:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3232:                                        // update keyboard flags in bios data area
1.1.1.35  root     3233:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3234:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3235:                                        } else {
1.1.1.35  root     3236:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3237:                                        }
1.1.1.35  root     3238:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3239:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3240:                                        } else {
1.1.1.35  root     3241:                                                mem[0x417] &= ~0x20;
                   3242:                                        }
                   3243:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3244:                                                mem[0x417] |= 0x10;
                   3245:                                        } else {
                   3246:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3247:                                        }
                   3248:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3249:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3250:                                                        mouse.status_alt |= 0x80;
                   3251:                                                }
1.1.1.33  root     3252:                                                mem[0x417] |= 0x08;
                   3253:                                        } else {
                   3254:                                                mem[0x417] &= ~0x08;
                   3255:                                        }
1.1.1.35  root     3256:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3257:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3258:                                                        mouse.status_alt |= 0x40;
                   3259:                                                }
1.1.1.35  root     3260:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3261:                                        } else {
1.1.1.35  root     3262:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3263:                                        }
                   3264:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3265:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3266:                                                        mouse.status_alt |= 0x20;
                   3267:                                                }
1.1.1.33  root     3268:                                                if(!(mem[0x417] & 0x03)) {
                   3269:                                                        mem[0x417] |= 0x02; // left shift
                   3270:                                                }
                   3271:                                        } else {
                   3272:                                                mem[0x417] &= ~0x03;
                   3273:                                        }
1.1.1.35  root     3274:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3275:                                                mem[0x418] |= 0x02;
                   3276:                                        } else {
                   3277:                                                mem[0x418] &= ~0x02;
                   3278:                                        }
                   3279:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3280:                                                mem[0x418] |= 0x01;
                   3281:                                        } else {
                   3282:                                                mem[0x418] &= ~0x01;
                   3283:                                        }
1.1.1.33  root     3284:                                        
1.1.1.28  root     3285:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.57  root     3286: //                                     kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
                   3287: //                                     kbd_status |= 1;
                   3288:                                        UINT8 tmp_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3289:                                        
                   3290:                                        // update dos key buffer
                   3291:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3292:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3293:                                        UINT8 scn_old = scn;
1.1.1.33  root     3294:                                        
                   3295:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3296:                                                // make
1.1.1.57  root     3297:                                                tmp_data &= 0x7f;
1.1.1.24  root     3298:                                                
1.1.1.33  root     3299:                                                if(chr == 0x00) {
1.1.1.24  root     3300:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3301:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3302:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3303:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3304:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3305:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3306:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3307:                                                                } else if(scn == 0x35) {
                   3308:                                                                        scn = 0xa4;             // keypad /
                   3309:                                                                }
                   3310:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3311:                                                                if(scn == 0x07) {
                   3312:                                                                        chr = 0x1e;     // Ctrl+^
                   3313:                                                                } else if(scn == 0x0c) {
                   3314:                                                                        chr = 0x1f;     // Ctrl+_
                   3315:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3316:                                                                        static const UINT8 ctrl_map[] = {
                   3317:                                                                                0x95,   // keypad /
                   3318:                                                                                0,
                   3319:                                                                                0x96,   // keypad *
                   3320:                                                                                0, 0, 0,
                   3321:                                                                                0x5e,   // F1
                   3322:                                                                                0x5f,   // F2
                   3323:                                                                                0x60,   // F3
                   3324:                                                                                0x61,   // F4
                   3325:                                                                                0x62,   // F5
                   3326:                                                                                0x63,   // F6
                   3327:                                                                                0x64,   // F7
                   3328:                                                                                0x65,   // F8
                   3329:                                                                                0x66,   // F9
                   3330:                                                                                0x67,   // F10
                   3331:                                                                                0,
                   3332:                                                                                0,
                   3333:                                                                                0x77,   // Home
                   3334:                                                                                0x8d,   // Up
                   3335:                                                                                0x84,   // PgUp
                   3336:                                                                                0x8e,   // keypad -
                   3337:                                                                                0x73,   // Left
                   3338:                                                                                0x8f,   // keypad center
                   3339:                                                                                0x74,   // Right
                   3340:                                                                                0x90,   // keyapd +
                   3341:                                                                                0x75,   // End
                   3342:                                                                                0x91,   // Down
                   3343:                                                                                0x76,   // PgDn
                   3344:                                                                                0x92,   // Insert
                   3345:                                                                                0x93,   // Delete
                   3346:                                                                                0, 0, 0,
                   3347:                                                                                0x89,   // F11
                   3348:                                                                                0x8a,   // F12
                   3349:                                                                        };
                   3350:                                                                        scn = ctrl_map[scn - 0x35];
                   3351:                                                                }
                   3352:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3353:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3354:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3355:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3356:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3357:                                                                }
                   3358:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3359:                                                                scn += 0x85 - 0x57;
                   3360:                                                        }
                   3361:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3362:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3363:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3364: #ifdef USE_SERVICE_THREAD
                   3365:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3366: #endif
1.1.1.32  root     3367:                                                                        if(chr == 0) {
1.1.1.51  root     3368:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3369:                                                                        }
1.1.1.51  root     3370:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3371: #ifdef USE_SERVICE_THREAD
                   3372:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3373: #endif
1.1.1.24  root     3374:                                                                }
                   3375:                                                        }
                   3376:                                                } else {
                   3377:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3378:                                                                chr = 0;
                   3379:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3380:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3381:                                                                }
                   3382:                                                        }
1.1.1.32  root     3383:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3384: #ifdef USE_SERVICE_THREAD
                   3385:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3386: #endif
1.1.1.51  root     3387:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3388: #ifdef USE_SERVICE_THREAD
                   3389:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3390: #endif
1.1.1.32  root     3391:                                                        }
1.1.1.24  root     3392:                                                }
1.1.1.57  root     3393:                                        } else {
                   3394:                                                if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3395:                                                        // ctrl-break, ctrl-c
                   3396:                                                        if(scn == 0x46) {
                   3397:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3398: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3399:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3400: #endif
1.1.1.57  root     3401:                                                                        pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3402: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3403:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3404: #endif
1.1.1.57  root     3405:                                                                }
                   3406:                                                                ctrl_break_pressed = true;
                   3407:                                                                mem[0x471] = 0x80;
                   3408:                                                                raise_int_1bh = true;
                   3409:                                                        } else {
                   3410:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3411: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3412:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3413: #endif
1.1.1.57  root     3414:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3415: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3416:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3417: #endif
1.1.1.57  root     3418:                                                                }
                   3419:                                                                ctrl_c_pressed = (scn == 0x2e);
1.1.1.33  root     3420:                                                        }
                   3421:                                                }
                   3422:                                                // break
1.1.1.57  root     3423:                                                tmp_data |= 0x80;
                   3424:                                        }
                   3425:                                        if(!(kbd_status & 1)) {
                   3426:                                                kbd_data = tmp_data;
                   3427:                                                kbd_status |= 1;
                   3428:                                        } else {
                   3429:                                                if(key_buf_data != NULL) {
                   3430: #ifdef USE_SERVICE_THREAD
                   3431:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3432: #endif
                   3433:                                                        key_buf_data->write(tmp_data);
                   3434: #ifdef USE_SERVICE_THREAD
                   3435:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3436: #endif
                   3437:                                                }
1.1       root     3438:                                        }
1.1.1.24  root     3439:                                        result = key_changed = true;
1.1.1.36  root     3440:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3441:                                        cursor_moved = true;
1.1       root     3442:                                }
                   3443:                        }
                   3444:                }
                   3445:        }
1.1.1.35  root     3446: #ifdef USE_SERVICE_THREAD
                   3447:        LeaveCriticalSection(&input_crit_sect);
                   3448: #endif
1.1.1.24  root     3449:        return(result);
1.1.1.8   root     3450: }
                   3451: 
1.1.1.14  root     3452: bool update_key_buffer()
1.1.1.8   root     3453: {
1.1.1.35  root     3454:        if(update_console_input()) {
                   3455:                return(true);
                   3456:        }
                   3457:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3458: #ifdef USE_SERVICE_THREAD
                   3459:                EnterCriticalSection(&key_buf_crit_sect);
                   3460: #endif
1.1.1.55  root     3461:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     3462: #ifdef USE_SERVICE_THREAD
                   3463:                LeaveCriticalSection(&key_buf_crit_sect);
                   3464: #endif
                   3465:                if(!empty) return(true);
                   3466:        }
                   3467:        return(false);
1.1.1.8   root     3468: }
                   3469: 
1.1.1.20  root     3470: /* ----------------------------------------------------------------------------
                   3471:        MS-DOS virtual machine
                   3472: ---------------------------------------------------------------------------- */
                   3473: 
1.1.1.32  root     3474: static const struct {
1.1.1.33  root     3475:        char *name;
                   3476:        DWORD lcid;
                   3477:        char *std;
                   3478:        char *dlt;
                   3479: } tz_table[] = {
                   3480:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3481: //     0       GMT             Greenwich Mean Time             GMT0
                   3482:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3483:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3484:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3485:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3486: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3487:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3488:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3489: //     3       BST             Brazil Standard Time            BST3
                   3490:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3491:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3492:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3493: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3494:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3495: //     3       GST             Greenland Standard Time         GST3
                   3496:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3497: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3498:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3499: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3500:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3501: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3502:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3503:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3504: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3505:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3506:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3507:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3508: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3509:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3510: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3511:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3512: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3513:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3514: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3515:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3516:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3517:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3518: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3519:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3520: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3521:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3522:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3523:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3524: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3525:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3526:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3527: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3528: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3529:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3530: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3531:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3532:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3533: //     11      SST             Samoa Standard Time             SST11
                   3534:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3535: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3536:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3537: //     -10     GST             Guam Standard Time              GST-10
                   3538:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3539: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3540:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3541:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3542:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3543: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3544:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3545:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3546: //     -9      JST             Japan Standard Time             JST-9
                   3547:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3548: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3549:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3550:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3551: //     -8      HKT             Hong Kong Time                  HKT-8
                   3552:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3553: //     -8      CCT             China Coast Time                CCT-8
                   3554:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3555:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3556: //     -8      SST             Singapore Standard Time         SST-8
                   3557:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3558: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3559:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3560:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3561: //     -7:30   JT              Java Standard Time              JST-7:30
                   3562: //     -7      NST             North Sumatra Time              NST-7
                   3563:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3564: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3565:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3566: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3567:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3568: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3569:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3570:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3571: //     -2      EET             Eastern Europe Time             EET-2
                   3572:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3573:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3574:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3575:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3576: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3577:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3578: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3579: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3580: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3581: //     -1      CET     CES     Central European Time           CET-1CES
                   3582:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3583:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3584:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3585:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3586: //     -1      WAT             West African Time               WAT-1
                   3587:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3588:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3589: //     0       UTC             Universal Coordinated Time      UTC0
                   3590:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3591:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3592:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3593:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3594:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3595:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3596: };
                   3597: 
1.1.1.53  root     3598: // FIXME: consider to build on non-Japanese environment :-(
                   3599: // message_japanese string must be in shift-jis
                   3600: 
1.1.1.33  root     3601: static const struct {
1.1.1.32  root     3602:        UINT16 code;
                   3603:        char *message_english;
                   3604:        char *message_japanese;
                   3605: } standard_error_table[] = {
                   3606:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3607:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3608:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3609:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3610:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3611:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3612:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3613:        {0x08,  "Insufficient memory", "������������܂���."},
                   3614:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3615:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3616:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3617:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3618:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3619:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3620:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3621:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3622:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3623:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3624:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3625:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3626:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3627:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3628:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3629:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3630:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3631:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3632:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3633:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3634:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3635:        {0x1F,  "General failure", "�G���[�ł�."},
                   3636:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3637:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3638:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3639:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3640:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3641:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3642:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3643:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3644: /*
                   3645:        {0x32,  "Network request not supported", NULL},
                   3646:        {0x33,  "Remote computer not listening", NULL},
                   3647:        {0x34,  "Duplicate name on network", NULL},
                   3648:        {0x35,  "Network name not found", NULL},
                   3649:        {0x36,  "Network busy", NULL},
                   3650:        {0x37,  "Network device no longer exists", NULL},
                   3651:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3652:        {0x39,  "Network adapter hardware error", NULL},
                   3653:        {0x3A,  "Incorrect response from network", NULL},
                   3654:        {0x3B,  "Unexpected network error", NULL},
                   3655:        {0x3C,  "Incompatible remote adapter", NULL},
                   3656:        {0x3D,  "Print queue full", NULL},
                   3657:        {0x3E,  "Queue not full", NULL},
                   3658:        {0x3F,  "Not enough space to print file", NULL},
                   3659:        {0x40,  "Network name was deleted", NULL},
                   3660:        {0x41,  "Network: Access denied", NULL},
                   3661:        {0x42,  "Network device type incorrect", NULL},
                   3662:        {0x43,  "Network name not found", NULL},
                   3663:        {0x44,  "Network name limit exceeded", NULL},
                   3664:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3665:        {0x46,  "Temporarily paused", NULL},
                   3666:        {0x47,  "Network request not accepted", NULL},
                   3667:        {0x48,  "Network print/disk redirection paused", NULL},
                   3668:        {0x49,  "Network software not installed", NULL},
                   3669:        {0x4A,  "Unexpected adapter close", NULL},
                   3670: */
                   3671:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3672:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3673:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3674:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3675:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3676:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3677:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3678:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3679:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3680:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53  root     3681: #ifdef SUPPORT_MSCDEX
1.1.1.32  root     3682:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3683:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3684:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3685:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3686:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
1.1.1.53  root     3687: #endif
1.1.1.32  root     3688:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3689:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3690:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3691:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3692:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3693:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3694: };
                   3695: 
                   3696: static const struct {
                   3697:        UINT16 code;
                   3698:        char *message_english;
                   3699:        char *message_japanese;
                   3700: } param_error_table[] = {
                   3701:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3702:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3703:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3704:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3705:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3706:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3707:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3708:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3709:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3710:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3711:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3712: };
                   3713: 
                   3714: static const struct {
                   3715:        UINT16 code;
                   3716:        char *message_english;
                   3717:        char *message_japanese;
                   3718: } critical_error_table[] = {
                   3719:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3720:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3721:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3722:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3723:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3724:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3725:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3726:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3727:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3728:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3729:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3730:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3731:        {0x0C,  "General failure", "�G���[�ł�."},
                   3732:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3733:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3734:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3735:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3736:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3737:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3738:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3739:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3740:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3741: };
                   3742: 
1.1.1.20  root     3743: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3744: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3745: void msdos_putch(UINT8 data);
1.1.1.50  root     3746: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3747: #ifdef USE_SERVICE_THREAD
                   3748: void msdos_putch_tmp(UINT8 data);
                   3749: #endif
1.1.1.45  root     3750: const char *msdos_short_path(const char *path);
1.1.1.44  root     3751: bool msdos_is_valid_drive(int drv);
                   3752: bool msdos_is_removable_drive(int drv);
                   3753: bool msdos_is_cdrom_drive(int drv);
                   3754: bool msdos_is_remote_drive(int drv);
                   3755: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3756: 
1.1       root     3757: // process info
                   3758: 
                   3759: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3760: {
                   3761:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3762:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3763:                        memset(&process[i], 0, sizeof(process_t));
                   3764:                        process[i].psp = psp_seg;
                   3765:                        return(&process[i]);
                   3766:                }
                   3767:        }
                   3768:        fatalerror("too many processes\n");
                   3769:        return(NULL);
                   3770: }
                   3771: 
1.1.1.52  root     3772: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3773: {
                   3774:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3775:                if(process[i].psp == psp_seg) {
                   3776:                        return(&process[i]);
                   3777:                }
                   3778:        }
1.1.1.33  root     3779:        if(show_error) {
                   3780:                fatalerror("invalid psp address\n");
                   3781:        }
1.1       root     3782:        return(NULL);
                   3783: }
                   3784: 
1.1.1.23  root     3785: void msdos_sda_update(int psp_seg)
                   3786: {
                   3787:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3788:        
                   3789:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3790:                if(process[i].psp == psp_seg) {
                   3791:                        sda->switchar = process[i].switchar;
                   3792:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3793:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3794:                        sda->current_psp = process[i].psp;
                   3795:                        break;
                   3796:                }
                   3797:        }
                   3798:        sda->malloc_strategy = malloc_strategy;
                   3799:        sda->return_code = retval;
                   3800:        sda->current_drive = _getdrive();
                   3801: }
                   3802: 
1.1.1.13  root     3803: // dta info
                   3804: 
                   3805: void msdos_dta_info_init()
                   3806: {
1.1.1.14  root     3807:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3808:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3809:        }
                   3810: }
                   3811: 
                   3812: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3813: {
                   3814:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3815:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3816:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3817:                        if(free_dta == NULL) {
1.1.1.13  root     3818:                                free_dta = &dtalist[i];
                   3819:                        }
1.1.1.14  root     3820:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3821:                        return(&dtalist[i]);
                   3822:                }
                   3823:        }
1.1.1.14  root     3824:        if(free_dta) {
1.1.1.13  root     3825:                free_dta->psp = psp_seg;
                   3826:                free_dta->dta = dta_laddr;
                   3827:                return(free_dta);
                   3828:        }
                   3829:        fatalerror("too many dta\n");
                   3830:        return(NULL);
                   3831: }
                   3832: 
                   3833: void msdos_dta_info_free(UINT16 psp_seg)
                   3834: {
1.1.1.14  root     3835:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3836:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3837:                        FindClose(dtalist[i].find_handle);
                   3838:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3839:                }
                   3840:        }
                   3841: }
                   3842: 
1.1       root     3843: void msdos_cds_update(int drv)
                   3844: {
1.1.1.44  root     3845:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     3846:        
1.1.1.44  root     3847:        memset(cds, 0, 88);
                   3848:        
                   3849:        if(msdos_is_valid_drive(drv)) {
                   3850:                char path[MAX_PATH];
                   3851:                if(msdos_is_remote_drive(drv)) {
                   3852:                        cds->drive_attrib = 0xc000;     // network drive
                   3853:                } else if(msdos_is_subst_drive(drv)) {
                   3854:                        cds->drive_attrib = 0x5000;     // subst drive
                   3855:                } else {
                   3856:                        cds->drive_attrib = 0x4000;     // physical drive
                   3857:                }
                   3858:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   3859:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   3860:                }
                   3861:        }
                   3862:        if(cds->path_name[0] == '\0') {
                   3863:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3864:        }
                   3865:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   3866:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   3867:        cds->word_1 = cds->word_2 = 0xffff;
                   3868:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   3869:        cds->bs_offset = 2;
                   3870: }
                   3871: 
1.1.1.45  root     3872: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     3873: {
                   3874:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   3875:        
                   3876:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     3877: }
                   3878: 
1.1.1.17  root     3879: // nls information tables
                   3880: 
                   3881: // uppercase table (func 6502h)
                   3882: void msdos_upper_table_update()
                   3883: {
                   3884:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3885:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3886:                UINT8 c[4];
1.1.1.33  root     3887:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3888:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3889:                c[0] = 0x80 + i;
                   3890:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3891:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3892:        }
                   3893: }
                   3894: 
1.1.1.23  root     3895: // lowercase table (func 6503h)
                   3896: void msdos_lower_table_update()
                   3897: {
                   3898:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3899:        for(unsigned i = 0; i < 0x80; ++i) {
                   3900:                UINT8 c[4];
1.1.1.33  root     3901:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3902:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3903:                c[0] = 0x80 + i;
                   3904:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3905:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3906:        }
                   3907: }
                   3908: 
1.1.1.17  root     3909: // filename uppercase table (func 6504h)
                   3910: void msdos_filename_upper_table_init()
                   3911: {
                   3912:        // depended on (file)system, not on active codepage
                   3913:        // temporary solution: just filling data
                   3914:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3915:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3916:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3917:        }
                   3918: }
                   3919: 
                   3920: // filaname terminator table (func 6505h)
                   3921: void msdos_filename_terminator_table_init()
                   3922: {
                   3923:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3924:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3925:        
                   3926:        data[2] = 1;            // marker? (permissible character value)
                   3927:        data[3] = 0x00;         // 00h...FFh
                   3928:        data[4] = 0xff;
                   3929:        data[5] = 0;            // marker? (excluded character)
                   3930:        data[6] = 0x00;         // 00h...20h
                   3931:        data[7] = 0x20;
                   3932:        data[8] = 2;            // marker? (illegal characters for filename)
                   3933:        data[9] = (UINT8)strlen(illegal_chars);
                   3934:        memcpy(data + 10, illegal_chars, data[9]);
                   3935:        
                   3936:        // total length
                   3937:        *(UINT16 *)data = (10 - 2) + data[9];
                   3938: }
                   3939: 
                   3940: // collating table (func 6506h)
                   3941: void msdos_collating_table_update()
                   3942: {
                   3943:        // temporary solution: just filling data
                   3944:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3945:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3946:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3947:        }
                   3948: }
                   3949: 
1.1       root     3950: // dbcs
                   3951: 
                   3952: void msdos_dbcs_table_update()
                   3953: {
                   3954:        UINT8 dbcs_data[DBCS_SIZE];
                   3955:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3956:        
                   3957:        CPINFO info;
                   3958:        GetCPInfo(active_code_page, &info);
                   3959:        
                   3960:        if(info.MaxCharSize != 1) {
                   3961:                for(int i = 0;; i += 2) {
                   3962:                        UINT8 lo = info.LeadByte[i + 0];
                   3963:                        UINT8 hi = info.LeadByte[i + 1];
                   3964:                        dbcs_data[2 + i + 0] = lo;
                   3965:                        dbcs_data[2 + i + 1] = hi;
                   3966:                        if(lo == 0 && hi == 0) {
                   3967:                                dbcs_data[0] = i + 2;
                   3968:                                break;
                   3969:                        }
                   3970:                }
                   3971:        } else {
                   3972:                dbcs_data[0] = 2;       // ???
                   3973:        }
                   3974:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   3975: }
                   3976: 
1.1.1.17  root     3977: void msdos_dbcs_table_finish()
                   3978: {
1.1.1.32  root     3979:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     3980:                _setmbcp(system_code_page);
                   3981:        }
1.1.1.32  root     3982:        if(console_code_page != GetConsoleCP()) {
                   3983:                SetConsoleCP(console_code_page);
                   3984:                SetConsoleOutputCP(console_code_page);
                   3985:        }
1.1.1.17  root     3986: }
                   3987: 
                   3988: void msdos_nls_tables_init()
1.1       root     3989: {
1.1.1.32  root     3990:        active_code_page = console_code_page = GetConsoleCP();
                   3991:        system_code_page = _getmbcp();
                   3992:        
                   3993:        if(active_code_page != system_code_page) {
                   3994:                if(_setmbcp(active_code_page) != 0) {
                   3995:                        active_code_page = system_code_page;
                   3996:                }
                   3997:        }
                   3998:        
1.1.1.17  root     3999:        msdos_upper_table_update();
1.1.1.23  root     4000:        msdos_lower_table_update();
1.1.1.17  root     4001:        msdos_filename_terminator_table_init();
                   4002:        msdos_filename_upper_table_init();
                   4003:        msdos_collating_table_update();
1.1       root     4004:        msdos_dbcs_table_update();
                   4005: }
                   4006: 
1.1.1.17  root     4007: void msdos_nls_tables_update()
1.1       root     4008: {
1.1.1.17  root     4009:        msdos_dbcs_table_update();
                   4010:        msdos_upper_table_update();
1.1.1.23  root     4011:        msdos_lower_table_update();
                   4012: //     msdos_collating_table_update();
1.1       root     4013: }
                   4014: 
                   4015: int msdos_lead_byte_check(UINT8 code)
                   4016: {
                   4017:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   4018:        
                   4019:        for(int i = 0;; i += 2) {
                   4020:                UINT8 lo = dbcs_table[i + 0];
                   4021:                UINT8 hi = dbcs_table[i + 1];
                   4022:                if(lo == 0 && hi == 0) {
                   4023:                        break;
                   4024:                }
                   4025:                if(lo <= code && code <= hi) {
                   4026:                        return(1);
                   4027:                }
                   4028:        }
                   4029:        return(0);
                   4030: }
                   4031: 
1.1.1.20  root     4032: int msdos_ctrl_code_check(UINT8 code)
                   4033: {
1.1.1.22  root     4034:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     4035: }
                   4036: 
1.1.1.36  root     4037: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   4038: {
                   4039:        int is_kanji_1st = 0;
                   4040:        int is_kanji_2nd = 0;
                   4041:        
                   4042:        for(int p = 0;; p++) {
                   4043:                if(is_kanji_1st) {
                   4044:                        is_kanji_1st = 0;
                   4045:                        is_kanji_2nd = 1;
                   4046:                } else if(msdos_lead_byte_check(buf[p])) {
                   4047:                        is_kanji_1st = 1;
                   4048:                }
                   4049:                if(p == n) {
                   4050:                        return(is_kanji_2nd);
                   4051:                }
                   4052:                is_kanji_2nd = 0;
                   4053:        }
                   4054: }
                   4055: 
1.1       root     4056: // file control
                   4057: 
1.1.1.45  root     4058: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     4059: {
                   4060:        static char tmp[MAX_PATH];
                   4061:        
                   4062:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     4063:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     4064:                memcpy(tmp, path + 1, strlen(path) - 2);
                   4065:        } else {
                   4066:                strcpy(tmp, path);
                   4067:        }
                   4068:        return(tmp);
                   4069: }
                   4070: 
1.1.1.45  root     4071: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     4072: {
                   4073:        static char tmp[MAX_PATH];
                   4074:        
                   4075:        strcpy(tmp, path);
1.1.1.45  root     4076:        
                   4077:        // for example "C:\" case, the end separator should not be removed
                   4078:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   4079:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     4080:        }
                   4081:        return(tmp);
                   4082: }
                   4083: 
1.1.1.45  root     4084: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     4085: {
                   4086:        static char tmp[MAX_PATH];
1.1.1.45  root     4087:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     4088:        
                   4089:        if(strlen(tmp_dir) == 0) {
                   4090:                strcpy(tmp, file);
                   4091:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   4092:                sprintf(tmp, "%s%s", tmp_dir, file);
                   4093:        } else {
                   4094:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   4095:        }
                   4096:        return(tmp);
                   4097: }
                   4098: 
1.1.1.45  root     4099: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     4100: {
                   4101:        static char tmp[MAX_PATH];
                   4102:        
                   4103:        if(lfn) {
                   4104:                strcpy(tmp, path);
                   4105:        } else {
                   4106:                // remove space in the path
1.1.1.45  root     4107:                const char *src = path;
                   4108:                char *dst = tmp;
1.1       root     4109:                
                   4110:                while(*src != '\0') {
                   4111:                        if(msdos_lead_byte_check(*src)) {
                   4112:                                *dst++ = *src++;
                   4113:                                *dst++ = *src++;
                   4114:                        } else if(*src != ' ') {
                   4115:                                *dst++ = *src++;
                   4116:                        } else {
                   4117:                                src++;  // skip space
                   4118:                        }
                   4119:                }
                   4120:                *dst = '\0';
                   4121:        }
1.1.1.14  root     4122:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   4123:                // redirect C:\COMMAND.COM to comspec_path
                   4124:                strcpy(tmp, comspec_path);
                   4125:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   4126:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   4127:                static int root_drive_protected = -1;
                   4128:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   4129:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   4130:                
                   4131:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   4132:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   4133:                        strcpy(name, name_temp);
                   4134:                        name_temp[0] = '\0';
                   4135:                        
                   4136:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   4137:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   4138:                                if(root_drive_protected == -1) {
                   4139:                                        FILE *fp = NULL;
                   4140:                                        
                   4141:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   4142:                                        root_drive_protected = 1;
                   4143:                                        try {
                   4144:                                                if((fp = fopen(temp, "w")) != NULL) {
                   4145:                                                        if(fprintf(fp, "TEST") == 4) {
                   4146:                                                                root_drive_protected = 0;
                   4147:                                                        }
                   4148:                                                }
                   4149:                                        } catch(...) {
                   4150:                                        }
                   4151:                                        if(fp != NULL) {
                   4152:                                                fclose(fp);
                   4153:                                        }
                   4154:                                        if(_access(temp, 0) == 0) {
                   4155:                                                remove(temp);
                   4156:                                        }
                   4157:                                }
                   4158:                                if(root_drive_protected == 1) {
                   4159:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   4160:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   4161:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4162:                                        }
                   4163:                                }
                   4164:                        }
                   4165:                }
                   4166:        }
1.1       root     4167:        return(tmp);
                   4168: }
                   4169: 
1.1.1.45  root     4170: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4171: {
1.1.1.32  root     4172:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4173:        static char env_path[ENV_SIZE];
                   4174:        char tmp[ENV_SIZE], *token;
                   4175:        
                   4176:        memset(env_path, 0, sizeof(env_path));
                   4177:        strcpy(tmp, src);
                   4178:        token = my_strtok(tmp, ";");
                   4179:        
                   4180:        while(token != NULL) {
                   4181:                if(token[0] != '\0') {
1.1.1.45  root     4182:                        const char *path = msdos_remove_double_quote(token);
                   4183:                        char short_path[MAX_PATH];
1.1.1.32  root     4184:                        if(path != NULL && strlen(path) != 0) {
                   4185:                                if(env_path[0] != '\0') {
                   4186:                                        strcat(env_path, ";");
                   4187:                                }
1.1.1.28  root     4188:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4189:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4190:                                } else {
                   4191:                                        my_strupr(short_path);
1.1.1.32  root     4192:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4193:                                }
                   4194:                        }
                   4195:                }
                   4196:                token = my_strtok(NULL, ";");
                   4197:        }
                   4198:        return(env_path);
                   4199: }
                   4200: 
1.1.1.45  root     4201: bool match(const char *text, const char *pattern)
1.1       root     4202: {
1.1.1.24  root     4203:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4204:        switch(*pattern) {
1.1       root     4205:        case '\0':
                   4206:                return !*text;
                   4207:        case '*':
1.1.1.14  root     4208:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4209:        case '?':
                   4210:                return *text && match(text + 1, pattern + 1);
                   4211:        default:
                   4212:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4213:        }
                   4214: }
                   4215: 
1.1.1.45  root     4216: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4217: {
1.1.1.45  root     4218:        const char *p = NULL;
1.1       root     4219:        
1.1.1.14  root     4220:        if(!*volume) {
                   4221:                return false;
                   4222:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4223:                return msdos_match_volume_label(p + 1, volume);
                   4224:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4225:                return msdos_match_volume_label(p + 1, volume);
                   4226:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4227:                char tmp[MAX_PATH];
                   4228:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4229:                return match(volume, tmp);
1.1       root     4230:        } else {
                   4231:                return match(volume, path);
                   4232:        }
                   4233: }
                   4234: 
1.1.1.45  root     4235: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4236: {
                   4237:        static char tmp[MAX_PATH];
                   4238:        char name[9], ext[4];
                   4239:        
                   4240:        memset(name, 0, sizeof(name));
                   4241:        memcpy(name, fcb->file_name, 8);
                   4242:        strcpy(name, msdos_trimmed_path(name, 0));
                   4243:        
                   4244:        memset(ext, 0, sizeof(ext));
                   4245:        memcpy(ext, fcb->file_name + 8, 3);
                   4246:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4247:        
                   4248:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4249:                strcpy(name, "*");
                   4250:        }
                   4251:        if(ext[0] == '\0') {
                   4252:                strcpy(tmp, name);
                   4253:        } else {
                   4254:                if(strcmp(ext, "???") == 0) {
                   4255:                        strcpy(ext, "*");
                   4256:                }
                   4257:                sprintf(tmp, "%s.%s", name, ext);
                   4258:        }
                   4259:        return(tmp);
                   4260: }
                   4261: 
1.1.1.45  root     4262: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4263: {
                   4264:        char *ext = my_strchr(path, '.');
                   4265:        
                   4266:        memset(fcb->file_name, 0x20, 8 + 3);
                   4267:        if(ext != NULL && path[0] != '.') {
                   4268:                *ext = '\0';
                   4269:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4270:        }
                   4271:        memcpy(fcb->file_name, path, strlen(path));
                   4272: }
                   4273: 
1.1.1.45  root     4274: const char *msdos_short_path(const char *path)
1.1       root     4275: {
                   4276:        static char tmp[MAX_PATH];
                   4277:        
1.1.1.24  root     4278:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4279:                strcpy(tmp, path);
                   4280:        }
1.1       root     4281:        my_strupr(tmp);
                   4282:        return(tmp);
                   4283: }
                   4284: 
1.1.1.45  root     4285: const char *msdos_short_name(WIN32_FIND_DATA *fd)
1.1.1.13  root     4286: {
                   4287:        static char tmp[MAX_PATH];
1.1.1.45  root     4288:        
1.1.1.14  root     4289:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4290:                strcpy(tmp, fd->cAlternateFileName);
                   4291:        } else {
                   4292:                strcpy(tmp, fd->cFileName);
                   4293:        }
                   4294:        my_strupr(tmp);
                   4295:        return(tmp);
                   4296: }
                   4297: 
1.1.1.45  root     4298: const char *msdos_short_full_path(const char *path)
1.1       root     4299: {
                   4300:        static char tmp[MAX_PATH];
                   4301:        char full[MAX_PATH], *name;
                   4302:        
1.1.1.14  root     4303:        // Full works with non-existent files, but Short does not
1.1       root     4304:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4305:        *tmp = '\0';
                   4306:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4307:                name[-1] = '\0';
                   4308:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4309:                if(len == 0) {
                   4310:                        strcpy(tmp, full);
                   4311:                } else {
                   4312:                        tmp[len++] = '\\';
                   4313:                        strcpy(tmp + len, name);
                   4314:                }
                   4315:        }
1.1       root     4316:        my_strupr(tmp);
                   4317:        return(tmp);
                   4318: }
                   4319: 
1.1.1.45  root     4320: const char *msdos_short_full_dir(const char *path)
1.1       root     4321: {
                   4322:        static char tmp[MAX_PATH];
                   4323:        char full[MAX_PATH], *name;
                   4324:        
                   4325:        GetFullPathName(path, MAX_PATH, full, &name);
                   4326:        name[-1] = '\0';
1.1.1.24  root     4327:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4328:                strcpy(tmp, full);
                   4329:        }
1.1       root     4330:        my_strupr(tmp);
                   4331:        return(tmp);
                   4332: }
                   4333: 
1.1.1.45  root     4334: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4335: {
1.1.1.45  root     4336:        static char trimmed[MAX_PATH];
                   4337:        
                   4338:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4339: #if 0
                   4340:        // I have forgotten the reason of this routine... :-(
1.1       root     4341:        if(_access(trimmed, 0) != 0) {
                   4342:                process_t *process = msdos_process_info_get(current_psp);
                   4343:                static char tmp[MAX_PATH];
                   4344:                
                   4345:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4346:                if(_access(tmp, 0) == 0) {
                   4347:                        return(tmp);
                   4348:                }
                   4349:        }
1.1.1.14  root     4350: #endif
1.1       root     4351:        return(trimmed);
                   4352: }
                   4353: 
1.1.1.45  root     4354: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4355: {
                   4356:        char full[MAX_PATH], *name;
                   4357:        
1.1.1.24  root     4358:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4359:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4360:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4361:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4362:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4363:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4364:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4365:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4366:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4367:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4368:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4369:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4370:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4371:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4372:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4373:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4374:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4375:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4376:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4377:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4378:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4379:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4380:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4381:                        return(true);
                   4382:                } else if(name != NULL) {
                   4383:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4384:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4385:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4386: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4387:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4388:                                return(true);
                   4389:                        }
                   4390:                }
1.1.1.24  root     4391:        }
                   4392:        return(false);
1.1.1.11  root     4393: }
                   4394: 
1.1.1.45  root     4395: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4396: {
1.1.1.14  root     4397:        char full[MAX_PATH], *name;
1.1.1.8   root     4398:        
1.1.1.24  root     4399:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4400:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4401:        }
                   4402:        return(false);
                   4403: }
                   4404: 
1.1.1.45  root     4405: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4406: {
                   4407:        char full[MAX_PATH], *name;
                   4408:        
                   4409:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4410:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4411:                        return(1);
                   4412:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4413:                        return(2);
                   4414:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4415:                        return(3);
                   4416:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4417:                        return(4);
1.1.1.24  root     4418:                }
                   4419:        }
1.1.1.29  root     4420:        return(0);
                   4421: }
                   4422: 
1.1.1.45  root     4423: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4424: {
                   4425:        // 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     4426:        const char *p = NULL;
1.1.1.37  root     4427:        
                   4428:        if((p = strstr(path, ":")) != NULL) {
                   4429:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4430:                
                   4431:                // baud rate
                   4432:                int baud = max(110, min(9600, atoi(p + 1)));
                   4433:                UINT16 divisor = 115200 / baud;
                   4434:                
                   4435:                if((p = strstr(p + 1, ",")) != NULL) {
                   4436:                        // parity
                   4437:                        if(p[1] == 'N' || p[1] == 'n') {
                   4438:                                selector = (selector & ~0x38) | 0x00;
                   4439:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4440:                                selector = (selector & ~0x38) | 0x08;
                   4441:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4442:                                selector = (selector & ~0x38) | 0x18;
                   4443:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4444:                                selector = (selector & ~0x38) | 0x28;
                   4445:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4446:                                selector = (selector & ~0x38) | 0x38;
                   4447:                        }
                   4448:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4449:                                // word length
                   4450:                                if(p[1] == '8') {
                   4451:                                        selector = (selector & ~0x03) | 0x03;
                   4452:                                } else if(p[1] == '7') {
                   4453:                                        selector = (selector & ~0x03) | 0x02;
                   4454:                                } else if(p[1] == '6') {
                   4455:                                        selector = (selector & ~0x03) | 0x01;
                   4456:                                } else if(p[1] == '5') {
                   4457:                                        selector = (selector & ~0x03) | 0x00;
                   4458:                                }
                   4459:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4460:                                        // stop bits
                   4461:                                        float bits = atof(p + 1);
                   4462:                                        if(bits > 1.0F) {
                   4463:                                                selector |= 0x04;
                   4464:                                        } else {
                   4465:                                                selector &= ~0x04;
                   4466:                                        }
                   4467:                                }
                   4468:                        }
                   4469:                }
                   4470:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4471:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4472:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4473:                sio_write(sio_port - 1, 3, selector);
                   4474:        }
                   4475: }
                   4476: 
1.1.1.45  root     4477: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4478: {
                   4479:        char full[MAX_PATH], *name;
                   4480:        
                   4481:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4482:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4483:                        return(1);
                   4484:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4485:                        return(1);
                   4486:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4487:                        return(2);
                   4488:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4489:                        return(3);
                   4490:                }
                   4491:        }
                   4492:        return(0);
                   4493: }
                   4494: 
1.1.1.44  root     4495: bool msdos_is_valid_drive(int drv)
                   4496: {
                   4497:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4498: }
                   4499: 
                   4500: bool msdos_is_removable_drive(int drv)
                   4501: {
                   4502:        char volume[] = "A:\\";
                   4503:        
                   4504:        volume[0] = 'A' + drv;
                   4505:        
                   4506:        return(GetDriveType(volume) == DRIVE_REMOVABLE);
                   4507: }
                   4508: 
                   4509: bool msdos_is_cdrom_drive(int drv)
                   4510: {
                   4511:        char volume[] = "A:\\";
                   4512:        
                   4513:        volume[0] = 'A' + drv;
                   4514:        
                   4515:        return(GetDriveType(volume) == DRIVE_CDROM);
                   4516: }
                   4517: 
                   4518: bool msdos_is_remote_drive(int drv)
                   4519: {
                   4520:        char volume[] = "A:\\";
                   4521:        
                   4522:        volume[0] = 'A' + drv;
                   4523:        
                   4524:        return(GetDriveType(volume) == DRIVE_REMOTE);
                   4525: }
                   4526: 
                   4527: bool msdos_is_subst_drive(int drv)
                   4528: {
                   4529:        char device[] = "A:", path[MAX_PATH];
                   4530:        
                   4531:        device[0] = 'A' + drv;
                   4532:        
                   4533:        if(QueryDosDevice(device, path, MAX_PATH)) {
                   4534:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4535:                        return(true);
                   4536:                }
                   4537:        }
                   4538:        return(false);
                   4539: }
                   4540: 
1.1.1.45  root     4541: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4542: {
                   4543:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4544:        WIN32_FIND_DATA FindData;
                   4545:        HANDLE hFind;
                   4546:        
                   4547:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4548:                FindClose(hFind);
                   4549:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4550:        }
                   4551:        return(false);
1.1.1.8   root     4552: }
                   4553: 
1.1.1.45  root     4554: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4555: {
                   4556:        static char tmp[MAX_PATH];
1.1.1.28  root     4557:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4558:        
1.1.1.28  root     4559:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4560:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4561:                sprintf(file_name, "COMMAND.COM");
                   4562:                if(_access(tmp, 0) == 0) {
                   4563:                        return(tmp);
                   4564:                }
                   4565:        }
1.1.1.28  root     4566:        
                   4567:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4568:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4569:                sprintf(file_name, "COMMAND.COM");
                   4570:                if(_access(tmp, 0) == 0) {
                   4571:                        return(tmp);
                   4572:                }
                   4573:        }
1.1.1.28  root     4574:        
                   4575:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4576:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4577:                if(_access(tmp, 0) == 0) {
                   4578:                        return(tmp);
                   4579:                }
                   4580:        }
1.1.1.28  root     4581:        
                   4582:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4583:        strcpy(path, env_path);
                   4584:        char *token = my_strtok(path, ";");
1.1.1.9   root     4585:        while(token != NULL) {
1.1.1.14  root     4586:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4587:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4588:                        if(_access(tmp, 0) == 0) {
                   4589:                                return(tmp);
                   4590:                        }
                   4591:                }
                   4592:                token = my_strtok(NULL, ";");
                   4593:        }
                   4594:        return(NULL);
                   4595: }
                   4596: 
1.1.1.14  root     4597: int msdos_drive_number(const char *path)
1.1       root     4598: {
                   4599:        char tmp[MAX_PATH], *name;
                   4600:        
1.1.1.45  root     4601:        if(GetFullPathName(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
                   4602:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4603:                        return(tmp[0] - 'a');
                   4604:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4605:                        return(tmp[0] - 'A');
                   4606:                }
1.1       root     4607:        }
1.1.1.45  root     4608: //     return(msdos_drive_number("."));
                   4609:        return(_getdrive() - 1);
1.1       root     4610: }
                   4611: 
1.1.1.45  root     4612: const char *msdos_volume_label(const char *path)
1.1       root     4613: {
                   4614:        static char tmp[MAX_PATH];
                   4615:        char volume[] = "A:\\";
                   4616:        
                   4617:        if(path[1] == ':') {
                   4618:                volume[0] = path[0];
                   4619:        } else {
                   4620:                volume[0] = 'A' + _getdrive() - 1;
                   4621:        }
                   4622:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4623:                memset(tmp, 0, sizeof(tmp));
                   4624:        }
                   4625:        return(tmp);
                   4626: }
                   4627: 
1.1.1.45  root     4628: const char *msdos_short_volume_label(const char *label)
1.1       root     4629: {
                   4630:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4631:        const char *src = label;
1.1       root     4632:        int remain = strlen(label);
                   4633:        char *dst_n = tmp;
                   4634:        char *dst_e = tmp + 9;
                   4635:        
                   4636:        strcpy(tmp, "        .   ");
                   4637:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4638:                if(msdos_lead_byte_check(*src)) {
                   4639:                        if(++i == 8) {
                   4640:                                break;
                   4641:                        }
                   4642:                        *dst_n++ = *src++;
                   4643:                        remain--;
                   4644:                }
                   4645:                *dst_n++ = *src++;
                   4646:                remain--;
                   4647:        }
                   4648:        if(remain > 0) {
                   4649:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4650:                        if(msdos_lead_byte_check(*src)) {
                   4651:                                if(++i == 3) {
                   4652:                                        break;
                   4653:                                }
                   4654:                                *dst_e++ = *src++;
                   4655:                                remain--;
                   4656:                        }
                   4657:                        *dst_e++ = *src++;
                   4658:                        remain--;
                   4659:                }
                   4660:                *dst_e = '\0';
                   4661:        } else {
                   4662:                *dst_n = '\0';
                   4663:        }
                   4664:        my_strupr(tmp);
                   4665:        return(tmp);
                   4666: }
                   4667: 
1.1.1.13  root     4668: errno_t msdos_maperr(unsigned long oserrno)
                   4669: {
                   4670:        _doserrno = oserrno;
1.1.1.14  root     4671:        switch(oserrno) {
1.1.1.13  root     4672:        case ERROR_FILE_NOT_FOUND:         // 2
                   4673:        case ERROR_PATH_NOT_FOUND:         // 3
                   4674:        case ERROR_INVALID_DRIVE:          // 15
                   4675:        case ERROR_NO_MORE_FILES:          // 18
                   4676:        case ERROR_BAD_NETPATH:            // 53
                   4677:        case ERROR_BAD_NET_NAME:           // 67
                   4678:        case ERROR_BAD_PATHNAME:           // 161
                   4679:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4680:                return ENOENT;
                   4681:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4682:                return EMFILE;
                   4683:        case ERROR_ACCESS_DENIED:          // 5
                   4684:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4685:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4686:        case ERROR_CANNOT_MAKE:            // 82
                   4687:        case ERROR_FAIL_I24:               // 83
                   4688:        case ERROR_DRIVE_LOCKED:           // 108
                   4689:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4690:        case ERROR_NOT_LOCKED:             // 158
                   4691:        case ERROR_LOCK_FAILED:            // 167
                   4692:                return EACCES;
                   4693:        case ERROR_INVALID_HANDLE:         // 6
                   4694:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4695:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4696:                return EBADF;
                   4697:        case ERROR_ARENA_TRASHED:          // 7
                   4698:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4699:        case ERROR_INVALID_BLOCK:          // 9
                   4700:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4701:                return ENOMEM;
                   4702:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4703:                return E2BIG;
                   4704:        case ERROR_BAD_FORMAT:             // 11
                   4705:                return ENOEXEC;
                   4706:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4707:                return EXDEV;
                   4708:        case ERROR_FILE_EXISTS:            // 80
                   4709:        case ERROR_ALREADY_EXISTS:         // 183
                   4710:                return EEXIST;
                   4711:        case ERROR_NO_PROC_SLOTS:          // 89
                   4712:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4713:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4714:                return EAGAIN;
                   4715:        case ERROR_BROKEN_PIPE:            // 109
                   4716:                return EPIPE;
                   4717:        case ERROR_DISK_FULL:              // 112
                   4718:                return ENOSPC;
                   4719:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4720:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4721:                return ECHILD;
                   4722:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4723:                return ENOTEMPTY;
                   4724:        }
1.1.1.14  root     4725:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4726:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4727:                return EACCES;
                   4728:        }
1.1.1.14  root     4729:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4730:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4731:                return ENOEXEC;
                   4732:        }
                   4733:        return EINVAL;
                   4734: }
                   4735: 
1.1.1.45  root     4736: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4737: {
1.1.1.14  root     4738:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4739:                return(_open(path, oflag));
1.1.1.13  root     4740:        }
1.1.1.14  root     4741:        
                   4742:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4743:        DWORD disposition;
1.1.1.14  root     4744:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4745:        default:
1.1.1.13  root     4746:        case _O_EXCL:
                   4747:                disposition = OPEN_EXISTING;
                   4748:                break;
                   4749:        case _O_CREAT:
                   4750:                disposition = OPEN_ALWAYS;
                   4751:                break;
                   4752:        case _O_CREAT | _O_EXCL:
                   4753:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4754:                disposition = CREATE_NEW;
                   4755:                break;
                   4756:        case _O_TRUNC:
                   4757:        case _O_TRUNC | _O_EXCL:
                   4758:                disposition = TRUNCATE_EXISTING;
                   4759:                break;
                   4760:        case _O_CREAT | _O_TRUNC:
                   4761:                disposition = CREATE_ALWAYS;
                   4762:                break;
                   4763:        }
1.1.1.14  root     4764:        
1.1.1.45  root     4765:        HANDLE h = CreateFile(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4766:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4767:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4768:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4769:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4770:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.45  root     4771:                h = CreateFile(path, GENERIC_READ,
1.1.1.13  root     4772:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4773:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4774:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4775:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4776:                        return(-1);
1.1.1.13  root     4777:                }
                   4778:        }
1.1.1.14  root     4779:        
1.1.1.13  root     4780:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4781:        if(fd == -1) {
1.1.1.13  root     4782:                CloseHandle(h);
                   4783:        }
1.1.1.45  root     4784:        return(fd);
                   4785: }
                   4786: 
                   4787: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4788: {
                   4789:        int fd = -1;
                   4790:        
                   4791:        *sio_port = *lpt_port = 0;
                   4792:        
                   4793:        if(msdos_is_con_path(path)) {
                   4794:                // MODE.COM opens CON device with read/write mode :-(
                   4795:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   4796:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   4797:                        oflag |= _O_RDONLY;
                   4798:                }
                   4799:                if((fd = msdos_open("CON", oflag)) == -1) {
                   4800: //                     fd = msdos_open("NUL", oflag);
                   4801:                }
                   4802:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   4803:                fd = msdos_open("NUL", oflag);
                   4804:                msdos_set_comm_params(*sio_port, path);
                   4805:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   4806:                fd = msdos_open("NUL", oflag);
                   4807:        } else if(msdos_is_device_path(path)) {
                   4808:                fd = msdos_open("NUL", oflag);
                   4809: //     } else if(oflag & _O_CREAT) {
                   4810: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   4811: //     } else {
                   4812: //             fd = _open(path, oflag);
                   4813:        }
                   4814:        return(fd);
                   4815: }
                   4816: 
                   4817: UINT16 msdos_device_info(const char *path)
                   4818: {
                   4819:        if(msdos_is_con_path(path)) {
                   4820:                return(0x80d3);
                   4821:        } else if(msdos_is_comm_path(path)) {
                   4822:                return(0x80a0);
                   4823:        } else if(msdos_is_prn_path(path)) {
                   4824: //             return(0xa8c0);
                   4825:                return(0x80a0);
                   4826:        } else if(msdos_is_device_path(path)) {
                   4827:                if(strstr(path, "EMMXXXX0") != NULL) {
                   4828:                        return(0xc0c0);
                   4829:                } else if(strstr(path, "MSCD001") != NULL) {
                   4830:                        return(0xc880);
                   4831:                } else {
                   4832:                        return(0x8084);
                   4833:                }
                   4834:        } else {
                   4835:                return(msdos_drive_number(path));
                   4836:        }
1.1.1.13  root     4837: }
                   4838: 
1.1.1.52  root     4839: 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     4840: {
                   4841:        static int id = 0;
                   4842:        char full[MAX_PATH], *name;
                   4843:        
                   4844:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4845:                strcpy(file_handler[fd].path, full);
                   4846:        } else {
                   4847:                strcpy(file_handler[fd].path, path);
                   4848:        }
1.1.1.14  root     4849:        // isatty makes no distinction between CON & NUL
                   4850:        // GetFileSize fails on CON, succeeds on NUL
                   4851:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     4852:                if(info == 0x80d3) {
                   4853:                        info = 0x8084;
                   4854:                }
1.1.1.14  root     4855:                atty = 0;
                   4856:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     4857: //             info = msdos_drive_number(".");
                   4858:                info = msdos_drive_number(path);
1.1.1.14  root     4859:        }
1.1       root     4860:        file_handler[fd].valid = 1;
                   4861:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     4862:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4863:        file_handler[fd].mode = mode;
                   4864:        file_handler[fd].info = info;
                   4865:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     4866:        file_handler[fd].sio_port = sio_port;
                   4867:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4868:        
                   4869:        // init system file table
                   4870:        if(fd < 20) {
                   4871:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4872:                
                   4873:                memset(sft, 0, 0x3b);
                   4874:                
                   4875:                *(UINT16 *)(sft + 0x00) = 1;
                   4876:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4877:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4878:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4879:                
                   4880:                if(!(file_handler[fd].info & 0x80)) {
                   4881:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4882:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4883:                        
                   4884:                        FILETIME time, local;
                   4885:                        HANDLE hHandle;
                   4886:                        WORD dos_date = 0, dos_time = 0;
                   4887:                        DWORD file_size = 0;
                   4888:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4889:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4890:                                        FileTimeToLocalFileTime(&time, &local);
                   4891:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4892:                                }
                   4893:                                file_size = GetFileSize(hHandle, NULL);
                   4894:                        }
                   4895:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4896:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4897:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4898:                }
                   4899:                
                   4900:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4901:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4902:                my_strupr(fname);
                   4903:                my_strupr(ext);
                   4904:                memset(sft + 0x20, 0x20, 11);
                   4905:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4906:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4907:                
                   4908:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4909:        }
1.1       root     4910: }
                   4911: 
                   4912: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4913: {
                   4914:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4915:        file_handler[dst].valid = 1;
                   4916:        file_handler[dst].id = file_handler[src].id;
                   4917:        file_handler[dst].atty = file_handler[src].atty;
                   4918:        file_handler[dst].mode = file_handler[src].mode;
                   4919:        file_handler[dst].info = file_handler[src].info;
                   4920:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     4921:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   4922:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4923: }
                   4924: 
1.1.1.20  root     4925: void msdos_file_handler_close(int fd)
1.1       root     4926: {
                   4927:        file_handler[fd].valid = 0;
1.1.1.21  root     4928:        
                   4929:        if(fd < 20) {
                   4930:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4931:        }
1.1       root     4932: }
                   4933: 
1.1.1.14  root     4934: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4935: {
1.1.1.14  root     4936:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4937:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4938:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4939: }
                   4940: 
                   4941: // find file
                   4942: 
                   4943: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4944: {
                   4945:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4946:                return(0);      // search directory only !!!
                   4947:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4948:                return(0);
                   4949:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4950:                return(0);
                   4951:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4952:                return(0);
                   4953:        } else if((attribute & required_mask) != required_mask) {
                   4954:                return(0);
                   4955:        } else {
                   4956:                return(1);
                   4957:        }
                   4958: }
                   4959: 
1.1.1.13  root     4960: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4961: {
1.1.1.14  root     4962:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     4963:                return(1);
1.1.1.13  root     4964:        }
                   4965:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4966:        if(len > 12) {
1.1.1.42  root     4967:                return(0);
1.1.1.13  root     4968:        }
                   4969:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4970:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     4971:                return(0);
1.1.1.13  root     4972:        }
1.1.1.42  root     4973:        return(1);
1.1.1.13  root     4974: }
                   4975: 
1.1       root     4976: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4977: {
                   4978:        FILETIME local;
                   4979:        
                   4980:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4981:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4982:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4983:        
                   4984:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4985:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4986:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4987:        
                   4988:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4989:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4990:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4991: }
                   4992: 
                   4993: // i/o
                   4994: 
                   4995: void msdos_stdio_reopen()
                   4996: {
                   4997:        if(!file_handler[0].valid) {
                   4998:                _dup2(DUP_STDIN, 0);
                   4999:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   5000:        }
                   5001:        if(!file_handler[1].valid) {
                   5002:                _dup2(DUP_STDOUT, 1);
                   5003:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   5004:        }
                   5005:        if(!file_handler[2].valid) {
                   5006:                _dup2(DUP_STDERR, 2);
                   5007:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   5008:        }
1.1.1.21  root     5009:        if(!file_handler[3].valid) {
                   5010:                _dup2(DUP_STDAUX, 3);
                   5011:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   5012:        }
                   5013:        if(!file_handler[4].valid) {
                   5014:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     5015: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   5016:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     5017:        }
                   5018:        for(int i = 0; i < 5; i++) {
                   5019:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   5020:                        msdos_psp_set_file_table(i, i, current_psp);
                   5021:                }
                   5022:        }
1.1       root     5023: }
                   5024: 
1.1.1.37  root     5025: int msdos_read(int fd, void *buffer, unsigned int count)
                   5026: {
                   5027:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5028:                // read from serial port
                   5029:                int read = 0;
                   5030:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5031:                        UINT8 *buf = (UINT8 *)buffer;
                   5032:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5033:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5034:                        DWORD timeout = timeGetTime() + 1000;
                   5035:                        while(read < count) {
                   5036:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   5037:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   5038:                                        timeout = timeGetTime() + 1000;
                   5039:                                } else {
                   5040:                                        if(timeGetTime() > timeout) {
                   5041:                                                break;
                   5042:                                        }
                   5043:                                        Sleep(10);
1.1.1.37  root     5044:                                }
                   5045:                        }
                   5046:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5047:                }
                   5048:                return(read);
                   5049:        }
                   5050:        return(_read(fd, buffer, count));
                   5051: }
                   5052: 
1.1       root     5053: int msdos_kbhit()
                   5054: {
                   5055:        msdos_stdio_reopen();
                   5056:        
1.1.1.20  root     5057:        process_t *process = msdos_process_info_get(current_psp);
                   5058:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5059:        
                   5060:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5061:                // stdin is redirected to file
1.1.1.20  root     5062:                return(eof(fd) == 0);
1.1       root     5063:        }
                   5064:        
                   5065:        // check keyboard status
1.1.1.35  root     5066:        if(key_recv != 0) {
1.1       root     5067:                return(1);
                   5068:        }
1.1.1.35  root     5069:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5070: #ifdef USE_SERVICE_THREAD
                   5071:                EnterCriticalSection(&key_buf_crit_sect);
                   5072: #endif
1.1.1.55  root     5073:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5074: #ifdef USE_SERVICE_THREAD
                   5075:                LeaveCriticalSection(&key_buf_crit_sect);
                   5076: #endif
                   5077:                if(!empty) return(1);
                   5078:        }
                   5079:        return(_kbhit());
1.1       root     5080: }
                   5081: 
                   5082: int msdos_getch_ex(int echo)
                   5083: {
                   5084:        static char prev = 0;
                   5085:        
                   5086:        msdos_stdio_reopen();
                   5087:        
1.1.1.20  root     5088:        process_t *process = msdos_process_info_get(current_psp);
                   5089:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5090:        
                   5091:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5092:                // stdin is redirected to file
                   5093: retry:
                   5094:                char data;
1.1.1.37  root     5095:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     5096:                        char tmp = data;
                   5097:                        if(data == 0x0a) {
                   5098:                                if(prev == 0x0d) {
                   5099:                                        goto retry; // CRLF -> skip LF
                   5100:                                } else {
                   5101:                                        data = 0x0d; // LF only -> CR
                   5102:                                }
                   5103:                        }
                   5104:                        prev = tmp;
                   5105:                        return(data);
                   5106:                }
                   5107:                return(EOF);
                   5108:        }
                   5109:        
                   5110:        // input from console
1.1.1.5   root     5111:        int key_char, key_scan;
1.1.1.33  root     5112:        if(key_recv != 0) {
1.1.1.5   root     5113:                key_char = (key_code >> 0) & 0xff;
                   5114:                key_scan = (key_code >> 8) & 0xff;
                   5115:                key_code >>= 16;
1.1.1.33  root     5116:                key_recv >>= 16;
1.1.1.5   root     5117:        } else {
1.1.1.54  root     5118:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35  root     5119:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5120: #ifdef USE_SERVICE_THREAD
                   5121:                                EnterCriticalSection(&key_buf_crit_sect);
                   5122: #endif
1.1.1.55  root     5123:                                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5124: #ifdef USE_SERVICE_THREAD
                   5125:                                LeaveCriticalSection(&key_buf_crit_sect);
                   5126: #endif
                   5127:                                if(!empty) break;
                   5128:                        }
1.1.1.23  root     5129:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   5130:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   5131:                                if(_kbhit()) {
1.1.1.32  root     5132:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5133: #ifdef USE_SERVICE_THREAD
                   5134:                                                EnterCriticalSection(&key_buf_crit_sect);
                   5135: #endif
1.1.1.51  root     5136:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     5137: #ifdef USE_SERVICE_THREAD
                   5138:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   5139: #endif
1.1.1.32  root     5140:                                        }
1.1.1.23  root     5141:                                } else {
                   5142:                                        Sleep(10);
                   5143:                                }
                   5144:                        } else {
                   5145:                                if(!update_key_buffer()) {
                   5146:                                        Sleep(10);
                   5147:                                }
1.1.1.14  root     5148:                        }
                   5149:                }
1.1.1.54  root     5150:                if(m_exit) {
1.1.1.33  root     5151:                        // insert CR to terminate input loops
1.1.1.14  root     5152:                        key_char = 0x0d;
                   5153:                        key_scan = 0;
1.1.1.32  root     5154:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5155: #ifdef USE_SERVICE_THREAD
                   5156:                        EnterCriticalSection(&key_buf_crit_sect);
                   5157: #endif
1.1.1.51  root     5158:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     5159: #ifdef USE_SERVICE_THREAD
                   5160:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5161: #endif
1.1.1.5   root     5162:                }
1.1       root     5163:        }
                   5164:        if(echo && key_char) {
                   5165:                msdos_putch(key_char);
                   5166:        }
                   5167:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5168: }
                   5169: 
                   5170: inline int msdos_getch()
                   5171: {
                   5172:        return(msdos_getch_ex(0));
                   5173: }
                   5174: 
                   5175: inline int msdos_getche()
                   5176: {
                   5177:        return(msdos_getch_ex(1));
                   5178: }
                   5179: 
                   5180: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5181: {
1.1.1.37  root     5182:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5183:                // write to serial port
1.1.1.38  root     5184:                int written = 0;
1.1.1.37  root     5185:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5186:                        UINT8 *buf = (UINT8 *)buffer;
                   5187:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5188:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5189:                        DWORD timeout = timeGetTime() + 1000;
                   5190:                        while(written < count) {
                   5191:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5192:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5193:                                        timeout = timeGetTime() + 1000;
                   5194:                                } else {
                   5195:                                        if(timeGetTime() > timeout) {
                   5196:                                                break;
                   5197:                                        }
                   5198:                                        Sleep(10);
                   5199:                                }
1.1.1.37  root     5200:                        }
                   5201:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5202:                }
1.1.1.38  root     5203:                return(written);
1.1.1.37  root     5204:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5205:                // write to printer port
                   5206:                UINT8 *buf = (UINT8 *)buffer;
                   5207:                for(unsigned int i = 0; i < count; i++) {
                   5208: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5209:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5210:                }
                   5211:                return(count);
                   5212:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5213:                // CR+LF -> LF
1.1.1.37  root     5214:                static int is_cr = 0;
1.1       root     5215:                UINT8 *buf = (UINT8 *)buffer;
                   5216:                for(unsigned int i = 0; i < count; i++) {
                   5217:                        UINT8 data = buf[i];
                   5218:                        if(is_cr) {
                   5219:                                if(data != 0x0a) {
                   5220:                                        UINT8 tmp = 0x0d;
                   5221:                                        _write(1, &tmp, 1);
                   5222:                                }
                   5223:                                _write(1, &data, 1);
                   5224:                                is_cr = 0;
                   5225:                        } else if(data == 0x0d) {
                   5226:                                is_cr = 1;
                   5227:                        } else {
                   5228:                                _write(1, &data, 1);
                   5229:                        }
                   5230:                }
                   5231:                return(count);
                   5232:        }
1.1.1.14  root     5233:        vram_flush();
1.1       root     5234:        return(_write(fd, buffer, count));
                   5235: }
                   5236: 
                   5237: void msdos_putch(UINT8 data)
1.1.1.50  root     5238: {
                   5239:        msdos_stdio_reopen();
                   5240:        
                   5241:        process_t *process = msdos_process_info_get(current_psp);
                   5242:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5243:        
                   5244:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5245:                // stdout is redirected to file
                   5246:                msdos_write(fd, &data, 1);
                   5247:                return;
                   5248:        }
                   5249:        
                   5250:        // call int 29h ?
1.1.1.58! root     5251:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     5252:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5253:                // int 29h is not hooked, no need to call int 29h
                   5254:                msdos_putch_fast(data);
                   5255: #ifdef USE_SERVICE_THREAD
                   5256:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5257:                // XXX: in usually we should not reach here
                   5258:                // this is called from service thread to echo the input
                   5259:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5260:                msdos_putch_fast(data);
                   5261: #endif
1.1.1.51  root     5262:        } else if(in_service_29h) {
1.1.1.50  root     5263:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5264:                msdos_putch_fast(data);
                   5265:        } else {
                   5266:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5267:                in_service_29h = true;
1.1.1.50  root     5268:                try {
                   5269:                        UINT32 tmp_pc = m_pc;
                   5270:                        UINT16 tmp_ax = REG16(AX);
                   5271:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5272:                        
                   5273:                        // call int 29h routine is at fffc:0027
                   5274:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5275:                        REG8(AL) = data;
                   5276:                        
                   5277:                        // run cpu until call int 29h routine is done
1.1.1.54  root     5278:                        while(!m_exit && tmp_pc != m_pc) {
1.1.1.50  root     5279:                                try {
                   5280:                                        hardware_run_cpu();
                   5281:                                } catch(...) {
                   5282:                                }
                   5283:                        }
                   5284:                        REG16(AX) = tmp_ax;
                   5285:                        REG16(BX) = tmp_bx;
                   5286:                } catch(...) {
                   5287:                }
1.1.1.51  root     5288:                in_service_29h = false;
1.1.1.50  root     5289:        }
                   5290: }
                   5291: 
                   5292: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5293: #ifdef USE_SERVICE_THREAD
                   5294: {
                   5295:        EnterCriticalSection(&putch_crit_sect);
                   5296:        msdos_putch_tmp(data);
                   5297:        LeaveCriticalSection(&putch_crit_sect);
                   5298: }
                   5299: void msdos_putch_tmp(UINT8 data)
                   5300: #endif
1.1       root     5301: {
1.1.1.34  root     5302:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5303:        SMALL_RECT rect;
                   5304:        COORD co;
1.1       root     5305:        static int p = 0;
                   5306:        static int is_kanji = 0;
                   5307:        static int is_esc = 0;
                   5308:        static int stored_x;
                   5309:        static int stored_y;
                   5310:        static WORD stored_a;
1.1.1.20  root     5311:        static char tmp[64], out[64];
1.1       root     5312:        
1.1.1.23  root     5313:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5314:        
                   5315:        // output to console
                   5316:        tmp[p++] = data;
                   5317:        
1.1.1.14  root     5318:        vram_flush();
                   5319:        
1.1       root     5320:        if(is_kanji) {
                   5321:                // kanji character
                   5322:                is_kanji = 0;
                   5323:        } else if(is_esc) {
                   5324:                // escape sequense
                   5325:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5326:                        p = is_esc = 0;
                   5327:                } else if(tmp[1] == '=' && p == 4) {
                   5328:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5329:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5330:                        SetConsoleCursorPosition(hStdout, co);
                   5331:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5332:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5333:                        cursor_moved = false;
                   5334:                        p = is_esc = 0;
                   5335:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   5336:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5337:                        co.X = csbi.dwCursorPosition.X;
                   5338:                        co.Y = csbi.dwCursorPosition.Y;
                   5339:                        WORD wAttributes = csbi.wAttributes;
                   5340:                        
                   5341:                        if(tmp[1] == 'D') {
                   5342:                                co.Y++;
                   5343:                        } else if(tmp[1] == 'E') {
                   5344:                                co.X = 0;
                   5345:                                co.Y++;
                   5346:                        } else if(tmp[1] == 'M') {
                   5347:                                co.Y--;
                   5348:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5349:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5350:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5351:                                co.X = 0;
                   5352:                                co.Y = csbi.srWindow.Top;
1.1       root     5353:                        } else if(tmp[1] == '[') {
                   5354:                                int param[256], params = 0;
                   5355:                                memset(param, 0, sizeof(param));
                   5356:                                for(int i = 2; i < p; i++) {
                   5357:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5358:                                                param[params] *= 10;
                   5359:                                                param[params] += tmp[i] - '0';
                   5360:                                        } else {
                   5361:                                                params++;
                   5362:                                        }
                   5363:                                }
                   5364:                                if(data == 'A') {
1.1.1.14  root     5365:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5366:                                } else if(data == 'B') {
1.1.1.14  root     5367:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5368:                                } else if(data == 'C') {
1.1.1.14  root     5369:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5370:                                } else if(data == 'D') {
1.1.1.14  root     5371:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5372:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5373:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5374:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5375:                                } else if(data == 'J') {
1.1.1.14  root     5376:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5377:                                        if(param[0] == 0) {
                   5378:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5379:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5380:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5381:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5382:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5383:                                                }
                   5384:                                        } else if(param[0] == 1) {
1.1.1.14  root     5385:                                                if(co.Y > csbi.srWindow.Top) {
                   5386:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5387:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5388:                                                }
                   5389:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5390:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5391:                                        } else if(param[0] == 2) {
1.1.1.14  root     5392:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5393:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5394:                                                co.X = co.Y = 0;
                   5395:                                        }
                   5396:                                } else if(data == 'K') {
1.1.1.14  root     5397:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5398:                                        if(param[0] == 0) {
                   5399:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5400:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5401:                                        } else if(param[0] == 1) {
                   5402:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5403:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5404:                                        } else if(param[0] == 2) {
                   5405:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5406:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5407:                                        }
                   5408:                                } else if(data == 'L') {
1.1.1.14  root     5409:                                        if(params == 0) {
                   5410:                                                param[0] = 1;
1.1       root     5411:                                        }
1.1.1.14  root     5412:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5413:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5414:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5415:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5416:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5417:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5418:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5419:                                        co.X = 0;
                   5420:                                } else if(data == 'M') {
1.1.1.14  root     5421:                                        if(params == 0) {
                   5422:                                                param[0] = 1;
                   5423:                                        }
                   5424:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5425:                                                clear_scr_buffer(csbi.wAttributes);
                   5426:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5427:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5428:                                        } else {
1.1.1.14  root     5429:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5430:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5431:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5432:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5433:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5434:                                        }
                   5435:                                        co.X = 0;
                   5436:                                } else if(data == 'h') {
                   5437:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5438:                                                CONSOLE_CURSOR_INFO cur;
                   5439:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5440:                                                if(cur.bVisible) {
                   5441:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5442: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5443:                                                }
                   5444:                                        }
                   5445:                                } else if(data == 'l') {
                   5446:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5447:                                                CONSOLE_CURSOR_INFO cur;
                   5448:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5449:                                                if(!cur.bVisible) {
                   5450:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5451: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5452:                                                }
                   5453:                                        }
                   5454:                                } else if(data == 'm') {
                   5455:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5456:                                        int reverse = 0, hidden = 0;
                   5457:                                        for(int i = 0; i < params; i++) {
                   5458:                                                if(param[i] == 1) {
                   5459:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5460:                                                } else if(param[i] == 4) {
                   5461:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5462:                                                } else if(param[i] == 7) {
                   5463:                                                        reverse = 1;
                   5464:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5465:                                                        hidden = 1;
                   5466:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5467:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5468:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5469:                                                                param[i] -= 16;
                   5470:                                                        } else {
                   5471:                                                                param[i] -= 30;
                   5472:                                                        }
                   5473:                                                        if(param[i] & 1) {
                   5474:                                                                wAttributes |= FOREGROUND_RED;
                   5475:                                                        }
                   5476:                                                        if(param[i] & 2) {
                   5477:                                                                wAttributes |= FOREGROUND_GREEN;
                   5478:                                                        }
                   5479:                                                        if(param[i] & 4) {
                   5480:                                                                wAttributes |= FOREGROUND_BLUE;
                   5481:                                                        }
                   5482:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5483:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5484:                                                        if((param[i] - 40) & 1) {
                   5485:                                                                wAttributes |= BACKGROUND_RED;
                   5486:                                                        }
                   5487:                                                        if((param[i] - 40) & 2) {
                   5488:                                                                wAttributes |= BACKGROUND_GREEN;
                   5489:                                                        }
                   5490:                                                        if((param[i] - 40) & 4) {
                   5491:                                                                wAttributes |= BACKGROUND_BLUE;
                   5492:                                                        }
                   5493:                                                }
                   5494:                                        }
                   5495:                                        if(reverse) {
                   5496:                                                wAttributes &= ~0xff;
                   5497:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5498:                                        }
                   5499:                                        if(hidden) {
                   5500:                                                wAttributes &= ~0x0f;
                   5501:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5502:                                        }
                   5503:                                } else if(data == 'n') {
                   5504:                                        if(param[0] == 6) {
                   5505:                                                char tmp[16];
                   5506:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5507:                                                int len = strlen(tmp);
1.1.1.32  root     5508:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5509: #ifdef USE_SERVICE_THREAD
                   5510:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5511: #endif
1.1.1.32  root     5512:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5513:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5514:                                                        }
1.1.1.35  root     5515: #ifdef USE_SERVICE_THREAD
                   5516:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5517: #endif
1.1       root     5518:                                                }
                   5519:                                        }
                   5520:                                } else if(data == 's') {
                   5521:                                        stored_x = co.X;
                   5522:                                        stored_y = co.Y;
                   5523:                                        stored_a = wAttributes;
                   5524:                                } else if(data == 'u') {
                   5525:                                        co.X = stored_x;
                   5526:                                        co.Y = stored_y;
                   5527:                                        wAttributes = stored_a;
                   5528:                                }
                   5529:                        }
                   5530:                        if(co.X < 0) {
                   5531:                                co.X = 0;
                   5532:                        } else if(co.X >= csbi.dwSize.X) {
                   5533:                                co.X = csbi.dwSize.X - 1;
                   5534:                        }
1.1.1.14  root     5535:                        if(co.Y < csbi.srWindow.Top) {
                   5536:                                co.Y = csbi.srWindow.Top;
                   5537:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5538:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5539:                        }
                   5540:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5541:                                SetConsoleCursorPosition(hStdout, co);
                   5542:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5543:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5544:                                cursor_moved = false;
                   5545:                        }
                   5546:                        if(wAttributes != csbi.wAttributes) {
                   5547:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5548:                        }
                   5549:                        p = is_esc = 0;
                   5550:                }
                   5551:                return;
                   5552:        } else {
                   5553:                if(msdos_lead_byte_check(data)) {
                   5554:                        is_kanji = 1;
                   5555:                        return;
                   5556:                } else if(data == 0x1b) {
                   5557:                        is_esc = 1;
                   5558:                        return;
                   5559:                }
                   5560:        }
1.1.1.20  root     5561:        
                   5562:        DWORD q = 0, num;
                   5563:        is_kanji = 0;
                   5564:        for(int i = 0; i < p; i++) {
                   5565:                UINT8 c = tmp[i];
                   5566:                if(is_kanji) {
                   5567:                        is_kanji = 0;
                   5568:                } else if(msdos_lead_byte_check(data)) {
                   5569:                        is_kanji = 1;
                   5570:                } else if(msdos_ctrl_code_check(data)) {
                   5571:                        out[q++] = '^';
                   5572:                        c += 'A' - 1;
                   5573:                }
                   5574:                out[q++] = c;
                   5575:        }
1.1.1.34  root     5576:        if(q == 1 && out[0] == 0x08) {
                   5577:                // back space
                   5578:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5579:                if(csbi.dwCursorPosition.X > 0) {
                   5580:                        co.X = csbi.dwCursorPosition.X - 1;
                   5581:                        co.Y = csbi.dwCursorPosition.Y;
                   5582:                        SetConsoleCursorPosition(hStdout, co);
                   5583:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5584:                        co.X = csbi.dwSize.X - 1;
                   5585:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5586:                        SetConsoleCursorPosition(hStdout, co);
                   5587:                } else {
                   5588:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5589:                }
                   5590:        } else {
                   5591:                WriteConsole(hStdout, out, q, &num, NULL);
                   5592:        }
1.1       root     5593:        p = 0;
1.1.1.14  root     5594:        
1.1.1.15  root     5595:        if(!restore_console_on_exit) {
                   5596:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5597:                scr_top = csbi.srWindow.Top;
                   5598:        }
1.1       root     5599:        cursor_moved = true;
                   5600: }
                   5601: 
                   5602: int msdos_aux_in()
                   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(3, current_psp);
                   5608:        
                   5609:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5610:                char data = 0;
1.1.1.37  root     5611:                msdos_read(fd, &data, 1);
1.1       root     5612:                return(data);
                   5613:        } else {
                   5614:                return(EOF);
                   5615:        }
                   5616: }
                   5617: 
                   5618: void msdos_aux_out(char data)
                   5619: {
1.1.1.21  root     5620:        msdos_stdio_reopen();
                   5621:        
1.1.1.20  root     5622:        process_t *process = msdos_process_info_get(current_psp);
                   5623:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5624:        
                   5625:        if(fd < process->max_files && file_handler[fd].valid) {
                   5626:                msdos_write(fd, &data, 1);
1.1       root     5627:        }
                   5628: }
                   5629: 
                   5630: void msdos_prn_out(char data)
                   5631: {
1.1.1.21  root     5632:        msdos_stdio_reopen();
                   5633:        
1.1.1.20  root     5634:        process_t *process = msdos_process_info_get(current_psp);
                   5635:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5636:        
                   5637:        if(fd < process->max_files && file_handler[fd].valid) {
                   5638:                msdos_write(fd, &data, 1);
1.1       root     5639:        }
                   5640: }
                   5641: 
                   5642: // memory control
                   5643: 
1.1.1.52  root     5644: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5645: {
                   5646:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5647:        
                   5648:        mcb->mz = mz;
                   5649:        mcb->psp = psp;
1.1.1.30  root     5650:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5651:        
                   5652:        if(prog_name != NULL) {
                   5653:                memset(mcb->prog_name, 0, 8);
                   5654:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5655:        }
1.1       root     5656:        return(mcb);
                   5657: }
                   5658: 
                   5659: void msdos_mcb_check(mcb_t *mcb)
                   5660: {
                   5661:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5662:                #if 0
                   5663:                        // shutdown now !!!
                   5664:                        fatalerror("broken memory control block\n");
                   5665:                #else
                   5666:                        // return error code and continue
                   5667:                        throw(0x07); // broken memory control block
                   5668:                #endif
1.1       root     5669:        }
                   5670: }
                   5671: 
1.1.1.39  root     5672: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5673: {
                   5674:        int mcb_seg = seg - 1;
                   5675:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5676:        msdos_mcb_check(mcb);
                   5677:        
1.1.1.30  root     5678:        if(mcb->paragraphs > paragraphs) {
1.1       root     5679:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5680:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5681:                
                   5682:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5683:                mcb->mz = 'M';
1.1.1.30  root     5684:                mcb->paragraphs = paragraphs;
1.1       root     5685:        }
                   5686: }
                   5687: 
                   5688: void msdos_mem_merge(int seg)
                   5689: {
                   5690:        int mcb_seg = seg - 1;
                   5691:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5692:        msdos_mcb_check(mcb);
                   5693:        
                   5694:        while(1) {
                   5695:                if(mcb->mz == 'Z') {
                   5696:                        break;
                   5697:                }
1.1.1.30  root     5698:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5699:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5700:                msdos_mcb_check(next_mcb);
                   5701:                
                   5702:                if(next_mcb->psp != 0) {
                   5703:                        break;
                   5704:                }
                   5705:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5706:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5707:        }
                   5708: }
                   5709: 
1.1.1.8   root     5710: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5711: {
                   5712:        while(1) {
                   5713:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5714:                bool last_block;
1.1       root     5715:                
1.1.1.14  root     5716:                if(mcb->psp == 0) {
                   5717:                        msdos_mem_merge(mcb_seg + 1);
                   5718:                } else {
                   5719:                        msdos_mcb_check(mcb);
                   5720:                }
1.1.1.33  root     5721:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5722:                        // check if the next is dummy mcb to link to umb
                   5723:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5724:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5725:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5726:                }
                   5727:                if(!(new_process && !last_block)) {
1.1.1.30  root     5728:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5729:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5730:                                mcb->psp = current_psp;
                   5731:                                return(mcb_seg + 1);
                   5732:                        }
                   5733:                }
                   5734:                if(mcb->mz == 'Z') {
                   5735:                        break;
                   5736:                }
1.1.1.30  root     5737:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5738:        }
                   5739:        return(-1);
                   5740: }
                   5741: 
                   5742: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5743: {
                   5744:        int mcb_seg = seg - 1;
                   5745:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5746:        msdos_mcb_check(mcb);
1.1.1.30  root     5747:        int current_paragraphs = mcb->paragraphs;
1.1       root     5748:        
                   5749:        msdos_mem_merge(seg);
1.1.1.30  root     5750:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5751:                if(max_paragraphs) {
1.1.1.30  root     5752:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5753:                }
1.1       root     5754:                msdos_mem_split(seg, current_paragraphs);
                   5755:                return(-1);
                   5756:        }
                   5757:        msdos_mem_split(seg, paragraphs);
                   5758:        return(0);
                   5759: }
                   5760: 
                   5761: void msdos_mem_free(int seg)
                   5762: {
                   5763:        int mcb_seg = seg - 1;
                   5764:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5765:        msdos_mcb_check(mcb);
                   5766:        
                   5767:        mcb->psp = 0;
                   5768:        msdos_mem_merge(seg);
                   5769: }
                   5770: 
1.1.1.8   root     5771: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5772: {
                   5773:        int max_paragraphs = 0;
                   5774:        
                   5775:        while(1) {
                   5776:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5777:                bool last_block;
                   5778:                
1.1       root     5779:                msdos_mcb_check(mcb);
                   5780:                
1.1.1.33  root     5781:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5782:                        // check if the next is dummy mcb to link to umb
                   5783:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5784:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5785:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5786:                }
                   5787:                if(!(new_process && !last_block)) {
1.1.1.30  root     5788:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5789:                                max_paragraphs = mcb->paragraphs;
1.1       root     5790:                        }
                   5791:                }
                   5792:                if(mcb->mz == 'Z') {
                   5793:                        break;
                   5794:                }
1.1.1.30  root     5795:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5796:        }
1.1.1.14  root     5797:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5798: }
                   5799: 
1.1.1.8   root     5800: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5801: {
                   5802:        int last_seg = -1;
                   5803:        
                   5804:        while(1) {
                   5805:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5806:                msdos_mcb_check(mcb);
                   5807:                
1.1.1.14  root     5808:                if(mcb->psp == psp) {
1.1.1.8   root     5809:                        last_seg = mcb_seg;
                   5810:                }
1.1.1.14  root     5811:                if(mcb->mz == 'Z') {
                   5812:                        break;
                   5813:                }
1.1.1.30  root     5814:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5815:        }
                   5816:        return(last_seg);
                   5817: }
                   5818: 
1.1.1.19  root     5819: int msdos_mem_get_umb_linked()
                   5820: {
1.1.1.33  root     5821:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5822:        msdos_mcb_check(mcb);
1.1.1.19  root     5823:        
1.1.1.33  root     5824:        if(mcb->mz == 'M') {
                   5825:                return(-1);
1.1.1.19  root     5826:        }
                   5827:        return(0);
                   5828: }
                   5829: 
1.1.1.33  root     5830: void msdos_mem_link_umb()
1.1.1.19  root     5831: {
1.1.1.33  root     5832:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5833:        msdos_mcb_check(mcb);
1.1.1.19  root     5834:        
1.1.1.33  root     5835:        mcb->mz = 'M';
                   5836:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     5837:        
                   5838:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5839: }
                   5840: 
1.1.1.33  root     5841: void msdos_mem_unlink_umb()
1.1.1.19  root     5842: {
1.1.1.33  root     5843:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5844:        msdos_mcb_check(mcb);
1.1.1.19  root     5845:        
1.1.1.33  root     5846:        mcb->mz = 'Z';
                   5847:        mcb->paragraphs = 0;
1.1.1.39  root     5848:        
                   5849:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5850: }
                   5851: 
1.1.1.29  root     5852: #ifdef SUPPORT_HMA
                   5853: 
                   5854: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5855: {
                   5856:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5857:        
                   5858:        mcb->ms[0] = 'M';
                   5859:        mcb->ms[1] = 'S';
                   5860:        mcb->owner = owner;
                   5861:        mcb->size = size;
                   5862:        mcb->next = next;
                   5863:        return(mcb);
                   5864: }
                   5865: 
                   5866: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5867: {
                   5868:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5869: }
                   5870: 
                   5871: int msdos_hma_mem_split(int offset, int size)
                   5872: {
                   5873:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5874:        
                   5875:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5876:                return(-1);
                   5877:        }
                   5878:        if(mcb->size >= size + 0x10) {
                   5879:                int new_offset = offset + 0x10 + size;
                   5880:                int new_size = mcb->size - 0x10 - size;
                   5881:                
                   5882:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5883:                mcb->size = size;
                   5884:                mcb->next = new_offset;
                   5885:                return(0);
                   5886:        }
                   5887:        return(-1);
                   5888: }
                   5889: 
                   5890: void msdos_hma_mem_merge(int offset)
                   5891: {
                   5892:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5893:        
                   5894:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5895:                return;
                   5896:        }
                   5897:        while(1) {
                   5898:                if(mcb->next == 0) {
                   5899:                        break;
                   5900:                }
                   5901:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5902:                
                   5903:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5904:                        return;
                   5905:                }
                   5906:                if(next_mcb->owner != 0) {
                   5907:                        break;
                   5908:                }
                   5909:                mcb->size += 0x10 + next_mcb->size;
                   5910:                mcb->next = next_mcb->next;
                   5911:        }
                   5912: }
                   5913: 
                   5914: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5915: {
                   5916:        int offset = 0x10; // first mcb in HMA
                   5917:        
                   5918:        while(1) {
                   5919:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5920:                
                   5921:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5922:                        return(-1);
                   5923:                }
                   5924:                if(mcb->owner == 0) {
                   5925:                        msdos_hma_mem_merge(offset);
                   5926:                }
                   5927:                if(mcb->owner == 0 && mcb->size >= size) {
                   5928:                        msdos_hma_mem_split(offset, size);
                   5929:                        mcb->owner = owner;
                   5930:                        return(offset);
                   5931:                }
                   5932:                if(mcb->next == 0) {
                   5933:                        break;
                   5934:                }
                   5935:                offset = mcb->next;
                   5936:        }
                   5937:        return(-1);
                   5938: }
                   5939: 
                   5940: int msdos_hma_mem_realloc(int offset, int size)
                   5941: {
                   5942:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5943:        
                   5944:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5945:                return(-1);
                   5946:        }
                   5947:        if(mcb->size < size) {
                   5948:                return(-1);
                   5949:        }
                   5950:        msdos_hma_mem_split(offset, size);
                   5951:        return(0);
                   5952: }
                   5953: 
                   5954: void msdos_hma_mem_free(int offset)
                   5955: {
                   5956:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5957:        
                   5958:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5959:                return;
                   5960:        }
                   5961:        mcb->owner = 0;
                   5962:        msdos_hma_mem_merge(offset);
                   5963: }
                   5964: 
                   5965: int msdos_hma_mem_get_free(int *available_offset)
                   5966: {
                   5967:        int offset = 0x10; // first mcb in HMA
                   5968:        int size = 0;
                   5969:        
                   5970:        while(1) {
                   5971:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5972:                
                   5973:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5974:                        return(0);
                   5975:                }
                   5976:                if(mcb->owner == 0 && size < mcb->size) {
                   5977:                        if(available_offset != NULL) {
                   5978:                                *available_offset = offset;
                   5979:                        }
                   5980:                        size = mcb->size;
                   5981:                }
                   5982:                if(mcb->next == 0) {
                   5983:                        break;
                   5984:                }
                   5985:                offset = mcb->next;
                   5986:        }
                   5987:        return(size);
                   5988: }
                   5989: 
                   5990: #endif
                   5991: 
1.1       root     5992: // environment
                   5993: 
1.1.1.45  root     5994: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     5995: {
                   5996:        char *dst = (char *)(mem + (env_seg << 4));
                   5997:        
                   5998:        while(1) {
                   5999:                if(dst[0] == 0) {
                   6000:                        break;
                   6001:                }
                   6002:                dst += strlen(dst) + 1;
                   6003:        }
                   6004:        *dst++ = 0; // end of environment
                   6005:        *dst++ = 1; // top of argv[0]
                   6006:        *dst++ = 0;
                   6007:        memcpy(dst, argv, strlen(argv));
                   6008:        dst += strlen(argv);
                   6009:        *dst++ = 0;
                   6010:        *dst++ = 0;
                   6011: }
                   6012: 
1.1.1.45  root     6013: const char *msdos_env_get_argv(int env_seg)
1.1       root     6014: {
                   6015:        static char env[ENV_SIZE];
                   6016:        char *src = env;
                   6017:        
                   6018:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6019:        while(1) {
                   6020:                if(src[0] == 0) {
                   6021:                        if(src[1] == 1) {
                   6022:                                return(src + 3);
                   6023:                        }
                   6024:                        break;
                   6025:                }
                   6026:                src += strlen(src) + 1;
                   6027:        }
                   6028:        return(NULL);
                   6029: }
                   6030: 
1.1.1.45  root     6031: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     6032: {
                   6033:        static char env[ENV_SIZE];
                   6034:        char *src = env;
                   6035:        
                   6036:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6037:        while(1) {
                   6038:                if(src[0] == 0) {
                   6039:                        break;
                   6040:                }
                   6041:                int len = strlen(src);
                   6042:                char *n = my_strtok(src, "=");
                   6043:                char *v = src + strlen(n) + 1;
                   6044:                
                   6045:                if(_stricmp(name, n) == 0) {
                   6046:                        return(v);
                   6047:                }
                   6048:                src += len + 1;
                   6049:        }
                   6050:        return(NULL);
                   6051: }
                   6052: 
1.1.1.45  root     6053: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     6054: {
                   6055:        char env[ENV_SIZE];
                   6056:        char *src = env;
                   6057:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     6058:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     6059:        int done = 0;
                   6060:        
                   6061:        memcpy(src, dst, ENV_SIZE);
                   6062:        memset(dst, 0, ENV_SIZE);
                   6063:        while(1) {
                   6064:                if(src[0] == 0) {
                   6065:                        break;
                   6066:                }
                   6067:                int len = strlen(src);
                   6068:                char *n = my_strtok(src, "=");
                   6069:                char *v = src + strlen(n) + 1;
                   6070:                char tmp[1024];
                   6071:                
                   6072:                if(_stricmp(name, n) == 0) {
                   6073:                        sprintf(tmp, "%s=%s", n, value);
                   6074:                        done = 1;
                   6075:                } else {
                   6076:                        sprintf(tmp, "%s=%s", n, v);
                   6077:                }
                   6078:                memcpy(dst, tmp, strlen(tmp));
                   6079:                dst += strlen(tmp) + 1;
                   6080:                src += len + 1;
                   6081:        }
                   6082:        if(!done) {
                   6083:                char tmp[1024];
                   6084:                
                   6085:                sprintf(tmp, "%s=%s", name, value);
                   6086:                memcpy(dst, tmp, strlen(tmp));
                   6087:                dst += strlen(tmp) + 1;
                   6088:        }
                   6089:        if(argv) {
                   6090:                *dst++ = 0; // end of environment
                   6091:                *dst++ = 1; // top of argv[0]
                   6092:                *dst++ = 0;
                   6093:                memcpy(dst, argv, strlen(argv));
                   6094:                dst += strlen(argv);
                   6095:                *dst++ = 0;
                   6096:                *dst++ = 0;
                   6097:        }
                   6098: }
                   6099: 
                   6100: // process
                   6101: 
1.1.1.8   root     6102: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     6103: {
                   6104:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6105:        
                   6106:        memset(psp, 0, PSP_SIZE);
                   6107:        psp->exit[0] = 0xcd;
                   6108:        psp->exit[1] = 0x20;
1.1.1.8   root     6109:        psp->first_mcb = mcb_seg;
1.1.1.46  root     6110: #if 1
1.1.1.49  root     6111:        psp->call5[0] = 0xcd;   // int 30h
                   6112:        psp->call5[1] = 0x30;
1.1.1.46  root     6113:        psp->call5[2] = 0xc3;   // ret
                   6114: #else
                   6115:        psp->call5[0] = 0x8a;   // mov ah, cl
                   6116:        psp->call5[1] = 0xe1;
                   6117:        psp->call5[2] = 0xcd;   // int 21h
                   6118:        psp->call5[3] = 0x21;
                   6119:        psp->call5[4] = 0xc3;   // ret
                   6120: #endif
1.1       root     6121:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6122:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6123:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6124:        psp->parent_psp = parent_psp;
1.1.1.20  root     6125:        if(parent_psp == (UINT16)-1) {
                   6126:                for(int i = 0; i < 20; i++) {
                   6127:                        if(file_handler[i].valid) {
                   6128:                                psp->file_table[i] = i;
                   6129:                        } else {
                   6130:                                psp->file_table[i] = 0xff;
                   6131:                        }
1.1       root     6132:                }
1.1.1.20  root     6133:        } else {
                   6134:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     6135:        }
                   6136:        psp->env_seg = env_seg;
                   6137:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6138:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6139:        psp->file_table_size = 20;
                   6140:        psp->file_table_ptr.w.l = 0x18;
                   6141:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6142:        psp->service[0] = 0xcd;
                   6143:        psp->service[1] = 0x21;
                   6144:        psp->service[2] = 0xcb;
                   6145:        return(psp);
                   6146: }
                   6147: 
1.1.1.20  root     6148: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6149: {
                   6150:        if(psp_seg && fd < 20) {
                   6151:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6152:                psp->file_table[fd] = value;
                   6153:        }
                   6154: }
                   6155: 
                   6156: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6157: {
                   6158:        if(psp_seg && fd < 20) {
                   6159:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6160:                fd = psp->file_table[fd];
                   6161:        }
                   6162:        return fd;
                   6163: }
                   6164: 
1.1.1.52  root     6165: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     6166: {
                   6167:        // load command file
                   6168:        int fd = -1;
1.1.1.45  root     6169:        int sio_port = 0;
                   6170:        int lpt_port = 0;
1.1       root     6171:        int dos_command = 0;
1.1.1.24  root     6172:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6173:        char pipe_stdin_path[MAX_PATH] = {0};
                   6174:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6175:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6176:        
                   6177:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6178:        int opt_len = mem[opt_ofs];
                   6179:        memset(opt, 0, sizeof(opt));
                   6180:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6181:        
1.1.1.14  root     6182:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6183:                // this is a batch file, run command.com
                   6184:                char tmp[MAX_PATH];
                   6185:                if(opt_len != 0) {
                   6186:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6187:                } else {
                   6188:                        sprintf(tmp, "/C %s", cmd);
                   6189:                }
                   6190:                strcpy(opt, tmp);
                   6191:                opt_len = strlen(opt);
                   6192:                mem[opt_ofs] = opt_len;
                   6193:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6194:                strcpy(command, comspec_path);
                   6195:                strcpy(name_tmp, "COMMAND.COM");
                   6196:        } else {
                   6197:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6198:                        // redirect C:\COMMAND.COM to comspec_path
                   6199:                        strcpy(command, comspec_path);
                   6200:                } else {
                   6201:                        strcpy(command, cmd);
                   6202:                }
1.1.1.24  root     6203:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   6204:                        return(-1);
                   6205:                }
1.1.1.14  root     6206:                memset(name_tmp, 0, sizeof(name_tmp));
                   6207:                strcpy(name_tmp, name);
                   6208:                
                   6209:                // check command.com
1.1.1.38  root     6210:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6211:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6212:                        if(opt_len == 0) {
                   6213: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6214:                                process_t *current_process = NULL;
                   6215:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6216:                                        if(process[i].psp == current_psp) {
                   6217:                                                current_process = &process[i];
                   6218:                                                break;
                   6219:                                        }
                   6220:                                }
                   6221:                                if(current_process != NULL) {
                   6222:                                        param->cmd_line.dw = current_process->dta.dw;
                   6223:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6224:                                        opt_len = mem[opt_ofs];
                   6225:                                        memset(opt, 0, sizeof(opt));
                   6226:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6227:                                }
                   6228:                        }
                   6229:                        for(int i = 0; i < opt_len; i++) {
                   6230:                                if(opt[i] == ' ') {
                   6231:                                        continue;
                   6232:                                }
                   6233:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6234:                                        for(int j = i + 3; j < opt_len; j++) {
                   6235:                                                if(opt[j] == ' ') {
                   6236:                                                        continue;
                   6237:                                                }
                   6238:                                                char *token = my_strtok(opt + j, " ");
                   6239:                                                
1.1.1.38  root     6240:                                                strcpy(command, token);
                   6241:                                                char tmp[MAX_PATH];
                   6242:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6243:                                                strcpy(opt, "");
                   6244:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6245:                                                        if(tmp[i] != ' ') {
                   6246:                                                                strcpy(opt, tmp + i);
                   6247:                                                                break;
                   6248:                                                        }
                   6249:                                                }
                   6250:                                                strcpy(tmp, opt);
1.1.1.38  root     6251:                                                
                   6252:                                                if(al == 0x00) {
1.1.1.39  root     6253:                                                        #define GET_FILE_PATH() { \
                   6254:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6255:                                                                        token++; \
                   6256:                                                                } \
                   6257:                                                                token++; \
                   6258:                                                                while(*token == ' ') { \
                   6259:                                                                        token++; \
                   6260:                                                                } \
                   6261:                                                                char *ptr = token; \
                   6262:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6263:                                                                        ptr++; \
                   6264:                                                                } \
                   6265:                                                                *ptr = '\0'; \
                   6266:                                                        }
                   6267:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6268:                                                                GET_FILE_PATH();
1.1.1.38  root     6269:                                                                strcpy(pipe_stdin_path, token);
                   6270:                                                                strcpy(opt, tmp);
                   6271:                                                        }
1.1.1.39  root     6272:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6273:                                                                GET_FILE_PATH();
1.1.1.38  root     6274:                                                                strcpy(pipe_stdout_path, token);
                   6275:                                                                strcpy(opt, tmp);
                   6276:                                                        }
1.1.1.39  root     6277:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6278:                                                                GET_FILE_PATH();
                   6279:                                                                strcpy(pipe_stderr_path, token);
                   6280:                                                                strcpy(opt, tmp);
                   6281:                                                        }
                   6282:                                                        #undef GET_FILE_PATH
                   6283:                                                        
                   6284:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6285:                                                                *token = '\0';
                   6286:                                                        }
                   6287:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6288:                                                                *token = '\0';
                   6289:                                                        }
                   6290:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6291:                                                                *token = '\0';
                   6292:                                                        }
1.1.1.38  root     6293:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6294:                                                                *token = '\0';
                   6295:                                                        }
                   6296:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6297:                                                                *token = '\0';
                   6298:                                                        }
1.1.1.14  root     6299:                                                }
1.1.1.39  root     6300:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6301:                                                        opt[i] = '\0';
                   6302:                                                }
1.1.1.38  root     6303:                                                opt_len = strlen(opt);
                   6304:                                                mem[opt_ofs] = opt_len;
                   6305:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6306:                                                dos_command = 1;
1.1.1.14  root     6307:                                                break;
1.1       root     6308:                                        }
                   6309:                                }
1.1.1.14  root     6310:                                break;
1.1       root     6311:                        }
                   6312:                }
                   6313:        }
                   6314:        
                   6315:        // load command file
                   6316:        strcpy(path, command);
                   6317:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6318:                sprintf(path, "%s.COM", command);
                   6319:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6320:                        sprintf(path, "%s.EXE", command);
                   6321:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6322:                                sprintf(path, "%s.BAT", command);
                   6323:                                if(_access(path, 0) == 0) {
                   6324:                                        // this is a batch file, run command.com
                   6325:                                        char tmp[MAX_PATH];
                   6326:                                        if(opt_len != 0) {
                   6327:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6328:                                        } else {
                   6329:                                                sprintf(tmp, "/C %s", path);
                   6330:                                        }
                   6331:                                        strcpy(opt, tmp);
                   6332:                                        opt_len = strlen(opt);
                   6333:                                        mem[opt_ofs] = opt_len;
                   6334:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6335:                                        strcpy(path, comspec_path);
                   6336:                                        strcpy(name_tmp, "COMMAND.COM");
                   6337:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6338:                                } else {
                   6339:                                        // search path in parent environments
                   6340:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6341:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6342:                                        if(env != NULL) {
                   6343:                                                char env_path[4096];
                   6344:                                                strcpy(env_path, env);
                   6345:                                                char *token = my_strtok(env_path, ";");
                   6346:                                                
                   6347:                                                while(token != NULL) {
                   6348:                                                        if(strlen(token) != 0) {
                   6349:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6350:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6351:                                                                        break;
                   6352:                                                                }
                   6353:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6354:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6355:                                                                        break;
                   6356:                                                                }
                   6357:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6358:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6359:                                                                        break;
                   6360:                                                                }
                   6361:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6362:                                                                if(_access(path, 0) == 0) {
                   6363:                                                                        // this is a batch file, run command.com
                   6364:                                                                        char tmp[MAX_PATH];
                   6365:                                                                        if(opt_len != 0) {
                   6366:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6367:                                                                        } else {
                   6368:                                                                                sprintf(tmp, "/C %s", path);
                   6369:                                                                        }
                   6370:                                                                        strcpy(opt, tmp);
                   6371:                                                                        opt_len = strlen(opt);
                   6372:                                                                        mem[opt_ofs] = opt_len;
                   6373:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6374:                                                                        strcpy(path, comspec_path);
                   6375:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6376:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6377:                                                                        break;
                   6378:                                                                }
1.1.1.8   root     6379:                                                        }
1.1.1.14  root     6380:                                                        token = my_strtok(NULL, ";");
1.1       root     6381:                                                }
                   6382:                                        }
                   6383:                                }
                   6384:                        }
                   6385:                }
                   6386:        }
                   6387:        if(fd == -1) {
1.1.1.38  root     6388:                // we can not find command.com in the path, so open comspec_path
                   6389:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6390:                        strcpy(command, comspec_path);
                   6391:                        strcpy(path, command);
                   6392:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6393:                }
                   6394:        }
                   6395:        if(fd == -1) {
1.1.1.52  root     6396:                if(!first_process && al == 0 && dos_command) {
1.1       root     6397:                        // may be dos command
                   6398:                        char tmp[MAX_PATH];
1.1.1.52  root     6399:                        if(opt_len != 0) {
                   6400:                                sprintf(tmp, "%s %s", command, opt);
                   6401:                        } else {
                   6402:                                sprintf(tmp, "%s", command);
                   6403:                        }
                   6404:                        retval = system(tmp);
1.1       root     6405:                        return(0);
                   6406:                } else {
                   6407:                        return(-1);
                   6408:                }
                   6409:        }
1.1.1.52  root     6410:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6411:        _read(fd, file_buffer, sizeof(file_buffer));
                   6412:        _close(fd);
                   6413:        
1.1.1.52  root     6414:        // check if this is win32 program
                   6415:        if(!first_process && al == 0) {
                   6416:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
                   6417:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
                   6418:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
                   6419:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
                   6420:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
                   6421:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
                   6422:                                char tmp[MAX_PATH];
                   6423:                                if(opt_len != 0) {
                   6424:                                        sprintf(tmp, "\"%s\" %s", path, opt);
                   6425:                                } else {
                   6426:                                        sprintf(tmp, "\"%s\"", path);
                   6427:                                }
                   6428:                                retval = system(tmp);
                   6429:                                return(0);
                   6430:                        }
                   6431:                }
                   6432:        }
                   6433:        
1.1       root     6434:        // copy environment
1.1.1.29  root     6435:        int umb_linked, env_seg, psp_seg;
1.1       root     6436:        
1.1.1.29  root     6437:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6438:                msdos_mem_unlink_umb();
                   6439:        }
1.1.1.8   root     6440:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6441:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6442:                        if(umb_linked != 0) {
                   6443:                                msdos_mem_link_umb();
                   6444:                        }
                   6445:                        return(-1);
                   6446:                }
1.1       root     6447:        }
                   6448:        if(param->env_seg == 0) {
                   6449:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6450:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6451:        } else {
                   6452:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6453:        }
                   6454:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6455:        
                   6456:        // check exe header
                   6457:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6458:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6459:        UINT16 cs, ss, ip, sp;
                   6460:        
                   6461:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6462:                // memory allocation
                   6463:                int header_size = header->header_size * 16;
                   6464:                int load_size = header->pages * 512 - header_size;
                   6465:                if(header_size + load_size < 512) {
                   6466:                        load_size = 512 - header_size;
                   6467:                }
                   6468:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6469:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6470:                        msdos_mem_free(env_seg);
                   6471:                        return(-1);
                   6472:                }
                   6473:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6474:                if(paragraphs > free_paragraphs) {
                   6475:                        paragraphs = free_paragraphs;
                   6476:                }
1.1.1.8   root     6477:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6478:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6479:                                if(umb_linked != 0) {
                   6480:                                        msdos_mem_link_umb();
                   6481:                                }
                   6482:                                msdos_mem_free(env_seg);
                   6483:                                return(-1);
                   6484:                        }
1.1       root     6485:                }
                   6486:                // relocation
                   6487:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6488:                for(int i = 0; i < header->relocations; i++) {
                   6489:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6490:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6491:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6492:                }
                   6493:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6494:                // segments
                   6495:                cs = header->init_cs + start_seg;
                   6496:                ss = header->init_ss + start_seg;
                   6497:                ip = header->init_ip;
                   6498:                sp = header->init_sp - 2; // for symdeb
                   6499:        } else {
                   6500:                // memory allocation
                   6501:                paragraphs = free_paragraphs;
1.1.1.8   root     6502:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6503:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6504:                                if(umb_linked != 0) {
                   6505:                                        msdos_mem_link_umb();
                   6506:                                }
                   6507:                                msdos_mem_free(env_seg);
                   6508:                                return(-1);
                   6509:                        }
1.1       root     6510:                }
                   6511:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6512:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6513:                // segments
                   6514:                cs = ss = psp_seg;
                   6515:                ip = 0x100;
                   6516:                sp = 0xfffe;
                   6517:        }
1.1.1.29  root     6518:        if(umb_linked != 0) {
                   6519:                msdos_mem_link_umb();
                   6520:        }
1.1       root     6521:        
                   6522:        // create psp
1.1.1.3   root     6523:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6524:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6525:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6526:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6527:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6528:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6529:        
                   6530:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6531:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6532:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6533:        
1.1.1.4   root     6534:        for(int i = 0; i < 8; i++) {
                   6535:                if(name_tmp[i] == '.') {
                   6536:                        mcb_psp->prog_name[i] = '\0';
                   6537:                        break;
                   6538:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6539:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6540:                        i++;
                   6541:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6542:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6543:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6544:                } else {
                   6545:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6546:                }
                   6547:        }
                   6548:        
1.1       root     6549:        // process info
                   6550:        process_t *process = msdos_process_info_create(psp_seg);
                   6551:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6552: #ifdef USE_DEBUGGER
                   6553:        strcpy(process->module_path, path);
                   6554: #endif
1.1       root     6555:        process->dta.w.l = 0x80;
                   6556:        process->dta.w.h = psp_seg;
                   6557:        process->switchar = '/';
                   6558:        process->max_files = 20;
                   6559:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6560:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6561:        process->parent_ds = SREG(DS);
1.1.1.31  root     6562:        process->parent_es = SREG(ES);
1.1       root     6563:        
                   6564:        current_psp = psp_seg;
1.1.1.23  root     6565:        msdos_sda_update(current_psp);
1.1       root     6566:        
                   6567:        if(al == 0x00) {
                   6568:                int_10h_feh_called = int_10h_ffh_called = false;
                   6569:                
1.1.1.38  root     6570:                // pipe
                   6571:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6572: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6573:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6574:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6575:                        } else {
                   6576:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6577:                        }
                   6578:                        if(fd != -1) {
                   6579:                                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     6580:                                psp->file_table[0] = fd;
                   6581:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6582:                        }
                   6583:                }
                   6584:                if(pipe_stdout_path[0] != '\0') {
                   6585:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6586:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6587:                                DeleteFile(pipe_stdout_path);
                   6588:                        }
1.1.1.45  root     6589: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6590:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6591:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6592:                        } else {
                   6593:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6594:                        }
                   6595:                        if(fd != -1) {
                   6596:                                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     6597:                                psp->file_table[1] = fd;
                   6598:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6599:                        }
                   6600:                }
1.1.1.39  root     6601:                if(pipe_stderr_path[0] != '\0') {
                   6602:                        if(_access(pipe_stderr_path, 0) == 0) {
                   6603:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6604:                                DeleteFile(pipe_stderr_path);
                   6605:                        }
1.1.1.45  root     6606: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6607:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6608:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6609:                        } else {
                   6610:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6611:                        }
                   6612:                        if(fd != -1) {
                   6613:                                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     6614:                                psp->file_table[2] = fd;
                   6615:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6616:                        }
                   6617:                }
1.1.1.38  root     6618:                
1.1       root     6619:                // registers and segments
                   6620:                REG16(AX) = REG16(BX) = 0x00;
                   6621:                REG16(CX) = 0xff;
                   6622:                REG16(DX) = psp_seg;
                   6623:                REG16(SI) = ip;
                   6624:                REG16(DI) = sp;
                   6625:                REG16(SP) = sp;
1.1.1.3   root     6626:                SREG(DS) = SREG(ES) = psp_seg;
                   6627:                SREG(SS) = ss;
                   6628:                i386_load_segment_descriptor(DS);
                   6629:                i386_load_segment_descriptor(ES);
                   6630:                i386_load_segment_descriptor(SS);
1.1       root     6631:                
                   6632:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6633:                i386_jmp_far(cs, ip);
                   6634:        } else if(al == 0x01) {
                   6635:                // copy ss:sp and cs:ip to param block
                   6636:                param->sp = sp;
                   6637:                param->ss = ss;
                   6638:                param->ip = ip;
                   6639:                param->cs = cs;
1.1.1.31  root     6640:                
                   6641:                // the AX value to be passed to the child program is put on top of the child's stack
                   6642:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6643:        }
                   6644:        return(0);
                   6645: }
                   6646: 
                   6647: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6648: {
                   6649:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6650:        
                   6651:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6652:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6653:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6654:        
1.1.1.3   root     6655:        SREG(SS) = psp->stack.w.h;
                   6656:        i386_load_segment_descriptor(SS);
1.1       root     6657:        REG16(SP) = psp->stack.w.l;
                   6658:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6659:        
1.1.1.28  root     6660: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6661:        process_t *current_process = NULL;
                   6662:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6663:                if(process[i].psp == psp_seg) {
                   6664:                        current_process = &process[i];
                   6665:                        break;
                   6666:                }
                   6667:        }
                   6668:        if(current_process == NULL) {
                   6669:                throw(0x1f); // general failure
                   6670:        }
                   6671:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6672:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6673:        if(current_process->called_by_int2eh) {
                   6674:                REG16(AX) = ret;
                   6675:        }
                   6676:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6677:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6678:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6679:        i386_load_segment_descriptor(ES);
1.1       root     6680:        
                   6681:        if(mem_free) {
1.1.1.8   root     6682:                int mcb_seg;
                   6683:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6684:                        msdos_mem_free(mcb_seg + 1);
                   6685:                }
                   6686:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6687:                        msdos_mem_free(mcb_seg + 1);
                   6688:                }
1.1       root     6689:                
                   6690:                for(int i = 0; i < MAX_FILES; i++) {
                   6691:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6692:                                _close(i);
1.1.1.20  root     6693:                                msdos_file_handler_close(i);
                   6694:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6695:                        }
                   6696:                }
1.1.1.13  root     6697:                msdos_dta_info_free(psp_seg);
1.1       root     6698:        }
1.1.1.14  root     6699:        msdos_stdio_reopen();
1.1       root     6700:        
1.1.1.28  root     6701:        memset(current_process, 0, sizeof(process_t));
1.1       root     6702:        
                   6703:        current_psp = psp->parent_psp;
                   6704:        retval = ret;
1.1.1.23  root     6705:        msdos_sda_update(current_psp);
1.1       root     6706: }
                   6707: 
                   6708: // drive
                   6709: 
1.1.1.42  root     6710: int pcbios_update_drive_param(int drive_num, int force_update);
                   6711: 
1.1       root     6712: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6713: {
1.1.1.41  root     6714:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6715:                return(0);
                   6716:        }
1.1.1.42  root     6717:        pcbios_update_drive_param(drive_num, force_update);
                   6718:        
                   6719:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6720:        *seg = DPB_TOP >> 4;
                   6721:        *ofs = sizeof(dpb_t) * drive_num;
                   6722:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6723:        
                   6724:        memset(dpb, 0, sizeof(dpb_t));
                   6725:        
1.1.1.41  root     6726:        dpb->drive_num = drive_num;
                   6727:        dpb->unit_num = drive_num;
1.1.1.42  root     6728:        
                   6729:        if(drive_param->valid) {
                   6730:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6731:                
                   6732:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6733:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6734:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6735:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6736:                switch(geo->MediaType) {
                   6737:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6738:                        dpb->media_type = 0xff;
                   6739:                        break;
                   6740:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6741:                        dpb->media_type = 0xfe;
                   6742:                        break;
                   6743:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6744:                        dpb->media_type = 0xfd;
                   6745:                        break;
                   6746:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6747:                        dpb->media_type = 0xfc;
                   6748:                        break;
                   6749:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6750:                case F3_1Pt2_512:
                   6751:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6752:                case F5_720_512:
                   6753:                        dpb->media_type = 0xf9;
                   6754:                        break;
                   6755:                case FixedMedia:        // hard disk
                   6756:                case RemovableMedia:
                   6757:                case Unknown:
                   6758:                        dpb->media_type = 0xf8;
                   6759:                        break;
                   6760:                default:
                   6761:                        dpb->media_type = 0xf0;
                   6762:                        break;
                   6763:                }
                   6764:        }
1.1.1.41  root     6765:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6766:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6767:        dpb->info_sector = 0xffff;
                   6768:        dpb->backup_boot_sector = 0xffff;
                   6769:        dpb->free_clusters = 0xffff;
                   6770:        dpb->free_search_cluster = 0xffffffff;
                   6771:        
                   6772:        return(drive_param->valid);
1.1       root     6773: }
                   6774: 
                   6775: // pc bios
                   6776: 
1.1.1.35  root     6777: #ifdef USE_SERVICE_THREAD
                   6778: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6779: {
                   6780: #if defined(HAS_I386)
                   6781:        if(m_SF != 0) {
                   6782:                m_SF = 0;
1.1.1.49  root     6783:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6784:        } else {
                   6785:                m_SF = 1;
1.1.1.49  root     6786:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6787:        }
                   6788: #else
                   6789:        if(m_SignVal < 0) {
                   6790:                m_SignVal = 0;
1.1.1.49  root     6791:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6792:        } else {
                   6793:                m_SignVal = -1;
1.1.1.49  root     6794:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6795:        }
                   6796: #endif
1.1.1.49  root     6797:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     6798:        in_service = true;
                   6799:        service_exit = false;
                   6800:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6801: }
                   6802: 
                   6803: void finish_service_loop()
                   6804: {
                   6805:        if(in_service && service_exit) {
                   6806: #if defined(HAS_I386)
                   6807:                if(m_SF != 0) {
                   6808:                        m_SF = 0;
                   6809:                } else {
                   6810:                        m_SF = 1;
                   6811:                }
                   6812: #else
                   6813:                if(m_SignVal < 0) {
                   6814:                        m_SignVal = 0;
                   6815:                } else {
                   6816:                        m_SignVal = -1;
                   6817:                }
                   6818: #endif
                   6819:                in_service = false;
                   6820:        }
                   6821: }
                   6822: #endif
                   6823: 
1.1.1.19  root     6824: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6825: {
                   6826:        static unsigned __int64 start_msec_since_midnight = 0;
                   6827:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6828:        
                   6829:        if(start_msec_since_midnight == 0) {
                   6830:                SYSTEMTIME time;
                   6831:                GetLocalTime(&time);
                   6832:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6833:                start_msec_since_hostboot = cur_msec;
                   6834:        }
                   6835:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6836:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6837:        return (UINT32)tick;
                   6838: }
                   6839: 
                   6840: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6841: {
                   6842:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6843:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6844:        
                   6845:        if(prev_tick > next_tick) {
                   6846:                mem[0x470] = 1;
                   6847:        }
                   6848:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6849: }
                   6850: 
1.1.1.14  root     6851: inline void pcbios_irq0()
                   6852: {
                   6853:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6854:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6855: }
                   6856: 
1.1.1.16  root     6857: int pcbios_get_text_vram_address(int page)
1.1       root     6858: {
                   6859:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6860:                return TEXT_VRAM_TOP;
1.1       root     6861:        } else {
1.1.1.14  root     6862:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6863:        }
                   6864: }
                   6865: 
1.1.1.16  root     6866: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6867: {
1.1.1.14  root     6868:        if(!int_10h_feh_called) {
1.1.1.16  root     6869:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6870:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6871:                return SHADOW_BUF_TOP;
                   6872:        } else {
1.1.1.14  root     6873:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6874:        }
                   6875: }
                   6876: 
1.1.1.16  root     6877: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6878: {
1.1.1.16  root     6879:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6880: }
                   6881: 
1.1.1.56  root     6882: bool pcbios_set_font_size(int width, int height)
                   6883: {
                   6884:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   6885:        return(set_console_font_size(hStdout, width, height));
                   6886: }
                   6887: 
1.1.1.16  root     6888: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6889: {
1.1.1.14  root     6890:        // clear the existing screen, not just the new one
                   6891:        int clr_height = max(height, scr_height);
                   6892:        
1.1.1.16  root     6893:        if(scr_width != width || scr_height != height) {
                   6894:                change_console_size(width, height);
1.1.1.14  root     6895:        }
                   6896:        mem[0x462] = 0;
                   6897:        *(UINT16 *)(mem + 0x44e) = 0;
                   6898:        
1.1.1.16  root     6899:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6900:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6901:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6902:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     6903:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     6904:        
1.1.1.23  root     6905:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6906:        if(clr_screen) {
1.1.1.14  root     6907:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6908:                        mem[ofs++] = 0x20;
                   6909:                        mem[ofs++] = 0x07;
                   6910:                }
                   6911:                
1.1.1.35  root     6912: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6913:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6914: #endif
1.1.1.14  root     6915:                for(int y = 0; y < clr_height; y++) {
                   6916:                        for(int x = 0; x < scr_width; x++) {
                   6917:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6918:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6919:                        }
                   6920:                }
                   6921:                SMALL_RECT rect;
1.1.1.14  root     6922:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6923:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6924:                vram_length_char = vram_last_length_char = 0;
                   6925:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6926: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6927:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6928: #endif
1.1       root     6929:        }
1.1.1.14  root     6930:        COORD co;
                   6931:        co.X = 0;
                   6932:        co.Y = scr_top;
                   6933:        SetConsoleCursorPosition(hStdout, co);
                   6934:        cursor_moved = true;
                   6935:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6936: }
                   6937: 
1.1.1.36  root     6938: void pcbios_update_cursor_position()
                   6939: {
                   6940:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6941:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6942:        if(!restore_console_on_exit) {
                   6943:                scr_top = csbi.srWindow.Top;
                   6944:        }
                   6945:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6946:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6947: }
                   6948: 
1.1.1.16  root     6949: inline void pcbios_int_10h_00h()
                   6950: {
                   6951:        switch(REG8(AL) & 0x7f) {
                   6952:        case 0x70: // v-text mode
                   6953:        case 0x71: // extended cga v-text mode
                   6954:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6955:                break;
                   6956:        default:
                   6957:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6958:                break;
                   6959:        }
                   6960:        if(REG8(AL) & 0x80) {
                   6961:                mem[0x487] |= 0x80;
                   6962:        } else {
                   6963:                mem[0x487] &= ~0x80;
                   6964:        }
                   6965:        mem[0x449] = REG8(AL) & 0x7f;
                   6966: }
                   6967: 
1.1       root     6968: inline void pcbios_int_10h_01h()
                   6969: {
1.1.1.13  root     6970:        mem[0x460] = REG8(CL);
                   6971:        mem[0x461] = REG8(CH);
1.1.1.14  root     6972:        
1.1.1.23  root     6973:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6974:        CONSOLE_CURSOR_INFO ci;
                   6975:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.58! root     6976:        
        !          6977: //     BOOL bVisible = ((REG8(CH) & 0x20) == 0 || (REG8(CH) & 7) > (REG8(CL) & 7));
        !          6978:        BOOL bVisible = TRUE;
        !          6979:        DWORD dwSize = ((REG8(CL) & 7) + 1) * 100 / 8;
        !          6980:        
        !          6981:        if(ci.bVisible != bVisible || ci.dwSize != dwSize) {
        !          6982:                ci.bVisible = bVisible;
        !          6983:                ci.dwSize = dwSize;
        !          6984:                SetConsoleCursorInfo(hStdout, &ci);
        !          6985:        }
1.1       root     6986: }
                   6987: 
                   6988: inline void pcbios_int_10h_02h()
                   6989: {
1.1.1.14  root     6990:        // continuously setting the cursor effectively stops it blinking
                   6991:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6992:                COORD co;
                   6993:                co.X = REG8(DL);
1.1.1.14  root     6994:                co.Y = REG8(DH) + scr_top;
                   6995:                
                   6996:                // some programs hide the cursor by moving it off screen
                   6997:                static bool hidden = false;
1.1.1.23  root     6998:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6999:                CONSOLE_CURSOR_INFO ci;
                   7000:                GetConsoleCursorInfo(hStdout, &ci);
                   7001:                
                   7002:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   7003:                        if(ci.bVisible) {
                   7004:                                ci.bVisible = FALSE;
                   7005: //                             SetConsoleCursorInfo(hStdout, &ci);
                   7006:                                hidden = true;
                   7007:                        }
                   7008:                } else if(hidden) {
                   7009:                        if(!ci.bVisible) {
                   7010:                                ci.bVisible = TRUE;
                   7011: //                             SetConsoleCursorInfo(hStdout, &ci);
                   7012:                        }
                   7013:                        hidden = false;
                   7014:                }
1.1       root     7015:        }
1.1.1.14  root     7016:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   7017:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     7018: }
                   7019: 
                   7020: inline void pcbios_int_10h_03h()
                   7021: {
1.1.1.14  root     7022:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7023:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7024:        REG8(CL) = mem[0x460];
                   7025:        REG8(CH) = mem[0x461];
                   7026: }
                   7027: 
                   7028: inline void pcbios_int_10h_05h()
                   7029: {
1.1.1.14  root     7030:        if(REG8(AL) >= vram_pages) {
                   7031:                return;
                   7032:        }
                   7033:        if(mem[0x462] != REG8(AL)) {
                   7034:                vram_flush();
                   7035:                
1.1.1.23  root     7036:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7037:                SMALL_RECT rect;
1.1.1.14  root     7038:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   7039:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7040:                
1.1.1.16  root     7041:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     7042:                        for(int x = 0; x < scr_width; x++) {
                   7043:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7044:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7045:                        }
                   7046:                }
1.1.1.16  root     7047:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     7048:                        for(int x = 0; x < scr_width; x++) {
                   7049:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   7050:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     7051:                        }
                   7052:                }
1.1.1.14  root     7053:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7054:                
                   7055:                COORD co;
1.1.1.14  root     7056:                co.X = mem[0x450 + REG8(AL) * 2];
                   7057:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   7058:                if(co.Y < scr_top + scr_height) {
                   7059:                        SetConsoleCursorPosition(hStdout, co);
                   7060:                }
1.1       root     7061:        }
1.1.1.14  root     7062:        mem[0x462] = REG8(AL);
                   7063:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   7064:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     7065:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     7066:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     7067:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     7068:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     7069:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7070: }
                   7071: 
                   7072: inline void pcbios_int_10h_06h()
                   7073: {
1.1.1.14  root     7074:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7075:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7076:                return;
                   7077:        }
                   7078:        vram_flush();
                   7079:        
1.1.1.23  root     7080:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7081:        SMALL_RECT rect;
1.1.1.14  root     7082:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   7083:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   7084:        
                   7085:        int right = min(REG8(DL), scr_width - 1);
                   7086:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7087:        
                   7088:        if(REG8(AL) == 0) {
1.1.1.14  root     7089:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7090:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7091:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7092:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7093:                        }
                   7094:                }
                   7095:        } else {
1.1.1.14  root     7096:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     7097:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7098:                                if(y2 <= bottom) {
                   7099:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7100:                                } else {
1.1.1.14  root     7101:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7102:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7103:                                }
1.1.1.14  root     7104:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7105:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7106:                        }
                   7107:                }
                   7108:        }
1.1.1.14  root     7109:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7110: }
                   7111: 
                   7112: inline void pcbios_int_10h_07h()
                   7113: {
1.1.1.14  root     7114:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7115:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7116:                return;
                   7117:        }
                   7118:        vram_flush();
                   7119:        
1.1.1.23  root     7120:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7121:        SMALL_RECT rect;
1.1.1.14  root     7122:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   7123:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   7124:        
                   7125:        int right = min(REG8(DL), scr_width - 1);
                   7126:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7127:        
                   7128:        if(REG8(AL) == 0) {
1.1.1.14  root     7129:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7130:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7131:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7132:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7133:                        }
                   7134:                }
                   7135:        } else {
1.1.1.14  root     7136:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     7137:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7138:                                if(y2 >= REG8(CH)) {
                   7139:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7140:                                } else {
1.1.1.14  root     7141:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7142:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7143:                                }
1.1.1.14  root     7144:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7145:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7146:                        }
                   7147:                }
                   7148:        }
1.1.1.14  root     7149:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7150: }
                   7151: 
                   7152: inline void pcbios_int_10h_08h()
                   7153: {
                   7154:        COORD co;
                   7155:        DWORD num;
                   7156:        
1.1.1.14  root     7157:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7158:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7159:        
                   7160:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7161:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7162:                co.Y += scr_top;
                   7163:                vram_flush();
1.1       root     7164:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   7165:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   7166:                REG8(AL) = scr_char[0];
                   7167:                REG8(AH) = scr_attr[0];
                   7168:        } else {
1.1.1.16  root     7169:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     7170:        }
                   7171: }
                   7172: 
                   7173: inline void pcbios_int_10h_09h()
                   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:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7192:                        mem[dest++] = REG8(BL);
1.1       root     7193:                }
1.1.1.35  root     7194: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7195:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7196: #endif
1.1       root     7197:        } else {
1.1.1.14  root     7198:                while(dest < end) {
1.1       root     7199:                        mem[dest++] = REG8(AL);
                   7200:                        mem[dest++] = REG8(BL);
                   7201:                }
                   7202:        }
                   7203: }
                   7204: 
                   7205: inline void pcbios_int_10h_0ah()
                   7206: {
                   7207:        COORD co;
                   7208:        
1.1.1.14  root     7209:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7210:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7211:        
1.1.1.16  root     7212:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7213:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7214:        
                   7215:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7216: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7217:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7218: #endif
1.1.1.16  root     7219:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7220:                while(dest < end) {
                   7221:                        write_text_vram_char(dest - vram, REG8(AL));
                   7222:                        mem[dest++] = REG8(AL);
                   7223:                        dest++;
1.1       root     7224:                }
1.1.1.35  root     7225: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7226:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7227: #endif
1.1       root     7228:        } else {
1.1.1.14  root     7229:                while(dest < end) {
1.1       root     7230:                        mem[dest++] = REG8(AL);
                   7231:                        dest++;
                   7232:                }
                   7233:        }
                   7234: }
                   7235: 
1.1.1.40  root     7236: HDC get_console_window_device_context()
                   7237: {
                   7238:        static HWND hwndFound = 0;
                   7239:        
                   7240:        if(hwndFound == 0) {
                   7241:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   7242:                char pszNewWindowTitle[1024];
                   7243:                char pszOldWindowTitle[1024];
                   7244:                
                   7245:                GetConsoleTitle(pszOldWindowTitle, 1024);
                   7246:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
                   7247:                SetConsoleTitle(pszNewWindowTitle);
                   7248:                Sleep(100);
                   7249:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
                   7250:                SetConsoleTitle(pszOldWindowTitle);
                   7251:        }
                   7252:        return GetDC(hwndFound);
                   7253: }
                   7254: 
                   7255: inline void pcbios_int_10h_0ch()
                   7256: {
                   7257:        HDC hdc = get_console_window_device_context();
                   7258:        
                   7259:        if(hdc != NULL) {
                   7260:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7261:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7262:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7263:                
                   7264:                if(REG8(AL) & 0x80) {
                   7265:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7266:                        if(color != CLR_INVALID) {
                   7267:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7268:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7269:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7270:                        }
                   7271:                }
                   7272:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7273:        }
                   7274: }
                   7275: 
                   7276: inline void pcbios_int_10h_0dh()
                   7277: {
                   7278:        HDC hdc = get_console_window_device_context();
                   7279:        BYTE r = 0;
                   7280:        BYTE g = 0;
                   7281:        BYTE b = 0;
                   7282:        
                   7283:        if(hdc != NULL) {
                   7284:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7285:                if(color != CLR_INVALID) {
                   7286:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7287:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7288:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7289:                }
                   7290:        }
                   7291:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7292: }
                   7293: 
1.1       root     7294: inline void pcbios_int_10h_0eh()
                   7295: {
1.1.1.14  root     7296:        DWORD num;
                   7297:        COORD co;
                   7298:        
1.1.1.54  root     7299:        co.X = mem[0x450 + mem[0x462] * 2];
                   7300:        co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14  root     7301:        
                   7302:        if(REG8(AL) == 7) {
                   7303:                //MessageBeep(-1);
                   7304:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7305:                if(REG8(AL) == 10) {
                   7306:                        vram_flush();
                   7307:                }
1.1.1.23  root     7308:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7309:                cursor_moved = true;
                   7310:        } else {
1.1.1.54  root     7311:                int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35  root     7312: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7313:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7314: #endif
1.1.1.54  root     7315:                int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
                   7316:                write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7317: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7318:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7319: #endif
1.1.1.54  root     7320:                
                   7321:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   7322:                if(++co.X == scr_width) {
                   7323:                        co.X = 0;
                   7324:                        if(++co.Y == scr_height) {
                   7325:                                vram_flush();
                   7326:                                WriteConsole(hStdout, "\n", 1, &num, NULL);
1.1.1.14  root     7327:                                cursor_moved = true;
                   7328:                        }
                   7329:                }
1.1.1.54  root     7330:                if(!cursor_moved) {
                   7331:                        co.Y += scr_top;
                   7332:                        SetConsoleCursorPosition(hStdout, co);
                   7333:                        cursor_moved = true;
                   7334:                }
1.1.1.14  root     7335:                mem[dest] = REG8(AL);
                   7336:        }
1.1       root     7337: }
                   7338: 
                   7339: inline void pcbios_int_10h_0fh()
                   7340: {
                   7341:        REG8(AL) = mem[0x449];
                   7342:        REG8(AH) = mem[0x44a];
                   7343:        REG8(BH) = mem[0x462];
                   7344: }
                   7345: 
1.1.1.14  root     7346: inline void pcbios_int_10h_11h()
                   7347: {
                   7348:        switch(REG8(AL)) {
1.1.1.58! root     7349:        case 0x00:
        !          7350:        case 0x10:
        !          7351:                if(REG8(BH) != 0 && pcbios_set_font_size(8, REG8(BH))) {
        !          7352:                        pcbios_set_console_size(80, (25 * 16) / REG8(BH), true);
        !          7353:                } else {
        !          7354:                        m_CF = 1; // never failed in real PC BIOS
        !          7355:                }
        !          7356:                break;
1.1.1.16  root     7357:        case 0x01:
1.1.1.14  root     7358:        case 0x11:
1.1.1.58! root     7359: //             if(pcbios_set_font_size(8, 14)) {
        !          7360:                if(pcbios_set_font_size(8, 12)) {
        !          7361:                        pcbios_set_console_size(80, 28, true); // 28 = 25 * 16 / 14
        !          7362:                } else {
        !          7363:                        m_CF = 1; // never failed in real PC BIOS
1.1.1.56  root     7364:                }
1.1.1.14  root     7365:                break;
1.1.1.16  root     7366:        case 0x02:
1.1.1.14  root     7367:        case 0x12:
1.1.1.58! root     7368:                if(pcbios_set_font_size(8, 8)) {
        !          7369:                        pcbios_set_console_size(80, 50, true); // 50 = 25 * 16 / 8
        !          7370:                } else {
        !          7371:                        m_CF = 1; // never failed in real PC BIOS
1.1.1.56  root     7372:                }
1.1.1.14  root     7373:                break;
1.1.1.16  root     7374:        case 0x04:
1.1.1.14  root     7375:        case 0x14:
1.1.1.58! root     7376: //             if(pcbios_set_font_size(8, 16)) {
        !          7377:                if(pcbios_set_font_size(8, 18)) {
        !          7378:                        pcbios_set_console_size(80, 25, true);
        !          7379:                } else {
        !          7380:                        m_CF = 1; // never failed in real PC BIOS
1.1.1.56  root     7381:                }
1.1.1.58! root     7382:                break;
        !          7383:        case 0x18:
        !          7384: //             if(pcbios_set_font_size(8, 16)) {
        !          7385:                if(pcbios_set_font_size(8, 18)) {
1.1.1.56  root     7386:                        pcbios_set_console_size(80, 50, true);
                   7387:                } else {
1.1.1.58! root     7388:                        m_CF = 1; // never failed in real PC BIOS
1.1.1.56  root     7389:                }
1.1.1.14  root     7390:                break;
                   7391:        case 0x30:
                   7392:                SREG(ES) = 0;
                   7393:                i386_load_segment_descriptor(ES);
                   7394:                REG16(BP) = 0;
                   7395:                REG16(CX) = mem[0x485];
                   7396:                REG8(DL) = mem[0x484];
                   7397:                break;
1.1.1.54  root     7398:        default:
                   7399:                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));
                   7400:                m_CF = 1;
                   7401:                break;
1.1.1.14  root     7402:        }
                   7403: }
                   7404: 
                   7405: inline void pcbios_int_10h_12h()
                   7406: {
1.1.1.16  root     7407:        switch(REG8(BL)) {
                   7408:        case 0x10:
1.1.1.14  root     7409:                REG16(BX) = 0x0003;
                   7410:                REG16(CX) = 0x0009;
1.1.1.16  root     7411:                break;
1.1.1.14  root     7412:        }
                   7413: }
                   7414: 
1.1       root     7415: inline void pcbios_int_10h_13h()
                   7416: {
1.1.1.3   root     7417:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7418:        COORD co;
                   7419:        DWORD num;
                   7420:        
                   7421:        co.X = REG8(DL);
1.1.1.14  root     7422:        co.Y = REG8(DH) + scr_top;
                   7423:        
                   7424:        vram_flush();
1.1       root     7425:        
                   7426:        switch(REG8(AL)) {
                   7427:        case 0x00:
                   7428:        case 0x01:
                   7429:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7430:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7431:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7432:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7433:                        SetConsoleCursorPosition(hStdout, co);
                   7434:                        
                   7435:                        if(csbi.wAttributes != REG8(BL)) {
                   7436:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7437:                        }
1.1.1.14  root     7438:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   7439:                        
1.1       root     7440:                        if(csbi.wAttributes != REG8(BL)) {
                   7441:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7442:                        }
                   7443:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7444:                                if(!restore_console_on_exit) {
                   7445:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7446:                                        scr_top = csbi.srWindow.Top;
                   7447:                                }
1.1.1.14  root     7448:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7449:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7450:                                SetConsoleCursorPosition(hStdout, co);
                   7451:                        } else {
                   7452:                                cursor_moved = true;
                   7453:                        }
                   7454:                } else {
1.1.1.3   root     7455:                        m_CF = 1;
1.1       root     7456:                }
                   7457:                break;
                   7458:        case 0x02:
                   7459:        case 0x03:
                   7460:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7461:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7462:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7463:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7464:                        SetConsoleCursorPosition(hStdout, co);
                   7465:                        
                   7466:                        WORD wAttributes = csbi.wAttributes;
                   7467:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7468:                                if(wAttributes != mem[ofs + 1]) {
                   7469:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7470:                                        wAttributes = mem[ofs + 1];
                   7471:                                }
1.1.1.14  root     7472:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7473:                        }
                   7474:                        if(csbi.wAttributes != wAttributes) {
                   7475:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7476:                        }
                   7477:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7478:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7479:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7480:                                SetConsoleCursorPosition(hStdout, co);
                   7481:                        } else {
                   7482:                                cursor_moved = true;
                   7483:                        }
                   7484:                } else {
1.1.1.3   root     7485:                        m_CF = 1;
1.1       root     7486:                }
                   7487:                break;
                   7488:        case 0x10:
                   7489:        case 0x11:
                   7490:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7491:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7492:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7493:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7494:                        for(int i = 0; i < num; i++) {
                   7495:                                mem[ofs++] = scr_char[i];
                   7496:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7497:                                if(REG8(AL) & 0x01) {
1.1       root     7498:                                        mem[ofs++] = 0;
                   7499:                                        mem[ofs++] = 0;
                   7500:                                }
                   7501:                        }
                   7502:                } else {
1.1.1.16  root     7503:                        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     7504:                                mem[ofs++] = mem[src++];
                   7505:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7506:                                if(REG8(AL) & 0x01) {
1.1       root     7507:                                        mem[ofs++] = 0;
                   7508:                                        mem[ofs++] = 0;
                   7509:                                }
1.1.1.14  root     7510:                                if(++co.X == scr_width) {
                   7511:                                        if(++co.Y == scr_height) {
1.1       root     7512:                                                break;
                   7513:                                        }
                   7514:                                        co.X = 0;
                   7515:                                }
                   7516:                        }
                   7517:                }
                   7518:                break;
1.1.1.45  root     7519:        case 0x12: // ???
                   7520:        case 0x13: // ???
1.1       root     7521:        case 0x20:
                   7522:        case 0x21:
                   7523:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7524:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7525:                        int len = min(REG16(CX), scr_width * scr_height);
                   7526:                        for(int i = 0; i < len; i++) {
1.1       root     7527:                                scr_char[i] = mem[ofs++];
                   7528:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7529:                                if(REG8(AL) & 0x01) {
1.1       root     7530:                                        ofs += 2;
                   7531:                                }
                   7532:                        }
1.1.1.14  root     7533:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7534:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7535:                } else {
1.1.1.16  root     7536:                        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     7537:                                mem[dest++] = mem[ofs++];
                   7538:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7539:                                if(REG8(AL) & 0x01) {
1.1       root     7540:                                        ofs += 2;
                   7541:                                }
1.1.1.14  root     7542:                                if(++co.X == scr_width) {
                   7543:                                        if(++co.Y == scr_height) {
1.1       root     7544:                                                break;
                   7545:                                        }
                   7546:                                        co.X = 0;
                   7547:                                }
                   7548:                        }
                   7549:                }
                   7550:                break;
                   7551:        default:
1.1.1.22  root     7552:                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     7553:                m_CF = 1;
1.1       root     7554:                break;
                   7555:        }
                   7556: }
                   7557: 
1.1.1.30  root     7558: inline void pcbios_int_10h_18h()
                   7559: {
                   7560:        switch(REG8(AL)) {
                   7561:        case 0x00:
                   7562:        case 0x01:
                   7563: //             REG8(AL) = 0x86;
                   7564:                REG8(AL) = 0x00;
                   7565:                break;
                   7566:        default:
                   7567:                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));
                   7568:                m_CF = 1;
                   7569:                break;
                   7570:        }
                   7571: }
                   7572: 
1.1.1.14  root     7573: inline void pcbios_int_10h_1ah()
                   7574: {
                   7575:        switch(REG8(AL)) {
                   7576:        case 0x00:
                   7577:                REG8(AL) = 0x1a;
                   7578:                REG8(BL) = 0x08;
                   7579:                REG8(BH) = 0x00;
                   7580:                break;
                   7581:        default:
1.1.1.22  root     7582:                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     7583:                m_CF = 1;
                   7584:                break;
                   7585:        }
                   7586: }
                   7587: 
1.1       root     7588: inline void pcbios_int_10h_1dh()
                   7589: {
                   7590:        switch(REG8(AL)) {
1.1.1.43  root     7591:        case 0x00:
                   7592:                // DOS/V Shift Status Line Control is not supported
                   7593:                m_CF = 1;
                   7594:                break;
1.1       root     7595:        case 0x01:
                   7596:                break;
                   7597:        case 0x02:
                   7598:                REG16(BX) = 0;
                   7599:                break;
                   7600:        default:
1.1.1.22  root     7601:                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));
                   7602:                m_CF = 1;
                   7603:                break;
                   7604:        }
                   7605: }
                   7606: 
                   7607: inline void pcbios_int_10h_4fh()
                   7608: {
                   7609:        switch(REG8(AL)) {
                   7610:        case 0x00:
                   7611:                REG8(AH) = 0x02; // not supported
                   7612:                break;
                   7613:        case 0x01:
                   7614:        case 0x02:
                   7615:        case 0x03:
                   7616:        case 0x04:
                   7617:        case 0x05:
                   7618:        case 0x06:
                   7619:        case 0x07:
                   7620:        case 0x08:
                   7621:        case 0x09:
                   7622:        case 0x0a:
                   7623:        case 0x0b:
                   7624:        case 0x0c:
                   7625:                REG8(AH) = 0x01; // failed
                   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));
1.1.1.3   root     7629:                m_CF = 1;
1.1       root     7630:                break;
                   7631:        }
                   7632: }
                   7633: 
                   7634: inline void pcbios_int_10h_82h()
                   7635: {
                   7636:        static UINT8 mode = 0;
                   7637:        
                   7638:        switch(REG8(AL)) {
1.1.1.22  root     7639:        case 0x00:
1.1       root     7640:                if(REG8(BL) != 0xff) {
                   7641:                        mode = REG8(BL);
                   7642:                }
                   7643:                REG8(AL) = mode;
                   7644:                break;
                   7645:        default:
1.1.1.22  root     7646:                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     7647:                m_CF = 1;
1.1       root     7648:                break;
                   7649:        }
                   7650: }
                   7651: 
1.1.1.22  root     7652: inline void pcbios_int_10h_83h()
                   7653: {
                   7654:        static UINT8 mode = 0;
                   7655:        
                   7656:        switch(REG8(AL)) {
                   7657:        case 0x00:
                   7658:                REG16(AX) = 0; // offset???
                   7659:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7660:                i386_load_segment_descriptor(ES);
                   7661:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7662:                break;
                   7663:        default:
                   7664:                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));
                   7665:                m_CF = 1;
                   7666:                break;
                   7667:        }
                   7668: }
                   7669: 
                   7670: inline void pcbios_int_10h_90h()
                   7671: {
                   7672:        REG8(AL) = mem[0x449];
                   7673: }
                   7674: 
                   7675: inline void pcbios_int_10h_91h()
                   7676: {
                   7677:        REG8(AL) = 0x04; // VGA
                   7678: }
                   7679: 
                   7680: inline void pcbios_int_10h_efh()
                   7681: {
                   7682:        REG16(DX) = 0xffff;
                   7683: }
                   7684: 
1.1       root     7685: inline void pcbios_int_10h_feh()
                   7686: {
                   7687:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7688:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7689:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7690:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7691:        }
                   7692:        int_10h_feh_called = true;
                   7693: }
                   7694: 
                   7695: inline void pcbios_int_10h_ffh()
                   7696: {
                   7697:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7698:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7699:                COORD co;
                   7700:                DWORD num;
                   7701:                
1.1.1.14  root     7702:                vram_flush();
                   7703:                
                   7704:                co.X = (REG16(DI) >> 1) % scr_width;
                   7705:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7706:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7707:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7708:                int len;
                   7709:                for(len = 0; ofs < end; len++) {
                   7710:                        scr_char[len] = mem[ofs++];
                   7711:                        scr_attr[len] = mem[ofs++];
                   7712:                }
                   7713:                co.Y += scr_top;
                   7714:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7715:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7716:        }
                   7717:        int_10h_ffh_called = true;
                   7718: }
                   7719: 
1.1.1.42  root     7720: int pcbios_update_drive_param(int drive_num, int force_update)
                   7721: {
                   7722:        if(drive_num >= 0 && drive_num < 26) {
                   7723:                drive_param_t *drive_param = &drive_params[drive_num];
                   7724:                
                   7725:                if(force_update || !drive_param->initialized) {
                   7726:                        drive_param->valid = 0;
                   7727:                        char dev[64];
                   7728:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7729:                        
                   7730:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7731:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7732:                                DWORD dwSize;
                   7733:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7734:                                        drive_param->valid = 1;
                   7735:                                }
                   7736:                                CloseHandle(hFile);
                   7737:                        }
                   7738:                        drive_param->initialized = 1;
                   7739:                }
                   7740:                return(drive_param->valid);
                   7741:        }
                   7742:        return(0);
                   7743: }
                   7744: 
                   7745: inline void pcbios_int_13h_00h()
                   7746: {
                   7747:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7748:        
                   7749:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7750:                REG8(AH) = 0x00; // successful completion
                   7751:        } else {
                   7752:                if(REG8(DL) & 0x80) {
                   7753:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7754:                } else {
                   7755:                        REG8(AH) = 0x80; // timeout (not ready)
                   7756:                }
                   7757:                m_CF = 1;
                   7758:        }
                   7759: }
                   7760: 
                   7761: inline void pcbios_int_13h_02h()
                   7762: {
                   7763:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7764:        
                   7765:        if(REG8(AL) == 0) {
                   7766:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7767:                m_CF = 1;
                   7768:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7769:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7770:                m_CF = 1;
                   7771:        } else {
                   7772:                drive_param_t *drive_param = &drive_params[drive_num];
                   7773:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7774:                char dev[64];
                   7775:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7776:                
                   7777:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7778:                if(hFile == INVALID_HANDLE_VALUE) {
                   7779:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7780:                        m_CF = 1;
                   7781:                } else {
                   7782:                        UINT32 sector_num = REG8(AL);
                   7783:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7784:                        UINT32 head = REG8(DH);
                   7785:                        UINT32 sector = REG8(CL) & 0x3f;
                   7786:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7787:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7788:                        DWORD dwSize;
                   7789:                        
                   7790: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7791: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7792: //                             m_CF = 1;
                   7793: //                     } else 
                   7794:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7795:                                REG8(AH) = 0x04; // sector not found/read error
                   7796:                                m_CF = 1;
                   7797:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7798:                                REG8(AH) = 0x04; // sector not found/read error
                   7799:                                m_CF = 1;
                   7800:                        } else {
                   7801:                                REG8(AH) = 0x00; // successful completion
                   7802:                        }
                   7803:                        CloseHandle(hFile);
                   7804:                }
                   7805:        }
                   7806: }
                   7807: 
                   7808: inline void pcbios_int_13h_03h()
                   7809: {
                   7810:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7811:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7812:        
                   7813:        if(REG8(AL) == 0) {
                   7814:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7815:                m_CF = 1;
                   7816:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7817:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7818:                m_CF = 1;
                   7819:        } else if(!drive_params[drive_num].is_fdd()) {
                   7820:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7821:                m_CF = 1;
                   7822:        } else {
                   7823:                drive_param_t *drive_param = &drive_params[drive_num];
                   7824:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7825:                char dev[64];
                   7826:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7827:                
                   7828:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7829:                if(hFile == INVALID_HANDLE_VALUE) {
                   7830:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7831:                        m_CF = 1;
                   7832:                } else {
                   7833:                        UINT32 sector_num = REG8(AL);
                   7834:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7835:                        UINT32 head = REG8(DH);
                   7836:                        UINT32 sector = REG8(CL) & 0x3f;
                   7837:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7838:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7839:                        DWORD dwSize;
                   7840:                        
                   7841: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7842: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7843: //                             m_CF = 1;
                   7844: //                     } else 
                   7845:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7846:                                REG8(AH) = 0x04; // sector not found/read error
                   7847:                                m_CF = 1;
                   7848:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7849:                                REG8(AH) = 0x04; // sector not found/read error
                   7850:                                m_CF = 1;
                   7851:                        } else {
                   7852:                                REG8(AH) = 0x00; // successful completion
                   7853:                        }
                   7854:                        CloseHandle(hFile);
                   7855:                }
                   7856:        }
                   7857: }
                   7858: 
                   7859: inline void pcbios_int_13h_04h()
                   7860: {
                   7861:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7862:        
                   7863:        if(REG8(AL) == 0) {
                   7864:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7865:                m_CF = 1;
                   7866:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7867:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7868:                m_CF = 1;
                   7869:        } else {
                   7870:                drive_param_t *drive_param = &drive_params[drive_num];
                   7871:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7872:                char dev[64];
                   7873:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7874:                
                   7875:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7876:                if(hFile == INVALID_HANDLE_VALUE) {
                   7877:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7878:                        m_CF = 1;
                   7879:                } else {
                   7880:                        UINT32 sector_num = REG8(AL);
                   7881:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7882:                        UINT32 head = REG8(DH);
                   7883:                        UINT32 sector = REG8(CL) & 0x3f;
                   7884:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7885:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7886:                        DWORD dwSize;
                   7887:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   7888:                        
                   7889: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7890: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7891: //                             m_CF = 1;
                   7892: //                     } else 
                   7893:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7894:                                REG8(AH) = 0x04; // sector not found/read error
                   7895:                                m_CF = 1;
                   7896:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7897:                                REG8(AH) = 0x04; // sector not found/read error
                   7898:                                m_CF = 1;
                   7899:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   7900:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   7901:                                m_CF = 1;
                   7902:                        } else {
                   7903:                                REG8(AH) = 0x00; // successful completion
                   7904:                        }
                   7905:                        free(tmp_buffer);
                   7906:                        CloseHandle(hFile);
                   7907:                }
                   7908:        }
                   7909: }
                   7910: 
                   7911: inline void pcbios_int_13h_08h()
                   7912: {
                   7913:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7914:        
                   7915:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7916:                drive_param_t *drive_param = &drive_params[drive_num];
                   7917:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7918:                
                   7919:                REG16(AX) = 0x0000;
                   7920:                switch(geo->MediaType) {
                   7921:                case F5_360_512:
                   7922:                case F5_320_512:
                   7923:                case F5_320_1024:
                   7924:                case F5_180_512:
                   7925:                case F5_160_512:
                   7926:                        REG8(BL) = 0x01; // 320K/360K disk
                   7927:                        break;
                   7928:                case F5_1Pt2_512:
                   7929:                case F3_1Pt2_512:
                   7930:                case F3_1Pt23_1024:
                   7931:                case F5_1Pt23_1024:
                   7932:                        REG8(BL) = 0x02; // 1.2M disk
                   7933:                        break;
                   7934:                case F3_720_512:
                   7935:                case F3_640_512:
                   7936:                case F5_640_512:
                   7937:                case F5_720_512:
                   7938:                        REG8(BL) = 0x03; // 720K disk
                   7939:                        break;
                   7940:                case F3_1Pt44_512:
                   7941:                        REG8(BL) = 0x04; // 1.44M disk
                   7942:                        break;
                   7943:                case F3_2Pt88_512:
                   7944:                        REG8(BL) = 0x06; // 2.88M disk
                   7945:                        break;
                   7946:                case RemovableMedia:
                   7947:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   7948:                        break;
                   7949:                default:
                   7950:                        REG8(BL) = 0x00; // unknown
                   7951:                        break;
                   7952:                }
                   7953:                if(REG8(DL) & 0x80) {
                   7954:                        switch(GetLogicalDrives() & 0x0c) {
                   7955:                        case 0x00: REG8(DL) = 0x00; break;
                   7956:                        case 0x04:
                   7957:                        case 0x08: REG8(DL) = 0x01; break;
                   7958:                        case 0x0c: REG8(DL) = 0x02; break;
                   7959:                        }
                   7960:                } else {
                   7961:                        switch(GetLogicalDrives() & 0x03) {
                   7962:                        case 0x00: REG8(DL) = 0x00; break;
                   7963:                        case 0x01:
                   7964:                        case 0x02: REG8(DL) = 0x01; break;
                   7965:                        case 0x03: REG8(DL) = 0x02; break;
                   7966:                        }
                   7967:                }
                   7968:                REG8(DH) = drive_param->head_num();
                   7969:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   7970:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   7971:                REG8(CH) = cyl & 0xff;
                   7972:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   7973:        } else {
                   7974:                REG8(AH) = 0x07;
                   7975:                m_CF = 1;
                   7976:        }
                   7977: }
                   7978: 
                   7979: inline void pcbios_int_13h_10h()
                   7980: {
                   7981:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7982:        
                   7983:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7984:                REG8(AH) = 0x00; // successful completion
                   7985:        } else {
                   7986:                if(REG8(DL) & 0x80) {
                   7987:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   7988:                } else {
                   7989:                        REG8(AH) = 0x80; // timeout (not ready)
                   7990:                }
                   7991:                m_CF = 1;
                   7992:        }
                   7993: }
                   7994: 
                   7995: inline void pcbios_int_13h_15h()
                   7996: {
                   7997:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7998:        
                   7999:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8000:                if(REG8(DL) & 0x80) {
                   8001:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   8002:                } else {
                   8003:                        REG8(AH) = 0x03; // hard disk
                   8004:                }
                   8005:        } else {
                   8006:                REG8(AH) = 0x00; // no such drive
                   8007:        }
                   8008: }
                   8009: 
1.1.1.43  root     8010: inline void pcbios_int_13h_41h()
                   8011: {
                   8012:        if(REG16(BX) == 0x55aa) {
                   8013:                // IBM/MS INT 13 Extensions is not installed
                   8014:                REG8(AH) = 0x01;
                   8015:                m_CF = 1;
                   8016:        } else {
                   8017:                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));
                   8018:                REG8(AH) = 0x01;
                   8019:                m_CF = 1;
                   8020:        }
                   8021: }
                   8022: 
1.1.1.25  root     8023: inline void pcbios_int_14h_00h()
                   8024: {
1.1.1.29  root     8025:        if(REG16(DX) < 4) {
1.1.1.25  root     8026:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   8027:                UINT8 selector = sio_read(REG16(DX), 3);
                   8028:                selector &= ~0x3f;
                   8029:                selector |= REG8(AL) & 0x1f;
                   8030:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   8031:                sio_write(REG16(DX), 3, selector | 0x80);
                   8032:                sio_write(REG16(DX), 0, divisor & 0xff);
                   8033:                sio_write(REG16(DX), 1, divisor >> 8);
                   8034:                sio_write(REG16(DX), 3, selector);
                   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_01h()
                   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:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8047:                sio_write(REG16(DX), 0, REG8(AL));
                   8048:                sio_write(REG16(DX), 3, selector);
                   8049:                REG8(AH) = sio_read(REG16(DX), 5);
                   8050:        } else {
                   8051:                REG8(AH) = 0x80;
                   8052:        }
                   8053: }
                   8054: 
                   8055: inline void pcbios_int_14h_02h()
                   8056: {
1.1.1.29  root     8057:        if(REG16(DX) < 4) {
1.1.1.25  root     8058:                UINT8 selector = sio_read(REG16(DX), 3);
                   8059:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8060:                REG8(AL) = sio_read(REG16(DX), 0);
                   8061:                sio_write(REG16(DX), 3, selector);
                   8062:                REG8(AH) = sio_read(REG16(DX), 5);
                   8063:        } else {
                   8064:                REG8(AH) = 0x80;
                   8065:        }
                   8066: }
                   8067: 
                   8068: inline void pcbios_int_14h_03h()
                   8069: {
1.1.1.29  root     8070:        if(REG16(DX) < 4) {
1.1.1.25  root     8071:                REG8(AH) = sio_read(REG16(DX), 5);
                   8072:                REG8(AL) = sio_read(REG16(DX), 6);
                   8073:        } else {
                   8074:                REG8(AH) = 0x80;
                   8075:        }
                   8076: }
                   8077: 
                   8078: inline void pcbios_int_14h_04h()
                   8079: {
1.1.1.29  root     8080:        if(REG16(DX) < 4) {
1.1.1.25  root     8081:                UINT8 selector = sio_read(REG16(DX), 3);
                   8082:                if(REG8(CH) <= 0x03) {
                   8083:                        selector = (selector & ~0x03) | REG8(CH);
                   8084:                }
                   8085:                if(REG8(BL) == 0x00) {
                   8086:                        selector &= ~0x04;
                   8087:                } else if(REG8(BL) == 0x01) {
                   8088:                        selector |= 0x04;
                   8089:                }
                   8090:                if(REG8(BH) == 0x00) {
                   8091:                        selector = (selector & ~0x38) | 0x00;
                   8092:                } else if(REG8(BH) == 0x01) {
                   8093:                        selector = (selector & ~0x38) | 0x08;
                   8094:                } else if(REG8(BH) == 0x02) {
                   8095:                        selector = (selector & ~0x38) | 0x18;
                   8096:                } else if(REG8(BH) == 0x03) {
                   8097:                        selector = (selector & ~0x38) | 0x28;
                   8098:                } else if(REG8(BH) == 0x04) {
                   8099:                        selector = (selector & ~0x38) | 0x38;
                   8100:                }
                   8101:                if(REG8(AL) == 0x00) {
                   8102:                        selector |= 0x40;
                   8103:                } else if(REG8(AL) == 0x01) {
                   8104:                        selector &= ~0x40;
                   8105:                }
                   8106:                if(REG8(CL) <= 0x0b) {
                   8107:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   8108:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   8109:                        sio_write(REG16(DX), 3, selector | 0x80);
                   8110:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   8111:                        sio_write(REG16(DX), 1, divisor >> 8);
                   8112:                }
                   8113:                sio_write(REG16(DX), 3, selector);
                   8114:                REG8(AH) = sio_read(REG16(DX), 5);
                   8115:                REG8(AL) = sio_read(REG16(DX), 6);
                   8116:        } else {
                   8117:                REG8(AH) = 0x80;
                   8118:        }
                   8119: }
                   8120: 
                   8121: inline void pcbios_int_14h_05h()
                   8122: {
1.1.1.29  root     8123:        if(REG16(DX) < 4) {
1.1.1.25  root     8124:                if(REG8(AL) == 0x00) {
                   8125:                        REG8(BL) = sio_read(REG16(DX), 4);
                   8126:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8127:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8128:                } else if(REG8(AL) == 0x01) {
                   8129:                        sio_write(REG16(DX), 4, REG8(BL));
                   8130:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8131:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8132:                } else {
                   8133:                        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));
                   8134:                }
                   8135:        } else {
                   8136:                REG8(AH) = 0x80;
                   8137:        }
                   8138: }
                   8139: 
1.1.1.14  root     8140: inline void pcbios_int_15h_10h()
                   8141: {
1.1.1.22  root     8142:        switch(REG8(AL)) {
                   8143:        case 0x00:
1.1.1.14  root     8144:                Sleep(10);
1.1.1.35  root     8145:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     8146:                break;
                   8147:        default:
                   8148:                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     8149:                REG8(AH) = 0x86;
                   8150:                m_CF = 1;
                   8151:        }
                   8152: }
                   8153: 
1.1       root     8154: inline void pcbios_int_15h_23h()
                   8155: {
                   8156:        switch(REG8(AL)) {
1.1.1.22  root     8157:        case 0x00:
1.1.1.8   root     8158:                REG8(CL) = cmos_read(0x2d);
                   8159:                REG8(CH) = cmos_read(0x2e);
1.1       root     8160:                break;
1.1.1.22  root     8161:        case 0x01:
1.1.1.8   root     8162:                cmos_write(0x2d, REG8(CL));
                   8163:                cmos_write(0x2e, REG8(CH));
1.1       root     8164:                break;
                   8165:        default:
1.1.1.22  root     8166:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     8167:                REG8(AH) = 0x86;
1.1.1.3   root     8168:                m_CF = 1;
1.1       root     8169:                break;
                   8170:        }
                   8171: }
                   8172: 
                   8173: inline void pcbios_int_15h_24h()
                   8174: {
                   8175:        switch(REG8(AL)) {
1.1.1.22  root     8176:        case 0x00:
1.1.1.3   root     8177:                i386_set_a20_line(0);
1.1       root     8178:                REG8(AH) = 0;
                   8179:                break;
1.1.1.22  root     8180:        case 0x01:
1.1.1.3   root     8181:                i386_set_a20_line(1);
1.1       root     8182:                REG8(AH) = 0;
                   8183:                break;
1.1.1.22  root     8184:        case 0x02:
1.1       root     8185:                REG8(AH) = 0;
1.1.1.3   root     8186:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     8187:                REG16(CX) = 0;
                   8188:                break;
1.1.1.22  root     8189:        case 0x03:
1.1       root     8190:                REG16(AX) = 0;
                   8191:                REG16(BX) = 0;
                   8192:                break;
1.1.1.22  root     8193:        default:
                   8194:                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));
                   8195:                REG8(AH) = 0x86;
                   8196:                m_CF = 1;
                   8197:                break;
1.1       root     8198:        }
                   8199: }
                   8200: 
                   8201: inline void pcbios_int_15h_49h()
                   8202: {
1.1.1.27  root     8203:        REG8(AH) = 0x00;
                   8204:        REG8(BL) = 0x00; // DOS/V
1.1       root     8205: }
                   8206: 
1.1.1.22  root     8207: inline void pcbios_int_15h_50h()
                   8208: {
                   8209:        switch(REG8(AL)) {
                   8210:        case 0x00:
                   8211:        case 0x01:
                   8212:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8213:                        REG8(AH) = 0x01; // invalid font type in bh
                   8214:                        m_CF = 1;
1.1.1.27  root     8215:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8216:                        REG8(AH) = 0x02; // bl not zero
                   8217:                        m_CF = 1;
                   8218:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8219:                        REG8(AH) = 0x04; // invalid code page
                   8220:                        m_CF = 1;
1.1.1.27  root     8221:                } else if(REG8(AL) == 0x01) {
                   8222:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8223:                        m_CF = 1;
1.1.1.27  root     8224:                } else {
1.1.1.49  root     8225:                        // dummy font read routine is at fffc:000d
                   8226:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8227:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8228:                        REG16(BX) = 0x000d;
1.1.1.27  root     8229:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8230:                }
                   8231:                break;
                   8232:        default:
                   8233:                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));
                   8234:                REG8(AH) = 0x86;
                   8235:                m_CF = 1;
                   8236:                break;
                   8237:        }
                   8238: }
                   8239: 
1.1.1.30  root     8240: inline void pcbios_int_15h_53h()
                   8241: {
                   8242:        switch(REG8(AL)) {
                   8243:        case 0x00:
                   8244:                // APM is not installed
                   8245:                REG8(AH) = 0x86;
                   8246:                m_CF = 1;
                   8247:                break;
                   8248:        default:
                   8249:                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));
                   8250:                REG8(AH) = 0x86;
                   8251:                m_CF = 1;
                   8252:                break;
                   8253:        }
                   8254: }
                   8255: 
1.1.1.43  root     8256: inline void pcbios_int_15h_84h()
                   8257: {
                   8258:        // joystick support (from DOSBox)
                   8259:        switch(REG16(DX)) {
                   8260:        case 0x00:
                   8261:                REG16(AX) = 0x00f0;
                   8262:                REG16(DX) = 0x0201;
                   8263:                m_CF = 1;
                   8264:                break;
                   8265:        case 0x01:
                   8266:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8267:                m_CF = 1;
                   8268:                break;
                   8269:        default:
                   8270:                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));
                   8271:                REG8(AH) = 0x86;
                   8272:                m_CF = 1;
                   8273:                break;
                   8274:        }
                   8275: }
1.1.1.35  root     8276: 
                   8277: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8278: {
                   8279:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8280:        UINT32 msec = usec / 1000;
                   8281:        
1.1.1.54  root     8282:        while(msec && !m_exit) {
1.1.1.14  root     8283:                UINT32 tmp = min(msec, 100);
                   8284:                if(msec - tmp < 10) {
                   8285:                        tmp = msec;
                   8286:                }
                   8287:                Sleep(tmp);
                   8288:                msec -= tmp;
                   8289:        }
1.1.1.35  root     8290:        
                   8291: #ifdef USE_SERVICE_THREAD
                   8292:        service_exit = true;
                   8293: #endif
                   8294:        return(0);
                   8295: }
                   8296: 
                   8297: inline void pcbios_int_15h_86h()
                   8298: {
                   8299:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8300: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8301:                if(!in_service && !in_service_29h) {
                   8302:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8303:                } else {
                   8304: #endif
                   8305:                        pcbios_int_15h_86h_thread(NULL);
                   8306:                        REQUEST_HARDWRE_UPDATE();
                   8307: #ifdef USE_SERVICE_THREAD
                   8308:                }
1.1.1.35  root     8309: #endif
                   8310:        }
1.1       root     8311: }
                   8312: 
                   8313: inline void pcbios_int_15h_87h()
                   8314: {
                   8315:        // copy extended memory (from DOSBox)
                   8316:        int len = REG16(CX) * 2;
1.1.1.3   root     8317:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8318:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8319:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8320:        memcpy(mem + dst, mem + src, len);
                   8321:        REG16(AX) = 0x00;
                   8322: }
                   8323: 
                   8324: inline void pcbios_int_15h_88h()
                   8325: {
1.1.1.17  root     8326:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8327: }
                   8328: 
                   8329: inline void pcbios_int_15h_89h()
                   8330: {
1.1.1.21  root     8331: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8332:        // switch to protected mode (from DOSBox)
                   8333:        write_io_byte(0x20, 0x10);
                   8334:        write_io_byte(0x21, REG8(BH));
                   8335:        write_io_byte(0x21, 0x00);
                   8336:        write_io_byte(0xa0, 0x10);
                   8337:        write_io_byte(0xa1, REG8(BL));
                   8338:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8339:        i386_set_a20_line(1);
                   8340:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8341:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8342:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8343:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8344:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8345: #if defined(HAS_I386)
                   8346:        m_cr[0] |= 1;
                   8347: #else
                   8348:        m_msw |= 1;
                   8349: #endif
                   8350:        SREG(DS) = 0x18;
                   8351:        SREG(ES) = 0x20;
                   8352:        SREG(SS) = 0x28;
                   8353:        i386_load_segment_descriptor(DS);
                   8354:        i386_load_segment_descriptor(ES);
                   8355:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8356:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8357:        REG16(SP) += 6;
1.1.1.3   root     8358: #if defined(HAS_I386)
1.1.1.21  root     8359:        UINT32 flags = get_flags();
                   8360:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8361:        set_flags(flags);
1.1.1.3   root     8362: #else
1.1.1.21  root     8363:        UINT32 flags = CompressFlags();
                   8364:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8365:        ExpandFlags(flags);
1.1.1.3   root     8366: #endif
1.1       root     8367:        REG16(AX) = 0x00;
1.1.1.21  root     8368:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8369: #else
1.1.1.21  root     8370:        // i86/i186/v30: protected mode is not supported
1.1       root     8371:        REG8(AH) = 0x86;
1.1.1.3   root     8372:        m_CF = 1;
1.1       root     8373: #endif
                   8374: }
                   8375: 
1.1.1.21  root     8376: inline void pcbios_int_15h_8ah()
                   8377: {
                   8378:        UINT32 size = MAX_MEM - 0x100000;
                   8379:        REG16(AX) = size & 0xffff;
                   8380:        REG16(DX) = size >> 16;
                   8381: }
                   8382: 
1.1.1.54  root     8383: #ifdef EXT_BIOS_TOP
                   8384: inline void pcbios_int_15h_c1h()
                   8385: {
                   8386:        SREG(ES) = EXT_BIOS_TOP >> 4;
                   8387:        i386_load_segment_descriptor(ES);
                   8388: }
                   8389: #endif
                   8390: 
                   8391: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
                   8392: {
                   8393:        // from DOSBox DoPS2Callback()
                   8394:        UINT16 mdat = 0x08;
                   8395:        INT16 xdiff = mouse.position.x - mouse.prev_position.x;
                   8396:        INT16 ydiff = mouse.prev_position.y - mouse.position.y;
                   8397:        
                   8398:        if(mouse.buttons[0].status) {
                   8399:                mdat |= 0x01;
                   8400:        }
                   8401:        if(mouse.buttons[1].status) {
                   8402:                mdat |= 0x02;
                   8403:        }
                   8404:        mouse.prev_position.x = mouse.position.x;
                   8405:        mouse.prev_position.y = mouse.position.y;
                   8406:        if((xdiff > 0xff) || (xdiff < -0xff)) {
                   8407:                mdat |= 0x40;   // x overflow
                   8408:        }
                   8409:        if((ydiff > 0xff) || (ydiff < -0xff)) {
                   8410:                mdat |= 0x80;   // y overflow
                   8411:        }
                   8412:        xdiff %= 256;
                   8413:        ydiff %= 256;
                   8414:        if(xdiff < 0) {
                   8415:                xdiff = (0x100 + xdiff);
                   8416:                mdat |= 0x10;
                   8417:        }
                   8418:        if(ydiff < 0) {
                   8419:                ydiff = (0x100 + ydiff);
                   8420:                mdat |= 0x20;
                   8421:        }
                   8422:        *data_1st = (UINT16)mdat;
                   8423:        *data_2nd = (UINT16)(xdiff % 256);
                   8424:        *data_3rd = (UINT16)(ydiff % 256);
                   8425: }
                   8426: 
                   8427: inline void pcbios_int_15h_c2h()
                   8428: {
                   8429:        static UINT8 enabled = 0;
                   8430:        static UINT8 sampling_rate = 5;
                   8431:        static UINT8 resolution = 2;
                   8432:        static UINT8 scaling = 1;
                   8433:        
                   8434:        switch(REG8(AL)) {
                   8435:        case 0x00:
                   8436:                if(REG8(BH) == 0x00 || REG8(BH) == 0x01) {
                   8437:                        enabled = REG8(BH);
                   8438:                        REG8(AH) = 0x00; // successful
                   8439:                } else {
                   8440:                        REG8(AH) = 0x01; // invalid function
                   8441:                        m_CF = 1;
                   8442:                }
                   8443:                break;
                   8444:        case 0x01:
                   8445:                REG8(BH) = 0x00; // device id
                   8446:                REG8(BL) = 0xaa; // mouse
                   8447:        case 0x05:
                   8448:                enabled = 0;
                   8449:                sampling_rate = 5;
                   8450:                resolution = 2;
                   8451:                scaling = 1;
                   8452:                REG8(AH) = 0x00; // successful
                   8453:                break;
                   8454:        case 0x02:
                   8455:                sampling_rate = REG8(BH);
                   8456:                REG8(AH) = 0x00; // successful
                   8457:                break;
                   8458:        case 0x03:
                   8459:                resolution = REG8(BH);
                   8460:                REG8(AH) = 0x00; // successful
                   8461:                break;
                   8462:        case 0x04:
                   8463:                REG8(BH) = 0x00; // device id
                   8464:                REG8(AH) = 0x00; // successful
                   8465:                break;
                   8466:        case 0x06:
                   8467:                switch(REG8(BH)) {
                   8468:                case 0x00:
                   8469:                        REG8(BL) = 0x00;
                   8470:                        if(mouse.buttons[1].status) {
                   8471:                                REG8(BL) |= 0x01;
                   8472:                        }
                   8473:                        if(mouse.buttons[0].status) {
                   8474:                                REG8(BL) |= 0x04;
                   8475:                        }
                   8476:                        if(scaling == 2) {
                   8477:                                REG8(BL) |= 0x10;
                   8478:                        }
                   8479:                        REG8(CL) = resolution;
                   8480:                        switch(sampling_rate) {
                   8481:                        case 0:  REG8(DL) =  10; break;
                   8482:                        case 1:  REG8(DL) =  20; break;
                   8483:                        case 2:  REG8(DL) =  40; break;
                   8484:                        case 3:  REG8(DL) =  60; break;
                   8485:                        case 4:  REG8(DL) =  80; break;
                   8486: //                     case 5:  REG8(DL) = 100; break;
                   8487:                        case 6:  REG8(DL) = 200; break;
                   8488:                        default: REG8(DL) = 100; break;
                   8489:                        }
                   8490:                        REG8(AH) = 0x00; // successful
                   8491:                        break;
                   8492:                case 0x01:
                   8493:                case 0x02:
                   8494:                        scaling = REG8(BH);
                   8495:                        REG8(AH) = 0x00; // successful
                   8496:                        break;
                   8497:                default:
                   8498:                        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));
                   8499:                        REG8(AH) = 0x01; // invalid function
                   8500:                        m_CF = 1;
                   8501:                        break;
                   8502:                }
                   8503:                break;
                   8504:        case 0x07: // set device handler addr
                   8505:                mouse.call_addr_ps2.w.l = REG16(BX);
                   8506:                mouse.call_addr_ps2.w.h = SREG(ES);
                   8507:                REG8(AH) = 0x00; // successful
                   8508:                break;
                   8509:        case 0x08:
                   8510:                REG8(AH) = 0x00; // successful
                   8511:                break;
                   8512:        case 0x09:
                   8513:                {
                   8514:                        UINT16 data_1st, data_2nd, data_3rd;
                   8515:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   8516:                        REG8(BL) = (UINT8)(data_1st & 0xff);
                   8517:                        REG8(CL) = (UINT8)(data_2nd & 0xff);
                   8518:                        REG8(DL) = (UINT8)(data_3rd & 0xff);
                   8519:                }
                   8520:                REG8(AH) = 0x00; // successful
                   8521:                break;
                   8522:        default:
                   8523:                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));
                   8524: //             REG8(AH) = 0x86;
                   8525:                REG8(AH) = 0x01; // invalid function
                   8526:                m_CF = 1;
                   8527:                break;
                   8528:        }
                   8529: }
                   8530: 
1.1.1.3   root     8531: #if defined(HAS_I386)
1.1       root     8532: inline void pcbios_int_15h_c9h()
                   8533: {
                   8534:        REG8(AH) = 0x00;
                   8535:        REG8(CH) = cpu_type;
                   8536:        REG8(CL) = cpu_step;
                   8537: }
1.1.1.3   root     8538: #endif
1.1       root     8539: 
                   8540: inline void pcbios_int_15h_cah()
                   8541: {
                   8542:        switch(REG8(AL)) {
1.1.1.22  root     8543:        case 0x00:
1.1       root     8544:                if(REG8(BL) > 0x3f) {
                   8545:                        REG8(AH) = 0x03;
1.1.1.3   root     8546:                        m_CF = 1;
1.1       root     8547:                } else if(REG8(BL) < 0x0e) {
                   8548:                        REG8(AH) = 0x04;
1.1.1.3   root     8549:                        m_CF = 1;
1.1       root     8550:                } else {
1.1.1.8   root     8551:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8552:                }
                   8553:                break;
1.1.1.22  root     8554:        case 0x01:
1.1       root     8555:                if(REG8(BL) > 0x3f) {
                   8556:                        REG8(AH) = 0x03;
1.1.1.3   root     8557:                        m_CF = 1;
1.1       root     8558:                } else if(REG8(BL) < 0x0e) {
                   8559:                        REG8(AH) = 0x04;
1.1.1.3   root     8560:                        m_CF = 1;
1.1       root     8561:                } else {
1.1.1.8   root     8562:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8563:                }
                   8564:                break;
                   8565:        default:
1.1.1.22  root     8566:                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     8567:                REG8(AH) = 0x86;
1.1.1.3   root     8568:                m_CF = 1;
1.1       root     8569:                break;
                   8570:        }
                   8571: }
                   8572: 
1.1.1.22  root     8573: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8574: {
1.1.1.22  root     8575:        switch(REG8(AL)) {
                   8576: #if defined(HAS_I386)
                   8577:        case 0x01:
                   8578:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8579:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8580:                break;
1.1.1.17  root     8581: #endif
1.1.1.22  root     8582:        default:
                   8583:                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));
                   8584:                REG8(AH) = 0x86;
                   8585:                m_CF = 1;
                   8586:                break;
                   8587:        }
                   8588: }
1.1.1.17  root     8589: 
1.1.1.55  root     8590: bool pcbios_is_key_buffer_empty()
                   8591: {
                   8592:        return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
                   8593: }
                   8594: 
1.1.1.51  root     8595: void pcbios_clear_key_buffer()
                   8596: {
                   8597:        key_buf_char->clear();
                   8598:        key_buf_scan->clear();
                   8599:        
                   8600:        // update key buffer
                   8601:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8602: }
                   8603: 
                   8604: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8605: {
                   8606:        // update key buffer
                   8607:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8608:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8609:        UINT16 next = tail + 2;
                   8610:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8611:                next = *(UINT16 *)(mem + 0x480);
                   8612:        }
                   8613:        if(next != head) {
                   8614:                *(UINT16 *)(mem + 0x41c) = next;
                   8615:                mem[0x400 + (tail++)] = key_char;
                   8616:                mem[0x400 + (tail++)] = key_scan;
1.1.1.55  root     8617:        } else {
                   8618:                // store to extra key buffer
                   8619:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8620:                        key_buf_char->write(key_char);
                   8621:                        key_buf_scan->write(key_scan);
                   8622:                }
1.1.1.51  root     8623:        }
                   8624: }
                   8625: 
                   8626: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8627: {
                   8628:        // update key buffer
                   8629:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8630:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8631:        UINT16 next = head + 2;
                   8632:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8633:                next = *(UINT16 *)(mem + 0x480);
                   8634:        }
                   8635:        if(head != tail) {
                   8636:                *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55  root     8637:                *key_char = mem[0x400 + (head++)];
                   8638:                *key_scan = mem[0x400 + (head++)];
                   8639:                
                   8640:                // restore from extra key buffer
                   8641:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8642:                        if(!key_buf_char->empty()) {
                   8643:                                pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
                   8644:                        }
                   8645:                }
                   8646:                return(true);
                   8647:        } else {
                   8648:                *key_char = 0x00;
                   8649:                *key_scan = 0x00;
                   8650:                return(false);
1.1.1.51  root     8651:        }
                   8652: }
                   8653: 
1.1.1.33  root     8654: void pcbios_update_key_code(bool wait)
1.1       root     8655: {
1.1.1.32  root     8656:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8657: #ifdef USE_SERVICE_THREAD
                   8658:                EnterCriticalSection(&key_buf_crit_sect);
                   8659: #endif
1.1.1.55  root     8660:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     8661: #ifdef USE_SERVICE_THREAD
                   8662:                LeaveCriticalSection(&key_buf_crit_sect);
                   8663: #endif
                   8664:                if(empty) {
1.1.1.32  root     8665:                        if(!update_key_buffer()) {
1.1.1.33  root     8666:                                if(wait) {
1.1.1.32  root     8667:                                        Sleep(10);
                   8668:                                } else {
                   8669:                                        maybe_idle();
                   8670:                                }
1.1.1.14  root     8671:                        }
                   8672:                }
1.1.1.34  root     8673:        }
                   8674:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8675: #ifdef USE_SERVICE_THREAD
                   8676:                EnterCriticalSection(&key_buf_crit_sect);
                   8677: #endif
1.1.1.51  root     8678:                int key_char, key_scan;
                   8679:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8680:                        key_code  = key_char << 0;
                   8681:                        key_code |= key_scan << 8;
1.1.1.35  root     8682:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8683:                }
                   8684:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8685:                        key_code |= key_char << 16;
                   8686:                        key_code |= key_scan << 24;
1.1.1.33  root     8687:                        key_recv |= 0xffff0000;
1.1.1.32  root     8688:                }
1.1.1.35  root     8689: #ifdef USE_SERVICE_THREAD
                   8690:                LeaveCriticalSection(&key_buf_crit_sect);
                   8691: #endif
1.1       root     8692:        }
                   8693: }
                   8694: 
1.1.1.35  root     8695: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8696: {
1.1.1.54  root     8697:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     8698:                pcbios_update_key_code(true);
1.1       root     8699:        }
1.1.1.33  root     8700:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8701:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8702:                        if(REG8(AH) == 0x10) {
                   8703:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8704:                        } else {
                   8705:                                key_code = ((key_code >> 16) & 0xff00);
                   8706:                        }
                   8707:                        key_recv >>= 16;
1.1       root     8708:                }
                   8709:        }
                   8710:        REG16(AX) = key_code & 0xffff;
                   8711:        key_code >>= 16;
1.1.1.33  root     8712:        key_recv >>= 16;
1.1.1.35  root     8713:        
                   8714: #ifdef USE_SERVICE_THREAD
                   8715:        service_exit = true;
                   8716: #endif
                   8717:        return(0);
                   8718: }
                   8719: 
                   8720: inline void pcbios_int_16h_00h()
                   8721: {
                   8722: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8723:        if(!in_service && !in_service_29h) {
                   8724:                start_service_loop(pcbios_int_16h_00h_thread);
                   8725:        } else {
                   8726: #endif
                   8727:                pcbios_int_16h_00h_thread(NULL);
                   8728:                REQUEST_HARDWRE_UPDATE();
                   8729: #ifdef USE_SERVICE_THREAD
                   8730:        }
1.1.1.35  root     8731: #endif
1.1       root     8732: }
                   8733: 
                   8734: inline void pcbios_int_16h_01h()
                   8735: {
1.1.1.33  root     8736:        if(key_recv == 0) {
                   8737:                pcbios_update_key_code(false);
1.1.1.5   root     8738:        }
1.1.1.33  root     8739:        if(key_recv != 0) {
                   8740:                UINT32 key_code_tmp = key_code;
                   8741:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8742:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8743:                                if(REG8(AH) == 0x11) {
                   8744:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8745:                                } else {
                   8746:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8747:                                }
                   8748:                        }
1.1       root     8749:                }
1.1.1.5   root     8750:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8751: #if defined(HAS_I386)
1.1.1.33  root     8752:                m_ZF = 0;
                   8753: #else
                   8754:                m_ZeroVal = 1;
                   8755: #endif
                   8756:        } else {
                   8757: #if defined(HAS_I386)
                   8758:                m_ZF = 1;
1.1.1.3   root     8759: #else
1.1.1.33  root     8760:                m_ZeroVal = 0;
1.1.1.3   root     8761: #endif
1.1.1.33  root     8762:        }
1.1       root     8763: }
                   8764: 
                   8765: inline void pcbios_int_16h_02h()
                   8766: {
                   8767:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8768:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8769:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8770:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8771:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8772:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8773:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8774:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8775: }
                   8776: 
                   8777: inline void pcbios_int_16h_03h()
                   8778: {
                   8779:        static UINT16 status = 0;
                   8780:        
                   8781:        switch(REG8(AL)) {
                   8782:        case 0x05:
                   8783:                status = REG16(BX);
                   8784:                break;
                   8785:        case 0x06:
                   8786:                REG16(BX) = status;
                   8787:                break;
                   8788:        default:
1.1.1.3   root     8789:                m_CF = 1;
1.1       root     8790:                break;
                   8791:        }
                   8792: }
                   8793: 
                   8794: inline void pcbios_int_16h_05h()
                   8795: {
1.1.1.32  root     8796:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8797: #ifdef USE_SERVICE_THREAD
                   8798:                EnterCriticalSection(&key_buf_crit_sect);
                   8799: #endif
1.1.1.51  root     8800:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     8801: #ifdef USE_SERVICE_THREAD
                   8802:                LeaveCriticalSection(&key_buf_crit_sect);
                   8803: #endif
1.1.1.32  root     8804:        }
1.1       root     8805:        REG8(AL) = 0x00;
                   8806: }
                   8807: 
                   8808: inline void pcbios_int_16h_12h()
                   8809: {
                   8810:        pcbios_int_16h_02h();
                   8811:        
                   8812:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8813:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8814:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8815:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8816:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8817:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8818:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8819:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8820: }
                   8821: 
                   8822: inline void pcbios_int_16h_13h()
                   8823: {
                   8824:        static UINT16 status = 0;
                   8825:        
                   8826:        switch(REG8(AL)) {
                   8827:        case 0x00:
                   8828:                status = REG16(DX);
                   8829:                break;
                   8830:        case 0x01:
                   8831:                REG16(DX) = status;
                   8832:                break;
                   8833:        default:
1.1.1.22  root     8834:                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     8835:                m_CF = 1;
1.1       root     8836:                break;
                   8837:        }
                   8838: }
                   8839: 
                   8840: inline void pcbios_int_16h_14h()
                   8841: {
                   8842:        static UINT8 status = 0;
                   8843:        
                   8844:        switch(REG8(AL)) {
                   8845:        case 0x00:
                   8846:        case 0x01:
                   8847:                status = REG8(AL);
                   8848:                break;
                   8849:        case 0x02:
                   8850:                REG8(AL) = status;
                   8851:                break;
                   8852:        default:
1.1.1.22  root     8853:                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     8854:                m_CF = 1;
1.1       root     8855:                break;
                   8856:        }
                   8857: }
                   8858: 
1.1.1.24  root     8859: inline void pcbios_int_16h_55h()
                   8860: {
                   8861:        switch(REG8(AL)) {
                   8862:        case 0x00:
                   8863:                // keyboard tsr is not present
                   8864:                break;
                   8865:        case 0xfe:
                   8866:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8867:                break;
                   8868:        case 0xff:
                   8869:                break;
                   8870:        default:
                   8871:                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));
                   8872:                m_CF = 1;
                   8873:                break;
                   8874:        }
                   8875: }
                   8876: 
1.1.1.30  root     8877: inline void pcbios_int_16h_6fh()
                   8878: {
                   8879:        switch(REG8(AL)) {
                   8880:        case 0x00:
                   8881:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8882:                break;
                   8883:        default:
                   8884:                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));
                   8885:                m_CF = 1;
                   8886:                break;
                   8887:        }
                   8888: }
                   8889: 
1.1.1.37  root     8890: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8891: {
                   8892:        UINT8 hi = jis >> 8;
                   8893:        UINT8 lo = jis & 0xff;
                   8894:        
                   8895:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8896:        hi = (hi - 0x21) / 2 + 0x81;
                   8897:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8898:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8899:        
                   8900:        return((hi << 8) + lo);
                   8901: }
                   8902: 
                   8903: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8904: {
                   8905:        UINT8 hi = sjis >> 8;
                   8906:        UINT8 lo = sjis & 0xff;
                   8907:        
                   8908:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8909:                return(0x2121);
                   8910:        }
                   8911:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8912:                return(0x2121);
                   8913:        }
                   8914:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8915:                // gaiji
                   8916:                if(lo >= 0x40 && lo <= 0x7e) {
                   8917:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   8918:                }
                   8919:                if(lo >= 0x80 && lo <= 0x9e) {
                   8920:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   8921:                }
                   8922:                if(lo >= 0x9f && lo <= 0xfc) {
                   8923:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   8924:                }
                   8925:        }
                   8926:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   8927:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   8928:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   8929:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   8930:        
                   8931:        return((hi << 8) + lo);
                   8932: }
                   8933: 
1.1.1.38  root     8934: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   8935: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   8936: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     8937: 
                   8938: void pcbios_printer_out(int c, UINT8 data)
                   8939: {
                   8940:        if(pio[c].conv_mode) {
                   8941:                if(pio[c].sjis_hi != 0) {
                   8942:                        if(!pio[c].jis_mode) {
                   8943:                                printer_out(c, 0x1c);
                   8944:                                printer_out(c, 0x26);
                   8945:                                pio[c].jis_mode = true;
                   8946:                        }
                   8947:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   8948:                        printer_out(c, jis >> 8);
                   8949:                        printer_out(c, jis & 0xff);
                   8950:                        pio[c].sjis_hi = 0;
                   8951:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   8952:                        printer_out(c, data);
                   8953:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8954:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8955:                        }
                   8956:                        pio[c].esc_len++;
                   8957:                        
                   8958:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8959:                        case 0x33: // 1Bh 33h XX
                   8960:                        case 0x4a: // 1Bh 4Ah XX
                   8961:                        case 0x4e: // 1Bh 4Eh XX
                   8962:                        case 0x51: // 1Bh 51h XX
                   8963:                        case 0x55: // 1Bh 55h XX
                   8964:                        case 0x6c: // 1Bh 6Ch XX
                   8965:                        case 0x71: // 1Bh 71h XX
                   8966:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     8967:                                if(pio[c].esc_len == 3) {
                   8968:                                        pio[c].esc_buf[0] = 0x00;
                   8969:                                }
                   8970:                                break;
1.1.1.38  root     8971:                        case 0x24: // 1Bh 24h XX XX
                   8972:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     8973:                                if(pio[c].esc_len == 4) {
                   8974:                                        pio[c].esc_buf[0] = 0x00;
                   8975:                                }
                   8976:                                break;
1.1.1.38  root     8977:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     8978:                                if(pio[c].esc_len >= 3) {
                   8979:                                        switch(pio[c].esc_buf[2]) {
                   8980:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   8981:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   8982:                                                        pio[c].esc_buf[0] = 0x00;
                   8983:                                                }
                   8984:                                                break;
                   8985:                                        case 32: case 33: case 38: case 39: case 40:
                   8986:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   8987:                                                        pio[c].esc_buf[0] = 0x00;
                   8988:                                                }
                   8989:                                                break;
1.1.1.38  root     8990:                                        case 71: case 72: case 73:
                   8991:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   8992:                                                        pio[c].esc_buf[0] = 0x00;
                   8993:                                                }
                   8994:                                                break;
1.1.1.37  root     8995:                                        default:
                   8996:                                                pio[c].esc_buf[0] = 0x00;
                   8997:                                                break;
                   8998:                                        }
                   8999:                                }
                   9000:                                break;
1.1.1.38  root     9001:                        case 0x40: // 1Bh 40h
1.1.1.37  root     9002:                                if(pio[c].jis_mode) {
                   9003:                                        printer_out(c, 0x1c);
                   9004:                                        printer_out(c, 0x2e);
                   9005:                                        pio[c].jis_mode = false;
                   9006:                                }
                   9007:                                pio[c].esc_buf[0] = 0x00;
                   9008:                                break;
1.1.1.38  root     9009:                        case 0x42: // 1Bh 42h data 00h
                   9010:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9011:                                if(pio[c].esc_len >= 3 && data == 0) {
                   9012:                                        pio[c].esc_buf[0] = 0x00;
                   9013:                                }
                   9014:                                break;
1.1.1.38  root     9015:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     9016:                                if(pio[c].esc_len >= 3 && data != 0) {
                   9017:                                        pio[c].esc_buf[0] = 0x00;
                   9018:                                }
                   9019:                                break;
1.1.1.38  root     9020:                        default: // 1Bh XX
1.1.1.37  root     9021:                                pio[c].esc_buf[0] = 0x00;
                   9022:                                break;
                   9023:                        }
                   9024:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   9025:                        printer_out(c, data);
                   9026:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9027:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9028:                        }
                   9029:                        pio[c].esc_len++;
                   9030:                        
                   9031:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9032:                        case 0x21: // 1Ch 21h XX
                   9033:                        case 0x2d: // 1Ch 2Dh XX
                   9034:                        case 0x57: // 1Ch 57h XX
                   9035:                        case 0x6b: // 1Ch 6Bh XX
                   9036:                        case 0x72: // 1Ch 72h XX
                   9037:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     9038:                                if(pio[c].esc_len == 3) {
                   9039:                                        pio[c].esc_buf[0] = 0x00;
                   9040:                                }
                   9041:                                break;
1.1.1.38  root     9042:                        case 0x26: // 1Ch 26h
1.1.1.37  root     9043:                                pio[c].jis_mode = true;
                   9044:                                pio[c].esc_buf[0] = 0x00;
                   9045:                                break;
1.1.1.38  root     9046:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     9047:                                pio[c].jis_mode = false;
                   9048:                                pio[c].esc_buf[0] = 0x00;
                   9049:                                break;
1.1.1.38  root     9050:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     9051:                                if(pio[c].esc_len == 76) {
                   9052:                                        pio[c].esc_buf[0] = 0x00;
                   9053:                                }
                   9054:                                break;
1.1.1.38  root     9055:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9056:                                if(pio[c].esc_len == 6) {
                   9057:                                        pio[c].esc_buf[0] = 0x00;
                   9058:                                }
                   9059:                                break;
1.1.1.38  root     9060:                        case 0x53: // 1Ch 53h XX XX
                   9061:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     9062:                                if(pio[c].esc_len == 4) {
                   9063:                                        pio[c].esc_buf[0] = 0x00;
                   9064:                                }
                   9065:                                break;
1.1.1.38  root     9066:                        default: // 1Ch XX
1.1.1.37  root     9067:                                pio[c].esc_buf[0] = 0x00;
                   9068:                                break;
                   9069:                        }
                   9070:                } else if(data == 0x1b || data == 0x1c) {
                   9071:                        printer_out(c, data);
                   9072:                        pio[c].esc_buf[0] = data;
                   9073:                        pio[c].esc_len = 1;
                   9074:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   9075:                        pio[c].sjis_hi = data;
                   9076:                } else {
                   9077:                        if(pio[c].jis_mode) {
                   9078:                                printer_out(c, 0x1c);
                   9079:                                printer_out(c, 0x2e);
                   9080:                                pio[c].jis_mode = false;
                   9081:                        }
                   9082:                        printer_out(c, data);
                   9083:                }
                   9084:        } else {
                   9085:                if(pio[c].jis_mode) {
                   9086:                        printer_out(c, 0x1c);
                   9087:                        printer_out(c, 0x2e);
                   9088:                        pio[c].jis_mode = false;
                   9089:                }
                   9090:                printer_out(c, data);
                   9091:        }
                   9092: }
                   9093: 
                   9094: inline void pcbios_int_17h_00h()
                   9095: {
                   9096:        if(REG16(DX) < 3) {
                   9097:                pcbios_printer_out(REG16(DX), REG8(AL));
                   9098:                REG8(AH) = 0xd0;
                   9099:        }
                   9100: }
                   9101: 
                   9102: inline void pcbios_int_17h_01h()
                   9103: {
                   9104:        if(REG16(DX) < 3) {
                   9105:                REG8(AH) = 0xd0;
                   9106:        }
                   9107: }
                   9108: 
                   9109: inline void pcbios_int_17h_02h()
                   9110: {
                   9111:        if(REG16(DX) < 3) {
                   9112:                REG8(AH) = 0xd0;
                   9113:        }
                   9114: }
                   9115: 
                   9116: inline void pcbios_int_17h_03h()
                   9117: {
                   9118:        switch(REG8(AL)) {
                   9119:        case 0x00:
                   9120:                if(REG16(DX) < 3) {
                   9121:                        if(pio[REG16(DX)].jis_mode) {
                   9122:                                printer_out(REG16(DX), 0x1c);
                   9123:                                printer_out(REG16(DX), 0x2e);
                   9124:                                pio[REG16(DX)].jis_mode = false;
                   9125:                        }
                   9126:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   9127:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   9128:                        }
                   9129:                        REG16(CX) = 0x0000;
                   9130:                        REG8(AH) = 0xd0;
                   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_50h()
                   9140: {
                   9141:        switch(REG8(AL)) {
                   9142:        case 0x00:
                   9143:                if(REG16(DX) < 3) {
                   9144:                        if(REG16(BX) = 0x0001) {
                   9145:                                pio[REG16(DX)].conv_mode = false;
                   9146:                                REG8(AL) = 0x00;
                   9147:                        } else if(REG16(BX) = 0x0051) {
                   9148:                                pio[REG16(DX)].conv_mode = true;
                   9149:                                REG8(AL) = 0x00;
                   9150:                        } else {
                   9151:                                REG8(AL) = 0x01;
                   9152:                        }
                   9153:                } else {
                   9154:                        REG8(AL) = 0x02;
                   9155:                }
                   9156:                break;
                   9157:        case 0x01:
                   9158:                if(REG16(DX) < 3) {
                   9159:                        if(pio[REG16(DX)].conv_mode) {
                   9160:                                REG16(BX) = 0x0051;
                   9161:                        } else {
                   9162:                                REG16(BX) = 0x0001;
                   9163:                        }
                   9164:                        REG8(AL) = 0x00;
                   9165:                } else {
                   9166:                        REG8(AL) = 0x02;
                   9167:                }
                   9168:                break;
                   9169:        default:
                   9170:                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));
                   9171:                break;
                   9172:        }
                   9173: }
                   9174: 
                   9175: inline void pcbios_int_17h_51h()
                   9176: {
                   9177:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9178:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9179:        } else {
                   9180:                REG16(DX) = 0x0000;
                   9181:        }
                   9182: }
                   9183: 
                   9184: inline void pcbios_int_17h_52h()
                   9185: {
                   9186:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9187:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9188:        } else {
                   9189:                REG16(DX) = 0x0000;
                   9190:        }
                   9191: }
                   9192: 
                   9193: inline void pcbios_int_17h_84h()
                   9194: {
                   9195:        if(REG16(DX) < 3) {
                   9196:                if(pio[REG16(DX)].jis_mode) {
                   9197:                        printer_out(REG16(DX), 0x1c);
                   9198:                        printer_out(REG16(DX), 0x2e);
                   9199:                        pio[REG16(DX)].jis_mode = false;
                   9200:                }
                   9201:                printer_out(REG16(DX), REG8(AL));
                   9202:                REG8(AH) = 0xd0;
                   9203:        }
                   9204: }
                   9205: 
                   9206: inline void pcbios_int_17h_85h()
                   9207: {
                   9208:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9209: }
                   9210: 
1.1       root     9211: inline void pcbios_int_1ah_00h()
                   9212: {
1.1.1.19  root     9213:        pcbios_update_daily_timer_counter(timeGetTime());
                   9214:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9215:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9216:        REG8(AL) = mem[0x470];
                   9217:        mem[0x470] = 0;
1.1       root     9218: }
                   9219: 
                   9220: inline int to_bcd(int t)
                   9221: {
                   9222:        int u = (t % 100) / 10;
                   9223:        return (u << 4) | (t % 10);
                   9224: }
                   9225: 
                   9226: inline void pcbios_int_1ah_02h()
                   9227: {
                   9228:        SYSTEMTIME time;
                   9229:        
                   9230:        GetLocalTime(&time);
                   9231:        REG8(CH) = to_bcd(time.wHour);
                   9232:        REG8(CL) = to_bcd(time.wMinute);
                   9233:        REG8(DH) = to_bcd(time.wSecond);
                   9234:        REG8(DL) = 0x00;
                   9235: }
                   9236: 
                   9237: inline void pcbios_int_1ah_04h()
                   9238: {
                   9239:        SYSTEMTIME time;
                   9240:        
                   9241:        GetLocalTime(&time);
                   9242:        REG8(CH) = to_bcd(time.wYear / 100);
                   9243:        REG8(CL) = to_bcd(time.wYear);
                   9244:        REG8(DH) = to_bcd(time.wMonth);
                   9245:        REG8(DL) = to_bcd(time.wDay);
                   9246: }
                   9247: 
                   9248: inline void pcbios_int_1ah_0ah()
                   9249: {
                   9250:        SYSTEMTIME time;
                   9251:        FILETIME file_time;
                   9252:        WORD dos_date, dos_time;
                   9253:        
                   9254:        GetLocalTime(&time);
                   9255:        SystemTimeToFileTime(&time, &file_time);
                   9256:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9257:        REG16(CX) = dos_date;
                   9258: }
                   9259: 
                   9260: // msdos system call
                   9261: 
1.1.1.43  root     9262: inline void msdos_int_21h_56h(int lfn);
                   9263: 
1.1       root     9264: inline void msdos_int_21h_00h()
                   9265: {
1.1.1.3   root     9266:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9267: }
                   9268: 
1.1.1.35  root     9269: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9270: {
                   9271:        REG8(AL) = msdos_getche();
1.1.1.33  root     9272:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9273:        
1.1.1.35  root     9274: #ifdef USE_SERVICE_THREAD
                   9275:        service_exit = true;
                   9276: #endif
                   9277:        return(0);
                   9278: }
                   9279: 
                   9280: inline void msdos_int_21h_01h()
                   9281: {
                   9282: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9283:        if(!in_service && !in_service_29h &&
1.1.1.58! root     9284:           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9285:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9286:                // msdos_putch() will be used in this service
                   9287:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9288:                start_service_loop(msdos_int_21h_01h_thread);
                   9289:        } else {
                   9290: #endif
                   9291:                msdos_int_21h_01h_thread(NULL);
                   9292:                REQUEST_HARDWRE_UPDATE();
                   9293: #ifdef USE_SERVICE_THREAD
                   9294:        }
1.1.1.35  root     9295: #endif
1.1       root     9296: }
                   9297: 
                   9298: inline void msdos_int_21h_02h()
                   9299: {
1.1.1.33  root     9300:        UINT8 data = REG8(DL);
                   9301:        msdos_putch(data);
                   9302:        REG8(AL) = data;
                   9303:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9304: }
                   9305: 
                   9306: inline void msdos_int_21h_03h()
                   9307: {
                   9308:        REG8(AL) = msdos_aux_in();
                   9309: }
                   9310: 
                   9311: inline void msdos_int_21h_04h()
                   9312: {
                   9313:        msdos_aux_out(REG8(DL));
                   9314: }
                   9315: 
                   9316: inline void msdos_int_21h_05h()
                   9317: {
                   9318:        msdos_prn_out(REG8(DL));
                   9319: }
                   9320: 
                   9321: inline void msdos_int_21h_06h()
                   9322: {
                   9323:        if(REG8(DL) == 0xff) {
                   9324:                if(msdos_kbhit()) {
                   9325:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9326: #if defined(HAS_I386)
                   9327:                        m_ZF = 0;
                   9328: #else
                   9329:                        m_ZeroVal = 1;
                   9330: #endif
1.1       root     9331:                } else {
                   9332:                        REG8(AL) = 0;
1.1.1.3   root     9333: #if defined(HAS_I386)
                   9334:                        m_ZF = 1;
                   9335: #else
                   9336:                        m_ZeroVal = 0;
                   9337: #endif
1.1.1.14  root     9338:                        maybe_idle();
1.1       root     9339:                }
                   9340:        } else {
1.1.1.33  root     9341:                UINT8 data = REG8(DL);
                   9342:                msdos_putch(data);
                   9343:                REG8(AL) = data;
1.1       root     9344:        }
                   9345: }
                   9346: 
1.1.1.35  root     9347: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9348: {
                   9349:        REG8(AL) = msdos_getch();
1.1.1.26  root     9350:        
1.1.1.35  root     9351: #ifdef USE_SERVICE_THREAD
                   9352:        service_exit = true;
                   9353: #endif
                   9354:        return(0);
1.1       root     9355: }
                   9356: 
1.1.1.35  root     9357: inline void msdos_int_21h_07h()
                   9358: {
                   9359: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9360:        if(!in_service && !in_service_29h) {
                   9361:                start_service_loop(msdos_int_21h_07h_thread);
                   9362:        } else {
                   9363: #endif
                   9364:                msdos_int_21h_07h_thread(NULL);
                   9365:                REQUEST_HARDWRE_UPDATE();
                   9366: #ifdef USE_SERVICE_THREAD
                   9367:        }
1.1.1.35  root     9368: #endif
                   9369: }
                   9370: 
                   9371: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9372: {
                   9373:        REG8(AL) = msdos_getch();
1.1.1.33  root     9374:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9375:        
1.1.1.35  root     9376: #ifdef USE_SERVICE_THREAD
                   9377:        service_exit = true;
                   9378: #endif
                   9379:        return(0);
                   9380: }
                   9381: 
                   9382: inline void msdos_int_21h_08h()
                   9383: {
                   9384: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9385:        if(!in_service && !in_service_29h) {
                   9386:                start_service_loop(msdos_int_21h_08h_thread);
                   9387:        } else {
                   9388: #endif
                   9389:                msdos_int_21h_08h_thread(NULL);
                   9390:                REQUEST_HARDWRE_UPDATE();
                   9391: #ifdef USE_SERVICE_THREAD
                   9392:        }
1.1.1.35  root     9393: #endif
1.1       root     9394: }
                   9395: 
                   9396: inline void msdos_int_21h_09h()
                   9397: {
1.1.1.21  root     9398:        msdos_stdio_reopen();
                   9399:        
1.1.1.20  root     9400:        process_t *process = msdos_process_info_get(current_psp);
                   9401:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9402:        
1.1.1.14  root     9403:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9404:        int len = 0;
1.1       root     9405:        
1.1.1.14  root     9406:        while(str[len] != '$' && len < 0x10000) {
                   9407:                len++;
                   9408:        }
1.1.1.20  root     9409:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9410:                // stdout is redirected to file
1.1.1.20  root     9411:                msdos_write(fd, str, len);
1.1       root     9412:        } else {
                   9413:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9414:                        msdos_putch(str[i]);
1.1       root     9415:                }
                   9416:        }
1.1.1.33  root     9417:        REG8(AL) = '$';
                   9418:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9419: }
                   9420: 
1.1.1.35  root     9421: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9422: {
1.1.1.3   root     9423:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9424:        int max = mem[ofs] - 1;
                   9425:        UINT8 *buf = mem + ofs + 2;
                   9426:        int chr, p = 0;
                   9427:        
                   9428:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9429:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9430:                        p = 0;
1.1.1.33  root     9431:                        msdos_putch(0x03);
                   9432:                        msdos_putch(0x0d);
                   9433:                        msdos_putch(0x0a);
1.1.1.26  root     9434:                        break;
1.1.1.33  root     9435:                } else if(ctrl_break_pressed) {
                   9436:                        // skip this byte
1.1.1.26  root     9437:                } else if(chr == 0x00) {
1.1       root     9438:                        // skip 2nd byte
                   9439:                        msdos_getch();
                   9440:                } else if(chr == 0x08) {
                   9441:                        // back space
                   9442:                        if(p > 0) {
                   9443:                                p--;
1.1.1.20  root     9444:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9445:                                        msdos_putch(0x08);
                   9446:                                        msdos_putch(0x08);
                   9447:                                        msdos_putch(0x20);
                   9448:                                        msdos_putch(0x20);
                   9449:                                        msdos_putch(0x08);
                   9450:                                        msdos_putch(0x08);
1.1.1.36  root     9451:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9452:                                        p--;
                   9453:                                        msdos_putch(0x08);
                   9454:                                        msdos_putch(0x08);
                   9455:                                        msdos_putch(0x20);
                   9456:                                        msdos_putch(0x20);
                   9457:                                        msdos_putch(0x08);
                   9458:                                        msdos_putch(0x08);
1.1.1.34  root     9459:                                } else {
                   9460:                                        msdos_putch(0x08);
                   9461:                                        msdos_putch(0x20);
                   9462:                                        msdos_putch(0x08);
                   9463:                                }
                   9464:                        }
                   9465:                } else if(chr == 0x1b) {
                   9466:                        // escape
                   9467:                        while(p > 0) {
                   9468:                                p--;
                   9469:                                if(msdos_ctrl_code_check(buf[p])) {
                   9470:                                        msdos_putch(0x08);
                   9471:                                        msdos_putch(0x08);
                   9472:                                        msdos_putch(0x20);
                   9473:                                        msdos_putch(0x20);
                   9474:                                        msdos_putch(0x08);
                   9475:                                        msdos_putch(0x08);
1.1.1.20  root     9476:                                } else {
1.1.1.34  root     9477:                                        msdos_putch(0x08);
                   9478:                                        msdos_putch(0x20);
                   9479:                                        msdos_putch(0x08);
1.1.1.20  root     9480:                                }
1.1       root     9481:                        }
                   9482:                } else if(p < max) {
                   9483:                        buf[p++] = chr;
                   9484:                        msdos_putch(chr);
                   9485:                }
                   9486:        }
                   9487:        buf[p] = 0x0d;
                   9488:        mem[ofs + 1] = p;
1.1.1.33  root     9489:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9490:        
1.1.1.35  root     9491: #ifdef USE_SERVICE_THREAD
                   9492:        service_exit = true;
                   9493: #endif
                   9494:        return(0);
                   9495: }
                   9496: 
                   9497: inline void msdos_int_21h_0ah()
                   9498: {
                   9499:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9500: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9501:                if(!in_service && !in_service_29h &&
1.1.1.58! root     9502:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9503:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9504:                        // msdos_putch() will be used in this service
                   9505:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9506:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9507:                } else {
                   9508: #endif
                   9509:                        msdos_int_21h_0ah_thread(NULL);
                   9510:                        REQUEST_HARDWRE_UPDATE();
                   9511: #ifdef USE_SERVICE_THREAD
                   9512:                }
1.1.1.35  root     9513: #endif
                   9514:        }
1.1       root     9515: }
                   9516: 
                   9517: inline void msdos_int_21h_0bh()
                   9518: {
                   9519:        if(msdos_kbhit()) {
                   9520:                REG8(AL) = 0xff;
                   9521:        } else {
                   9522:                REG8(AL) = 0x00;
1.1.1.14  root     9523:                maybe_idle();
1.1       root     9524:        }
1.1.1.33  root     9525:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9526: }
                   9527: 
                   9528: inline void msdos_int_21h_0ch()
                   9529: {
                   9530:        // clear key buffer
1.1.1.21  root     9531:        msdos_stdio_reopen();
                   9532:        
1.1.1.20  root     9533:        process_t *process = msdos_process_info_get(current_psp);
                   9534:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9535:        
                   9536:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9537:                // stdin is redirected to file
                   9538:        } else {
                   9539:                while(msdos_kbhit()) {
                   9540:                        msdos_getch();
                   9541:                }
                   9542:        }
                   9543:        
                   9544:        switch(REG8(AL)) {
                   9545:        case 0x01:
                   9546:                msdos_int_21h_01h();
                   9547:                break;
                   9548:        case 0x06:
                   9549:                msdos_int_21h_06h();
                   9550:                break;
                   9551:        case 0x07:
                   9552:                msdos_int_21h_07h();
                   9553:                break;
                   9554:        case 0x08:
                   9555:                msdos_int_21h_08h();
                   9556:                break;
                   9557:        case 0x0a:
                   9558:                msdos_int_21h_0ah();
                   9559:                break;
                   9560:        default:
1.1.1.48  root     9561:                // the buffer is flushed but no input is attempted
1.1       root     9562:                break;
                   9563:        }
                   9564: }
                   9565: 
                   9566: inline void msdos_int_21h_0dh()
                   9567: {
                   9568: }
                   9569: 
                   9570: inline void msdos_int_21h_0eh()
                   9571: {
                   9572:        if(REG8(DL) < 26) {
                   9573:                _chdrive(REG8(DL) + 1);
                   9574:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9575:                msdos_sda_update(current_psp);
1.1       root     9576:        }
                   9577:        REG8(AL) = 26; // zdrive
                   9578: }
                   9579: 
1.1.1.14  root     9580: inline void msdos_int_21h_0fh()
                   9581: {
                   9582:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9583:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9584:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9585:        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     9586:        
1.1.1.14  root     9587:        if(hFile == INVALID_HANDLE_VALUE) {
                   9588:                REG8(AL) = 0xff;
                   9589:        } else {
                   9590:                REG8(AL) = 0;
                   9591:                fcb->current_block = 0;
                   9592:                fcb->record_size = 128;
                   9593:                fcb->file_size = GetFileSize(hFile, NULL);
                   9594:                fcb->handle = hFile;
                   9595:                fcb->cur_record = 0;
                   9596:        }
                   9597: }
                   9598: 
                   9599: inline void msdos_int_21h_10h()
                   9600: {
                   9601:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9602:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9603:        
                   9604:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9605: }
                   9606: 
1.1       root     9607: inline void msdos_int_21h_11h()
                   9608: {
1.1.1.3   root     9609:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9610:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9611:        
                   9612:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9613:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9614:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9615:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9616:        const char *path = msdos_fcb_path(fcb);
1.1       root     9617:        WIN32_FIND_DATA fd;
                   9618:        
1.1.1.13  root     9619:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9620:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9621:                FindClose(dtainfo->find_handle);
                   9622:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9623:        }
                   9624:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9625:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9626:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9627:        
1.1.1.14  root     9628:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9629:                dtainfo->allowable_mask &= ~8;
1.1       root     9630:        }
1.1.1.14  root     9631:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9632:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9633:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9634:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9635:                                FindClose(dtainfo->find_handle);
                   9636:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9637:                                break;
                   9638:                        }
                   9639:                }
                   9640:        }
1.1.1.13  root     9641:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9642:                if(ext_fcb->flag == 0xff) {
                   9643:                        ext_find->flag = 0xff;
                   9644:                        memset(ext_find->reserved, 0, 5);
                   9645:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9646:                }
                   9647:                find->drive = _getdrive();
1.1.1.13  root     9648:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9649:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9650:                find->nt_res = 0;
                   9651:                msdos_find_file_conv_local_time(&fd);
                   9652:                find->create_time_ms = 0;
                   9653:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9654:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9655:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9656:                find->cluster_hi = find->cluster_lo = 0;
                   9657:                find->file_size = fd.nFileSizeLow;
                   9658:                REG8(AL) = 0x00;
1.1.1.14  root     9659:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9660:                if(ext_fcb->flag == 0xff) {
                   9661:                        ext_find->flag = 0xff;
                   9662:                        memset(ext_find->reserved, 0, 5);
                   9663:                        ext_find->attribute = 8;
                   9664:                }
                   9665:                find->drive = _getdrive();
                   9666:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9667:                find->attribute = 8;
                   9668:                find->nt_res = 0;
                   9669:                msdos_find_file_conv_local_time(&fd);
                   9670:                find->create_time_ms = 0;
                   9671:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9672:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9673:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9674:                find->cluster_hi = find->cluster_lo = 0;
                   9675:                find->file_size = 0;
1.1.1.14  root     9676:                dtainfo->allowable_mask &= ~8;
1.1       root     9677:                REG8(AL) = 0x00;
                   9678:        } else {
                   9679:                REG8(AL) = 0xff;
                   9680:        }
                   9681: }
                   9682: 
                   9683: inline void msdos_int_21h_12h()
                   9684: {
1.1.1.3   root     9685:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9686: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9687:        
                   9688:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9689:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9690:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9691:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9692:        WIN32_FIND_DATA fd;
                   9693:        
1.1.1.13  root     9694:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9695:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9696:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9697:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9698:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9699:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9700:                                        FindClose(dtainfo->find_handle);
                   9701:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9702:                                        break;
                   9703:                                }
                   9704:                        }
                   9705:                } else {
1.1.1.13  root     9706:                        FindClose(dtainfo->find_handle);
                   9707:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9708:                }
                   9709:        }
1.1.1.13  root     9710:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9711:                if(ext_fcb->flag == 0xff) {
                   9712:                        ext_find->flag = 0xff;
                   9713:                        memset(ext_find->reserved, 0, 5);
                   9714:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9715:                }
                   9716:                find->drive = _getdrive();
1.1.1.13  root     9717:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9718:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9719:                find->nt_res = 0;
                   9720:                msdos_find_file_conv_local_time(&fd);
                   9721:                find->create_time_ms = 0;
                   9722:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9723:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9724:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9725:                find->cluster_hi = find->cluster_lo = 0;
                   9726:                find->file_size = fd.nFileSizeLow;
                   9727:                REG8(AL) = 0x00;
1.1.1.14  root     9728:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9729:                if(ext_fcb->flag == 0xff) {
                   9730:                        ext_find->flag = 0xff;
                   9731:                        memset(ext_find->reserved, 0, 5);
                   9732:                        ext_find->attribute = 8;
                   9733:                }
                   9734:                find->drive = _getdrive();
                   9735:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9736:                find->attribute = 8;
                   9737:                find->nt_res = 0;
                   9738:                msdos_find_file_conv_local_time(&fd);
                   9739:                find->create_time_ms = 0;
                   9740:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9741:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9742:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9743:                find->cluster_hi = find->cluster_lo = 0;
                   9744:                find->file_size = 0;
1.1.1.14  root     9745:                dtainfo->allowable_mask &= ~8;
1.1       root     9746:                REG8(AL) = 0x00;
                   9747:        } else {
                   9748:                REG8(AL) = 0xff;
                   9749:        }
                   9750: }
                   9751: 
                   9752: inline void msdos_int_21h_13h()
                   9753: {
1.1.1.3   root     9754:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9755:                REG8(AL) = 0xff;
                   9756:        } else {
                   9757:                REG8(AL) = 0x00;
                   9758:        }
                   9759: }
                   9760: 
1.1.1.16  root     9761: inline void msdos_int_21h_14h()
                   9762: {
                   9763:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9764:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9765:        process_t *process = msdos_process_info_get(current_psp);
                   9766:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9767:        DWORD num = 0;
                   9768:        
                   9769:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9770:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9771:                REG8(AL) = 1;
                   9772:        } else {
                   9773:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9774:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9775:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9776:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9777:        }
                   9778: }
                   9779: 
                   9780: inline void msdos_int_21h_15h()
1.1.1.14  root     9781: {
                   9782:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9783:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9784:        process_t *process = msdos_process_info_get(current_psp);
                   9785:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9786:        DWORD num = 0;
1.1.1.14  root     9787:        
1.1.1.16  root     9788:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9789:                REG8(AL) = 1;
                   9790:        } else {
                   9791:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9792:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9793:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9794:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9795:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9796:        }
                   9797: }
                   9798: 
                   9799: inline void msdos_int_21h_16h()
                   9800: {
                   9801:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9802:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9803:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9804:        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     9805:        
1.1.1.14  root     9806:        if(hFile == INVALID_HANDLE_VALUE) {
                   9807:                REG8(AL) = 0xff;
                   9808:        } else {
                   9809:                REG8(AL) = 0;
                   9810:                fcb->current_block = 0;
                   9811:                fcb->record_size = 128;
                   9812:                fcb->file_size = 0;
                   9813:                fcb->handle = hFile;
                   9814:                fcb->cur_record = 0;
                   9815:        }
                   9816: }
                   9817: 
1.1.1.16  root     9818: inline void msdos_int_21h_17h()
                   9819: {
                   9820:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9821:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     9822: //     const char *path_src = msdos_fcb_path(fcb_src);
                   9823:        char path_src[MAX_PATH];
                   9824:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   9825:        
1.1.1.16  root     9826:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9827:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     9828: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   9829:        char path_dst[MAX_PATH];
                   9830:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9831:        
                   9832:        if(rename(path_src, path_dst)) {
                   9833:                REG8(AL) = 0xff;
                   9834:        } else {
                   9835:                REG8(AL) = 0;
                   9836:        }
                   9837: }
                   9838: 
1.1       root     9839: inline void msdos_int_21h_18h()
                   9840: {
                   9841:        REG8(AL) = 0x00;
                   9842: }
                   9843: 
                   9844: inline void msdos_int_21h_19h()
                   9845: {
                   9846:        REG8(AL) = _getdrive() - 1;
                   9847: }
                   9848: 
                   9849: inline void msdos_int_21h_1ah()
                   9850: {
                   9851:        process_t *process = msdos_process_info_get(current_psp);
                   9852:        
                   9853:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9854:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9855:        msdos_sda_update(current_psp);
1.1       root     9856: }
                   9857: 
                   9858: inline void msdos_int_21h_1bh()
                   9859: {
                   9860:        int drive_num = _getdrive() - 1;
                   9861:        UINT16 seg, ofs;
                   9862:        
                   9863:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9864:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9865:                REG8(AL) = dpb->highest_sector_num + 1;
                   9866:                REG16(CX) = dpb->bytes_per_sector;
                   9867:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9868:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9869:        } else {
                   9870:                REG8(AL) = 0xff;
1.1.1.3   root     9871:                m_CF = 1;
1.1       root     9872:        }
                   9873: 
                   9874: }
                   9875: 
                   9876: inline void msdos_int_21h_1ch()
                   9877: {
1.1.1.41  root     9878:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9879:        UINT16 seg, ofs;
                   9880:        
                   9881:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9882:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9883:                REG8(AL) = dpb->highest_sector_num + 1;
                   9884:                REG16(CX) = dpb->bytes_per_sector;
                   9885:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9886:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9887:        } else {
                   9888:                REG8(AL) = 0xff;
1.1.1.3   root     9889:                m_CF = 1;
1.1       root     9890:        }
                   9891: 
                   9892: }
                   9893: 
                   9894: inline void msdos_int_21h_1dh()
                   9895: {
                   9896:        REG8(AL) = 0;
                   9897: }
                   9898: 
                   9899: inline void msdos_int_21h_1eh()
                   9900: {
                   9901:        REG8(AL) = 0;
                   9902: }
                   9903: 
                   9904: inline void msdos_int_21h_1fh()
                   9905: {
                   9906:        int drive_num = _getdrive() - 1;
                   9907:        UINT16 seg, ofs;
                   9908:        
                   9909:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9910:                REG8(AL) = 0;
1.1.1.3   root     9911:                SREG(DS) = seg;
                   9912:                i386_load_segment_descriptor(DS);
1.1       root     9913:                REG16(BX) = ofs;
                   9914:        } else {
                   9915:                REG8(AL) = 0xff;
1.1.1.3   root     9916:                m_CF = 1;
1.1       root     9917:        }
                   9918: }
                   9919: 
                   9920: inline void msdos_int_21h_20h()
                   9921: {
                   9922:        REG8(AL) = 0;
                   9923: }
                   9924: 
1.1.1.14  root     9925: inline void msdos_int_21h_21h()
                   9926: {
                   9927:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9928:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9929:        
                   9930:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9931:                REG8(AL) = 1;
                   9932:        } else {
                   9933:                process_t *process = msdos_process_info_get(current_psp);
                   9934:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9935:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9936:                DWORD num = 0;
1.1.1.14  root     9937:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9938:                        REG8(AL) = 1;
                   9939:                } else {
                   9940:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9941:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9942:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9943:                }
                   9944:        }
                   9945: }
                   9946: 
                   9947: inline void msdos_int_21h_22h()
                   9948: {
                   9949:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9950:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9951:        
                   9952:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9953:                REG8(AL) = 0xff;
                   9954:        } else {
                   9955:                process_t *process = msdos_process_info_get(current_psp);
                   9956:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9957:                DWORD num = 0;
1.1.1.14  root     9958:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9959:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9960:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9961:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9962:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9963:        }
                   9964: }
                   9965: 
1.1.1.16  root     9966: inline void msdos_int_21h_23h()
                   9967: {
                   9968:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9969:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9970:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     9971:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9972:        
                   9973:        if(hFile == INVALID_HANDLE_VALUE) {
                   9974:                REG8(AL) = 0xff;
                   9975:        } else {
                   9976:                UINT32 size = GetFileSize(hFile, NULL);
                   9977:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9978:                REG8(AL) = 0;
                   9979:        }
                   9980: }
                   9981: 
                   9982: inline void msdos_int_21h_24h()
                   9983: {
                   9984:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9985:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9986:        
                   9987:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9988: }
                   9989: 
1.1       root     9990: inline void msdos_int_21h_25h()
                   9991: {
                   9992:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9993:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9994: }
                   9995: 
                   9996: inline void msdos_int_21h_26h()
                   9997: {
                   9998:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9999:        
                   10000:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     10001:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     10002:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10003:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10004:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10005:        psp->parent_psp = 0;
                   10006: }
                   10007: 
1.1.1.16  root     10008: inline void msdos_int_21h_27h()
                   10009: {
                   10010:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10011:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10012:        
                   10013:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10014:                REG8(AL) = 1;
                   10015:        } else {
                   10016:                process_t *process = msdos_process_info_get(current_psp);
                   10017:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10018:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   10019:                DWORD num = 0;
                   10020:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   10021:                        REG8(AL) = 1;
                   10022:                } else {
                   10023:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10024:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10025:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10026:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10027:                }
                   10028:        }
                   10029: }
                   10030: 
                   10031: inline void msdos_int_21h_28h()
                   10032: {
                   10033:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10034:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10035:        
                   10036:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10037:                REG8(AL) = 0xff;
                   10038:        } else {
                   10039:                process_t *process = msdos_process_info_get(current_psp);
                   10040:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10041:                DWORD num = 0;
                   10042:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   10043:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10044:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10045:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10046:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   10047:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10048:        }
                   10049: }
                   10050: 
1.1       root     10051: inline void msdos_int_21h_29h()
                   10052: {
1.1.1.20  root     10053:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   10054:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     10055:        UINT8 drv = 0;
                   10056:        char sep_chars[] = ":.;,=+";
                   10057:        char end_chars[] = "\\<>|/\"[]";
                   10058:        char spc_chars[] = " \t";
                   10059:        
1.1.1.20  root     10060:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   10061:        buffer[1023] = 0;
                   10062:        memset(name, 0x20, sizeof(name));
                   10063:        memset(ext, 0x20, sizeof(ext));
                   10064:        
1.1       root     10065:        if(REG8(AL) & 1) {
1.1.1.20  root     10066:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   10067:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     10068:                        ofs++;
                   10069:                }
                   10070:        }
1.1.1.20  root     10071:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     10072:        
1.1.1.24  root     10073:        if(buffer[ofs + 1] == ':') {
                   10074:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   10075:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     10076:                        ofs += 2;
1.1.1.24  root     10077:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10078:                                ofs++;
                   10079:                        }
                   10080:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   10081:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     10082:                        ofs += 2;
1.1.1.24  root     10083:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10084:                                ofs++;
                   10085:                        }
1.1       root     10086:                }
                   10087:        }
1.1.1.20  root     10088:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10089:                UINT8 c = buffer[ofs];
                   10090:                if(is_kanji) {
                   10091:                        is_kanji = 0;
                   10092:                } else if(msdos_lead_byte_check(c)) {
                   10093:                        is_kanji = 1;
                   10094:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10095:                        break;
                   10096:                } else if(c >= 'a' && c <= 'z') {
                   10097:                        c -= 0x20;
                   10098:                }
                   10099:                ofs++;
                   10100:                name[i] = c;
                   10101:        }
1.1.1.20  root     10102:        if(buffer[ofs] == '.') {
1.1       root     10103:                ofs++;
1.1.1.20  root     10104:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10105:                        UINT8 c = buffer[ofs];
                   10106:                        if(is_kanji) {
                   10107:                                is_kanji = 0;
                   10108:                        } else if(msdos_lead_byte_check(c)) {
                   10109:                                is_kanji = 1;
                   10110:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10111:                                break;
                   10112:                        } else if(c >= 'a' && c <= 'z') {
                   10113:                                c -= 0x20;
                   10114:                        }
                   10115:                        ofs++;
                   10116:                        ext[i] = c;
                   10117:                }
                   10118:        }
1.1.1.20  root     10119:        int si = REG16(SI) + ofs;
1.1.1.3   root     10120:        int ds = SREG(DS);
1.1       root     10121:        while(si > 0xffff) {
                   10122:                si -= 0x10;
                   10123:                ds++;
                   10124:        }
                   10125:        REG16(SI) = si;
1.1.1.3   root     10126:        SREG(DS) = ds;
                   10127:        i386_load_segment_descriptor(DS);
1.1       root     10128:        
1.1.1.3   root     10129:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     10130:        if(!(REG8(AL) & 2) || drv != 0) {
                   10131:                fcb[0] = drv;
                   10132:        }
                   10133:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   10134:                memcpy(fcb + 1, name, 8);
                   10135:        }
                   10136:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   10137:                memcpy(fcb + 9, ext, 3);
                   10138:        }
                   10139:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     10140:                if(fcb[i] == '*') {
                   10141:                        found_star = 1;
                   10142:                }
                   10143:                if(found_star) {
                   10144:                        fcb[i] = '?';
                   10145:                }
                   10146:        }
1.1.1.20  root     10147:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     10148:                if(fcb[i] == '*') {
                   10149:                        found_star = 1;
                   10150:                }
                   10151:                if(found_star) {
                   10152:                        fcb[i] = '?';
                   10153:                }
                   10154:        }
                   10155:        
1.1.1.44  root     10156:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     10157:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   10158:                        REG8(AL) = 0x01;
1.1.1.20  root     10159:                } else {
                   10160:                        REG8(AL) = 0x00;
1.1       root     10161:                }
                   10162:        } else {
                   10163:                REG8(AL) = 0xff;
                   10164:        }
                   10165: }
                   10166: 
                   10167: inline void msdos_int_21h_2ah()
                   10168: {
                   10169:        SYSTEMTIME sTime;
                   10170:        
                   10171:        GetLocalTime(&sTime);
                   10172:        REG16(CX) = sTime.wYear;
                   10173:        REG8(DH) = (UINT8)sTime.wMonth;
                   10174:        REG8(DL) = (UINT8)sTime.wDay;
                   10175:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10176: }
                   10177: 
                   10178: inline void msdos_int_21h_2bh()
                   10179: {
1.1.1.14  root     10180:        REG8(AL) = 0xff;
1.1       root     10181: }
                   10182: 
                   10183: inline void msdos_int_21h_2ch()
                   10184: {
                   10185:        SYSTEMTIME sTime;
                   10186:        
                   10187:        GetLocalTime(&sTime);
                   10188:        REG8(CH) = (UINT8)sTime.wHour;
                   10189:        REG8(CL) = (UINT8)sTime.wMinute;
                   10190:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10191:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10192: }
                   10193: 
                   10194: inline void msdos_int_21h_2dh()
                   10195: {
                   10196:        REG8(AL) = 0x00;
                   10197: }
                   10198: 
                   10199: inline void msdos_int_21h_2eh()
                   10200: {
                   10201:        process_t *process = msdos_process_info_get(current_psp);
                   10202:        
                   10203:        process->verify = REG8(AL);
                   10204: }
                   10205: 
                   10206: inline void msdos_int_21h_2fh()
                   10207: {
                   10208:        process_t *process = msdos_process_info_get(current_psp);
                   10209:        
                   10210:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10211:        SREG(ES) = process->dta.w.h;
                   10212:        i386_load_segment_descriptor(ES);
1.1       root     10213: }
                   10214: 
                   10215: inline void msdos_int_21h_30h()
                   10216: {
                   10217:        // Version Flag / OEM
1.1.1.27  root     10218:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10219: #ifdef SUPPORT_HMA
                   10220:                REG16(BX) = 0x0000;
                   10221: #else
                   10222:                REG16(BX) = 0x1000; // DOS is in HMA
                   10223: #endif
1.1       root     10224:        } else {
1.1.1.27  root     10225:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10226:                // but this is not correct on Windows 98 SE
                   10227: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10228:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10229:        }
1.1.1.27  root     10230:        REG16(CX) = 0x0000;
1.1.1.30  root     10231:        REG8(AL) = dos_major_version;   // 7
                   10232:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10233: }
                   10234: 
                   10235: inline void msdos_int_21h_31h()
                   10236: {
1.1.1.29  root     10237:        try {
                   10238:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10239:        } catch(...) {
                   10240:                // recover the broken mcb
                   10241:                int mcb_seg = current_psp - 1;
                   10242:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10243:                
1.1.1.29  root     10244:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10245:                        mcb->mz = 'M';
                   10246:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10247:                        
1.1.1.29  root     10248:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10249:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10250:                        } else {
1.1.1.39  root     10251:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10252:                        }
                   10253:                } else {
                   10254:                        mcb->mz = 'Z';
1.1.1.30  root     10255:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10256:                }
                   10257:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10258:        }
1.1       root     10259:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10260: }
                   10261: 
                   10262: inline void msdos_int_21h_32h()
                   10263: {
                   10264:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10265:        UINT16 seg, ofs;
                   10266:        
                   10267:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10268:                REG8(AL) = 0;
1.1.1.3   root     10269:                SREG(DS) = seg;
                   10270:                i386_load_segment_descriptor(DS);
1.1       root     10271:                REG16(BX) = ofs;
                   10272:        } else {
                   10273:                REG8(AL) = 0xff;
1.1.1.3   root     10274:                m_CF = 1;
1.1       root     10275:        }
                   10276: }
                   10277: 
                   10278: inline void msdos_int_21h_33h()
                   10279: {
                   10280:        char path[MAX_PATH];
1.1.1.48  root     10281:        char drive = 3; // C:
1.1       root     10282:        
                   10283:        switch(REG8(AL)) {
                   10284:        case 0x00:
1.1.1.33  root     10285:                REG8(DL) = ctrl_break_checking;
1.1       root     10286:                break;
                   10287:        case 0x01:
1.1.1.33  root     10288:                ctrl_break_checking = REG8(DL);
                   10289:                break;
                   10290:        case 0x02:
                   10291:                {
                   10292:                        UINT8 old = ctrl_break_checking;
                   10293:                        ctrl_break_checking = REG8(DL);
                   10294:                        REG8(DL) = old;
                   10295:                }
                   10296:                break;
                   10297:        case 0x03:
                   10298:        case 0x04:
                   10299:                // DOS 4.0+ - Unused
1.1       root     10300:                break;
                   10301:        case 0x05:
1.1.1.48  root     10302:                if(GetSystemDirectory(path, MAX_PATH) != 0) {
                   10303:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10304:                                drive = path[0] - 'a' + 1;
                   10305:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10306:                                drive = path[0] - 'A' + 1;
                   10307:                        }
1.1       root     10308:                }
1.1.1.48  root     10309:                REG8(DL) = (UINT8)drive;
1.1       root     10310:                break;
                   10311:        case 0x06:
1.1.1.2   root     10312:                // MS-DOS version (7.10)
1.1       root     10313:                REG8(BL) = 7;
1.1.1.2   root     10314:                REG8(BH) = 10;
1.1       root     10315:                REG8(DL) = 0;
1.1.1.29  root     10316: #ifdef SUPPORT_HMA
                   10317:                REG8(DH) = 0x00;
                   10318: #else
                   10319:                REG8(DH) = 0x10; // DOS is in HMA
                   10320: #endif
1.1       root     10321:                break;
1.1.1.6   root     10322:        case 0x07:
                   10323:                if(REG8(DL) == 0) {
                   10324:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10325:                } else if(REG8(DL) == 1) {
                   10326:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10327:                }
                   10328:                break;
1.1       root     10329:        default:
1.1.1.22  root     10330:                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     10331: //             REG16(AX) = 0x01;
                   10332: //             m_CF = 1;
                   10333:                REG8(AL) = 0xff;
1.1       root     10334:                break;
                   10335:        }
                   10336: }
                   10337: 
1.1.1.23  root     10338: inline void msdos_int_21h_34h()
                   10339: {
                   10340:        SREG(ES) = SDA_TOP >> 4;
                   10341:        i386_load_segment_descriptor(ES);
                   10342:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   10343: }
                   10344: 
1.1       root     10345: inline void msdos_int_21h_35h()
                   10346: {
                   10347:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10348:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10349:        i386_load_segment_descriptor(ES);
1.1       root     10350: }
                   10351: 
                   10352: inline void msdos_int_21h_36h()
                   10353: {
                   10354:        struct _diskfree_t df = {0};
                   10355:        
                   10356:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10357:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10358:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10359:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10360:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10361:        } else {
                   10362:                REG16(AX) = 0xffff;
                   10363:        }
                   10364: }
                   10365: 
                   10366: inline void msdos_int_21h_37h()
                   10367: {
1.1.1.22  root     10368:        static UINT8 dev_flag = 0xff;
1.1       root     10369:        
                   10370:        switch(REG8(AL)) {
                   10371:        case 0x00:
1.1.1.22  root     10372:                {
                   10373:                        process_t *process = msdos_process_info_get(current_psp);
                   10374:                        REG8(AL) = 0x00;
                   10375:                        REG8(DL) = process->switchar;
                   10376:                }
1.1       root     10377:                break;
                   10378:        case 0x01:
1.1.1.22  root     10379:                {
                   10380:                        process_t *process = msdos_process_info_get(current_psp);
                   10381:                        REG8(AL) = 0x00;
                   10382:                        process->switchar = REG8(DL);
1.1.1.23  root     10383:                        msdos_sda_update(current_psp);
1.1.1.22  root     10384:                }
                   10385:                break;
                   10386:        case 0x02:
                   10387:                REG8(DL) = dev_flag;
                   10388:                break;
                   10389:        case 0x03:
                   10390:                dev_flag = REG8(DL);
                   10391:                break;
                   10392:        case 0xd0:
                   10393:        case 0xd1:
                   10394:        case 0xd2:
                   10395:        case 0xd3:
                   10396:        case 0xd4:
                   10397:        case 0xd5:
                   10398:        case 0xd6:
                   10399:        case 0xd7:
                   10400:        case 0xdc:
                   10401:        case 0xdd:
                   10402:        case 0xde:
                   10403:        case 0xdf:
1.1.1.48  root     10404:                // DIET v1.43e
                   10405: //             REG16(AX) = 1;
                   10406:                REG8(AL) = 0xff;
1.1       root     10407:                break;
                   10408:        default:
1.1.1.22  root     10409:                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     10410: //             REG16(AX) = 1;
                   10411:                REG8(AL) = 0xff;
1.1       root     10412:                break;
                   10413:        }
                   10414: }
                   10415: 
1.1.1.52  root     10416: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10417: {
                   10418:        char LCdata[80];
                   10419:        
1.1.1.19  root     10420:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     10421:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10422:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     10423:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10424:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     10425:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10426:        ci->date_format = *LCdata - '0';
1.1.1.42  root     10427:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10428:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     10429:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10430:        *ci->date_sep = *LCdata;
1.1.1.42  root     10431:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10432:        *ci->dec_sep = *LCdata;
1.1.1.42  root     10433:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10434:        *ci->list_sep = *LCdata;
1.1.1.42  root     10435:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10436:        *ci->thou_sep = *LCdata;
1.1.1.42  root     10437:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10438:        *ci->time_sep = *LCdata;
1.1.1.42  root     10439:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10440:        if(strchr(LCdata, 'H') != NULL) {
                   10441:                ci->time_format = 1;
                   10442:        }
1.1.1.49  root     10443:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10444:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.42  root     10445:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10446:        return atoi(LCdata);
                   10447: }
                   10448: 
1.1.1.42  root     10449: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10450: {
                   10451:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10452: }
                   10453: 
1.1.1.43  root     10454: void set_country_info(country_info_t *ci, int size)
                   10455: {
                   10456:        char LCdata[80];
                   10457:        
                   10458:        if(size >= 0x00 + 2) {
                   10459:                memset(LCdata, 0, sizeof(LCdata));
                   10460:                *LCdata = '0' + ci->date_format;
                   10461:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   10462:        }
                   10463:        if(size >= 0x02 + 5) {
                   10464:                memset(LCdata, 0, sizeof(LCdata));
                   10465:                memcpy(LCdata, &ci->currency_symbol, 4);
                   10466:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   10467:        }
                   10468:        if(size >= 0x07 + 2) {
                   10469:                memset(LCdata, 0, sizeof(LCdata));
                   10470:                *LCdata = *ci->thou_sep;
                   10471:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   10472:        }
                   10473:        if(size >= 0x09 + 2) {
                   10474:                memset(LCdata, 0, sizeof(LCdata));
                   10475:                *LCdata = *ci->dec_sep;
                   10476:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   10477:        }
                   10478:        if(size >= 0x0b + 2) {
                   10479:                memset(LCdata, 0, sizeof(LCdata));
                   10480:                *LCdata = *ci->date_sep;
                   10481:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   10482:        }
                   10483:        if(size >= 0x0d + 2) {
                   10484:                memset(LCdata, 0, sizeof(LCdata));
                   10485:                *LCdata = *ci->time_sep;
                   10486:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   10487:        }
                   10488:        if(size >= 0x0f + 1) {
                   10489:                memset(LCdata, 0, sizeof(LCdata));
                   10490:                *LCdata = '0' + ci->currency_format;
                   10491:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   10492:        }
                   10493:        if(size >= 0x10 + 1) {
                   10494:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   10495:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   10496:        }
                   10497:        if(size >= 0x11 + 1) {
                   10498:                // FIXME: is time format always H/h:mm:ss ???
                   10499:                if(ci->time_format & 1) {
                   10500:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10501:                } else {
                   10502:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10503:                }
                   10504:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   10505:        }
                   10506:        if(size >= 0x12 + 4) {
                   10507:                // 12h  DWORD   address of case map routine
                   10508:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10509:        }
                   10510:        if(size >= 0x16 + 2) {
                   10511:                memset(LCdata, 0, sizeof(LCdata));
                   10512:                *LCdata = *ci->list_sep;
                   10513:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10514:        }
                   10515: }
                   10516: 
1.1.1.42  root     10517: #ifndef SUBLANG_SWAHILI
                   10518:        #define SUBLANG_SWAHILI 0x01
                   10519: #endif
                   10520: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10521:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10522: #endif
                   10523: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10524:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10525: #endif
                   10526: #ifndef LANG_BANGLA
                   10527:        #define LANG_BANGLA 0x45
                   10528: #endif
                   10529: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10530:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10531: #endif
                   10532: 
                   10533: static const struct {
                   10534:        int code;
                   10535:        USHORT usPrimaryLanguage;
                   10536:        USHORT usSubLanguage;
                   10537: } country_table[] = {
                   10538:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10539:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10540:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10541:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10542:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10543:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10544: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10545: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10546: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10547: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10548: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10549:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10550:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10551: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10552:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10553: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10554:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10555:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10556:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10557:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10558:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10559:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10560:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10561: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10562: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10563:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10564:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10565:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10566:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10567:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10568: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10569: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10570: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10571:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10572: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10573: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10574: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10575: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10576:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10577:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10578:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10579: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10580:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10581:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10582:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10583:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10584:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10585:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10586:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10587:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10588:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10589:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10590:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10591:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10592:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10593: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10594:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10595:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10596:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10597:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10598:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10599:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10600:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10601:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10602:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10603:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10604: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10605:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10606: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10607:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10608:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10609:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10610:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10611:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10612:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10613:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10614:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10615: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10616:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10617: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10618: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10619:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10620: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10621:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10622:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10623:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10624:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10625:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10626:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10627:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10628: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10629: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10630:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10631: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10632:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10633:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10634:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10635:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10636: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10637: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10638: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10639:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10640:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10641:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10642:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10643:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10644:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10645:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10646:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10647:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10648:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10649:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10650:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10651:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10652:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10653:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10654:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10655:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10656:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10657:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10658:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10659:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10660: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10661:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10662: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10663:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10664:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10665:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10666:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10667:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10668:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10669:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10670:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10671:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10672:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10673:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10674:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10675:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10676:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10677:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10678:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10679:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10680:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10681:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10682:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10683:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10684:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10685:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10686:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10687:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10688: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10689:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10690:        {-1, 0, 0},
                   10691: };
                   10692: 
1.1.1.14  root     10693: inline void msdos_int_21h_38h()
                   10694: {
                   10695:        switch(REG8(AL)) {
                   10696:        case 0x00:
1.1.1.19  root     10697:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10698:                break;
                   10699:        default:
1.1.1.42  root     10700:                for(int i = 0;; i++) {
                   10701:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10702:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10703:                                break;
                   10704:                        } else if(country_table[i].code == -1) {
                   10705: //                             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));
                   10706: //                             REG16(AX) = 2;
                   10707: //                             m_CF = 1;
1.1.1.48  root     10708:                                // get current coutry info
1.1.1.42  root     10709:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10710:                                break;
                   10711:                        }
                   10712:                }
1.1.1.14  root     10713:                break;
                   10714:        }
                   10715: }
                   10716: 
1.1       root     10717: inline void msdos_int_21h_39h(int lfn)
                   10718: {
1.1.1.3   root     10719:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10720:                REG16(AX) = errno;
1.1.1.3   root     10721:                m_CF = 1;
1.1       root     10722:        }
                   10723: }
                   10724: 
                   10725: inline void msdos_int_21h_3ah(int lfn)
                   10726: {
1.1.1.3   root     10727:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10728:                REG16(AX) = errno;
1.1.1.3   root     10729:                m_CF = 1;
1.1       root     10730:        }
                   10731: }
                   10732: 
                   10733: inline void msdos_int_21h_3bh(int lfn)
                   10734: {
1.1.1.45  root     10735:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10736:        
                   10737:        if(_chdir(path)) {
1.1.1.17  root     10738:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10739:                m_CF = 1;
1.1.1.44  root     10740:        } else {
                   10741:                int drv = _getdrive() - 1;
                   10742:                if(path[1] == ':') {
                   10743:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10744:                                drv = path[0] - 'A';
                   10745:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10746:                                drv = path[0] - 'a';
                   10747:                        }
                   10748:                }
                   10749:                msdos_cds_update(drv, path);
1.1       root     10750:        }
                   10751: }
                   10752: 
                   10753: inline void msdos_int_21h_3ch()
                   10754: {
1.1.1.45  root     10755:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10756:        int attr = GetFileAttributes(path);
1.1.1.37  root     10757:        int fd = -1;
                   10758:        int sio_port = 0;
                   10759:        int lpt_port = 0;
1.1       root     10760:        
1.1.1.45  root     10761:        if(msdos_is_device_path(path)) {
                   10762:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10763:        } else {
                   10764:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10765:        }
                   10766:        if(fd != -1) {
                   10767:                if(attr == -1) {
                   10768:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10769:                }
                   10770:                SetFileAttributes(path, attr);
                   10771:                REG16(AX) = fd;
1.1.1.45  root     10772:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10773:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10774:        } else {
                   10775:                REG16(AX) = errno;
1.1.1.3   root     10776:                m_CF = 1;
1.1       root     10777:        }
                   10778: }
                   10779: 
                   10780: inline void msdos_int_21h_3dh()
                   10781: {
1.1.1.45  root     10782:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10783:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10784:        int fd = -1;
                   10785:        int sio_port = 0;
                   10786:        int lpt_port = 0;
1.1       root     10787:        
                   10788:        if(mode < 0x03) {
1.1.1.45  root     10789:                if(msdos_is_device_path(path)) {
                   10790:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10791:                } else {
1.1.1.13  root     10792:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10793:                }
1.1       root     10794:                if(fd != -1) {
                   10795:                        REG16(AX) = fd;
1.1.1.45  root     10796:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10797:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10798:                } else {
                   10799:                        REG16(AX) = errno;
1.1.1.3   root     10800:                        m_CF = 1;
1.1       root     10801:                }
                   10802:        } else {
                   10803:                REG16(AX) = 0x0c;
1.1.1.3   root     10804:                m_CF = 1;
1.1       root     10805:        }
                   10806: }
                   10807: 
                   10808: inline void msdos_int_21h_3eh()
                   10809: {
                   10810:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10811:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10812:        
1.1.1.20  root     10813:        if(fd < process->max_files && file_handler[fd].valid) {
                   10814:                _close(fd);
                   10815:                msdos_file_handler_close(fd);
                   10816:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10817:        } else {
                   10818:                REG16(AX) = 0x06;
1.1.1.3   root     10819:                m_CF = 1;
1.1       root     10820:        }
                   10821: }
                   10822: 
1.1.1.35  root     10823: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10824: {
                   10825:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10826:        int max = REG16(CX);
                   10827:        int p = 0;
                   10828:        
                   10829:        while(max > p) {
                   10830:                int chr = msdos_getch();
                   10831:                
                   10832:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10833:                        p = 0;
                   10834:                        buf[p++] = 0x0d;
                   10835:                        if(max > p) {
                   10836:                                buf[p++] = 0x0a;
                   10837:                        }
                   10838:                        msdos_putch(0x03);
                   10839:                        msdos_putch(0x0d);
                   10840:                        msdos_putch(0x0a);
                   10841:                        break;
                   10842:                } else if(ctrl_break_pressed) {
                   10843:                        // skip this byte
                   10844:                } else if(chr == 0x00) {
                   10845:                        // skip 2nd byte
                   10846:                        msdos_getch();
                   10847:                } else if(chr == 0x0d) {
                   10848:                        // carriage return
                   10849:                        buf[p++] = 0x0d;
                   10850:                        if(max > p) {
                   10851:                                buf[p++] = 0x0a;
                   10852:                        }
                   10853:                        msdos_putch('\n');
                   10854:                        break;
                   10855:                } else if(chr == 0x08) {
                   10856:                        // back space
                   10857:                        if(p > 0) {
                   10858:                                p--;
                   10859:                                if(msdos_ctrl_code_check(buf[p])) {
                   10860:                                        msdos_putch(0x08);
                   10861:                                        msdos_putch(0x08);
                   10862:                                        msdos_putch(0x20);
                   10863:                                        msdos_putch(0x20);
                   10864:                                        msdos_putch(0x08);
                   10865:                                        msdos_putch(0x08);
1.1.1.36  root     10866:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10867:                                        p--;
                   10868:                                        msdos_putch(0x08);
                   10869:                                        msdos_putch(0x08);
                   10870:                                        msdos_putch(0x20);
                   10871:                                        msdos_putch(0x20);
                   10872:                                        msdos_putch(0x08);
                   10873:                                        msdos_putch(0x08);
1.1.1.35  root     10874:                                } else {
                   10875:                                        msdos_putch(0x08);
                   10876:                                        msdos_putch(0x20);
                   10877:                                        msdos_putch(0x08);
                   10878:                                }
                   10879:                        }
                   10880:                } else if(chr == 0x1b) {
                   10881:                        // escape
                   10882:                        while(p > 0) {
                   10883:                                p--;
                   10884:                                if(msdos_ctrl_code_check(buf[p])) {
                   10885:                                        msdos_putch(0x08);
                   10886:                                        msdos_putch(0x08);
                   10887:                                        msdos_putch(0x20);
                   10888:                                        msdos_putch(0x20);
                   10889:                                        msdos_putch(0x08);
                   10890:                                        msdos_putch(0x08);
                   10891:                                } else {
                   10892:                                        msdos_putch(0x08);
                   10893:                                        msdos_putch(0x20);
                   10894:                                        msdos_putch(0x08);
                   10895:                                }
                   10896:                        }
                   10897:                } else {
                   10898:                        buf[p++] = chr;
                   10899:                        msdos_putch(chr);
                   10900:                }
                   10901:        }
                   10902:        REG16(AX) = p;
                   10903:        
                   10904: #ifdef USE_SERVICE_THREAD
                   10905:        service_exit = true;
                   10906: #endif
                   10907:        return(0);
                   10908: }
                   10909: 
1.1       root     10910: inline void msdos_int_21h_3fh()
                   10911: {
                   10912:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10913:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10914:        
1.1.1.20  root     10915:        if(fd < process->max_files && file_handler[fd].valid) {
                   10916:                if(file_mode[file_handler[fd].mode].in) {
                   10917:                        if(file_handler[fd].atty) {
1.1       root     10918:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10919:                                if(REG16(CX) != 0) {
                   10920: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     10921:                                        if(!in_service && !in_service_29h &&
1.1.1.58! root     10922:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     10923:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   10924:                                                // msdos_putch() will be used in this service
                   10925:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   10926:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   10927:                                        } else {
                   10928: #endif
                   10929:                                                msdos_int_21h_3fh_thread(NULL);
                   10930:                                                REQUEST_HARDWRE_UPDATE();
                   10931: #ifdef USE_SERVICE_THREAD
                   10932:                                        }
1.1.1.35  root     10933: #endif
                   10934:                                } else {
                   10935:                                        REG16(AX) = 0;
1.1       root     10936:                                }
                   10937:                        } else {
1.1.1.37  root     10938:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10939:                        }
                   10940:                } else {
                   10941:                        REG16(AX) = 0x05;
1.1.1.3   root     10942:                        m_CF = 1;
1.1       root     10943:                }
                   10944:        } else {
                   10945:                REG16(AX) = 0x06;
1.1.1.3   root     10946:                m_CF = 1;
1.1       root     10947:        }
                   10948: }
                   10949: 
                   10950: inline void msdos_int_21h_40h()
                   10951: {
                   10952:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10953:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10954:        
1.1.1.20  root     10955:        if(fd < process->max_files && file_handler[fd].valid) {
                   10956:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10957:                        if(REG16(CX)) {
1.1.1.20  root     10958:                                if(file_handler[fd].atty) {
1.1       root     10959:                                        // BX is stdout/stderr or is redirected to stdout
                   10960:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10961:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10962:                                        }
                   10963:                                        REG16(AX) = REG16(CX);
                   10964:                                } else {
1.1.1.20  root     10965:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10966:                                }
                   10967:                        } else {
1.1.1.20  root     10968:                                UINT32 pos = _tell(fd);
                   10969:                                _lseek(fd, 0, SEEK_END);
                   10970:                                UINT32 size = _tell(fd);
1.1.1.12  root     10971:                                if(pos < size) {
1.1.1.20  root     10972:                                        _lseek(fd, pos, SEEK_SET);
                   10973:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10974:                                } else {
                   10975:                                        for(UINT32 i = size; i < pos; i++) {
                   10976:                                                UINT8 tmp = 0;
1.1.1.23  root     10977:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10978:                                        }
1.1.1.20  root     10979:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10980:                                }
1.1.1.23  root     10981:                                REG16(AX) = 0;
1.1       root     10982:                        }
                   10983:                } else {
                   10984:                        REG16(AX) = 0x05;
1.1.1.3   root     10985:                        m_CF = 1;
1.1       root     10986:                }
                   10987:        } else {
                   10988:                REG16(AX) = 0x06;
1.1.1.3   root     10989:                m_CF = 1;
1.1       root     10990:        }
                   10991: }
                   10992: 
                   10993: inline void msdos_int_21h_41h(int lfn)
                   10994: {
1.1.1.3   root     10995:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10996:                REG16(AX) = errno;
1.1.1.3   root     10997:                m_CF = 1;
1.1       root     10998:        }
                   10999: }
                   11000: 
                   11001: inline void msdos_int_21h_42h()
                   11002: {
                   11003:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11004:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11005:        
1.1.1.20  root     11006:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11007:                if(REG8(AL) < 0x03) {
1.1.1.35  root     11008:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     11009:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   11010:                        UINT32 pos = _tell(fd);
1.1       root     11011:                        REG16(AX) = pos & 0xffff;
                   11012:                        REG16(DX) = (pos >> 16);
                   11013:                } else {
                   11014:                        REG16(AX) = 0x01;
1.1.1.3   root     11015:                        m_CF = 1;
1.1       root     11016:                }
                   11017:        } else {
                   11018:                REG16(AX) = 0x06;
1.1.1.3   root     11019:                m_CF = 1;
1.1       root     11020:        }
                   11021: }
                   11022: 
                   11023: inline void msdos_int_21h_43h(int lfn)
                   11024: {
1.1.1.45  root     11025:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     11026:        int attr;
                   11027:        
1.1.1.14  root     11028:        if(!lfn && REG8(AL) > 2) {
                   11029:                REG16(AX) = 0x01;
                   11030:                m_CF = 1;
                   11031:                return;
                   11032:        }
                   11033:        switch(REG8(lfn ? BL : AL)) {
1.1       root     11034:        case 0x00:
                   11035:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     11036:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   11037:                } else {
                   11038:                        REG16(AX) = (UINT16)GetLastError();
                   11039:                        m_CF = 1;
                   11040:                }
                   11041:                break;
                   11042:        case 0x01:
                   11043:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   11044:                        REG16(AX) = (UINT16)GetLastError();
                   11045:                        m_CF = 1;
                   11046:                }
                   11047:                break;
                   11048:        case 0x02:
                   11049:                {
1.1.1.45  root     11050:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
                   11051:                        if(compressed_size != INVALID_FILE_SIZE) {
                   11052:                                if(compressed_size != 0) {
                   11053:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   11054:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   11055:                                                file_size = GetFileSize(hFile, NULL);
                   11056:                                                CloseHandle(hFile);
                   11057:                                        }
                   11058:                                        if(compressed_size == file_size) {
                   11059:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11060:                                                // this isn't correct if the file is in the NTFS MFT
                   11061:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   11062:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   11063:                                                }
1.1.1.14  root     11064:                                        }
                   11065:                                }
1.1.1.45  root     11066:                                REG16(AX) = LOWORD(compressed_size);
                   11067:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     11068:                        } else {
                   11069:                                REG16(AX) = (UINT16)GetLastError();
                   11070:                                m_CF = 1;
1.1       root     11071:                        }
1.1.1.14  root     11072:                }
                   11073:                break;
                   11074:        case 0x03:
                   11075:        case 0x05:
                   11076:        case 0x07:
1.1.1.48  root     11077:                if(lfn) {
1.1.1.14  root     11078:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   11079:                        if(hFile != INVALID_HANDLE_VALUE) {
                   11080:                                FILETIME local, time;
                   11081:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   11082:                                if(REG8(BL) == 7) {
                   11083:                                        ULARGE_INTEGER hund;
                   11084:                                        hund.LowPart = local.dwLowDateTime;
                   11085:                                        hund.HighPart = local.dwHighDateTime;
                   11086:                                        hund.QuadPart += REG16(SI) * 100000;
                   11087:                                        local.dwLowDateTime = hund.LowPart;
                   11088:                                        local.dwHighDateTime = hund.HighPart;
                   11089:                                }
                   11090:                                LocalFileTimeToFileTime(&local, &time);
                   11091:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   11092:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   11093:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   11094:                                        REG16(AX) = (UINT16)GetLastError();
                   11095:                                        m_CF = 1;
                   11096:                                }
                   11097:                                CloseHandle(hFile);
                   11098:                        } else {
                   11099:                                REG16(AX) = (UINT16)GetLastError();
                   11100:                                m_CF = 1;
1.1       root     11101:                        }
1.1.1.48  root     11102:                } else {
                   11103:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   11104:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   11105:                        // 214307 DR DOS 6.0 - Set File Owner
                   11106: //                     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));
                   11107:                        REG16(AX) = 0x01;
                   11108:                        m_CF = 1;
1.1.1.14  root     11109:                }
                   11110:                break;
                   11111:        case 0x04:
                   11112:        case 0x06:
                   11113:        case 0x08:
1.1.1.48  root     11114:                if(lfn) {
1.1.1.14  root     11115:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   11116:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   11117:                                FILETIME *time, local;
                   11118:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   11119:                                                   0x06 ? &fad.ftLastAccessTime :
                   11120:                                                          &fad.ftCreationTime;
                   11121:                                FileTimeToLocalFileTime(time, &local);
                   11122:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   11123:                                if(REG8(BL) == 0x08) {
                   11124:                                        ULARGE_INTEGER hund;
                   11125:                                        hund.LowPart = local.dwLowDateTime;
                   11126:                                        hund.HighPart = local.dwHighDateTime;
                   11127:                                        hund.QuadPart /= 100000;
                   11128:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   11129:                                }
                   11130:                        } else {
                   11131:                                REG16(AX) = (UINT16)GetLastError();
                   11132:                                m_CF = 1;
1.1       root     11133:                        }
1.1.1.48  root     11134:                } else {
                   11135:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   11136:                        // 214306 DR DOS 6.0 - Get File Owner
                   11137: //                     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));
                   11138:                        REG16(AX) = 0x01;
                   11139:                        m_CF = 1;
1.1.1.14  root     11140:                }
                   11141:                break;
1.1.1.43  root     11142:        case 0xff:
1.1.1.48  root     11143:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     11144:                        if(REG8(CL) == 0x39) {
                   11145:                                msdos_int_21h_39h(1);
                   11146:                                break;
                   11147:                        } else if(REG8(CL) == 0x56) {
                   11148:                                msdos_int_21h_56h(1);
                   11149:                                break;
                   11150:                        }
                   11151:                }
1.1.1.14  root     11152:        default:
1.1.1.22  root     11153:                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     11154:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     11155:                m_CF = 1;
                   11156:                break;
                   11157:        }
                   11158: }
                   11159: 
                   11160: inline void msdos_int_21h_44h()
                   11161: {
1.1.1.22  root     11162:        static UINT16 iteration_count = 0;
                   11163:        
1.1.1.44  root     11164:        process_t *process;
                   11165:        int fd, drv;
1.1.1.14  root     11166:        
                   11167:        switch(REG8(AL)) {
                   11168:        case 0x00:
                   11169:        case 0x01:
                   11170:        case 0x02:
                   11171:        case 0x03:
                   11172:        case 0x04:
                   11173:        case 0x05:
                   11174:        case 0x06:
                   11175:        case 0x07:
1.1.1.44  root     11176:                process = msdos_process_info_get(current_psp);
                   11177:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11178:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11179:                        REG16(AX) = 0x06;
                   11180:                        m_CF = 1;
                   11181:                        return;
1.1.1.14  root     11182:                }
                   11183:                break;
                   11184:        case 0x08:
                   11185:        case 0x09:
1.1.1.44  root     11186:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11187:                if(!msdos_is_valid_drive(drv)) {
                   11188:                        // invalid drive
1.1.1.14  root     11189:                        REG16(AX) = 0x0f;
                   11190:                        m_CF = 1;
                   11191:                        return;
1.1       root     11192:                }
                   11193:                break;
                   11194:        }
                   11195:        switch(REG8(AL)) {
1.1.1.48  root     11196:        case 0x00: // Get Device Information
1.1.1.20  root     11197:                REG16(DX) = file_handler[fd].info;
1.1       root     11198:                break;
1.1.1.48  root     11199:        case 0x01: // Set Device Information
1.1.1.45  root     11200:                if(REG8(DH) != 0) {
                   11201: //                     REG16(AX) = 0x0d; // data invalid
                   11202: //                     m_CF = 1;
                   11203:                        file_handler[fd].info = REG16(DX);
                   11204:                } else {
                   11205:                        file_handler[fd].info &= 0xff00;
                   11206:                        file_handler[fd].info |= REG8(DL);
                   11207:                }
1.1       root     11208:                break;
1.1.1.48  root     11209:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11210:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11211:                        // from DOSBox
                   11212:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11213:                        case 0x00:
                   11214:                                if(REG16(CX) >= 6) {
                   11215:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11216:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11217:                                        REG16(AX) = 6; // number of bytes actually read
                   11218:                                } else {
                   11219:                                        REG16(AX) = 0x0d; // data invalid
                   11220:                                        m_CF = 1;
                   11221:                                }
                   11222:                                break;
                   11223:                        case 0x01:
                   11224:                                if(REG16(CX) >= 6) {
                   11225:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11226:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11227:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11228:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11229:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11230:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11231:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11232:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11233:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11234:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11235:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11236:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11237:                                                } else {
                   11238:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11239:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11240:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11241:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11242:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11243:                                                }
                   11244:                                        }
                   11245:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11246:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11247:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11248:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11249:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11250:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11251:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11252:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11253:                                        
                   11254:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   11255:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   11256:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11257:                                        REG16(AX) = 6; // number of bytes actually read
                   11258:                                } else {
                   11259:                                        REG16(AX) = 0x0d; // data invalid
                   11260:                                        m_CF = 1;
                   11261:                                }
                   11262:                                break;
                   11263:                        case 0x02:
                   11264:                                if(REG16(CX) >= 2) {
                   11265:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11266:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11267:                                        REG16(AX) = 2; // number of bytes actually read
                   11268:                                } else {
                   11269:                                        REG16(AX) = 0x0d; // data invalid
                   11270:                                        m_CF = 1;
                   11271:                                }
                   11272:                                break;
                   11273:                        case 0x03:
                   11274:                                if(REG16(CX) >= 4) {
                   11275:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11276:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11277:                                        REG16(AX) = 4; // number of bytes actually read
                   11278:                                } else {
                   11279:                                        REG16(AX) = 0x0d; // data invalid
                   11280:                                        m_CF = 1;
                   11281:                                }
                   11282:                                break;
                   11283:                        default:
                   11284:                                REG16(AX) = 0x01; // function number invalid
                   11285:                                m_CF = 1;
                   11286:                        }
                   11287:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11288:                        if(REG16(CX) >= 5) {
                   11289:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11290:                                REG16(AX) = 5; // number of bytes actually read
                   11291:                        } else {
                   11292:                                REG16(AX) = 0x0d; // data invalid
                   11293:                                m_CF = 1;
                   11294:                        }
                   11295:                } else {
                   11296: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11297: //                     REG16(AX) = REG16(CX);
                   11298:                        REG16(AX) = 0x05; // access denied
                   11299:                        m_CF = 1;
                   11300:                }
                   11301:                break;
1.1.1.48  root     11302:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11303: //             REG16(AX) = 0x05;
                   11304: //             m_CF = 1;
                   11305:                REG16(AX) = 0x00; // success
                   11306:                break;
1.1.1.48  root     11307:        case 0x04: // Read From Block Device Control Channel
                   11308:        case 0x05: // Write To Block Device Control Channel
1.1       root     11309:                REG16(AX) = 0x05;
1.1.1.3   root     11310:                m_CF = 1;
1.1       root     11311:                break;
1.1.1.48  root     11312:        case 0x06: // Get Input Status
1.1.1.20  root     11313:                if(file_mode[file_handler[fd].mode].in) {
                   11314:                        if(file_handler[fd].atty) {
1.1.1.14  root     11315:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11316:                        } else {
1.1.1.20  root     11317:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11318:                        }
1.1.1.14  root     11319:                } else {
                   11320:                        REG8(AL) = 0x00;
1.1       root     11321:                }
                   11322:                break;
1.1.1.48  root     11323:        case 0x07: // Get Output Status
1.1.1.20  root     11324:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11325:                        REG8(AL) = 0xff;
                   11326:                } else {
                   11327:                        REG8(AL) = 0x00;
1.1       root     11328:                }
                   11329:                break;
1.1.1.48  root     11330:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11331:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11332:                        // removable drive
                   11333:                        REG16(AX) = 0x00;
1.1       root     11334:                } else {
1.1.1.14  root     11335:                        // fixed drive
                   11336:                        REG16(AX) = 0x01;
1.1       root     11337:                }
                   11338:                break;
1.1.1.48  root     11339:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11340:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11341:                        // remote drive
                   11342:                        REG16(DX) = 0x1000;
1.1.1.44  root     11343:                } else if(msdos_is_subst_drive(drv)) {
                   11344:                        // subst drive
                   11345:                        REG16(DX) = 0x8000;
1.1       root     11346:                } else {
1.1.1.14  root     11347:                        // local drive
1.1.1.44  root     11348:                        REG16(DX) = 0x0000;
1.1       root     11349:                }
                   11350:                break;
1.1.1.48  root     11351:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11352:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11353:                        REG16(DX) = 0x8000;
                   11354:                } else {
                   11355:                        REG16(DX) = 0x0000;
                   11356:                }
1.1.1.21  root     11357:                break;
1.1.1.48  root     11358:        case 0x0b: // Set Sharing Retry Count
1.1       root     11359:                break;
1.1.1.48  root     11360:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11361:                if(REG8(CL) == 0x45) {
                   11362:                        // set iteration (retry) count
                   11363:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11364:                } else if(REG8(CL) == 0x4a) {
                   11365:                        // select code page
                   11366:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11367:                        msdos_nls_tables_update();
1.1.1.44  root     11368:                } else if(REG8(CL) == 0x4c) {
                   11369:                        // start code-page preparation
                   11370:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11371:                        int count = 1, offset = 0;
                   11372:                        if(active_code_page != 437) {
                   11373:                                ids[count++] = active_code_page;
                   11374:                        }
                   11375:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11376:                                ids[count++] = system_code_page;
                   11377:                        }
                   11378:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11379:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11380:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11381:                        for(int i = 0; i < count; i++) {
                   11382:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11383:                        }
                   11384:                } else if(REG8(CL) == 0x4d) {
                   11385:                        // end code-page preparation
                   11386:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11387:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11388:                } else if(REG8(CL) == 0x5f) {
                   11389:                        // set display information
                   11390:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11391:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11392:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11393:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11394:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11395:                                
                   11396:                                if(cur_width != new_width || cur_height != new_height) {
                   11397:                                        pcbios_set_console_size(new_width, new_height, true);
                   11398:                                }
                   11399:                        }
1.1.1.22  root     11400:                } else if(REG8(CL) == 0x65) {
                   11401:                        // get iteration (retry) count
                   11402:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11403:                } else if(REG8(CL) == 0x6a) {
                   11404:                        // query selected code page
                   11405:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11406:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11407:                        
                   11408:                        CPINFO info;
                   11409:                        GetCPInfo(active_code_page, &info);
                   11410:                        
                   11411:                        if(info.MaxCharSize != 1) {
                   11412:                                for(int i = 0;; i++) {
                   11413:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11414:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11415:                                        
                   11416:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11417:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11418:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11419:                                        
                   11420:                                        if(lo == 0 && hi == 0) {
                   11421:                                                break;
                   11422:                                        }
                   11423:                                }
                   11424:                        }
1.1.1.44  root     11425:                } else if(REG8(CL) == 0x6b) {
                   11426:                        // query prepare list
                   11427:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11428:                        int count = 1, offset = 0;
                   11429:                        if(active_code_page != 437) {
                   11430:                                ids[count++] = active_code_page;
                   11431:                        }
                   11432:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11433:                                ids[count++] = system_code_page;
                   11434:                        }
                   11435:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11436:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11437:                        for(int i = 0; i < count; i++) {
                   11438:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11439:                        }
                   11440:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11441:                        for(int i = 0; i < count; i++) {
                   11442:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11443:                        }
1.1.1.22  root     11444:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11445:                        // get display information
1.1.1.50  root     11446:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11447:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11448:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11449:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11450:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11451:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11452:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11453:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11454:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11455:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11456:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11457:                } else {
                   11458:                        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));
                   11459:                        REG16(AX) = 0x01; // invalid function
                   11460:                        m_CF = 1;
                   11461:                }
                   11462:                break;
1.1.1.48  root     11463:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11464:                if(REG8(CL) == 0x40) {
                   11465:                        // set device parameters
1.1.1.48  root     11466: //             } else if(REG8(CL) == 0x41) {
                   11467: //                     // write logical device track
                   11468: //             } else if(REG8(CL) == 0x42) {
                   11469: //                     // format and verify logical device track
1.1.1.22  root     11470:                } else if(REG8(CL) == 0x46) {
                   11471:                        // set volume serial number
1.1.1.48  root     11472:                } else if(REG8(CL) == 0x47) {
                   11473:                        // set access flag
                   11474: //             } else if(REG8(CL) == 0x48) {
                   11475: //                     // set media lock state
                   11476: //             } else if(REG8(CL) == 0x49) {
                   11477: //                     // eject media in drive
1.1.1.22  root     11478:                } else if(REG8(CL) == 0x4a) {
                   11479:                        // lock logical volume
                   11480:                } else if(REG8(CL) == 0x4b) {
                   11481:                        // lock physical volume
                   11482:                } else if(REG8(CL) == 0x60) {
                   11483:                        // get device parameters
1.1.1.42  root     11484:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11485:                        
1.1.1.42  root     11486:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11487:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11488:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11489:                                
                   11490:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11491:                                switch(geo->MediaType) {
                   11492:                                case F5_360_512:
                   11493:                                case F5_320_512:
                   11494:                                case F5_320_1024:
                   11495:                                case F5_180_512:
                   11496:                                case F5_160_512:
                   11497:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11498:                                        break;
                   11499:                                case F5_1Pt2_512:
                   11500:                                case F3_1Pt2_512:
                   11501:                                case F3_1Pt23_1024:
                   11502:                                case F5_1Pt23_1024:
                   11503:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11504:                                        break;
                   11505:                                case F3_720_512:
                   11506:                                case F3_640_512:
                   11507:                                case F5_640_512:
                   11508:                                case F5_720_512:
                   11509:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11510:                                        break;
                   11511:                                case F8_256_128:
                   11512:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11513:                                        break;
                   11514:                                case FixedMedia:
                   11515:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11516:                                        break;
                   11517:                                case F3_1Pt44_512:
                   11518:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11519:                                        break;
                   11520:                                case F3_2Pt88_512:
                   11521:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11522:                                        break;
                   11523:                                default:
                   11524:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11525: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11526:                                        break;
1.1.1.22  root     11527:                                }
1.1.1.42  root     11528:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11529:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11530:                                switch(geo->MediaType) {
                   11531:                                case F5_360_512:
                   11532:                                case F5_320_512:
                   11533:                                case F5_320_1024:
                   11534:                                case F5_180_512:
                   11535:                                case F5_160_512:
                   11536:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11537:                                        break;
                   11538:                                default:
                   11539:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11540:                                        break;
                   11541:                                }
                   11542:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11543:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11544:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11545:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11546:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11547:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11548:                                switch(geo->MediaType) {
                   11549:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11550:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11551:                                        break;
                   11552:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11553:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11554:                                        break;
                   11555:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11556:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11557:                                        break;
                   11558:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11559:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11560:                                        break;
                   11561:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11562:                                case F3_1Pt2_512:
                   11563:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11564:                                case F5_720_512:
                   11565:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11566:                                        break;
                   11567:                                case FixedMedia:        // hard disk
                   11568:                                case RemovableMedia:
                   11569:                                case Unknown:
                   11570:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11571:                                        break;
                   11572:                                default:
                   11573:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11574:                                        break;
                   11575:                                }
                   11576:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11577:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11578:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11579:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11580:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11581:                                // 21h  BYTE    device type
                   11582:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11583:                        } else {
                   11584:                                REG16(AX) = 0x0f; // invalid drive
                   11585:                                m_CF = 1;
                   11586:                        }
1.1.1.48  root     11587: //             } else if(REG8(CL) == 0x61) {
                   11588: //                     // read logical device track
                   11589: //             } else if(REG8(CL) == 0x62) {
                   11590: //                     // verify logical device track
1.1.1.22  root     11591:                } else if(REG8(CL) == 0x66) {
                   11592:                        // get volume serial number
                   11593:                        char path[] = "A:\\";
                   11594:                        char volume_label[MAX_PATH];
                   11595:                        DWORD serial_number = 0;
                   11596:                        char file_system[MAX_PATH];
                   11597:                        
                   11598:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11599:                        
                   11600:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11601:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11602:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11603:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11604:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11605:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11606:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11607:                        } else {
                   11608:                                REG16(AX) = 0x0f; // invalid drive
                   11609:                                m_CF = 1;
                   11610:                        }
                   11611:                } else if(REG8(CL) == 0x67) {
                   11612:                        // get access flag
                   11613:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11614:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11615:                } else if(REG8(CL) == 0x68) {
                   11616:                        // sense media type
1.1.1.42  root     11617:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11618:                        
1.1.1.42  root     11619:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11620:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11621:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11622:                                
                   11623:                                switch(geo->MediaType) {
                   11624:                                case F3_720_512:
                   11625:                                case F5_720_512:
                   11626:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11627:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11628:                                        break;
                   11629:                                case F3_1Pt44_512:
                   11630:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11631:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11632:                                        break;
                   11633:                                case F3_2Pt88_512:
                   11634:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11635:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11636:                                        break;
                   11637:                                default:
                   11638:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11639:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11640:                                        break;
1.1.1.22  root     11641:                                }
                   11642:                        } else {
                   11643:                                REG16(AX) = 0x0f; // invalid drive
                   11644:                                m_CF = 1;
                   11645:                        }
                   11646:                } else if(REG8(CL) == 0x6a) {
                   11647:                        // unlock logical volume
                   11648:                } else if(REG8(CL) == 0x6b) {
                   11649:                        // unlock physical volume
1.1.1.48  root     11650: //             } else if(REG8(CL) == 0x6c) {
                   11651: //                     // get lock flag
                   11652: //             } else if(REG8(CL) == 0x6d) {
                   11653: //                     // enumerate open files
                   11654: //             } else if(REG8(CL) == 0x6e) {
                   11655: //                     // find swap file
                   11656: //             } else if(REG8(CL) == 0x6f) {
                   11657: //                     // get drive map information
                   11658: //             } else if(REG8(CL) == 0x70) {
                   11659: //                     // get current lock state
                   11660: //             } else if(REG8(CL) == 0x71) {
                   11661: //                     // get first cluster
1.1.1.22  root     11662:                } else {
                   11663:                        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));
                   11664:                        REG16(AX) = 0x01; // invalid function
                   11665:                        m_CF = 1;
                   11666:                }
                   11667:                break;
1.1.1.48  root     11668:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11669:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11670:                        REG16(AX) = 0x0f; // invalid drive
                   11671:                        m_CF = 1;
                   11672:                } else {
                   11673:                        REG8(AL) = 0;
1.1.1.22  root     11674:                }
                   11675:                break;
1.1.1.48  root     11676:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11677:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11678:                        REG16(AX) = 0x0f; // invalid drive
                   11679:                        m_CF = 1;
1.1.1.22  root     11680:                }
                   11681:                break;
1.1.1.48  root     11682:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11683:                switch(REG8(CL)) {
                   11684:                case 0x45:
                   11685:                case 0x4a:
1.1.1.48  root     11686:                case 0x4c:
                   11687:                case 0x4d:
1.1.1.22  root     11688:                case 0x65:
                   11689:                case 0x6a:
1.1.1.48  root     11690:                case 0x6b:
1.1.1.22  root     11691:                case 0x7f:
                   11692:                        REG16(AX) = 0x0000; // supported
                   11693:                        break;
                   11694:                default:
                   11695:                        REG8(AL) = 0x01; // ioctl capability not available
                   11696:                        m_CF = 1;
                   11697:                        break;
                   11698:                }
                   11699:                break;
1.1.1.48  root     11700:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     11701:                switch(REG8(CL)) {
                   11702:                case 0x40:
                   11703:                case 0x46:
                   11704:                case 0x4a:
                   11705:                case 0x4b:
                   11706:                case 0x60:
                   11707:                case 0x66:
                   11708:                case 0x67:
                   11709:                case 0x68:
                   11710:                case 0x6a:
                   11711:                case 0x6b:
1.1.1.48  root     11712:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   11713:                                // CH = 00h     Unknown
                   11714:                                // CH = 01h     COMn:
                   11715:                                // CH = 03h     CON
                   11716:                                // CH = 05h     LPTn:
                   11717:                                REG16(AX) = 0x0000; // supported
                   11718:                                break;
                   11719:                        }
1.1.1.22  root     11720:                default:
                   11721:                        REG8(AL) = 0x01; // ioctl capability not available
                   11722:                        m_CF = 1;
                   11723:                        break;
                   11724:                }
                   11725:                break;
1.1.1.48  root     11726:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   11727:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   11728:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   11729:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   11730:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   11731:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   11732:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   11733:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   11734:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   11735:        case 0x59: // DR Multiuser DOS 5.0 - API
                   11736:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     11737:                m_CF = 1;
                   11738:                break;
1.1       root     11739:        default:
1.1.1.22  root     11740:                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     11741:                REG16(AX) = 0x01;
1.1.1.3   root     11742:                m_CF = 1;
1.1       root     11743:                break;
                   11744:        }
                   11745: }
                   11746: 
                   11747: inline void msdos_int_21h_45h()
                   11748: {
                   11749:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11750:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11751:        
1.1.1.20  root     11752:        if(fd < process->max_files && file_handler[fd].valid) {
                   11753:                int dup_fd = _dup(fd);
                   11754:                if(dup_fd != -1) {
                   11755:                        REG16(AX) = dup_fd;
                   11756:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11757: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11758:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11759:                } else {
                   11760:                        REG16(AX) = errno;
1.1.1.3   root     11761:                        m_CF = 1;
1.1       root     11762:                }
                   11763:        } else {
                   11764:                REG16(AX) = 0x06;
1.1.1.3   root     11765:                m_CF = 1;
1.1       root     11766:        }
                   11767: }
                   11768: 
                   11769: inline void msdos_int_21h_46h()
                   11770: {
                   11771:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11772:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11773:        int dup_fd = REG16(CX);
                   11774:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11775:        
1.1.1.20  root     11776:        if(REG16(BX) == REG16(CX)) {
                   11777:                REG16(AX) = 0x06;
                   11778:                m_CF = 1;
                   11779:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11780:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11781:                        _close(tmp_fd);
                   11782:                        msdos_file_handler_close(tmp_fd);
                   11783:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11784:                }
                   11785:                if(_dup2(fd, dup_fd) != -1) {
                   11786:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11787: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11788:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11789:                } else {
                   11790:                        REG16(AX) = errno;
1.1.1.3   root     11791:                        m_CF = 1;
1.1       root     11792:                }
                   11793:        } else {
                   11794:                REG16(AX) = 0x06;
1.1.1.3   root     11795:                m_CF = 1;
1.1       root     11796:        }
                   11797: }
                   11798: 
                   11799: inline void msdos_int_21h_47h(int lfn)
                   11800: {
                   11801:        char path[MAX_PATH];
                   11802:        
                   11803:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     11804:                if(!lfn) {
                   11805:                        strcpy(path, msdos_short_path(path));
                   11806:                }
1.1       root     11807:                if(path[1] == ':') {
                   11808:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     11809:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11810:                } else {
1.1.1.45  root     11811:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11812:                }
                   11813:        } else {
                   11814:                REG16(AX) = errno;
1.1.1.3   root     11815:                m_CF = 1;
1.1       root     11816:        }
                   11817: }
                   11818: 
                   11819: inline void msdos_int_21h_48h()
                   11820: {
1.1.1.19  root     11821:        int seg, umb_linked;
1.1       root     11822:        
1.1.1.8   root     11823:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11824:                // unlink umb not to allocate memory in umb
                   11825:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11826:                        msdos_mem_unlink_umb();
                   11827:                }
1.1.1.8   root     11828:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11829:                        REG16(AX) = seg;
                   11830:                } else {
                   11831:                        REG16(AX) = 0x08;
                   11832:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11833:                        m_CF = 1;
                   11834:                }
1.1.1.19  root     11835:                if(umb_linked != 0) {
                   11836:                        msdos_mem_link_umb();
                   11837:                }
1.1.1.8   root     11838:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11839:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11840:                        REG16(AX) = seg;
                   11841:                } else {
                   11842:                        REG16(AX) = 0x08;
                   11843:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11844:                        m_CF = 1;
                   11845:                }
                   11846:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11847:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11848:                        REG16(AX) = seg;
                   11849:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11850:                        REG16(AX) = seg;
                   11851:                } else {
                   11852:                        REG16(AX) = 0x08;
                   11853:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11854:                        m_CF = 1;
                   11855:                }
1.1       root     11856:        }
                   11857: }
                   11858: 
                   11859: inline void msdos_int_21h_49h()
                   11860: {
1.1.1.14  root     11861:        int mcb_seg = SREG(ES) - 1;
                   11862:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11863:        
                   11864:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11865:                msdos_mem_free(SREG(ES));
                   11866:        } else {
1.1.1.33  root     11867:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11868:                m_CF = 1;
                   11869:        }
1.1       root     11870: }
                   11871: 
                   11872: inline void msdos_int_21h_4ah()
                   11873: {
1.1.1.14  root     11874:        int mcb_seg = SREG(ES) - 1;
                   11875:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11876:        int max_paragraphs;
                   11877:        
1.1.1.14  root     11878:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11879:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11880:                        REG16(AX) = 0x08;
                   11881:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11882:                        m_CF = 1;
                   11883:                }
                   11884:        } else {
1.1.1.33  root     11885:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11886:                m_CF = 1;
1.1       root     11887:        }
                   11888: }
                   11889: 
                   11890: inline void msdos_int_21h_4bh()
                   11891: {
1.1.1.3   root     11892:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11893:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11894:        
                   11895:        switch(REG8(AL)) {
                   11896:        case 0x00:
                   11897:        case 0x01:
                   11898:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11899:                        REG16(AX) = 0x02;
1.1.1.3   root     11900:                        m_CF = 1;
1.1       root     11901:                }
                   11902:                break;
1.1.1.14  root     11903:        case 0x03:
                   11904:                {
                   11905:                        int fd;
                   11906:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11907:                                REG16(AX) = 0x02;
                   11908:                                m_CF = 1;
                   11909:                                break;
                   11910:                        }
                   11911:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11912:                        _close(fd);
                   11913:                        
                   11914:                        UINT16 *overlay = (UINT16 *)param;
                   11915:                        
                   11916:                        // check exe header
                   11917:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11918:                        int header_size = 0;
                   11919:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11920:                                header_size = header->header_size * 16;
                   11921:                                // relocation
                   11922:                                int start_seg = overlay[1];
                   11923:                                for(int i = 0; i < header->relocations; i++) {
                   11924:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11925:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11926:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11927:                                }
                   11928:                        }
                   11929:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11930:                }
                   11931:                break;
1.1.1.48  root     11932:        case 0x04:
                   11933:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   11934: //     case 0x05:
                   11935: //             // DOS 5+ - Set Execution State
                   11936:        case 0x80:
                   11937:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   11938:        case 0xf0:
                   11939:        case 0xf1:
                   11940:                // DIET v1.10+
1.1.1.43  root     11941:        case 0xfd:
                   11942:        case 0xfe:
                   11943:                // unknown function called in FreeCOM
                   11944:                REG16(AX) = 0x01;
                   11945:                m_CF = 1;
                   11946:                break;
1.1       root     11947:        default:
1.1.1.22  root     11948:                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     11949:                REG16(AX) = 0x01;
1.1.1.3   root     11950:                m_CF = 1;
1.1       root     11951:                break;
                   11952:        }
                   11953: }
                   11954: 
                   11955: inline void msdos_int_21h_4ch()
                   11956: {
                   11957:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11958: }
                   11959: 
                   11960: inline void msdos_int_21h_4dh()
                   11961: {
                   11962:        REG16(AX) = retval;
                   11963: }
                   11964: 
                   11965: inline void msdos_int_21h_4eh()
                   11966: {
                   11967:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11968:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11969:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     11970:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11971:        WIN32_FIND_DATA fd;
                   11972:        
1.1.1.14  root     11973:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11974:        find->find_magic = FIND_MAGIC;
                   11975:        find->dta_index = dtainfo - dtalist;
1.1       root     11976:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11977:        dtainfo->allowable_mask = REG8(CL);
1.1.1.58! root     11978:        // note: SO1 dir command sets 0x3f, but only directories and volue label are found if bit3 is set :-(
        !          11979:        if((dtainfo->allowable_mask & 0x3f) == 0x3f) {
        !          11980:                dtainfo->allowable_mask &= ~0x08;
        !          11981:        }
1.1.1.14  root     11982:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11983:        
1.1.1.14  root     11984:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11985:                dtainfo->allowable_mask &= ~8;
1.1       root     11986:        }
1.1.1.14  root     11987:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11988:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11989:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11990:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11991:                                FindClose(dtainfo->find_handle);
                   11992:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11993:                                break;
                   11994:                        }
                   11995:                }
                   11996:        }
1.1.1.13  root     11997:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11998:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11999:                msdos_find_file_conv_local_time(&fd);
                   12000:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12001:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12002:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12003:                REG16(AX) = 0;
1.1.1.14  root     12004:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12005:                find->attrib = 8;
                   12006:                find->size = 0;
                   12007:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12008:                dtainfo->allowable_mask &= ~8;
1.1       root     12009:                REG16(AX) = 0;
                   12010:        } else {
                   12011:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12012:                m_CF = 1;
1.1       root     12013:        }
                   12014: }
                   12015: 
                   12016: inline void msdos_int_21h_4fh()
                   12017: {
                   12018:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12019:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12020:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     12021:        WIN32_FIND_DATA fd;
                   12022:        
1.1.1.14  root     12023:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   12024:                REG16(AX) = 0x12;
                   12025:                m_CF = 1;
                   12026:                return;
                   12027:        }
                   12028:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     12029:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12030:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12031:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12032:                              !msdos_find_file_has_8dot3name(&fd)) {
                   12033:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12034:                                        FindClose(dtainfo->find_handle);
                   12035:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12036:                                        break;
                   12037:                                }
                   12038:                        }
                   12039:                } else {
1.1.1.13  root     12040:                        FindClose(dtainfo->find_handle);
                   12041:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12042:                }
                   12043:        }
1.1.1.13  root     12044:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12045:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12046:                msdos_find_file_conv_local_time(&fd);
                   12047:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12048:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12049:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12050:                REG16(AX) = 0;
1.1.1.14  root     12051:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12052:                find->attrib = 8;
                   12053:                find->size = 0;
                   12054:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12055:                dtainfo->allowable_mask &= ~8;
1.1       root     12056:                REG16(AX) = 0;
                   12057:        } else {
                   12058:                REG16(AX) = 0x12;
1.1.1.3   root     12059:                m_CF = 1;
1.1       root     12060:        }
                   12061: }
                   12062: 
                   12063: inline void msdos_int_21h_50h()
                   12064: {
1.1.1.8   root     12065:        if(current_psp != REG16(BX)) {
                   12066:                process_t *process = msdos_process_info_get(current_psp);
                   12067:                if(process != NULL) {
                   12068:                        process->psp = REG16(BX);
                   12069:                }
                   12070:                current_psp = REG16(BX);
1.1.1.23  root     12071:                msdos_sda_update(current_psp);
1.1.1.8   root     12072:        }
1.1       root     12073: }
                   12074: 
                   12075: inline void msdos_int_21h_51h()
                   12076: {
                   12077:        REG16(BX) = current_psp;
                   12078: }
                   12079: 
                   12080: inline void msdos_int_21h_52h()
                   12081: {
1.1.1.25  root     12082:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     12083:        i386_load_segment_descriptor(ES);
1.1.1.25  root     12084:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     12085: }
                   12086: 
1.1.1.43  root     12087: inline void msdos_int_21h_53h()
                   12088: {
                   12089:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   12090:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   12091:        
                   12092:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   12093:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   12094:        dpb->shift_count = 0;
                   12095:        dpb->reserved_sectors = 0;
                   12096:        dpb->fat_num = bpb->fat_num;
                   12097:        dpb->root_entries = bpb->root_entries;
                   12098:        dpb->first_data_sector = 0;
                   12099:        if(bpb->sectors_per_cluster != 0) {
                   12100:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   12101:        } else {
                   12102:                dpb->highest_cluster_num = 0;
                   12103:        }
                   12104:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   12105:        dpb->first_dir_sector = 0;
                   12106:        dpb->device_driver_header = 0;
                   12107:        dpb->media_type = bpb->media_type;
                   12108:        dpb->drive_accessed = 0;
                   12109:        dpb->next_dpb_ofs = 0xffff;
                   12110:        dpb->next_dpb_seg = 0xffff;
                   12111:        dpb->first_free_cluster = 0;
                   12112:        dpb->free_clusters = 0xffff;
                   12113: }
                   12114: 
1.1       root     12115: inline void msdos_int_21h_54h()
                   12116: {
                   12117:        process_t *process = msdos_process_info_get(current_psp);
                   12118:        
                   12119:        REG8(AL) = process->verify;
                   12120: }
                   12121: 
                   12122: inline void msdos_int_21h_55h()
                   12123: {
                   12124:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   12125:        
                   12126:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   12127:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   12128:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   12129:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   12130:        psp->parent_psp = current_psp;
                   12131: }
                   12132: 
                   12133: inline void msdos_int_21h_56h(int lfn)
                   12134: {
                   12135:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     12136:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   12137:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     12138:        
                   12139:        if(rename(src, dst)) {
                   12140:                REG16(AX) = errno;
1.1.1.3   root     12141:                m_CF = 1;
1.1       root     12142:        }
                   12143: }
                   12144: 
                   12145: inline void msdos_int_21h_57h()
                   12146: {
                   12147:        FILETIME time, local;
1.1.1.14  root     12148:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     12149:        HANDLE hHandle;
1.1       root     12150:        
1.1.1.21  root     12151:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     12152:                REG16(AX) = (UINT16)GetLastError();
                   12153:                m_CF = 1;
                   12154:                return;
                   12155:        }
                   12156:        ctime = atime = mtime = NULL;
                   12157:        
1.1       root     12158:        switch(REG8(AL)) {
                   12159:        case 0x00:
1.1.1.6   root     12160:        case 0x01:
1.1.1.14  root     12161:                mtime = &time;
1.1.1.6   root     12162:                break;
                   12163:        case 0x04:
                   12164:        case 0x05:
1.1.1.14  root     12165:                atime = &time;
1.1       root     12166:                break;
1.1.1.6   root     12167:        case 0x06:
                   12168:        case 0x07:
1.1.1.14  root     12169:                ctime = &time;
                   12170:                break;
                   12171:        default:
1.1.1.22  root     12172:                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     12173:                REG16(AX) = 0x01;
                   12174:                m_CF = 1;
                   12175:                return;
                   12176:        }
                   12177:        if(REG8(AL) & 1) {
1.1       root     12178:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12179:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12180:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12181:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12182:                        m_CF = 1;
1.1       root     12183:                }
1.1.1.14  root     12184:        } else {
1.1.1.21  root     12185:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12186:                        // assume a device and use the current time
                   12187:                        GetSystemTimeAsFileTime(&time);
                   12188:                }
                   12189:                FileTimeToLocalFileTime(&time, &local);
                   12190:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12191:        }
                   12192: }
                   12193: 
                   12194: inline void msdos_int_21h_58h()
                   12195: {
                   12196:        switch(REG8(AL)) {
                   12197:        case 0x00:
1.1.1.7   root     12198:                REG16(AX) = malloc_strategy;
                   12199:                break;
                   12200:        case 0x01:
1.1.1.24  root     12201: //             switch(REG16(BX)) {
                   12202:                switch(REG8(BL)) {
1.1.1.7   root     12203:                case 0x0000:
                   12204:                case 0x0001:
                   12205:                case 0x0002:
                   12206:                case 0x0040:
                   12207:                case 0x0041:
                   12208:                case 0x0042:
                   12209:                case 0x0080:
                   12210:                case 0x0081:
                   12211:                case 0x0082:
                   12212:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12213:                        msdos_sda_update(current_psp);
1.1.1.7   root     12214:                        break;
                   12215:                default:
1.1.1.22  root     12216:                        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     12217:                        REG16(AX) = 0x01;
                   12218:                        m_CF = 1;
                   12219:                        break;
                   12220:                }
                   12221:                break;
                   12222:        case 0x02:
1.1.1.19  root     12223:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12224:                break;
                   12225:        case 0x03:
1.1.1.24  root     12226: //             switch(REG16(BX)) {
                   12227:                switch(REG8(BL)) {
1.1.1.7   root     12228:                case 0x0000:
1.1.1.19  root     12229:                        msdos_mem_unlink_umb();
                   12230:                        break;
1.1.1.7   root     12231:                case 0x0001:
1.1.1.19  root     12232:                        msdos_mem_link_umb();
1.1.1.7   root     12233:                        break;
                   12234:                default:
1.1.1.22  root     12235:                        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     12236:                        REG16(AX) = 0x01;
                   12237:                        m_CF = 1;
                   12238:                        break;
                   12239:                }
1.1       root     12240:                break;
                   12241:        default:
1.1.1.22  root     12242:                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     12243:                REG16(AX) = 0x01;
1.1.1.3   root     12244:                m_CF = 1;
1.1       root     12245:                break;
                   12246:        }
                   12247: }
                   12248: 
                   12249: inline void msdos_int_21h_59h()
                   12250: {
1.1.1.47  root     12251:        if(REG16(BX) == 0x0000) {
                   12252:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12253:                
                   12254:                REG16(AX) = sda->extended_error_code;
                   12255:                REG8(BH)  = sda->error_class;
                   12256:                REG8(BL)  = sda->suggested_action;
                   12257:                REG8(CH)  = sda->locus_of_last_error;
                   12258:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12259:                if(sda->int21h_5d0ah_called != 0) {
                   12260:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12261:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12262: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12263:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12264: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12265: //                     i386_load_segment_descriptor(DS);
                   12266:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12267:                        i386_load_segment_descriptor(ES);
                   12268:                }
                   12269:                sda->int21h_5d0ah_called = 0;
                   12270: //     } else if(REG16(BX) == 0x0001) {
                   12271: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12272:        } else {
                   12273:                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));
                   12274:                REG16(AX) = 0x01;
                   12275:                m_CF = 1;
                   12276:        }
1.1       root     12277: }
                   12278: 
                   12279: inline void msdos_int_21h_5ah()
                   12280: {
1.1.1.3   root     12281:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12282:        int len = strlen(path);
                   12283:        char tmp[MAX_PATH];
                   12284:        
                   12285:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   12286:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12287:                
                   12288:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12289:                REG16(AX) = fd;
                   12290:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12291:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12292:                
                   12293:                strcpy(path, tmp);
                   12294:                int dx = REG16(DX) + len;
1.1.1.3   root     12295:                int ds = SREG(DS);
1.1       root     12296:                while(dx > 0xffff) {
                   12297:                        dx -= 0x10;
                   12298:                        ds++;
                   12299:                }
                   12300:                REG16(DX) = dx;
1.1.1.3   root     12301:                SREG(DS) = ds;
                   12302:                i386_load_segment_descriptor(DS);
1.1       root     12303:        } else {
                   12304:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12305:                m_CF = 1;
1.1       root     12306:        }
                   12307: }
                   12308: 
                   12309: inline void msdos_int_21h_5bh()
                   12310: {
1.1.1.45  root     12311:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12312:        
1.1.1.45  root     12313: //     if(msdos_is_existing_file(path)) {
                   12314:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12315:                // already exists
                   12316:                REG16(AX) = 0x50;
1.1.1.3   root     12317:                m_CF = 1;
1.1       root     12318:        } else {
                   12319:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12320:                
                   12321:                if(fd != -1) {
                   12322:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12323:                        REG16(AX) = fd;
                   12324:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12325:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12326:                } else {
                   12327:                        REG16(AX) = errno;
1.1.1.3   root     12328:                        m_CF = 1;
1.1       root     12329:                }
                   12330:        }
                   12331: }
                   12332: 
                   12333: inline void msdos_int_21h_5ch()
                   12334: {
                   12335:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12336:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12337:        
1.1.1.20  root     12338:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12339:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12340:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12341:                        UINT32 pos = _tell(fd);
                   12342:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12343:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12344:                                REG16(AX) = errno;
1.1.1.3   root     12345:                                m_CF = 1;
1.1       root     12346:                        }
1.1.1.20  root     12347:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12348:                        
1.1       root     12349:                        // some seconds may be passed in _locking()
1.1.1.35  root     12350:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12351:                } else {
                   12352:                        REG16(AX) = 0x01;
1.1.1.3   root     12353:                        m_CF = 1;
1.1       root     12354:                }
                   12355:        } else {
                   12356:                REG16(AX) = 0x06;
1.1.1.3   root     12357:                m_CF = 1;
1.1       root     12358:        }
                   12359: }
                   12360: 
1.1.1.22  root     12361: inline void msdos_int_21h_5dh()
                   12362: {
                   12363:        switch(REG8(AL)) {
1.1.1.45  root     12364:        case 0x00:
                   12365:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12366:                        // current system
                   12367:                        static bool reenter = false;
                   12368:                        if(!reenter) {
                   12369:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12370:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12371:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12372:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12373:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12374:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12375:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12376:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12377:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12378:                                i386_load_segment_descriptor(DS);
                   12379:                                i386_load_segment_descriptor(ES);
                   12380:                                reenter = true;
                   12381:                                try {
                   12382:                                        msdos_syscall(0x21);
                   12383:                                } catch(...) {
                   12384:                                }
                   12385:                                reenter = false;
                   12386:                        }
                   12387:                } else {
                   12388:                        REG16(AX) = 0x49; //  network software not installed
                   12389:                        m_CF = 1;
                   12390:                }
                   12391:                break;
1.1.1.22  root     12392:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     12393:                SREG(DS) = (SDA_TOP >> 4);
                   12394:                i386_load_segment_descriptor(DS);
                   12395:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12396:                REG16(CX) = 0x80;
                   12397:                REG16(DX) = 0x1a;
                   12398:                break;
1.1.1.45  root     12399:        case 0x07: // get redirected printer mode
                   12400:        case 0x08: // set redirected printer mode
                   12401:        case 0x09: // flush redirected printer output
                   12402:                REG16(AX) = 0x49; //  network software not installed
                   12403:                m_CF = 1;
                   12404:                break;
1.1.1.43  root     12405:        case 0x0a: // set extended error information
                   12406:                {
                   12407:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12408:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12409:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12410:                        // XXX: which one is correct ???
                   12411: #if 1
                   12412:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12413:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12414:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12415:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12416:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12417: #else
                   12418:                        // PC DOS 7 Technical Update
                   12419:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12420:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12421:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12422:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12423: #endif
                   12424:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12425: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12426:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12427: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12428:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12429:                }
                   12430:                break;
1.1.1.23  root     12431:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12432:                REG16(AX) = 0x01;
                   12433:                m_CF = 1;
                   12434:                break;
                   12435:        default:
                   12436:                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));
                   12437:                REG16(AX) = 0x01;
                   12438:                m_CF = 1;
                   12439:                break;
                   12440:        }
                   12441: }
                   12442: 
1.1.1.42  root     12443: inline void msdos_int_21h_5eh()
                   12444: {
                   12445:        switch(REG8(AL)) {
                   12446:        case 0x00:
                   12447:                {
                   12448:                        char name[256] = {0};
                   12449:                        DWORD dwSize = 256;
                   12450:                        
                   12451:                        if(GetComputerName(name, &dwSize)) {
                   12452:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12453:                                for(int i = 0; i < 15; i++) {
                   12454:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12455:                                }
                   12456:                                dest[15] = '\0';
                   12457:                                REG8(CH) = 0x01; // nonzero valid
                   12458:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12459:                        } else {
                   12460:                                REG16(AX) = 0x01;
                   12461:                                m_CF = 1;
                   12462:                        }
                   12463:                }
                   12464:                break;
                   12465:        default:
1.1.1.45  root     12466: //             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));
                   12467: //             REG16(AX) = 0x01;
                   12468:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12469:                m_CF = 1;
                   12470:                break;
                   12471:        }
                   12472: }
                   12473: 
1.1.1.30  root     12474: inline void msdos_int_21h_5fh()
                   12475: {
                   12476:        switch(REG8(AL)) {
1.1.1.42  root     12477:        case 0x05:
1.1.1.44  root     12478:                REG16(BP) = 0;
                   12479:                for(int i = 0; i < 26; i++) {
                   12480:                        if(msdos_is_remote_drive(i)) {
                   12481:                                REG16(BP)++;
1.1.1.42  root     12482:                        }
                   12483:                }
1.1.1.30  root     12484:        case 0x02:
1.1.1.44  root     12485:                for(int i = 0, index = 0; i < 26; i++) {
                   12486:                        if(msdos_is_remote_drive(i)) {
                   12487:                                if(index == REG16(BX)) {
                   12488:                                        char volume[] = "A:";
1.1.1.30  root     12489:                                        volume[0] = 'A' + i;
1.1.1.44  root     12490:                                        DWORD dwSize = 128;
                   12491:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   12492:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   12493:                                        REG8(BH) = 0x00; // valid
                   12494:                                        REG8(BL) = 0x04; // disk drive
                   12495:                                        REG16(CX) = 0x00; // LANtastic
                   12496:                                        return;
1.1.1.30  root     12497:                                }
1.1.1.44  root     12498:                                index++;
1.1.1.30  root     12499:                        }
                   12500:                }
                   12501:                REG16(AX) = 0x12; // no more files
                   12502:                m_CF = 1;
                   12503:                break;
1.1.1.44  root     12504:        case 0x07:
                   12505:                if(msdos_is_valid_drive(REG8(DL))) {
                   12506:                        msdos_cds_update(REG8(DL));
                   12507:                } else {
                   12508:                        REG16(AX) = 0x0f; // invalid drive
                   12509:                        m_CF = 1;
                   12510:                }
                   12511:                break;
                   12512:        case 0x08:
                   12513:                if(msdos_is_valid_drive(REG8(DL))) {
                   12514:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12515:                        cds->drive_attrib = 0x0000;
                   12516:                } else {
                   12517:                        REG16(AX) = 0x0f; // invalid drive
                   12518:                        m_CF = 1;
                   12519:                }
                   12520:                break;
1.1.1.30  root     12521:        default:
1.1.1.45  root     12522: //             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));
                   12523: //             REG16(AX) = 0x01;
                   12524:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12525:                m_CF = 1;
                   12526:                break;
                   12527:        }
                   12528: }
                   12529: 
1.1       root     12530: inline void msdos_int_21h_60h(int lfn)
                   12531: {
1.1.1.45  root     12532:        char full[MAX_PATH];
                   12533:        const char *path = NULL;
1.1.1.14  root     12534:        
1.1       root     12535:        if(lfn) {
1.1.1.14  root     12536:                char *name;
                   12537:                *full = '\0';
1.1.1.3   root     12538:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12539:                switch(REG8(CL)) {
                   12540:                case 1:
                   12541:                        GetShortPathName(full, full, MAX_PATH);
                   12542:                        my_strupr(full);
                   12543:                        break;
                   12544:                case 2:
                   12545:                        GetLongPathName(full, full, MAX_PATH);
                   12546:                        break;
                   12547:                }
                   12548:                path = full;
                   12549:        } else {
                   12550:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12551:        }
                   12552:        if(*path != '\0') {
                   12553:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12554:        } else {
1.1.1.14  root     12555:                REG16(AX) = (UINT16)GetLastError();
                   12556:                m_CF = 1;
1.1       root     12557:        }
                   12558: }
                   12559: 
                   12560: inline void msdos_int_21h_61h()
                   12561: {
                   12562:        REG8(AL) = 0;
                   12563: }
                   12564: 
                   12565: inline void msdos_int_21h_62h()
                   12566: {
                   12567:        REG16(BX) = current_psp;
                   12568: }
                   12569: 
                   12570: inline void msdos_int_21h_63h()
                   12571: {
                   12572:        switch(REG8(AL)) {
                   12573:        case 0x00:
1.1.1.3   root     12574:                SREG(DS) = (DBCS_TABLE >> 4);
                   12575:                i386_load_segment_descriptor(DS);
1.1       root     12576:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12577:                REG8(AL) = 0x00;
                   12578:                break;
1.1.1.22  root     12579:        case 0x01: // set korean input mode
                   12580:        case 0x02: // get korean input mode
                   12581:                REG8(AL) = 0xff; // not supported
                   12582:                break;
1.1       root     12583:        default:
1.1.1.22  root     12584:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     12585:                REG16(AX) = 0x01;
1.1.1.3   root     12586:                m_CF = 1;
1.1       root     12587:                break;
                   12588:        }
                   12589: }
                   12590: 
1.1.1.25  root     12591: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12592: {
1.1.1.25  root     12593:        switch(func) {
1.1.1.17  root     12594:        case 0x01:
                   12595:                if(REG16(CX) >= 5) {
1.1.1.19  root     12596:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12597:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12598:                                REG16(CX) = sizeof(data);
                   12599:                        ZeroMemory(data, sizeof(data));
                   12600:                        data[0] = 0x01;
                   12601:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12602:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12603:                        *(UINT16 *)(data + 5) = active_code_page;
                   12604:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12605: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12606:                } else {
1.1.1.25  root     12607:                        return(0x08); // insufficient memory
1.1.1.17  root     12608:                }
                   12609:                break;
                   12610:        case 0x02:
                   12611:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12612:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12613:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12614: //             REG16(AX) = active_code_page;
1.1.1.17  root     12615:                REG16(CX) = 0x05;
                   12616:                break;
1.1.1.23  root     12617:        case 0x03:
                   12618:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12619:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12620:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12621: //             REG16(AX) = active_code_page;
1.1.1.23  root     12622:                REG16(CX) = 0x05;
                   12623:                break;
1.1.1.17  root     12624:        case 0x04:
                   12625:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12626:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12627:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12628: //             REG16(AX) = active_code_page;
1.1.1.17  root     12629:                REG16(CX) = 0x05;
                   12630:                break;
                   12631:        case 0x05:
                   12632:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12633:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12634:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12635: //             REG16(AX) = active_code_page;
1.1.1.17  root     12636:                REG16(CX) = 0x05;
                   12637:                break;
                   12638:        case 0x06:
                   12639:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12640:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12641:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12642: //             REG16(AX) = active_code_page;
1.1.1.17  root     12643:                REG16(CX) = 0x05;
                   12644:                break;
1.1       root     12645:        case 0x07:
1.1.1.3   root     12646:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12647:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12648:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12649: //             REG16(AX) = active_code_page;
1.1       root     12650:                REG16(CX) = 0x05;
                   12651:                break;
1.1.1.25  root     12652:        default:
                   12653:                return(0x01); // function number invalid
                   12654:        }
                   12655:        return(0x00);
                   12656: }
                   12657: 
                   12658: inline void msdos_int_21h_65h()
                   12659: {
                   12660:        char tmp[0x10000];
                   12661:        
                   12662:        switch(REG8(AL)) {
1.1.1.43  root     12663:        case 0x00:
                   12664:                if(REG16(CX) >= 7) {
                   12665:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12666:                        REG16(AX) = system_code_page;
                   12667:                } else {
                   12668:                        REG16(AX) = 0x0c;
                   12669:                        m_CF = 1;
                   12670:                }
                   12671:                break;
1.1.1.25  root     12672:        case 0x01:
                   12673:        case 0x02:
                   12674:        case 0x03:
                   12675:        case 0x04:
                   12676:        case 0x05:
                   12677:        case 0x06:
                   12678:        case 0x07:
                   12679:                {
                   12680:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12681:                        if(result) {
                   12682:                                REG16(AX) = result;
                   12683:                                m_CF = 1;
                   12684:                        } else {
                   12685:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12686:                        }
                   12687:                }
                   12688:                break;
1.1       root     12689:        case 0x20:
1.1.1.25  root     12690:        case 0xa0:
1.1.1.19  root     12691:                memset(tmp, 0, sizeof(tmp));
                   12692:                tmp[0] = REG8(DL);
1.1       root     12693:                my_strupr(tmp);
                   12694:                REG8(DL) = tmp[0];
                   12695:                break;
                   12696:        case 0x21:
1.1.1.25  root     12697:        case 0xa1:
1.1       root     12698:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12699:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12700:                my_strupr(tmp);
1.1.1.3   root     12701:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12702:                break;
                   12703:        case 0x22:
1.1.1.25  root     12704:        case 0xa2:
1.1.1.3   root     12705:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12706:                break;
1.1.1.25  root     12707:        case 0x23:
                   12708:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12709:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12710:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12711:                        REG16(AX) = 0x00;
                   12712:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12713:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12714:                        REG16(AX) = 0x01;
                   12715:                } else {
                   12716:                        REG16(AX) = 0x02;
                   12717:                }
                   12718:                break;
1.1       root     12719:        default:
1.1.1.22  root     12720:                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     12721:                REG16(AX) = 0x01;
1.1.1.3   root     12722:                m_CF = 1;
1.1       root     12723:                break;
                   12724:        }
                   12725: }
                   12726: 
                   12727: inline void msdos_int_21h_66h()
                   12728: {
                   12729:        switch(REG8(AL)) {
                   12730:        case 0x01:
                   12731:                REG16(BX) = active_code_page;
                   12732:                REG16(DX) = system_code_page;
                   12733:                break;
                   12734:        case 0x02:
                   12735:                if(active_code_page == REG16(BX)) {
                   12736:                        REG16(AX) = 0xeb41;
                   12737:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12738:                        active_code_page = REG16(BX);
1.1.1.17  root     12739:                        msdos_nls_tables_update();
1.1       root     12740:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12741:                        SetConsoleCP(active_code_page);
                   12742:                        SetConsoleOutputCP(active_code_page);
1.1       root     12743:                } else {
                   12744:                        REG16(AX) = 0x25;
1.1.1.3   root     12745:                        m_CF = 1;
1.1       root     12746:                }
                   12747:                break;
                   12748:        default:
1.1.1.22  root     12749:                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     12750:                REG16(AX) = 0x01;
1.1.1.3   root     12751:                m_CF = 1;
1.1       root     12752:                break;
                   12753:        }
                   12754: }
                   12755: 
                   12756: inline void msdos_int_21h_67h()
                   12757: {
                   12758:        process_t *process = msdos_process_info_get(current_psp);
                   12759:        
                   12760:        if(REG16(BX) <= MAX_FILES) {
                   12761:                process->max_files = max(REG16(BX), 20);
                   12762:        } else {
                   12763:                REG16(AX) = 0x08;
1.1.1.3   root     12764:                m_CF = 1;
1.1       root     12765:        }
                   12766: }
                   12767: 
                   12768: inline void msdos_int_21h_68h()
                   12769: {
                   12770:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12771:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12772:        
1.1.1.20  root     12773:        if(fd < process->max_files && file_handler[fd].valid) {
                   12774:                // fflush(_fdopen(fd, ""));
1.1       root     12775:        } else {
                   12776:                REG16(AX) = 0x06;
1.1.1.3   root     12777:                m_CF = 1;
1.1       root     12778:        }
                   12779: }
                   12780: 
                   12781: inline void msdos_int_21h_69h()
                   12782: {
1.1.1.3   root     12783:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12784:        char path[] = "A:\\";
                   12785:        char volume_label[MAX_PATH];
                   12786:        DWORD serial_number = 0;
                   12787:        char file_system[MAX_PATH];
                   12788:        
                   12789:        if(REG8(BL) == 0) {
                   12790:                path[0] = 'A' + _getdrive() - 1;
                   12791:        } else {
                   12792:                path[0] = 'A' + REG8(BL) - 1;
                   12793:        }
                   12794:        
                   12795:        switch(REG8(AL)) {
                   12796:        case 0x00:
                   12797:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12798:                        info->info_level = 0;
                   12799:                        info->serial_number = serial_number;
                   12800:                        memset(info->volume_label, 0x20, 11);
                   12801:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12802:                        memset(info->file_system, 0x20, 8);
                   12803:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12804:                } else {
                   12805:                        REG16(AX) = errno;
1.1.1.3   root     12806:                        m_CF = 1;
1.1       root     12807:                }
                   12808:                break;
                   12809:        case 0x01:
                   12810:                REG16(AX) = 0x03;
1.1.1.3   root     12811:                m_CF = 1;
1.1.1.45  root     12812:                break;
                   12813:        default:
                   12814:                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));
                   12815:                REG16(AX) = 0x01;
                   12816:                m_CF = 1;
                   12817:                break;
1.1       root     12818:        }
                   12819: }
                   12820: 
                   12821: inline void msdos_int_21h_6ah()
                   12822: {
                   12823:        REG8(AH) = 0x68;
                   12824:        msdos_int_21h_68h();
                   12825: }
                   12826: 
                   12827: inline void msdos_int_21h_6bh()
                   12828: {
1.1.1.45  root     12829:        REG8(AL) = 0x00;
1.1       root     12830: }
                   12831: 
                   12832: inline void msdos_int_21h_6ch(int lfn)
                   12833: {
1.1.1.45  root     12834:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12835:        int mode = REG8(BL) & 0x03;
                   12836:        
                   12837:        if(mode < 0x03) {
1.1.1.29  root     12838:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12839:                        // file exists
                   12840:                        if(REG8(DL) & 1) {
1.1.1.37  root     12841:                                int fd = -1;
                   12842:                                int sio_port = 0;
                   12843:                                int lpt_port = 0;
1.1       root     12844:                                
1.1.1.45  root     12845:                                if(msdos_is_device_path(path)) {
                   12846:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12847:                                } else {
1.1.1.13  root     12848:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12849:                                }
1.1       root     12850:                                if(fd != -1) {
                   12851:                                        REG16(AX) = fd;
                   12852:                                        REG16(CX) = 1;
1.1.1.45  root     12853:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12854:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12855:                                } else {
                   12856:                                        REG16(AX) = errno;
1.1.1.3   root     12857:                                        m_CF = 1;
1.1       root     12858:                                }
                   12859:                        } else if(REG8(DL) & 2) {
                   12860:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12861:                                int fd = -1;
                   12862:                                int sio_port = 0;
                   12863:                                int lpt_port = 0;
1.1       root     12864:                                
1.1.1.45  root     12865:                                if(msdos_is_device_path(path)) {
                   12866:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12867:                                } else {
                   12868:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12869:                                }
                   12870:                                if(fd != -1) {
                   12871:                                        if(attr == -1) {
                   12872:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12873:                                        }
                   12874:                                        SetFileAttributes(path, attr);
                   12875:                                        REG16(AX) = fd;
                   12876:                                        REG16(CX) = 3;
1.1.1.45  root     12877:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12878:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12879:                                } else {
                   12880:                                        REG16(AX) = errno;
1.1.1.3   root     12881:                                        m_CF = 1;
1.1       root     12882:                                }
                   12883:                        } else {
                   12884:                                REG16(AX) = 0x50;
1.1.1.3   root     12885:                                m_CF = 1;
1.1       root     12886:                        }
                   12887:                } else {
                   12888:                        // file not exists
                   12889:                        if(REG8(DL) & 0x10) {
                   12890:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12891:                                
                   12892:                                if(fd != -1) {
                   12893:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12894:                                        REG16(AX) = fd;
                   12895:                                        REG16(CX) = 2;
                   12896:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12897:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12898:                                } else {
                   12899:                                        REG16(AX) = errno;
1.1.1.3   root     12900:                                        m_CF = 1;
1.1       root     12901:                                }
                   12902:                        } else {
                   12903:                                REG16(AX) = 0x02;
1.1.1.3   root     12904:                                m_CF = 1;
1.1       root     12905:                        }
                   12906:                }
                   12907:        } else {
                   12908:                REG16(AX) = 0x0c;
1.1.1.3   root     12909:                m_CF = 1;
1.1       root     12910:        }
                   12911: }
                   12912: 
1.1.1.43  root     12913: inline void msdos_int_21h_70h()
                   12914: {
                   12915:        switch(REG8(AL)) {
1.1.1.48  root     12916:        case 0x00: // get ??? info
                   12917:        case 0x01: // set above info
                   12918: //             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));
                   12919:                REG16(AX) = 0x7000;
                   12920:                m_CF = 1;
                   12921:                break;
                   12922:        case 0x02: // set general internationalization info
1.1.1.43  root     12923:                if(REG16(CX) >= 7) {
                   12924:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12925:                        msdos_nls_tables_update();
                   12926:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12927:                        REG16(AX) = system_code_page;
                   12928:                } else {
                   12929:                        REG16(AX) = 0x0c;
                   12930:                        m_CF = 1;
                   12931:                }
                   12932:                break;
                   12933:        default:
                   12934:                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     12935:                REG16(AX) = 0x7000;
1.1.1.43  root     12936:                m_CF = 1;
                   12937:                break;
                   12938:        }
                   12939: }
                   12940: 
1.1       root     12941: inline void msdos_int_21h_710dh()
                   12942: {
                   12943:        // reset drive
                   12944: }
                   12945: 
1.1.1.48  root     12946: inline void msdos_int_21h_7141h()
1.1.1.17  root     12947: {
                   12948:        if(REG16(SI) == 0) {
1.1.1.48  root     12949:                msdos_int_21h_41h(1);
1.1.1.17  root     12950:                return;
                   12951:        }
                   12952:        if(REG16(SI) != 1) {
                   12953:                REG16(AX) = 5;
                   12954:                m_CF = 1;
                   12955:        }
                   12956:        /* wild card and matching attributes... */
                   12957:        char tmp[MAX_PATH * 2];
                   12958:        // copy search pathname (and quick check overrun)
                   12959:        ZeroMemory(tmp, sizeof(tmp));
                   12960:        tmp[MAX_PATH - 1] = '\0';
                   12961:        tmp[MAX_PATH] = 1;
                   12962:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12963:        
                   12964:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12965:                REG16(AX) = 1;
                   12966:                m_CF = 1;
                   12967:                return;
                   12968:        }
                   12969:        for(char *s = tmp; *s; ++s) {
                   12970:                if(*s == '/') {
                   12971:                        *s = '\\';
                   12972:                }
                   12973:        }
                   12974:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12975:        if(tmp_name) {
                   12976:                ++tmp_name;
                   12977:        } else {
                   12978:                tmp_name = strchr(tmp, ':');
                   12979:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12980:        }
                   12981:        
                   12982:        WIN32_FIND_DATAA fd;
                   12983:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12984:        if(fh == INVALID_HANDLE_VALUE) {
                   12985:                REG16(AX) = 2;
                   12986:                m_CF = 1;
                   12987:                return;
                   12988:        }
                   12989:        do {
                   12990:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12991:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     12992:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     12993:                                REG16(AX) = 5;
                   12994:                                m_CF = 1;
                   12995:                                break;
                   12996:                        }
                   12997:                }
                   12998:        } while(FindNextFileA(fh, &fd));
                   12999:        if(!m_CF) {
                   13000:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   13001:                        m_CF = 1;
                   13002:                        REG16(AX) = 2;
                   13003:                }
                   13004:        }
                   13005:        FindClose(fh);
                   13006: }
                   13007: 
1.1       root     13008: inline void msdos_int_21h_714eh()
                   13009: {
                   13010:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13011:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   13012:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13013:        WIN32_FIND_DATA fd;
                   13014:        
1.1.1.13  root     13015:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   13016:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13017:                FindClose(dtainfo->find_handle);
                   13018:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13019:        }
                   13020:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     13021:        dtainfo->allowable_mask = REG8(CL);
                   13022:        dtainfo->required_mask = REG8(CH);
                   13023:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     13024:        
1.1.1.14  root     13025:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   13026:                dtainfo->allowable_mask &= ~8;
1.1       root     13027:        }
1.1.1.14  root     13028:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   13029:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     13030:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   13031:                                FindClose(dtainfo->find_handle);
                   13032:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13033:                                break;
                   13034:                        }
                   13035:                }
                   13036:        }
1.1.1.13  root     13037:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13038:                find->attrib = fd.dwFileAttributes;
                   13039:                msdos_find_file_conv_local_time(&fd);
                   13040:                if(REG16(SI) == 0) {
                   13041:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13042:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13043:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13044:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13045:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13046:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13047:                } else {
                   13048:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13049:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13050:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13051:                }
                   13052:                find->size_hi = fd.nFileSizeHigh;
                   13053:                find->size_lo = fd.nFileSizeLow;
                   13054:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13055:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13056:                REG16(AX) = dtainfo - dtalist + 1;
                   13057:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13058:                // volume label
                   13059:                find->attrib = 8;
                   13060:                find->size_hi = find->size_lo = 0;
                   13061:                strcpy(find->full_name, process->volume_label);
                   13062:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13063:                dtainfo->allowable_mask &= ~8;
                   13064:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     13065:        } else {
                   13066:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     13067:                m_CF = 1;
1.1       root     13068:        }
                   13069: }
                   13070: 
                   13071: inline void msdos_int_21h_714fh()
                   13072: {
                   13073:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13074:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13075:        WIN32_FIND_DATA fd;
                   13076:        
1.1.1.14  root     13077:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13078:                REG16(AX) = 6;
1.1.1.13  root     13079:                m_CF = 1;
                   13080:                return;
                   13081:        }
1.1.1.14  root     13082:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13083:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13084:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     13085:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     13086:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   13087:                                        FindClose(dtainfo->find_handle);
                   13088:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13089:                                        break;
                   13090:                                }
                   13091:                        }
                   13092:                } else {
1.1.1.13  root     13093:                        FindClose(dtainfo->find_handle);
                   13094:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13095:                }
                   13096:        }
1.1.1.13  root     13097:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13098:                find->attrib = fd.dwFileAttributes;
                   13099:                msdos_find_file_conv_local_time(&fd);
                   13100:                if(REG16(SI) == 0) {
                   13101:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13102:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13103:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13104:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13105:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13106:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13107:                } else {
                   13108:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13109:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13110:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13111:                }
                   13112:                find->size_hi = fd.nFileSizeHigh;
                   13113:                find->size_lo = fd.nFileSizeLow;
                   13114:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13115:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13116:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13117:                // volume label
                   13118:                find->attrib = 8;
                   13119:                find->size_hi = find->size_lo = 0;
                   13120:                strcpy(find->full_name, process->volume_label);
                   13121:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13122:                dtainfo->allowable_mask &= ~8;
1.1       root     13123:        } else {
                   13124:                REG16(AX) = 0x12;
1.1.1.3   root     13125:                m_CF = 1;
1.1       root     13126:        }
                   13127: }
                   13128: 
                   13129: inline void msdos_int_21h_71a0h()
                   13130: {
                   13131:        DWORD max_component_len, file_sys_flag;
                   13132:        
1.1.1.14  root     13133:        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))) {
                   13134:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   13135:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     13136:                REG16(CX) = (UINT16)max_component_len;          // 255
                   13137:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   13138:        } else {
                   13139:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13140:                m_CF = 1;
1.1       root     13141:        }
                   13142: }
                   13143: 
                   13144: inline void msdos_int_21h_71a1h()
                   13145: {
1.1.1.14  root     13146:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13147:                REG16(AX) = 6;
1.1.1.13  root     13148:                m_CF = 1;
                   13149:                return;
                   13150:        }
1.1.1.14  root     13151:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13152:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13153:                FindClose(dtainfo->find_handle);
                   13154:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13155:        }
                   13156: }
                   13157: 
                   13158: inline void msdos_int_21h_71a6h()
                   13159: {
                   13160:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13161:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13162:        
1.1.1.3   root     13163:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13164:        struct _stat64 status;
                   13165:        DWORD serial_number = 0;
                   13166:        
1.1.1.20  root     13167:        if(fd < process->max_files && file_handler[fd].valid) {
                   13168:                if(_fstat64(fd, &status) == 0) {
                   13169:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13170:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13171:                                char volume[] = "A:\\";
1.1.1.20  root     13172:                                volume[0] = file_handler[fd].path[1];
1.1       root     13173:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   13174:                        }
1.1.1.20  root     13175:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     13176:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13177:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13178:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13179:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13180:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13181:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13182:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13183:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13184:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13185:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13186:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13187:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13188:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13189:                } else {
                   13190:                        REG16(AX) = errno;
1.1.1.3   root     13191:                        m_CF = 1;
1.1       root     13192:                }
                   13193:        } else {
                   13194:                REG16(AX) = 0x06;
1.1.1.3   root     13195:                m_CF = 1;
1.1       root     13196:        }
                   13197: }
                   13198: 
                   13199: inline void msdos_int_21h_71a7h()
                   13200: {
                   13201:        switch(REG8(BL)) {
                   13202:        case 0x00:
1.1.1.3   root     13203:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13204:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13205:                        m_CF = 1;
1.1       root     13206:                }
                   13207:                break;
                   13208:        case 0x01:
                   13209:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13210:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13211:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13212:                        m_CF = 1;
1.1       root     13213:                }
                   13214:                break;
                   13215:        default:
1.1.1.22  root     13216:                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     13217:                REG16(AX) = 0x7100;
1.1.1.3   root     13218:                m_CF = 1;
1.1       root     13219:                break;
                   13220:        }
                   13221: }
                   13222: 
                   13223: inline void msdos_int_21h_71a8h()
                   13224: {
                   13225:        if(REG8(DH) == 0) {
                   13226:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13227:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13228:                memset(fcb, 0x20, sizeof(fcb));
                   13229:                int len = strlen(tmp);
1.1.1.21  root     13230:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13231:                        if(tmp[i] == '.') {
                   13232:                                pos = 8;
                   13233:                        } else {
                   13234:                                if(msdos_lead_byte_check(tmp[i])) {
                   13235:                                        fcb[pos++] = tmp[i++];
                   13236:                                }
                   13237:                                fcb[pos++] = tmp[i];
                   13238:                        }
                   13239:                }
1.1.1.3   root     13240:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13241:        } else {
1.1.1.3   root     13242:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13243:        }
                   13244: }
                   13245: 
1.1.1.22  root     13246: inline void msdos_int_21h_71aah()
                   13247: {
                   13248:        char drv[] = "A:", path[MAX_PATH];
                   13249:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13250:        
                   13251:        if(REG8(BL) == 0) {
                   13252:                drv[0] = 'A' + _getdrive() - 1;
                   13253:        } else {
                   13254:                drv[0] = 'A' + REG8(BL) - 1;
                   13255:        }
                   13256:        switch(REG8(BH)) {
                   13257:        case 0x00:
1.1.1.44  root     13258:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13259:                        REG16(AX) = 0x0f; // invalid drive
                   13260:                        m_CF = 1;
                   13261:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   13262:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13263:                        m_CF = 1;
                   13264:                }
                   13265:                break;
                   13266:        case 0x01:
1.1.1.44  root     13267:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13268:                        REG16(AX) = 0x0f; // invalid drive
                   13269:                        m_CF = 1;
                   13270:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13271:                        REG16(AX) = 0x0f; // invalid drive
                   13272:                        m_CF = 1;
                   13273:                }
                   13274:                break;
                   13275:        case 0x02:
1.1.1.44  root     13276:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13277:                        REG16(AX) = 0x0f; // invalid drive
                   13278:                        m_CF = 1;
                   13279:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13280:                        REG16(AX) = 0x0f; // invalid drive
                   13281:                        m_CF = 1;
                   13282:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13283:                        REG16(AX) = 0x0f; // invalid drive
                   13284:                        m_CF = 1;
                   13285:                } else {
                   13286:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13287:                }
                   13288:                break;
                   13289:        default:
                   13290:                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     13291:                REG16(AX) = 0x7100;
1.1.1.22  root     13292:                m_CF = 1;
                   13293:                break;
                   13294:        }
                   13295: }
                   13296: 
1.1.1.14  root     13297: inline void msdos_int_21h_7300h()
                   13298: {
1.1.1.44  root     13299:        REG8(AL) = REG8(CL);
                   13300:        REG8(AH) = 0;
1.1.1.14  root     13301: }
                   13302: 
                   13303: inline void msdos_int_21h_7302h()
                   13304: {
                   13305:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13306:        UINT16 seg, ofs;
                   13307:        
                   13308:        if(REG16(CX) < 0x3f) {
                   13309:                REG8(AL) = 0x18;
                   13310:                m_CF = 1;
                   13311:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13312:                REG8(AL) = 0xff;
                   13313:                m_CF = 1;
                   13314:        } else {
                   13315:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13316:        }
                   13317: }
                   13318: 
1.1       root     13319: inline void msdos_int_21h_7303h()
                   13320: {
1.1.1.3   root     13321:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13322:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13323:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13324:        
                   13325:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   13326:                info->size_of_structure = sizeof(ext_space_info_t);
                   13327:                info->structure_version = 0;
                   13328:                info->sectors_per_cluster = sectors_per_cluster;
                   13329:                info->bytes_per_sector = bytes_per_sector;
                   13330:                info->available_clusters_on_drive = free_clusters;
                   13331:                info->total_clusters_on_drive = total_clusters;
                   13332:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13333:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13334:                info->available_allocation_units = free_clusters;       // ???
                   13335:                info->total_allocation_units = total_clusters;          // ???
                   13336:        } else {
                   13337:                REG16(AX) = errno;
1.1.1.3   root     13338:                m_CF = 1;
1.1       root     13339:        }
                   13340: }
                   13341: 
1.1.1.30  root     13342: inline void msdos_int_21h_dbh()
                   13343: {
                   13344:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13345:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13346:        REG8(AL) = dos_info->last_drive;
                   13347: }
                   13348: 
                   13349: inline void msdos_int_21h_dch()
                   13350: {
                   13351:        // Novell NetWare - Connection Services - Get Connection Number
                   13352:        REG8(AL) = 0x00;
                   13353: }
                   13354: 
1.1.1.32  root     13355: inline void msdos_int_24h()
                   13356: {
                   13357:        const char *message = NULL;
                   13358:        int key = 0;
                   13359:        
                   13360:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13361:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13362:                        if(active_code_page == 932) {
                   13363:                                message = critical_error_table[i].message_japanese;
                   13364:                        }
                   13365:                        if(message == NULL) {
                   13366:                                message = critical_error_table[i].message_english;
                   13367:                        }
                   13368:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13369:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13370:                        
                   13371:                        SREG(ES) = WORK_TOP >> 4;
                   13372:                        i386_load_segment_descriptor(ES);
                   13373:                        REG16(DI) = 0x0000;
                   13374:                        break;
                   13375:                }
                   13376:        }
                   13377:        fprintf(stderr, "\n%s", message);
                   13378:        if(!(REG8(AH) & 0x80)) {
                   13379:                if(REG8(AH) & 0x01) {
                   13380:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13381:                } else {
                   13382:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13383:                }
                   13384:        }
                   13385:        fprintf(stderr, "\n");
                   13386:        
1.1.1.33  root     13387:        {
1.1.1.32  root     13388:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13389:        }
1.1.1.32  root     13390:        if(REG8(AH) & 0x10) {
                   13391:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13392:        }
                   13393:        if(REG8(AH) & 0x20) {
                   13394:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13395:        }
                   13396:        if(REG8(AH) & 0x08) {
                   13397:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13398:        }
                   13399:        fprintf(stderr, "? ");
                   13400:        
                   13401:        while(1) {
                   13402:                while(!_kbhit()) {
                   13403:                        Sleep(10);
                   13404:                }
                   13405:                key = _getch();
                   13406:                
                   13407:                if(key == 'I' || key == 'i') {
                   13408:                        if(REG8(AH) & 0x20) {
                   13409:                                REG8(AL) = 0;
                   13410:                                break;
                   13411:                        }
                   13412:                } else if(key == 'R' || key == 'r') {
                   13413:                        if(REG8(AH) & 0x10) {
                   13414:                                REG8(AL) = 1;
                   13415:                                break;
                   13416:                        }
                   13417:                } else if(key == 'A' || key == 'a') {
                   13418:                        REG8(AL) = 2;
                   13419:                        break;
                   13420:                } else if(key == 'F' || key == 'f') {
                   13421:                        if(REG8(AH) & 0x08) {
                   13422:                                REG8(AL) = 3;
                   13423:                                break;
                   13424:                        }
                   13425:                }
                   13426:        }
                   13427:        fprintf(stderr, "%c\n", key);
                   13428: }
                   13429: 
1.1       root     13430: inline void msdos_int_25h()
                   13431: {
                   13432:        UINT16 seg, ofs;
                   13433:        DWORD dwSize;
                   13434:        
1.1.1.3   root     13435: #if defined(HAS_I386)
                   13436:        I386OP(pushf)();
                   13437: #else
                   13438:        PREFIX86(_pushf());
                   13439: #endif
1.1       root     13440:        
                   13441:        if(!(REG8(AL) < 26)) {
                   13442:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13443:                m_CF = 1;
1.1       root     13444:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13445:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13446:                m_CF = 1;
1.1       root     13447:        } else {
                   13448:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13449:                char dev[64];
                   13450:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13451:                
                   13452:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13453:                if(hFile == INVALID_HANDLE_VALUE) {
                   13454:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13455:                        m_CF = 1;
1.1       root     13456:                } else {
1.1.1.19  root     13457:                        UINT32 top_sector  = REG16(DX);
                   13458:                        UINT16 sector_num  = REG16(CX);
                   13459:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13460:                        
                   13461:                        if(sector_num == 0xffff) {
                   13462:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13463:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13464:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13465:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13466:                                buffer_addr = (seg << 4) + ofs;
                   13467:                        }
                   13468: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13469: //                             REG8(AL) = 0x02; // drive not ready
                   13470: //                             m_CF = 1;
                   13471: //                     } else 
                   13472:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13473:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13474:                                m_CF = 1;
1.1.1.19  root     13475:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13476:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13477:                                m_CF = 1;
1.1       root     13478:                        }
                   13479:                        CloseHandle(hFile);
                   13480:                }
                   13481:        }
                   13482: }
                   13483: 
                   13484: inline void msdos_int_26h()
                   13485: {
1.1.1.42  root     13486:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13487:        UINT16 seg, ofs;
                   13488:        DWORD dwSize;
                   13489:        
1.1.1.3   root     13490: #if defined(HAS_I386)
                   13491:        I386OP(pushf)();
                   13492: #else
                   13493:        PREFIX86(_pushf());
                   13494: #endif
1.1       root     13495:        
                   13496:        if(!(REG8(AL) < 26)) {
                   13497:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13498:                m_CF = 1;
1.1       root     13499:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13500:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13501:                m_CF = 1;
1.1       root     13502:        } else {
                   13503:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13504:                char dev[64];
                   13505:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13506:                
                   13507:                if(dpb->media_type == 0xf8) {
                   13508:                        // this drive is not a floppy
1.1.1.6   root     13509: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13510: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13511: //                     }
1.1       root     13512:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13513:                        m_CF = 1;
1.1       root     13514:                } else {
                   13515:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13516:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13517:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13518:                                m_CF = 1;
1.1       root     13519:                        } else {
1.1.1.19  root     13520:                                UINT32 top_sector  = REG16(DX);
                   13521:                                UINT16 sector_num  = REG16(CX);
                   13522:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13523:                                
                   13524:                                if(sector_num == 0xffff) {
                   13525:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13526:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13527:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13528:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13529:                                        buffer_addr = (seg << 4) + ofs;
                   13530:                                }
1.1       root     13531:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13532:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13533:                                        m_CF = 1;
1.1.1.19  root     13534:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13535:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13536:                                        m_CF = 1;
1.1.1.19  root     13537:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13538:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13539:                                        m_CF = 1;
1.1       root     13540:                                }
                   13541:                                CloseHandle(hFile);
                   13542:                        }
                   13543:                }
                   13544:        }
                   13545: }
                   13546: 
                   13547: inline void msdos_int_27h()
                   13548: {
1.1.1.29  root     13549:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13550:        try {
                   13551:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13552:        } catch(...) {
                   13553:                // recover the broken mcb
                   13554:                int mcb_seg = SREG(CS) - 1;
                   13555:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13556:                
1.1.1.29  root     13557:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13558:                        mcb->mz = 'M';
                   13559:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13560:                        
1.1.1.29  root     13561:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13562:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13563:                        } else {
1.1.1.39  root     13564:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13565:                        }
                   13566:                } else {
                   13567:                        mcb->mz = 'Z';
1.1.1.30  root     13568:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13569:                }
                   13570:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13571:        }
1.1.1.3   root     13572:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13573: }
                   13574: 
                   13575: inline void msdos_int_29h()
                   13576: {
1.1.1.50  root     13577:        msdos_putch_fast(REG8(AL));
1.1       root     13578: }
                   13579: 
                   13580: inline void msdos_int_2eh()
                   13581: {
                   13582:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13583:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13584:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13585:        char *token = my_strtok(tmp, " ");
                   13586:        strcpy(command, token);
                   13587:        strcpy(opt, token + strlen(token) + 1);
                   13588:        
                   13589:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13590:        param->env_seg = 0;
                   13591:        param->cmd_line.w.l = 44;
                   13592:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13593:        param->fcb1.w.l = 24;
                   13594:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13595:        param->fcb2.w.l = 24;
                   13596:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13597:        
                   13598:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13599:        
                   13600:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13601:        cmd_line->len = strlen(opt);
                   13602:        strcpy(cmd_line->cmd, opt);
                   13603:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13604:        
1.1.1.28  root     13605:        try {
                   13606:                if(msdos_process_exec(command, param, 0)) {
                   13607:                        REG16(AX) = 0xffff; // error before processing command
                   13608:                } else {
                   13609:                        // set flag to set retval to ax when the started process is terminated
                   13610:                        process_t *process = msdos_process_info_get(current_psp);
                   13611:                        process->called_by_int2eh = true;
                   13612:                }
                   13613:        } catch(...) {
                   13614:                REG16(AX) = 0xffff; // error before processing command
                   13615:        }
1.1       root     13616: }
                   13617: 
1.1.1.29  root     13618: inline void msdos_int_2fh_05h()
                   13619: {
                   13620:        switch(REG8(AL)) {
                   13621:        case 0x00:
1.1.1.49  root     13622:                // critical error handler is installed
1.1.1.32  root     13623:                REG8(AL) = 0xff;
                   13624:                break;
                   13625:        case 0x01:
                   13626:        case 0x02:
                   13627:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13628:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13629:                                const char *message = NULL;
                   13630:                                if(active_code_page == 932) {
                   13631:                                        message = standard_error_table[i].message_japanese;
                   13632:                                }
                   13633:                                if(message == NULL) {
                   13634:                                        message = standard_error_table[i].message_english;
                   13635:                                }
                   13636:                                strcpy((char *)(mem + WORK_TOP), message);
                   13637:                                
                   13638:                                SREG(ES) = WORK_TOP >> 4;
                   13639:                                i386_load_segment_descriptor(ES);
                   13640:                                REG16(DI) = 0x0000;
                   13641:                                REG8(AL) = 0x01;
                   13642:                                break;
                   13643:                        }
                   13644:                }
1.1.1.29  root     13645:                break;
                   13646:        default:
                   13647:                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     13648:                REG16(AX) = 0x01;
1.1.1.29  root     13649:                m_CF = 1;
                   13650:        }
                   13651: }
                   13652: 
1.1.1.44  root     13653: inline void msdos_int_2fh_06h()
                   13654: {
                   13655:        switch(REG8(AL)) {
                   13656:        case 0x00:
                   13657:                // ASSIGN is not installed
1.1.1.49  root     13658: //             REG8(AL) = 0x00;
1.1.1.44  root     13659:                break;
                   13660:        case 0x01:
                   13661:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13662:                REG16(AX) = 0x01;
                   13663:                m_CF = 1;
                   13664:                break;
                   13665:        default:
                   13666:                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));
                   13667:                REG16(AX) = 0x01;
                   13668:                m_CF = 1;
                   13669:                break;
                   13670:        }
                   13671: }
                   13672: 
1.1.1.22  root     13673: inline void msdos_int_2fh_11h()
                   13674: {
                   13675:        switch(REG8(AL)) {
                   13676:        case 0x00:
1.1.1.29  root     13677:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     13678: #ifdef SUPPORT_MSCDEX
                   13679:                        // MSCDEX is installed
                   13680:                        REG8(AL) = 0xff;
                   13681:                        i386_write_stack(0xadad);
                   13682: #else
1.1.1.29  root     13683:                        // MSCDEX is not installed
                   13684: //                     REG8(AL) = 0x00;
1.1.1.53  root     13685: #endif
1.1.1.29  root     13686:                } else {
                   13687:                        // Network Redirector is not installed
                   13688: //                     REG8(AL) = 0x00;
                   13689:                }
1.1.1.22  root     13690:                break;
                   13691:        default:
1.1.1.43  root     13692: //             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     13693:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13694:                m_CF = 1;
                   13695:                break;
                   13696:        }
                   13697: }
                   13698: 
1.1.1.21  root     13699: inline void msdos_int_2fh_12h()
                   13700: {
                   13701:        switch(REG8(AL)) {
1.1.1.22  root     13702:        case 0x00:
1.1.1.29  root     13703:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13704:                REG8(AL) = 0xff;
                   13705:                break;
1.1.1.29  root     13706: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13707:        case 0x02:
                   13708:                {
                   13709:                        UINT16 stack = i386_read_stack();
                   13710:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13711:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13712:                        i386_load_segment_descriptor(ES);
                   13713:                }
                   13714:                break;
1.1.1.30  root     13715:        case 0x03:
                   13716:                SREG(DS) = (DEVICE_TOP >> 4);
                   13717:                i386_load_segment_descriptor(DS);
                   13718:                break;
1.1.1.29  root     13719:        case 0x04:
                   13720:                {
                   13721:                        UINT16 stack = i386_read_stack();
                   13722:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13723: #if defined(HAS_I386)
                   13724:                        m_ZF = (REG8(AL) == '\\');
                   13725: #else
                   13726:                        m_ZeroVal = (REG8(AL) != '\\');
                   13727: #endif
                   13728:                }
                   13729:                break;
                   13730:        case 0x05:
1.1.1.49  root     13731:                {
                   13732:                        UINT16 c = i386_read_stack();
                   13733:                        if((c >> 0) & 0xff) {
                   13734:                                msdos_putch((c >> 0) & 0xff);
                   13735:                        }
                   13736:                        if((c >> 8) & 0xff) {
                   13737:                                msdos_putch((c >> 8) & 0xff);
                   13738:                        }
                   13739:                }
1.1.1.29  root     13740:                break;
1.1.1.49  root     13741: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     13742: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13743: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     13744: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     13745: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13746: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13747: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13748:        case 0x0d:
                   13749:                {
                   13750:                        SYSTEMTIME time;
                   13751:                        FILETIME file_time;
                   13752:                        WORD dos_date, dos_time;
                   13753:                        GetLocalTime(&time);
                   13754:                        SystemTimeToFileTime(&time, &file_time);
                   13755:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13756:                        REG16(AX) = dos_date;
                   13757:                        REG16(DX) = dos_time;
                   13758:                }
                   13759:                break;
                   13760: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13761: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13762: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13763:        case 0x11:
                   13764:                {
                   13765:                        char path[MAX_PATH], *p;
                   13766:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13767:                        my_strupr(path);
                   13768:                        while((p = my_strchr(path, '/')) != NULL) {
                   13769:                                *p = '\\';
                   13770:                        }
                   13771:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13772:                }
                   13773:                break;
                   13774:        case 0x12:
                   13775:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13776:                break;
                   13777:        case 0x13:
                   13778:                {
                   13779:                        char tmp[2] = {0};
                   13780:                        tmp[0] = i386_read_stack();
                   13781:                        my_strupr(tmp);
                   13782:                        REG8(AL) = tmp[0];
                   13783:                }
                   13784:                break;
                   13785:        case 0x14:
                   13786: #if defined(HAS_I386)
                   13787:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13788: #else
                   13789:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13790: #endif
                   13791:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13792:                break;
                   13793: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13794:        case 0x16:
                   13795:                if(REG16(BX) < 20) {
                   13796:                        SREG(ES) = SFT_TOP >> 4;
                   13797:                        i386_load_segment_descriptor(ES);
                   13798:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13799:                        
                   13800:                        // update system file table
                   13801:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13802:                        if(file_handler[REG16(BX)].valid) {
                   13803:                                int count = 0;
                   13804:                                for(int i = 0; i < 20; i++) {
                   13805:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13806:                                                count++;
                   13807:                                        }
                   13808:                                }
                   13809:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13810:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13811:                                _lseek(REG16(BX), 0, SEEK_END);
                   13812:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13813:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13814:                        } else {
                   13815:                                memset(sft, 0, 0x3b);
                   13816:                        }
                   13817:                } else {
                   13818:                        REG16(AX) = 0x06;
                   13819:                        m_CF = 1;
                   13820:                }
                   13821:                break;
1.1.1.49  root     13822:        case 0x17:
                   13823:                {
                   13824:                        UINT16 drive = i386_read_stack();
                   13825:                        if(msdos_is_valid_drive(drive)) {
                   13826:                                msdos_cds_update(drive);
                   13827:                        }
                   13828:                        REG16(SI) = 88 * drive;
                   13829:                        SREG(DS) = (CDS_TOP >> 4);
                   13830:                        i386_load_segment_descriptor(DS);
                   13831:                }
                   13832:                break;
1.1.1.29  root     13833: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13834: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13835:        case 0x1a:
                   13836:                {
                   13837:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13838:                        if(path[1] == ':') {
                   13839:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13840:                                        REG8(AL) = path[0] - 'a' + 1;
                   13841:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13842:                                        REG8(AL) = path[0] - 'A' + 1;
                   13843:                                } else {
                   13844:                                        REG8(AL) = 0xff; // invalid
                   13845:                                }
                   13846:                                strcpy(full, path);
                   13847:                                strcpy(path, full + 2);
                   13848:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13849:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13850:                                        REG8(AL) = full[0] - 'a' + 1;
                   13851:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13852:                                        REG8(AL) = full[0] - 'A' + 1;
                   13853:                                } else {
                   13854:                                        REG8(AL) = 0xff; // invalid
                   13855:                                }
                   13856:                        } else {
                   13857:                                REG8(AL) = 0x00; // default
                   13858:                        }
                   13859:                }
                   13860:                break;
                   13861:        case 0x1b:
                   13862:                {
                   13863:                        int year = REG16(CX) + 1980;
                   13864:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13865:                }
                   13866:                break;
                   13867: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13868: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13869:        case 0x1e:
                   13870:                {
                   13871:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13872:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13873:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13874: #if defined(HAS_I386)
                   13875:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13876: #else
                   13877:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13878: #endif
                   13879:                        } else {
                   13880: #if defined(HAS_I386)
                   13881:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13882: #else
                   13883:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13884: #endif
                   13885:                        }
                   13886:                }
                   13887:                break;
1.1.1.49  root     13888:        case 0x1f:
                   13889:                {
                   13890:                        UINT16 drive = i386_read_stack();
                   13891:                        if(msdos_is_valid_drive(drive)) {
                   13892:                                msdos_cds_update(drive);
                   13893:                        }
                   13894:                        REG16(SI) = 88 * drive;
                   13895:                        SREG(ES) = (CDS_TOP >> 4);
                   13896:                        i386_load_segment_descriptor(ES);
                   13897:                }
                   13898:                break;
1.1.1.21  root     13899:        case 0x20:
                   13900:                {
                   13901:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13902:                        
                   13903:                        if(fd < 20) {
                   13904:                                SREG(ES) = current_psp;
                   13905:                                i386_load_segment_descriptor(ES);
                   13906:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13907:                        } else {
                   13908:                                REG16(AX) = 0x06;
                   13909:                                m_CF = 1;
                   13910:                        }
                   13911:                }
                   13912:                break;
1.1.1.29  root     13913:        case 0x21:
                   13914:                msdos_int_21h_60h(0);
                   13915:                break;
1.1.1.49  root     13916:        case 0x22:
                   13917:                {
                   13918:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13919:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   13920:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   13921:                        }
                   13922:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   13923:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   13924:                        }
                   13925:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   13926:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   13927:                        }
                   13928:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   13929:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   13930:                        }
                   13931:                }
                   13932:                break;
1.1.1.29  root     13933: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13934: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13935:        case 0x25:
                   13936:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13937:                break;
                   13938:        case 0x26:
                   13939:                REG8(AL) = REG8(CL);
                   13940:                msdos_int_21h_3dh();
                   13941:                break;
                   13942:        case 0x27:
                   13943:                msdos_int_21h_3eh();
                   13944:                break;
                   13945:        case 0x28:
                   13946:                REG16(AX) = REG16(BP);
                   13947:                msdos_int_21h_42h();
                   13948:                break;
                   13949:        case 0x29:
                   13950:                msdos_int_21h_3fh();
                   13951:                break;
                   13952: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13953:        case 0x2b:
                   13954:                REG16(AX) = REG16(BP);
                   13955:                msdos_int_21h_44h();
                   13956:                break;
                   13957:        case 0x2c:
                   13958:                REG16(BX) = DEVICE_TOP >> 4;
                   13959:                REG16(AX) = 22;
                   13960:                break;
                   13961:        case 0x2d:
                   13962:                {
                   13963:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13964:                        REG16(AX) = sda->extended_error_code;
                   13965:                }
                   13966:                break;
                   13967:        case 0x2e:
                   13968:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13969:                        SREG(ES) = 0x0001;
                   13970:                        i386_load_segment_descriptor(ES);
                   13971:                        REG16(DI) = 0x00;
                   13972:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     13973:                        // dummy parameter error message read routine is at fffc:0010
                   13974:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     13975:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13976:                        REG16(DI) = 0x0010;
1.1.1.22  root     13977:                }
                   13978:                break;
1.1.1.29  root     13979:        case 0x2f:
                   13980:                if(REG16(DX) != 0) {
1.1.1.30  root     13981:                        dos_major_version = REG8(DL);
                   13982:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13983:                } else {
                   13984:                        REG8(DL) = 7;
                   13985:                        REG8(DH) = 10;
                   13986:                }
                   13987:                break;
                   13988: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13989: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13990:        default:
                   13991:                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));
                   13992:                REG16(AX) = 0x01;
                   13993:                m_CF = 1;
                   13994:                break;
                   13995:        }
                   13996: }
                   13997: 
1.1.1.30  root     13998: inline void msdos_int_2fh_13h()
                   13999: {
                   14000:        static UINT16 prevDS = 0, prevDX = 0;
                   14001:        static UINT16 prevES = 0, prevBX = 0;
                   14002:        UINT16 tmp;
                   14003:        
                   14004:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   14005:        i386_load_segment_descriptor(DS);
                   14006:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   14007:        
                   14008:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   14009:        i386_load_segment_descriptor(ES);
                   14010:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   14011: }
                   14012: 
1.1.1.22  root     14013: inline void msdos_int_2fh_14h()
                   14014: {
                   14015:        switch(REG8(AL)) {
                   14016:        case 0x00:
1.1.1.29  root     14017:                // NLSFUNC.COM is installed
                   14018:                REG8(AL) = 0xff;
1.1.1.25  root     14019:                break;
                   14020:        case 0x01:
                   14021:        case 0x03:
                   14022:                REG8(AL) = 0x00;
                   14023:                active_code_page = REG16(BX);
                   14024:                msdos_nls_tables_update();
                   14025:                break;
                   14026:        case 0x02:
                   14027:                REG8(AL) = get_extended_country_info(REG16(BP));
                   14028:                break;
                   14029:        case 0x04:
1.1.1.42  root     14030:                for(int i = 0;; i++) {
                   14031:                        if(country_table[i].code == REG16(DX)) {
                   14032:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   14033:                                break;
                   14034:                        } else if(country_table[i].code == -1) {
                   14035:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14036:                                break;
                   14037:                        }
                   14038:                }
1.1.1.25  root     14039:                REG8(AL) = 0x00;
1.1.1.22  root     14040:                break;
                   14041:        default:
                   14042:                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));
                   14043:                REG16(AX) = 0x01;
                   14044:                m_CF = 1;
                   14045:                break;
                   14046:        }
                   14047: }
                   14048: 
                   14049: inline void msdos_int_2fh_15h()
                   14050: {
                   14051:        switch(REG8(AL)) {
1.1.1.29  root     14052:        case 0x00: // CD-ROM - Installation Check
                   14053:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     14054: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     14055:                        // MSCDEX is installed
                   14056:                        REG16(BX) = 0;
                   14057:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     14058:                                if(msdos_is_cdrom_drive(i)) {
                   14059:                                        if(REG16(BX) == 0) {
                   14060:                                                REG16(CX) = i;
1.1.1.43  root     14061:                                        }
1.1.1.44  root     14062:                                        REG16(BX)++;
1.1.1.43  root     14063:                                }
                   14064:                        }
                   14065: #else
1.1.1.29  root     14066:                        // MSCDEX is not installed
                   14067: //                     REG8(AL) = 0x00;
1.1.1.43  root     14068: #endif
1.1.1.29  root     14069:                } else {
                   14070:                        // GRAPHICS.COM is not installed
                   14071: //                     REG8(AL) = 0x00;
                   14072:                }
1.1.1.22  root     14073:                break;
1.1.1.43  root     14074:        case 0x0b:
1.1.1.44  root     14075:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   14076:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   14077:                REG16(BX) = 0xadad;
1.1.1.43  root     14078:                break;
                   14079:        case 0x0d:
1.1.1.44  root     14080:                for(int i = 0, n = 0; i < 26; i++) {
                   14081:                        if(msdos_is_cdrom_drive(i)) {
                   14082:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     14083:                        }
                   14084:                }
                   14085:                break;
1.1.1.22  root     14086:        case 0xff:
1.1.1.29  root     14087:                if(REG16(BX) == 0x0000) {
                   14088:                        // CORELCDX is not installed
                   14089:                } else {
                   14090:                        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));
                   14091:                        REG16(AX) = 0x01;
                   14092:                        m_CF = 1;
                   14093:                }
1.1.1.22  root     14094:                break;
1.1.1.21  root     14095:        default:
1.1.1.22  root     14096:                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     14097:                REG16(AX) = 0x01;
                   14098:                m_CF = 1;
                   14099:                break;
                   14100:        }
                   14101: }
                   14102: 
1.1       root     14103: inline void msdos_int_2fh_16h()
                   14104: {
                   14105:        switch(REG8(AL)) {
                   14106:        case 0x00:
1.1.1.14  root     14107:                if(no_windows) {
1.1.1.29  root     14108:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   14109: //                     REG8(AL) = 0x00;
1.1.1.14  root     14110:                } else {
1.1.1.30  root     14111:                        REG8(AL) = win_major_version;
                   14112:                        REG8(AH) = win_minor_version;
1.1       root     14113:                }
                   14114:                break;
1.1.1.43  root     14115:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     14116:                // from DOSBox
                   14117:                i386_set_a20_line(1);
                   14118:                break;
1.1.1.49  root     14119:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     14120:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   14121:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   14122:                break;
                   14123:        case 0x07:
                   14124:                // Virtual Device Call API
                   14125:                break;
1.1.1.22  root     14126:        case 0x0a:
                   14127:                if(!no_windows) {
                   14128:                        REG16(AX) = 0x0000;
1.1.1.30  root     14129:                        REG8(BH) = win_major_version;
                   14130:                        REG8(BL) = win_minor_version;
1.1.1.49  root     14131: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     14132:                        REG16(CX) = 0x0003; // enhanced
                   14133:                }
                   14134:                break;
1.1.1.30  root     14135:        case 0x0b:
                   14136:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     14137:        case 0x0e:
                   14138:        case 0x0f:
1.1.1.30  root     14139:        case 0x10:
1.1.1.22  root     14140:        case 0x11:
                   14141:        case 0x12:
                   14142:        case 0x13:
                   14143:        case 0x14:
1.1.1.30  root     14144:        case 0x15:
1.1.1.43  root     14145:        case 0x81:
                   14146:        case 0x82:
1.1.1.44  root     14147:        case 0x84:
1.1.1.49  root     14148:        case 0x85:
1.1.1.33  root     14149:        case 0x86:
1.1.1.22  root     14150:        case 0x87:
1.1.1.30  root     14151:        case 0x89:
1.1.1.33  root     14152:        case 0x8a:
1.1.1.22  root     14153:                // function not supported, do not clear AX
                   14154:                break;
1.1.1.14  root     14155:        case 0x80:
                   14156:                Sleep(10);
1.1.1.35  root     14157:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     14158:                REG8(AL) = 0x00;
1.1.1.14  root     14159:                break;
1.1.1.33  root     14160:        case 0x83:
                   14161:                REG16(BX) = 0x01; // system vm id
                   14162:                break;
1.1.1.22  root     14163:        case 0x8e:
                   14164:                REG16(AX) = 0x00; // failed
                   14165:                break;
1.1.1.20  root     14166:        case 0x8f:
                   14167:                switch(REG8(DH)) {
                   14168:                case 0x01:
1.1.1.49  root     14169: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14170: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14171:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14172:                        break;
                   14173:                default:
                   14174:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14175:                        break;
                   14176:                }
                   14177:                break;
1.1       root     14178:        default:
1.1.1.22  root     14179:                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));
                   14180:                REG16(AX) = 0x01;
                   14181:                m_CF = 1;
                   14182:                break;
                   14183:        }
                   14184: }
                   14185: 
                   14186: inline void msdos_int_2fh_19h()
                   14187: {
                   14188:        switch(REG8(AL)) {
                   14189:        case 0x00:
1.1.1.29  root     14190:                // SHELLB.COM is not installed
                   14191: //             REG8(AL) = 0x00;
1.1.1.22  root     14192:                break;
                   14193:        case 0x01:
                   14194:        case 0x02:
                   14195:        case 0x03:
                   14196:        case 0x04:
                   14197:                REG16(AX) = 0x01;
                   14198:                m_CF = 1;
                   14199:                break;
1.1.1.29  root     14200:        case 0x80:
                   14201:                // IBM ROM-DOS v4.0 is not installed
                   14202: //             REG8(AL) = 0x00;
                   14203:                break;
1.1.1.22  root     14204:        default:
                   14205:                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     14206:                REG16(AX) = 0x01;
1.1.1.3   root     14207:                m_CF = 1;
1.1       root     14208:                break;
                   14209:        }
                   14210: }
                   14211: 
                   14212: inline void msdos_int_2fh_1ah()
                   14213: {
                   14214:        switch(REG8(AL)) {
                   14215:        case 0x00:
1.1.1.29  root     14216:                // ANSI.SYS is installed
1.1       root     14217:                REG8(AL) = 0xff;
                   14218:                break;
1.1.1.49  root     14219:        case 0x01:
1.1.1.50  root     14220:                if(REG8(CL) == 0x5f) {
                   14221:                        // set display information
                   14222:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14223:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14224:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14225:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14226:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14227:                                
                   14228:                                if(cur_width != new_width || cur_height != new_height) {
                   14229:                                        pcbios_set_console_size(new_width, new_height, true);
                   14230:                                }
                   14231:                        }
                   14232:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14233:                        // get display information
1.1.1.50  root     14234:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14235:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14236:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14237:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14238:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14239:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14240:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14241:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14242:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14243:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14244:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14245:                } else {
                   14246:                        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));
                   14247:                        REG16(AX) = 0x01;
                   14248:                        m_CF = 1;
                   14249:                }
                   14250:                break;
1.1       root     14251:        default:
1.1.1.22  root     14252:                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));
                   14253:                REG16(AX) = 0x01;
                   14254:                m_CF = 1;
                   14255:                break;
                   14256:        }
                   14257: }
                   14258: 
1.1.1.30  root     14259: inline void msdos_int_2fh_40h()
1.1.1.22  root     14260: {
                   14261:        switch(REG8(AL)) {
                   14262:        case 0x00:
1.1.1.30  root     14263:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14264:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14265:                break;
1.1.1.43  root     14266:        case 0x10:
                   14267:                // OS/2 v2.0+ - Installation Check
                   14268:                REG16(AX) = 0x01;
                   14269:                m_CF = 1;
                   14270:                break;
1.1.1.22  root     14271:        default:
                   14272:                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     14273:                REG16(AX) = 0x01;
1.1.1.3   root     14274:                m_CF = 1;
1.1       root     14275:                break;
                   14276:        }
                   14277: }
                   14278: 
                   14279: inline void msdos_int_2fh_43h()
                   14280: {
                   14281:        switch(REG8(AL)) {
                   14282:        case 0x00:
1.1.1.29  root     14283:                // XMS is installed ?
1.1.1.19  root     14284: #ifdef SUPPORT_XMS
                   14285:                if(support_xms) {
                   14286:                        REG8(AL) = 0x80;
1.1.1.44  root     14287:                }
                   14288: #endif
                   14289:                break;
                   14290:        case 0x08:
                   14291: #ifdef SUPPORT_XMS
                   14292:                if(support_xms) {
                   14293:                        REG8(AL) = 0x43;
                   14294:                        REG8(BL) = 0x01; // IBM PC/AT
                   14295:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14296:                }
1.1.1.19  root     14297: #endif
                   14298:                break;
                   14299:        case 0x10:
                   14300:                SREG(ES) = XMS_TOP >> 4;
                   14301:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14302:                REG16(BX) = 0x15;
1.1       root     14303:                break;
1.1.1.44  root     14304:        case 0xe0:
                   14305:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14306:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14307:                        break;
                   14308:                }
1.1       root     14309:        default:
1.1.1.22  root     14310:                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));
                   14311:                REG16(AX) = 0x01;
                   14312:                m_CF = 1;
                   14313:                break;
                   14314:        }
                   14315: }
                   14316: 
                   14317: inline void msdos_int_2fh_46h()
                   14318: {
                   14319:        switch(REG8(AL)) {
                   14320:        case 0x80:
1.1.1.29  root     14321:                // Windows v3.0 is not installed
                   14322: //             REG8(AL) = 0x00;
1.1.1.22  root     14323:                break;
                   14324:        default:
                   14325:                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));
                   14326:                REG16(AX) = 0x01;
                   14327:                m_CF = 1;
                   14328:                break;
                   14329:        }
                   14330: }
                   14331: 
                   14332: inline void msdos_int_2fh_48h()
                   14333: {
                   14334:        switch(REG8(AL)) {
                   14335:        case 0x00:
1.1.1.29  root     14336:                // DOSKEY is not installed
                   14337: //             REG8(AL) = 0x00;
1.1.1.22  root     14338:                break;
                   14339:        case 0x10:
                   14340:                msdos_int_21h_0ah();
                   14341:                REG16(AX) = 0x00;
                   14342:                break;
                   14343:        default:
                   14344:                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     14345:                REG16(AX) = 0x01;
1.1.1.3   root     14346:                m_CF = 1;
1.1       root     14347:                break;
                   14348:        }
                   14349: }
                   14350: 
                   14351: inline void msdos_int_2fh_4ah()
                   14352: {
                   14353:        switch(REG8(AL)) {
1.1.1.29  root     14354: #ifdef SUPPORT_HMA
                   14355:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14356:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14357:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14358:                                // restore first free mcb in high memory area
                   14359:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14360:                        }
                   14361:                        int offset = 0xffff;
                   14362:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14363:                                REG16(DI) = offset + 0x10;
                   14364:                        } else {
                   14365:                                REG16(DI) = 0xffff;
                   14366:                        }
                   14367:                } else {
                   14368:                        // HMA is already used
                   14369:                        REG16(BX) = 0;
                   14370:                        REG16(DI) = 0xffff;
                   14371:                }
                   14372:                SREG(ES) = 0xffff;
                   14373:                i386_load_segment_descriptor(ES);
                   14374:                break;
                   14375:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14376:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14377:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14378:                                // restore first free mcb in high memory area
                   14379:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14380:                        }
                   14381:                        int size = REG16(BX), offset;
                   14382:                        if((size % 16) != 0) {
                   14383:                                size &= ~15;
                   14384:                                size += 16;
                   14385:                        }
                   14386:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14387:                                REG16(BX) = size;
                   14388:                                REG16(DI) = offset + 0x10;
                   14389:                                is_hma_used_by_int_2fh = true;
                   14390:                        } else {
                   14391:                                REG16(BX) = 0;
                   14392:                                REG16(DI) = 0xffff;
                   14393:                        }
                   14394:                } else {
                   14395:                        // HMA is already used
                   14396:                        REG16(BX) = 0;
                   14397:                        REG16(DI) = 0xffff;
                   14398:                }
                   14399:                SREG(ES) = 0xffff;
                   14400:                i386_load_segment_descriptor(ES);
                   14401:                break;
                   14402:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14403:                if(REG8(DL) == 0x00) {
                   14404:                        if(!is_hma_used_by_xms) {
                   14405:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14406:                                        // restore first free mcb in high memory area
                   14407:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14408:                                        is_hma_used_by_int_2fh = false;
                   14409:                                }
                   14410:                                int size = REG16(BX), offset;
                   14411:                                if((size % 16) != 0) {
                   14412:                                        size &= ~15;
                   14413:                                        size += 16;
                   14414:                                }
                   14415:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14416: //                                     REG16(BX) = size;
                   14417:                                        SREG(ES) = 0xffff;
                   14418:                                        i386_load_segment_descriptor(ES);
                   14419:                                        REG16(DI) = offset + 0x10;
                   14420:                                        is_hma_used_by_int_2fh = true;
                   14421:                                } else {
                   14422:                                        REG16(DI) = 0xffff;
                   14423:                                }
                   14424:                        } else {
                   14425:                                REG16(DI) = 0xffff;
                   14426:                        }
                   14427:                } else if(REG8(DL) == 0x01) {
                   14428:                        if(!is_hma_used_by_xms) {
                   14429:                                int size = REG16(BX);
                   14430:                                if((size % 16) != 0) {
                   14431:                                        size &= ~15;
                   14432:                                        size += 16;
                   14433:                                }
                   14434:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14435:                                        // memory block address is not changed
                   14436:                                } else {
                   14437:                                        REG16(DI) = 0xffff;
                   14438:                                }
                   14439:                        } else {
                   14440:                                REG16(DI) = 0xffff;
                   14441:                        }
                   14442:                } else if(REG8(DL) == 0x02) {
                   14443:                        if(!is_hma_used_by_xms) {
                   14444:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14445:                                        // restore first free mcb in high memory area
                   14446:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14447:                                        is_hma_used_by_int_2fh = false;
                   14448:                                } else {
                   14449:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14450:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14451:                                                is_hma_used_by_int_2fh = false;
                   14452:                                        }
                   14453:                                }
                   14454:                        }
                   14455:                } else {
                   14456:                        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));
                   14457:                        REG16(AX) = 0x01;
                   14458:                        m_CF = 1;
                   14459:                }
                   14460:                break;
                   14461:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14462:                if(!is_hma_used_by_xms) {
                   14463:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14464:                                // restore first free mcb in high memory area
                   14465:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14466:                                is_hma_used_by_int_2fh = false;
                   14467:                        }
                   14468:                        REG16(AX) = 0x0000;
                   14469:                        SREG(ES) = 0xffff;
                   14470:                        i386_load_segment_descriptor(ES);
                   14471:                        REG16(DI) = 0x10;
                   14472:                }
                   14473:                break;
                   14474: #else
1.1       root     14475:        case 0x01:
                   14476:        case 0x02:
1.1.1.29  root     14477:                // HMA is already used
1.1.1.27  root     14478:                REG16(BX) = 0x0000;
1.1.1.3   root     14479:                SREG(ES) = 0xffff;
                   14480:                i386_load_segment_descriptor(ES);
1.1       root     14481:                REG16(DI) = 0xffff;
                   14482:                break;
1.1.1.19  root     14483:        case 0x03:
                   14484:                // unable to allocate
                   14485:                REG16(DI) = 0xffff;
                   14486:                break;
                   14487:        case 0x04:
                   14488:                // function not supported, do not clear AX
                   14489:                break;
1.1.1.29  root     14490: #endif
                   14491:        case 0x10:
1.1.1.42  root     14492:                switch(REG16(BX)) {
                   14493:                case 0x0000:
                   14494:                case 0x0001:
                   14495:                case 0x0002:
                   14496:                case 0x0003:
                   14497:                case 0x0004:
                   14498:                case 0x0005:
                   14499:                case 0x0006:
                   14500:                case 0x0007:
                   14501:                case 0x0008:
                   14502:                case 0x000a:
                   14503:                case 0x1234:
                   14504:                        // SMARTDRV v4.00+ is not installed
                   14505:                        break;
                   14506:                default:
1.1.1.29  root     14507:                        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));
                   14508:                        REG16(AX) = 0x01;
                   14509:                        m_CF = 1;
1.1.1.42  root     14510:                        break;
1.1.1.29  root     14511:                }
                   14512:                break;
                   14513:        case 0x11:
1.1.1.42  root     14514:                switch(REG16(BX)) {
                   14515:                case 0x0000:
                   14516:                case 0x0001:
                   14517:                case 0x0002:
                   14518:                case 0x0003:
                   14519:                case 0x0004:
                   14520:                case 0x0005:
                   14521:                case 0x0006:
                   14522:                case 0x0007:
                   14523:                case 0x0008:
                   14524:                case 0x0009:
                   14525:                case 0x000a:
                   14526:                case 0x000b:
                   14527:                case 0xfffe:
                   14528:                case 0xffff:
1.1.1.29  root     14529:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14530:                        break;
                   14531:                default:
                   14532:                        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));
                   14533:                        REG16(AX) = 0x01;
                   14534:                        m_CF = 1;
                   14535:                        break;
                   14536:                }
                   14537:                break;
                   14538:        case 0x12:
                   14539:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14540:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14541:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14542:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14543:                } else {
                   14544:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14545:                        REG16(AX) = 0x01;
                   14546:                        m_CF = 1;
                   14547:                }
1.1.1.22  root     14548:                break;
1.1.1.42  root     14549:        case 0x13:
                   14550:                // DBLSPACE.BIN is not installed
                   14551:                break;
1.1.1.22  root     14552:        default:
                   14553:                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));
                   14554:                REG16(AX) = 0x01;
                   14555:                m_CF = 1;
                   14556:                break;
                   14557:        }
                   14558: }
                   14559: 
                   14560: inline void msdos_int_2fh_4bh()
                   14561: {
                   14562:        switch(REG8(AL)) {
1.1.1.24  root     14563:        case 0x01:
1.1.1.22  root     14564:        case 0x02:
1.1.1.29  root     14565:                // Task Switcher is not installed
1.1.1.24  root     14566:                break;
                   14567:        case 0x03:
                   14568:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14569:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14570:                break;
1.1.1.30  root     14571:        case 0x04:
                   14572:                REG16(BX) = 0x0000; // free switcher id successfully
                   14573:                break;
1.1.1.43  root     14574:        case 0x05:
                   14575:                REG16(BX) = 0x0000; // no instance data chain
                   14576:                SREG(ES) = 0x0000;
                   14577:                i386_load_segment_descriptor(ES);
                   14578:                break;
1.1       root     14579:        default:
1.1.1.22  root     14580:                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     14581:                REG16(AX) = 0x01;
1.1.1.3   root     14582:                m_CF = 1;
1.1       root     14583:                break;
                   14584:        }
                   14585: }
                   14586: 
1.1.1.44  root     14587: inline void msdos_int_2fh_4dh()
                   14588: {
                   14589:        switch(REG8(AL)) {
                   14590:        case 0x00:
                   14591:                // KKCFUNC is not installed ???
                   14592:                break;
                   14593:        default:
                   14594: //             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));
                   14595:                REG16(AX) = 0x01; // invalid function
                   14596:                m_CF = 1;
                   14597:                break;
                   14598:        }
                   14599: }
                   14600: 
1.1       root     14601: inline void msdos_int_2fh_4fh()
                   14602: {
                   14603:        switch(REG8(AL)) {
                   14604:        case 0x00:
1.1.1.29  root     14605:                // BILING is installed
1.1.1.27  root     14606:                REG16(AX) = 0x0000;
                   14607:                REG8(DL) = 0x01;        // major version
                   14608:                REG8(DH) = 0x00;        // minor version
1.1       root     14609:                break;
                   14610:        case 0x01:
1.1.1.27  root     14611:                REG16(AX) = 0x0000;
1.1       root     14612:                REG16(BX) = active_code_page;
                   14613:                break;
                   14614:        default:
1.1.1.22  root     14615:                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));
                   14616:                REG16(AX) = 0x01;
                   14617:                m_CF = 1;
                   14618:                break;
                   14619:        }
                   14620: }
                   14621: 
                   14622: inline void msdos_int_2fh_55h()
                   14623: {
                   14624:        switch(REG8(AL)) {
                   14625:        case 0x00:
                   14626:        case 0x01:
                   14627: //             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));
                   14628:                break;
                   14629:        default:
                   14630:                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     14631:                REG16(AX) = 0x01;
1.1.1.3   root     14632:                m_CF = 1;
1.1       root     14633:                break;
                   14634:        }
                   14635: }
                   14636: 
1.1.1.44  root     14637: inline void msdos_int_2fh_56h()
                   14638: {
                   14639:        switch(REG8(AL)) {
                   14640:        case 0x00:
                   14641:                // INTERLNK is not installed
                   14642:                break;
                   14643:        case 0x01:
                   14644:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14645: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14646: //                     REG8(AL) = 0x00;
                   14647: //             }
                   14648:                break;
                   14649:        default:
                   14650:                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));
                   14651:                REG16(AX) = 0x01;
                   14652:                m_CF = 1;
                   14653:                break;
                   14654:        }
                   14655: }
                   14656: 
1.1.1.24  root     14657: inline void msdos_int_2fh_adh()
                   14658: {
                   14659:        switch(REG8(AL)) {
                   14660:        case 0x00:
1.1.1.29  root     14661:                // DISPLAY.SYS is installed
1.1.1.24  root     14662:                REG8(AL) = 0xff;
                   14663:                REG16(BX) = 0x100; // ???
                   14664:                break;
                   14665:        case 0x01:
                   14666:                active_code_page = REG16(BX);
                   14667:                msdos_nls_tables_update();
                   14668:                REG16(AX) = 0x01;
                   14669:                break;
                   14670:        case 0x02:
                   14671:                REG16(BX) = active_code_page;
                   14672:                break;
                   14673:        case 0x03:
                   14674:                // FIXME
                   14675:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14676:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14677:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14678:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14679:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14680:                break;
                   14681:        case 0x80:
1.1.1.49  root     14682:                // KEYB.COM is not installed
                   14683:                break;
1.1.1.24  root     14684:        default:
                   14685:                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));
                   14686:                REG16(AX) = 0x01;
                   14687:                m_CF = 1;
                   14688:                break;
                   14689:        }
                   14690: }
                   14691: 
1.1       root     14692: inline void msdos_int_2fh_aeh()
                   14693: {
                   14694:        switch(REG8(AL)) {
                   14695:        case 0x00:
1.1.1.28  root     14696:                // FIXME: we need to check the given command line
                   14697:                REG8(AL) = 0x00; // the command should be executed as usual
                   14698: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14699:                break;
                   14700:        case 0x01:
                   14701:                {
                   14702:                        char command[MAX_PATH];
                   14703:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14704:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14705:                        
                   14706:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14707:                        param->env_seg = 0;
                   14708:                        param->cmd_line.w.l = 44;
                   14709:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14710:                        param->fcb1.w.l = 24;
                   14711:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14712:                        param->fcb2.w.l = 24;
                   14713:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14714:                        
                   14715:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14716:                        
                   14717:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14718:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14719:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14720:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14721:                        
1.1.1.28  root     14722:                        try {
                   14723:                                msdos_process_exec(command, param, 0);
                   14724:                        } catch(...) {
                   14725:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14726:                        }
                   14727:                }
                   14728:                break;
                   14729:        default:
1.1.1.22  root     14730:                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     14731:                REG16(AX) = 0x01;
1.1.1.3   root     14732:                m_CF = 1;
1.1       root     14733:                break;
                   14734:        }
                   14735: }
                   14736: 
1.1.1.34  root     14737: inline void msdos_int_2fh_b7h()
                   14738: {
                   14739:        switch(REG8(AL)) {
                   14740:        case 0x00:
                   14741:                // APPEND is not installed
                   14742: //             REG8(AL) = 0x00;
                   14743:                break;
1.1.1.44  root     14744:        case 0x06:
                   14745:                REG16(BX) = 0x0000;
                   14746:                break;
1.1.1.34  root     14747:        case 0x07:
1.1.1.43  root     14748:        case 0x11:
1.1.1.34  root     14749:                // COMMAND.COM calls this service without checking APPEND is installed
                   14750:                break;
                   14751:        default:
                   14752:                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));
                   14753:                REG16(AX) = 0x01;
                   14754:                m_CF = 1;
                   14755:                break;
                   14756:        }
                   14757: }
                   14758: 
1.1.1.24  root     14759: inline void msdos_int_33h_0000h()
                   14760: {
                   14761:        REG16(AX) = 0xffff; // hardware/driver installed
                   14762:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14763: }
                   14764: 
                   14765: inline void msdos_int_33h_0001h()
                   14766: {
1.1.1.34  root     14767:        if(mouse.hidden > 0) {
                   14768:                mouse.hidden--;
                   14769:        }
                   14770:        if(mouse.hidden == 0) {
1.1.1.24  root     14771:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14772:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14773:                }
                   14774:                pic[1].imr &= ~0x10;    // enable irq12
                   14775:        }
                   14776: }
                   14777: 
                   14778: inline void msdos_int_33h_0002h()
                   14779: {
1.1.1.34  root     14780:        mouse.hidden++;
                   14781: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   14782:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     14783: }
                   14784: 
                   14785: inline void msdos_int_33h_0003h()
                   14786: {
1.1.1.34  root     14787: //     if(mouse.hidden > 0) {
                   14788:                update_console_input();
                   14789: //     }
1.1.1.24  root     14790:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14791:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14792:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14793: }
                   14794: 
                   14795: inline void msdos_int_33h_0004h()
                   14796: {
                   14797:        mouse.position.x = REG16(CX);
                   14798:        mouse.position.x = REG16(DX);
1.1.1.24  root     14799: }
                   14800: 
                   14801: inline void msdos_int_33h_0005h()
                   14802: {
1.1.1.34  root     14803: //     if(mouse.hidden > 0) {
                   14804:                update_console_input();
                   14805: //     }
1.1.1.24  root     14806:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14807:                int idx = REG16(BX);
1.1.1.34  root     14808:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14809:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14810:                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     14811:                mouse.buttons[idx].pressed_times = 0;
                   14812:        } else {
                   14813:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14814:        }
                   14815:        REG16(AX) = mouse.get_buttons();
                   14816: }
                   14817: 
                   14818: inline void msdos_int_33h_0006h()
                   14819: {
1.1.1.34  root     14820: //     if(mouse.hidden > 0) {
                   14821:                update_console_input();
                   14822: //     }
1.1.1.24  root     14823:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14824:                int idx = REG16(BX);
1.1.1.34  root     14825:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14826:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14827:                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     14828:                mouse.buttons[idx].released_times = 0;
                   14829:        } else {
                   14830:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14831:        }
                   14832:        REG16(AX) = mouse.get_buttons();
                   14833: }
                   14834: 
                   14835: inline void msdos_int_33h_0007h()
                   14836: {
                   14837:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14838:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14839: }
                   14840: 
                   14841: inline void msdos_int_33h_0008h()
                   14842: {
                   14843:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14844:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14845: }
                   14846: 
                   14847: inline void msdos_int_33h_0009h()
                   14848: {
                   14849:        mouse.hot_spot[0] = REG16(BX);
                   14850:        mouse.hot_spot[1] = REG16(CX);
                   14851: }
                   14852: 
1.1.1.49  root     14853: inline void msdos_int_33h_000ah()
                   14854: {
                   14855:        mouse.screen_mask = REG16(CX);
                   14856:        mouse.cursor_mask = REG16(DX);
                   14857: }
                   14858: 
1.1.1.24  root     14859: inline void msdos_int_33h_000bh()
                   14860: {
1.1.1.34  root     14861: //     if(mouse.hidden > 0) {
                   14862:                update_console_input();
                   14863: //     }
1.1.1.24  root     14864:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14865:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14866:        mouse.prev_position.x = mouse.position.x;
                   14867:        mouse.prev_position.y = mouse.position.y;
                   14868:        REG16(CX) = dx;
                   14869:        REG16(DX) = dy;
                   14870: }
                   14871: 
                   14872: inline void msdos_int_33h_000ch()
                   14873: {
                   14874:        mouse.call_mask = REG16(CX);
                   14875:        mouse.call_addr.w.l = REG16(DX);
                   14876:        mouse.call_addr.w.h = SREG(ES);
                   14877: }
                   14878: 
                   14879: inline void msdos_int_33h_000fh()
                   14880: {
                   14881:        mouse.mickey.x = REG16(CX);
                   14882:        mouse.mickey.y = REG16(DX);
                   14883: }
                   14884: 
                   14885: inline void msdos_int_33h_0011h()
                   14886: {
                   14887:        REG16(AX) = 0xffff;
                   14888:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14889: }
                   14890: 
                   14891: inline void msdos_int_33h_0014h()
                   14892: {
                   14893:        UINT16 old_mask = mouse.call_mask;
                   14894:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14895:        UINT16 old_seg = mouse.call_addr.w.h;
                   14896:        
                   14897:        mouse.call_mask = REG16(CX);
                   14898:        mouse.call_addr.w.l = REG16(DX);
                   14899:        mouse.call_addr.w.h = SREG(ES);
                   14900:        
                   14901:        REG16(CX) = old_mask;
                   14902:        REG16(DX) = old_ofs;
                   14903:        SREG(ES) = old_seg;
                   14904:        i386_load_segment_descriptor(ES);
                   14905: }
                   14906: 
                   14907: inline void msdos_int_33h_0015h()
                   14908: {
                   14909:        REG16(BX) = sizeof(mouse);
                   14910: }
                   14911: 
                   14912: inline void msdos_int_33h_0016h()
                   14913: {
                   14914:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14915: }
                   14916: 
                   14917: inline void msdos_int_33h_0017h()
                   14918: {
                   14919:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14920: }
                   14921: 
1.1.1.43  root     14922: inline void msdos_int_33h_0018h()
                   14923: {
                   14924:        for(int i = 0; i < 8; i++) {
                   14925:                if(REG16(CX) & (1 << i)) {
                   14926:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14927:                                // event handler already exists
                   14928:                                REG16(AX) = 0xffff;
                   14929:                                break;
                   14930:                        }
                   14931:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14932:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14933:                }
                   14934:        }
                   14935: }
                   14936: 
                   14937: inline void msdos_int_33h_0019h()
                   14938: {
                   14939:        UINT16 call_mask = REG16(CX);
                   14940:        
                   14941:        REG16(CX) = 0;
                   14942:        
                   14943:        for(int i = 0; i < 8; i++) {
                   14944:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14945:                        for(int j = 0; j < 8; j++) {
                   14946:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14947:                                        REG16(CX) |= (1 << j);
                   14948:                                }
                   14949:                        }
                   14950:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14951:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14952:                        break;
                   14953:                }
                   14954:        }
                   14955: }
                   14956: 
1.1.1.24  root     14957: inline void msdos_int_33h_001ah()
                   14958: {
                   14959:        mouse.sensitivity[0] = REG16(BX);
                   14960:        mouse.sensitivity[1] = REG16(CX);
                   14961:        mouse.sensitivity[2] = REG16(DX);
                   14962: }
                   14963: 
                   14964: inline void msdos_int_33h_001bh()
                   14965: {
                   14966:        REG16(BX) = mouse.sensitivity[0];
                   14967:        REG16(CX) = mouse.sensitivity[1];
                   14968:        REG16(DX) = mouse.sensitivity[2];
                   14969: }
                   14970: 
                   14971: inline void msdos_int_33h_001dh()
                   14972: {
                   14973:        mouse.display_page = REG16(BX);
                   14974: }
                   14975: 
                   14976: inline void msdos_int_33h_001eh()
                   14977: {
                   14978:        REG16(BX) = mouse.display_page;
                   14979: }
                   14980: 
1.1.1.34  root     14981: inline void msdos_int_33h_001fh()
                   14982: {
                   14983:        // from DOSBox
                   14984:        REG16(BX) = 0x0000;
                   14985:        SREG(ES) = 0x0000;
                   14986:        i386_load_segment_descriptor(ES);
                   14987:        mouse.enabled = false;
                   14988:        mouse.old_hidden = mouse.hidden;
                   14989:        mouse.hidden = 1;
                   14990: }
                   14991: 
                   14992: inline void msdos_int_33h_0020h()
                   14993: {
                   14994:        // from DOSBox
                   14995:        mouse.enabled = true;
                   14996:        mouse.hidden = mouse.old_hidden;
                   14997: }
                   14998: 
1.1.1.24  root     14999: inline void msdos_int_33h_0021h()
                   15000: {
                   15001:        REG16(AX) = 0xffff;
                   15002:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15003: }
                   15004: 
                   15005: inline void msdos_int_33h_0022h()
                   15006: {
                   15007:        mouse.language = REG16(BX);
                   15008: }
                   15009: 
                   15010: inline void msdos_int_33h_0023h()
                   15011: {
                   15012:        REG16(BX) = mouse.language;
                   15013: }
                   15014: 
                   15015: inline void msdos_int_33h_0024h()
                   15016: {
                   15017:        REG16(BX) = 0x0805; // V8.05
                   15018:        REG16(CX) = 0x0400; // PS/2
                   15019: }
                   15020: 
1.1.1.49  root     15021: inline void msdos_int_33h_0025h()
                   15022: {
                   15023:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   15024: }
                   15025: 
1.1.1.24  root     15026: inline void msdos_int_33h_0026h()
                   15027: {
                   15028:        REG16(BX) = 0x0000;
                   15029:        REG16(CX) = mouse.max_position.x;
                   15030:        REG16(DX) = mouse.max_position.y;
                   15031: }
                   15032: 
1.1.1.49  root     15033: inline void msdos_int_33h_0027h()
                   15034: {
                   15035: //     if(mouse.hidden > 0) {
                   15036:                update_console_input();
                   15037: //     }
                   15038:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15039:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15040:        mouse.prev_position.x = mouse.position.x;
                   15041:        mouse.prev_position.y = mouse.position.y;
                   15042:        REG16(AX) = mouse.screen_mask;
                   15043:        REG16(BX) = mouse.cursor_mask;
                   15044:        REG16(CX) = dx;
                   15045:        REG16(DX) = dy;
                   15046: }
                   15047: 
                   15048: inline void msdos_int_33h_0028h()
                   15049: {
                   15050:        if(REG16(CX) != 0) {
                   15051:                UINT8 tmp = REG8(AL);
                   15052:                REG8(AL) = REG8(CL);
                   15053:                pcbios_int_10h_00h();
                   15054:                REG8(AL) = tmp;
                   15055:        }
                   15056:        REG8(CL) = 0x00; // successful
                   15057: }
                   15058: 
                   15059: inline void msdos_int_33h_0029h()
                   15060: {
                   15061:        switch(REG16(CX)) {
                   15062:        case 0x0000:
                   15063:                REG16(CX) = 0x0003;
                   15064:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   15065:                break;
                   15066:        case 0x0003:
                   15067:                REG16(CX) = 0x0070;
                   15068:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15069:                break;
                   15070:        case 0x0070:
                   15071:                REG16(CX) = 0x0071;
                   15072:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15073:                break;
                   15074:        case 0x0071:
                   15075:                REG16(CX) = 0x0073;
                   15076:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   15077:                break;
                   15078:        default:
                   15079:                REG16(CX) = 0x0000;
                   15080:                break;
                   15081:        }
                   15082:        if(REG16(CX) != 0) {
                   15083:                SREG(DS) = (WORK_TOP >> 4);
                   15084:        } else {
                   15085:                SREG(DS) = 0x0000;
                   15086:        }
                   15087:        i386_load_segment_descriptor(DS);
                   15088:        REG16(DX) = 0x0000;
                   15089: }
                   15090: 
1.1.1.24  root     15091: inline void msdos_int_33h_002ah()
                   15092: {
1.1.1.34  root     15093:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     15094:        REG16(BX) = mouse.hot_spot[0];
                   15095:        REG16(CX) = mouse.hot_spot[1];
                   15096:        REG16(DX) = 4; // PS/2
                   15097: }
                   15098: 
                   15099: inline void msdos_int_33h_0031h()
                   15100: {
                   15101:        REG16(AX) = mouse.min_position.x;
                   15102:        REG16(BX) = mouse.min_position.y;
                   15103:        REG16(CX) = mouse.max_position.x;
                   15104:        REG16(DX) = mouse.max_position.y;
                   15105: }
                   15106: 
                   15107: inline void msdos_int_33h_0032h()
                   15108: {
                   15109:        REG16(AX) = 0;
1.1.1.49  root     15110:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     15111:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     15112:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     15113: //     REG16(AX) |= 0x1000; // 0028h
                   15114: //     REG16(AX) |= 0x0800; // 0029h
                   15115:        REG16(AX) |= 0x0400; // 002ah
                   15116: //     REG16(AX) |= 0x0200; // 002bh
                   15117: //     REG16(AX) |= 0x0100; // 002ch
                   15118: //     REG16(AX) |= 0x0080; // 002dh
                   15119: //     REG16(AX) |= 0x0040; // 002eh
                   15120:        REG16(AX) |= 0x0020; // 002fh
                   15121: //     REG16(AX) |= 0x0010; // 0030h
                   15122:        REG16(AX) |= 0x0008; // 0031h
                   15123:        REG16(AX) |= 0x0004; // 0032h
                   15124: //     REG16(AX) |= 0x0002; // 0033h
                   15125: //     REG16(AX) |= 0x0001; // 0034h
                   15126: }
                   15127: 
1.1.1.49  root     15128: inline void msdos_int_33h_004dh()
                   15129: {
                   15130:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   15131: }
                   15132: 
                   15133: inline void msdos_int_33h_006dh()
                   15134: {
                   15135:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   15136:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   15137: }
                   15138: 
1.1.1.19  root     15139: inline void msdos_int_67h_40h()
                   15140: {
                   15141:        if(!support_ems) {
                   15142:                REG8(AH) = 0x84;
                   15143:        } else {
                   15144:                REG8(AH) = 0x00;
                   15145:        }
                   15146: }
                   15147: 
                   15148: inline void msdos_int_67h_41h()
                   15149: {
                   15150:        if(!support_ems) {
                   15151:                REG8(AH) = 0x84;
                   15152:        } else {
                   15153:                REG8(AH) = 0x00;
                   15154:                REG16(BX) = EMS_TOP >> 4;
                   15155:        }
                   15156: }
                   15157: 
                   15158: inline void msdos_int_67h_42h()
                   15159: {
                   15160:        if(!support_ems) {
                   15161:                REG8(AH) = 0x84;
                   15162:        } else {
                   15163:                REG8(AH) = 0x00;
                   15164:                REG16(BX) = free_ems_pages;
                   15165:                REG16(DX) = MAX_EMS_PAGES;
                   15166:        }
                   15167: }
                   15168: 
                   15169: inline void msdos_int_67h_43h()
                   15170: {
                   15171:        if(!support_ems) {
                   15172:                REG8(AH) = 0x84;
                   15173:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15174:                REG8(AH) = 0x87;
                   15175:        } else if(REG16(BX) > free_ems_pages) {
                   15176:                REG8(AH) = 0x88;
                   15177:        } else if(REG16(BX) == 0) {
                   15178:                REG8(AH) = 0x89;
                   15179:        } else {
1.1.1.31  root     15180:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15181:                        if(!ems_handles[i].allocated) {
                   15182:                                ems_allocate_pages(i, REG16(BX));
                   15183:                                REG8(AH) = 0x00;
                   15184:                                REG16(DX) = i;
                   15185:                                return;
                   15186:                        }
                   15187:                }
                   15188:                REG8(AH) = 0x85;
                   15189:        }
                   15190: }
                   15191: 
                   15192: inline void msdos_int_67h_44h()
                   15193: {
                   15194:        if(!support_ems) {
                   15195:                REG8(AH) = 0x84;
1.1.1.31  root     15196:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15197:                REG8(AH) = 0x83;
                   15198:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15199:                REG8(AH) = 0x8a;
                   15200: //     } else if(!(REG8(AL) < 4)) {
                   15201: //             REG8(AH) = 0x8b;
                   15202:        } else if(REG16(BX) == 0xffff) {
                   15203:                ems_unmap_page(REG8(AL) & 3);
                   15204:                REG8(AH) = 0x00;
                   15205:        } else {
                   15206:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15207:                REG8(AH) = 0x00;
                   15208:        }
                   15209: }
                   15210: 
                   15211: inline void msdos_int_67h_45h()
                   15212: {
                   15213:        if(!support_ems) {
                   15214:                REG8(AH) = 0x84;
1.1.1.31  root     15215:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15216:                REG8(AH) = 0x83;
                   15217:        } else {
                   15218:                ems_release_pages(REG16(DX));
                   15219:                REG8(AH) = 0x00;
                   15220:        }
                   15221: }
                   15222: 
                   15223: inline void msdos_int_67h_46h()
                   15224: {
                   15225:        if(!support_ems) {
                   15226:                REG8(AH) = 0x84;
                   15227:        } else {
1.1.1.29  root     15228: //             REG16(AX) = 0x0032; // EMS 3.2
                   15229:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15230:        }
                   15231: }
                   15232: 
                   15233: inline void msdos_int_67h_47h()
                   15234: {
                   15235:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15236:        process_t *process = msdos_process_info_get(current_psp);
                   15237:        
                   15238:        if(!support_ems) {
                   15239:                REG8(AH) = 0x84;
1.1.1.31  root     15240: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15241: //             REG8(AH) = 0x83;
                   15242:        } else if(process->ems_pages_stored) {
                   15243:                REG8(AH) = 0x8d;
                   15244:        } else {
                   15245:                for(int i = 0; i < 4; i++) {
                   15246:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15247:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15248:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15249:                }
                   15250:                process->ems_pages_stored = true;
                   15251:                REG8(AH) = 0x00;
                   15252:        }
                   15253: }
                   15254: 
                   15255: inline void msdos_int_67h_48h()
                   15256: {
                   15257:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15258:        process_t *process = msdos_process_info_get(current_psp);
                   15259:        
                   15260:        if(!support_ems) {
                   15261:                REG8(AH) = 0x84;
1.1.1.31  root     15262: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15263: //             REG8(AH) = 0x83;
                   15264:        } else if(!process->ems_pages_stored) {
                   15265:                REG8(AH) = 0x8e;
                   15266:        } else {
                   15267:                for(int i = 0; i < 4; i++) {
                   15268:                        if(process->ems_pages[i].mapped) {
                   15269:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15270:                        } else {
                   15271:                                ems_unmap_page(i);
                   15272:                        }
                   15273:                }
                   15274:                process->ems_pages_stored = false;
                   15275:                REG8(AH) = 0x00;
                   15276:        }
                   15277: }
                   15278: 
                   15279: inline void msdos_int_67h_4bh()
                   15280: {
                   15281:        if(!support_ems) {
                   15282:                REG8(AH) = 0x84;
                   15283:        } else {
                   15284:                REG8(AH) = 0x00;
                   15285:                REG16(BX) = 0;
1.1.1.31  root     15286:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15287:                        if(ems_handles[i].allocated) {
                   15288:                                REG16(BX)++;
                   15289:                        }
                   15290:                }
                   15291:        }
                   15292: }
                   15293: 
                   15294: inline void msdos_int_67h_4ch()
                   15295: {
                   15296:        if(!support_ems) {
                   15297:                REG8(AH) = 0x84;
1.1.1.31  root     15298:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15299:                REG8(AH) = 0x83;
                   15300:        } else {
                   15301:                REG8(AH) = 0x00;
                   15302:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15303:        }
                   15304: }
                   15305: 
                   15306: inline void msdos_int_67h_4dh()
                   15307: {
                   15308:        if(!support_ems) {
                   15309:                REG8(AH) = 0x84;
                   15310:        } else {
                   15311:                REG8(AH) = 0x00;
                   15312:                REG16(BX) = 0;
1.1.1.31  root     15313:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15314:                        if(ems_handles[i].allocated) {
                   15315:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15316:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15317:                                REG16(BX)++;
                   15318:                        }
                   15319:                }
                   15320:        }
                   15321: }
                   15322: 
1.1.1.20  root     15323: inline void msdos_int_67h_4eh()
                   15324: {
                   15325:        if(!support_ems) {
                   15326:                REG8(AH) = 0x84;
                   15327:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15328:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15329:                        // save page map
                   15330:                        for(int i = 0; i < 4; i++) {
                   15331:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15332:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15333:                        }
                   15334:                }
                   15335:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15336:                        // restore page map
                   15337:                        for(int i = 0; i < 4; i++) {
                   15338:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15339:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15340:                                
1.1.1.31  root     15341:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15342:                                        ems_map_page(i, handle, page);
                   15343:                                } else {
                   15344:                                        ems_unmap_page(i);
                   15345:                                }
                   15346:                        }
                   15347:                }
                   15348:                REG8(AH) = 0x00;
                   15349:        } else if(REG8(AL) == 0x03) {
                   15350:                REG8(AH) = 0x00;
1.1.1.21  root     15351:                REG8(AL) = 4 * 4;
                   15352:        } else {
1.1.1.22  root     15353:                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     15354:                REG8(AH) = 0x8f;
                   15355:        }
                   15356: }
                   15357: 
                   15358: inline void msdos_int_67h_4fh()
                   15359: {
                   15360:        if(!support_ems) {
                   15361:                REG8(AH) = 0x84;
                   15362:        } else if(REG8(AL) == 0x00) {
                   15363:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15364:                
                   15365:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15366:                for(int i = 0; i < count; i++) {
                   15367:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15368:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15369:                        
                   15370: //                     if(!(physical < 4)) {
                   15371: //                             REG8(AH) = 0x8b;
                   15372: //                             return;
                   15373: //                     }
                   15374:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15375:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15376:                        *(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     15377:                }
                   15378:                REG8(AH) = 0x00;
                   15379:        } else if(REG8(AL) == 0x01) {
                   15380:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15381:                
                   15382:                for(int i = 0; i < count; i++) {
                   15383:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15384:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15385:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15386:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15387:                        
                   15388: //                     if(!(physical < 4)) {
                   15389: //                             REG8(AH) = 0x8b;
                   15390: //                             return;
                   15391: //                     } else
1.1.1.41  root     15392:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15393:                                ems_map_page(physical & 3, handle, logical);
                   15394:                        } else {
1.1.1.41  root     15395:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15396:                        }
                   15397:                }
                   15398:                REG8(AH) = 0x00;
                   15399:        } else if(REG8(AL) == 0x02) {
                   15400:                REG8(AH) = 0x00;
                   15401:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15402:        } else {
1.1.1.22  root     15403:                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     15404:                REG8(AH) = 0x8f;
                   15405:        }
                   15406: }
                   15407: 
                   15408: inline void msdos_int_67h_50h()
                   15409: {
                   15410:        if(!support_ems) {
                   15411:                REG8(AH) = 0x84;
1.1.1.31  root     15412:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15413:                REG8(AH) = 0x83;
                   15414:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15415:                for(int i = 0; i < REG16(CX); i++) {
                   15416:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15417:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15418:                        
                   15419:                        if(REG8(AL) == 0x01) {
                   15420:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15421:                        }
                   15422: //                     if(!(physical < 4)) {
                   15423: //                             REG8(AH) = 0x8b;
                   15424: //                             return;
                   15425: //                     } else
                   15426:                        if(logical == 0xffff) {
                   15427:                                ems_unmap_page(physical & 3);
                   15428:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15429:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15430:                        } else {
                   15431:                                REG8(AH) = 0x8a;
                   15432:                                return;
                   15433:                        }
                   15434:                }
                   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_51h()
                   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(REG16(BX) > MAX_EMS_PAGES) {
                   15449:                REG8(AH) = 0x87;
                   15450:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15451:                REG8(AH) = 0x88;
                   15452:        } else {
                   15453:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15454:                REG8(AH) = 0x00;
                   15455:        }
                   15456: }
                   15457: 
1.1.1.20  root     15458: inline void msdos_int_67h_52h()
                   15459: {
                   15460:        if(!support_ems) {
                   15461:                REG8(AH) = 0x84;
1.1.1.31  root     15462: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15463: //             REG8(AH) = 0x83;
1.1.1.20  root     15464:        } else if(REG8(AL) == 0x00) {
                   15465:                REG8(AL) = 0x00; // handle is volatile
                   15466:                REG8(AH) = 0x00;
                   15467:        } else if(REG8(AL) == 0x01) {
                   15468:                if(REG8(BL) == 0x00) {
                   15469:                        REG8(AH) = 0x00;
                   15470:                } else {
                   15471:                        REG8(AH) = 0x90; // undefined attribute type
                   15472:                }
                   15473:        } else if(REG8(AL) == 0x02) {
                   15474:                REG8(AL) = 0x00; // only volatile handles supported
                   15475:                REG8(AH) = 0x00;
                   15476:        } else {
1.1.1.22  root     15477:                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     15478:                REG8(AH) = 0x8f;
                   15479:        }
                   15480: }
                   15481: 
1.1.1.19  root     15482: inline void msdos_int_67h_53h()
                   15483: {
                   15484:        if(!support_ems) {
                   15485:                REG8(AH) = 0x84;
1.1.1.31  root     15486:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15487:                REG8(AH) = 0x83;
                   15488:        } else if(REG8(AL) == 0x00) {
                   15489:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15490:                REG8(AH) = 0x00;
                   15491:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15492:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15493:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15494:                                REG8(AH) = 0xa1;
                   15495:                                return;
                   15496:                        }
                   15497:                }
                   15498:                REG8(AH) = 0x00;
                   15499:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15500:        } else {
1.1.1.22  root     15501:                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     15502:                REG8(AH) = 0x8f;
1.1.1.19  root     15503:        }
                   15504: }
                   15505: 
                   15506: inline void msdos_int_67h_54h()
                   15507: {
                   15508:        if(!support_ems) {
                   15509:                REG8(AH) = 0x84;
                   15510:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15511:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15512:                        if(ems_handles[i].allocated) {
                   15513:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15514:                        } else {
                   15515:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15516:                        }
                   15517:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15518:                }
                   15519:                REG8(AH) = 0x00;
                   15520:                REG8(AL) = MAX_EMS_HANDLES;
                   15521:        } else if(REG8(AL) == 0x01) {
                   15522:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15523:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15524:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15525:                                REG8(AH) = 0x00;
                   15526:                                REG16(DX) = i;
                   15527:                                break;
                   15528:                        }
                   15529:                }
                   15530:        } else if(REG8(AL) == 0x02) {
                   15531:                REG8(AH) = 0x00;
                   15532:                REG16(BX) = MAX_EMS_HANDLES;
                   15533:        } else {
1.1.1.22  root     15534:                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     15535:                REG8(AH) = 0x8f;
                   15536:        }
                   15537: }
                   15538: 
1.1.1.49  root     15539: inline void msdos_int_67h_55h()
                   15540: {
                   15541:        if(!support_ems) {
                   15542:                REG8(AH) = 0x84;
                   15543:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15544:                REG8(AH) = 0x83;
                   15545:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15546:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15547:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15548:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15549:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15550:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15551:                
                   15552:                for(int i = 0; i < (int)entries; i++) {
                   15553:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15554:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15555:                        
                   15556:                        if(REG8(AL) == 0x01) {
                   15557:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15558:                        }
                   15559: //                     if(!(physical < 4)) {
                   15560: //                             REG8(AH) = 0x8b;
                   15561: //                             return;
                   15562: //                     } else
                   15563:                        if(logical == 0xffff) {
                   15564:                                ems_unmap_page(physical & 3);
                   15565:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15566:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15567:                        } else {
                   15568:                                REG8(AH) = 0x8a;
                   15569:                                return;
                   15570:                        }
                   15571:                }
                   15572:                i386_jmp_far(jump_seg, jump_ofs);
                   15573:                REG8(AH) = 0x00;
                   15574:        } else {
                   15575:                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));
                   15576:                REG8(AH) = 0x8f;
                   15577:        }
                   15578: }
                   15579: 
                   15580: inline void msdos_int_67h_56h()
                   15581: {
                   15582:        if(!support_ems) {
                   15583:                REG8(AH) = 0x84;
                   15584:        } else if(REG8(AL) == 0x02) {
                   15585:                REG16(BX) = (2 + 2) * 4;
                   15586:                REG8(AH) = 0x00;
                   15587:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15588:                REG8(AH) = 0x83;
                   15589:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15590:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15591:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15592:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15593:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15594:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15595: #if 0
                   15596:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15597:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15598:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15599: #endif
                   15600:                UINT16 handles[4], pages[4];
                   15601:                
                   15602:                // alter page map and call routine is at fffc:001f
                   15603:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15604:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15605:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15606:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15607:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15608:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15609:                } else {
                   15610:                        // invalid call addr :-(
                   15611:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15612:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15613:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15614:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15615:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15616:                }
                   15617:                // do call far (push cs/ip) in old mapping
                   15618:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15619:                
                   15620:                // get old mapping data
                   15621: #if 0
                   15622:                for(int i = 0; i < (int)old_entries; i++) {
                   15623:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15624:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15625:                        
                   15626:                        if(REG8(AL) == 0x01) {
                   15627:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15628:                        }
                   15629: //                     if(!(physical < 4)) {
                   15630: //                             REG8(AH) = 0x8b;
                   15631: //                             return;
                   15632: //                     } else
                   15633:                        if(logical == 0xffff) {
                   15634:                                ems_unmap_page(physical & 3);
                   15635:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15636:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15637:                        } else {
                   15638:                                REG8(AH) = 0x8a;
                   15639:                                return;
                   15640:                        }
                   15641:                }
                   15642: #endif
                   15643:                for(int i = 0; i < 4; i++) {
                   15644:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15645:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15646:                }
                   15647:                
                   15648:                // set new mapping
                   15649:                for(int i = 0; i < (int)new_entries; i++) {
                   15650:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   15651:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   15652:                        
                   15653:                        if(REG8(AL) == 0x01) {
                   15654:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15655:                        }
                   15656: //                     if(!(physical < 4)) {
                   15657: //                             REG8(AH) = 0x8b;
                   15658: //                             return;
                   15659: //                     } else
                   15660:                        if(logical == 0xffff) {
                   15661:                                ems_unmap_page(physical & 3);
                   15662:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15663:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15664:                        } else {
                   15665:                                REG8(AH) = 0x8a;
                   15666:                                return;
                   15667:                        }
                   15668:                }
                   15669:                
                   15670:                // push old mapping data in new mapping
                   15671:                for(int i = 0; i < 4; i++) {
                   15672:                        i386_push16(handles[i]);
                   15673:                        i386_push16(pages  [i]);
                   15674:                }
                   15675:                REG8(AH) = 0x00;
                   15676:        } else {
                   15677:                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));
                   15678:                REG8(AH) = 0x8f;
                   15679:        }
                   15680: }
                   15681: 
1.1.1.20  root     15682: inline void msdos_int_67h_57h_tmp()
                   15683: {
                   15684:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15685:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15686:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   15687:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   15688:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   15689:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   15690:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15691:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   15692:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   15693:        
1.1.1.32  root     15694:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     15695:        UINT32 src_addr, dest_addr;
                   15696:        UINT32 src_addr_max, dest_addr_max;
                   15697:        
                   15698:        if(src_type == 0) {
                   15699:                src_buffer = mem;
                   15700:                src_addr = (src_seg << 4) + src_ofs;
                   15701:                src_addr_max = MAX_MEM;
                   15702:        } else {
1.1.1.31  root     15703:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     15704:                        REG8(AH) = 0x83;
                   15705:                        return;
                   15706:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   15707:                        REG8(AH) = 0x8a;
                   15708:                        return;
                   15709:                }
1.1.1.32  root     15710:                if(ems_handles[src_handle].buffer != NULL) {
                   15711:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   15712:                }
1.1.1.20  root     15713:                src_addr = src_ofs;
1.1.1.32  root     15714:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     15715:        }
                   15716:        if(dest_type == 0) {
                   15717:                dest_buffer = mem;
                   15718:                dest_addr = (dest_seg << 4) + dest_ofs;
                   15719:                dest_addr_max = MAX_MEM;
                   15720:        } else {
1.1.1.31  root     15721:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     15722:                        REG8(AH) = 0x83;
                   15723:                        return;
                   15724:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   15725:                        REG8(AH) = 0x8a;
                   15726:                        return;
                   15727:                }
1.1.1.32  root     15728:                if(ems_handles[dest_handle].buffer != NULL) {
                   15729:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   15730:                }
1.1.1.20  root     15731:                dest_addr = dest_ofs;
1.1.1.32  root     15732:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     15733:        }
1.1.1.32  root     15734:        if(src_buffer != NULL && dest_buffer != NULL) {
                   15735:                for(int i = 0; i < copy_length; i++) {
                   15736:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15737:                                if(REG8(AL) == 0x00) {
                   15738:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15739:                                } else if(REG8(AL) == 0x01) {
                   15740:                                        UINT8 tmp = dest_buffer[dest_addr];
                   15741:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   15742:                                        src_buffer[src_addr++] = tmp;
                   15743:                                }
                   15744:                        } else {
                   15745:                                REG8(AH) = 0x93;
                   15746:                                return;
1.1.1.20  root     15747:                        }
                   15748:                }
1.1.1.32  root     15749:                REG8(AH) = 0x00;
                   15750:        } else {
                   15751:                REG8(AH) = 0x80;
1.1.1.20  root     15752:        }
                   15753: }
                   15754: 
                   15755: inline void msdos_int_67h_57h()
                   15756: {
                   15757:        if(!support_ems) {
                   15758:                REG8(AH) = 0x84;
                   15759:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15760:                struct {
                   15761:                        UINT16 handle;
                   15762:                        UINT16 page;
                   15763:                        bool mapped;
                   15764:                } tmp_pages[4];
                   15765:                
                   15766:                // unmap pages to copy memory data to ems buffer
                   15767:                for(int i = 0; i < 4; i++) {
                   15768:                        tmp_pages[i].handle = ems_pages[i].handle;
                   15769:                        tmp_pages[i].page   = ems_pages[i].page;
                   15770:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   15771:                        ems_unmap_page(i);
                   15772:                }
                   15773:                
                   15774:                // run move/exchange operation
                   15775:                msdos_int_67h_57h_tmp();
                   15776:                
                   15777:                // restore unmapped pages
                   15778:                for(int i = 0; i < 4; i++) {
                   15779:                        if(tmp_pages[i].mapped) {
                   15780:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   15781:                        }
                   15782:                }
                   15783:        } else {
1.1.1.22  root     15784:                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     15785:                REG8(AH) = 0x8f;
                   15786:        }
                   15787: }
                   15788: 
                   15789: inline void msdos_int_67h_58h()
                   15790: {
                   15791:        if(!support_ems) {
                   15792:                REG8(AH) = 0x84;
                   15793:        } else if(REG8(AL) == 0x00) {
                   15794:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     15795:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   15796:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     15797:                }
                   15798:                REG8(AH) = 0x00;
                   15799:                REG16(CX) = 4;
                   15800:        } else if(REG8(AL) == 0x01) {
                   15801:                REG8(AH) = 0x00;
                   15802:                REG16(CX) = 4;
                   15803:        } else {
1.1.1.22  root     15804:                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     15805:                REG8(AH) = 0x8f;
                   15806:        }
                   15807: }
                   15808: 
1.1.1.42  root     15809: inline void msdos_int_67h_59h()
                   15810: {
                   15811:        if(!support_ems) {
                   15812:                REG8(AH) = 0x84;
                   15813:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     15814:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   15815:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   15816:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   15817:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   15818:                REG8(AH) = 0x00;
                   15819: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     15820:        } else if(REG8(AL) == 0x01) {
                   15821:                REG8(AH) = 0x00;
                   15822:                REG16(BX) = free_ems_pages;
                   15823:                REG16(DX) = MAX_EMS_PAGES;
                   15824:        } else {
                   15825:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   15826:                REG8(AH) = 0x8f;
                   15827:        }
                   15828: }
                   15829: 
1.1.1.20  root     15830: inline void msdos_int_67h_5ah()
                   15831: {
                   15832:        if(!support_ems) {
1.1.1.19  root     15833:                REG8(AH) = 0x84;
1.1.1.20  root     15834:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15835:                REG8(AH) = 0x87;
                   15836:        } else if(REG16(BX) > free_ems_pages) {
                   15837:                REG8(AH) = 0x88;
                   15838: //     } else if(REG16(BX) == 0) {
                   15839: //             REG8(AH) = 0x89;
                   15840:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     15841:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     15842:                        if(!ems_handles[i].allocated) {
                   15843:                                ems_allocate_pages(i, REG16(BX));
                   15844:                                REG8(AH) = 0x00;
                   15845:                                REG16(DX) = i;
                   15846:                                return;
                   15847:                        }
                   15848:                }
                   15849:                REG8(AH) = 0x85;
                   15850:        } else {
1.1.1.22  root     15851:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.20  root     15852:                REG8(AH) = 0x8f;
1.1.1.19  root     15853:        }
                   15854: }
                   15855: 
1.1.1.49  root     15856: inline void msdos_int_67h_5bh()
                   15857: {
                   15858:        static UINT8  stored_bl = 0x00;
                   15859:        static UINT16 stored_es = 0x0000;
                   15860:        static UINT16 stored_di = 0x0000;
                   15861:        
                   15862:        if(!support_ems) {
                   15863:                REG8(AH) = 0x84;
                   15864:        } else if(REG8(AL) == 0x00) {
                   15865:                if(stored_bl == 0x00) {
                   15866:                        if(!(stored_es == 0 && stored_di == 0)) {
                   15867:                                for(int i = 0; i < 4; i++) {
                   15868:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15869:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15870:                                }
                   15871:                        }
                   15872:                        SREG(ES) = stored_es;
                   15873:                        i386_load_segment_descriptor(ES);
                   15874:                        REG16(DI) = stored_di;
                   15875:                } else {
                   15876:                        REG8(BL) = stored_bl;
                   15877:                }
                   15878:                REG8(AH) = 0x00;
                   15879:        } else if(REG8(AL) == 0x01) {
                   15880:                if(REG8(BL) == 0x00) {
                   15881:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   15882:                                for(int i = 0; i < 4; i++) {
                   15883:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   15884:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   15885:                                        
                   15886:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   15887:                                                ems_map_page(i, handle, page);
                   15888:                                        } else {
                   15889:                                                ems_unmap_page(i);
                   15890:                                        }
                   15891:                                }
                   15892:                        }
                   15893:                }
                   15894:                stored_bl = REG8(BL);
                   15895:                stored_es = SREG(ES);
                   15896:                stored_di = REG16(DI);
                   15897:                REG8(AH) = 0x00;
                   15898:        } else if(REG8(AL) == 0x02) {
                   15899:                REG16(DX) = 4 * 4;
                   15900:                REG8(AH) = 0x00;
                   15901:        } else if(REG8(AL) == 0x03) {
                   15902:                REG8(BL) = 0x00; // not supported
                   15903:                REG8(AH) = 0x00;
                   15904:        } else if(REG8(AL) == 0x04) {
                   15905:                REG8(AH) = 0x00;
                   15906:        } else if(REG8(AL) == 0x05) {
                   15907:                REG8(BL) = 0x00; // not supported
                   15908:                REG8(AH) = 0x00;
                   15909:        } else {
                   15910:                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));
                   15911:                REG8(AH) = 0x8f;
                   15912:        }
                   15913: }
                   15914: 
1.1.1.43  root     15915: inline void msdos_int_67h_5dh()
                   15916: {
                   15917:        if(!support_ems) {
                   15918:                REG8(AH) = 0x84;
                   15919:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15920:                REG8(AH) = 0xa4; // operating system denied access
                   15921:        } else {
                   15922:                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));
                   15923:                REG8(AH) = 0x8f;
                   15924:        }
                   15925: }
                   15926: 
1.1.1.49  root     15927: inline void msdos_int_67h_70h()
                   15928: {
                   15929:        if(!support_ems) {
                   15930:                REG8(AH) = 0x84;
                   15931:        } else if(REG8(AL) == 0x00) {
                   15932:                REG8(AL) = 0x00;
                   15933:                REG8(AH) = 0x00;
                   15934:        } else if(REG8(AL) == 0x01) {
                   15935:                REG8(AL) = 0x00;
                   15936: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   15937:                REG8(AH) = 0x00;
                   15938:        } else {
                   15939:                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));
                   15940:                REG8(AH) = 0x8f;
                   15941:        }
                   15942: }
                   15943: 
1.1.1.30  root     15944: inline void msdos_int_67h_deh()
                   15945: {
                   15946:        REG8(AH) = 0x84;
                   15947: }
                   15948: 
1.1.1.19  root     15949: #ifdef SUPPORT_XMS
                   15950: 
1.1.1.32  root     15951: void msdos_xms_init()
1.1.1.26  root     15952: {
1.1.1.30  root     15953:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15954:        emb_handle_top->address = EMB_TOP;
                   15955:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     15956:        xms_a20_local_enb_count = 0;
                   15957: }
                   15958: 
1.1.1.32  root     15959: void msdos_xms_finish()
                   15960: {
                   15961:        msdos_xms_release();
                   15962: }
                   15963: 
                   15964: void msdos_xms_release()
1.1.1.30  root     15965: {
                   15966:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   15967:                emb_handle_t *next_handle = emb_handle->next;
                   15968:                free(emb_handle);
                   15969:                emb_handle = next_handle;
                   15970:        }
                   15971: }
                   15972: 
                   15973: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   15974: {
                   15975:        if(handle != 0) {
                   15976:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15977:                        if(emb_handle->handle == handle) {
                   15978:                                return(emb_handle);
                   15979:                        }
                   15980:                }
                   15981:        }
                   15982:        return(NULL);
                   15983: }
                   15984: 
                   15985: int msdos_xms_get_unused_emb_handle_id()
                   15986: {
                   15987:        for(int handle = 1;; handle++) {
                   15988:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   15989:                        return(handle);
                   15990:                }
                   15991:        }
                   15992:        return(0);
                   15993: }
                   15994: 
                   15995: int msdos_xms_get_unused_emb_handle_count()
                   15996: {
                   15997:        int count = 64; //255;
                   15998:        
                   15999:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16000:                if(emb_handle->handle != 0) {
                   16001:                        if(--count == 1) {
                   16002:                                break;
                   16003:                        }
                   16004:                }
                   16005:        }
                   16006:        return(count);
                   16007: }
                   16008: 
                   16009: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   16010: {
                   16011:        if(emb_handle->size_kb > size_kb) {
                   16012:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16013:                
                   16014:                new_handle->address = emb_handle->address + size_kb * 1024;
                   16015:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   16016:                emb_handle->size_kb = size_kb;
                   16017:                
                   16018:                new_handle->prev = emb_handle;
                   16019:                new_handle->next = emb_handle->next;
                   16020:                if(emb_handle->next != NULL) {
                   16021:                        emb_handle->next->prev = new_handle;
                   16022:                }
                   16023:                emb_handle->next = new_handle;
                   16024:        }
                   16025: }
                   16026: 
                   16027: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   16028: {
                   16029:        emb_handle_t *next_handle = emb_handle->next;
                   16030:        
                   16031:        if(next_handle != NULL) {
                   16032:                emb_handle->size_kb += next_handle->size_kb;
                   16033:                
                   16034:                if(next_handle->next != NULL) {
                   16035:                        next_handle->next->prev = emb_handle;
                   16036:                }
                   16037:                emb_handle->next = next_handle->next;
                   16038:                free(next_handle);
                   16039:        }
                   16040: }
                   16041: 
                   16042: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   16043: {
                   16044:        emb_handle_t *target_handle = NULL;
                   16045:        
                   16046:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16047:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   16048:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   16049:                                target_handle = emb_handle;
                   16050:                        }
                   16051:                }
                   16052:        }
                   16053:        if(target_handle != NULL) {
                   16054:                if(target_handle->size_kb > size_kb) {
                   16055:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   16056:                }
                   16057: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16058:                return(target_handle);
                   16059:        }
                   16060:        return(NULL);
                   16061: }
                   16062: 
                   16063: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   16064: {
                   16065:        emb_handle_t *prev_handle = emb_handle->prev;
                   16066:        emb_handle_t *next_handle = emb_handle->next;
                   16067:        
                   16068:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   16069:                msdos_xms_combine_emb_handles(prev_handle);
                   16070:                emb_handle = prev_handle;
                   16071:        }
                   16072:        if(next_handle != NULL && next_handle->handle == 0) {
                   16073:                msdos_xms_combine_emb_handles(emb_handle);
                   16074:        }
                   16075:        emb_handle->handle = 0;
                   16076: }
                   16077: 
1.1.1.19  root     16078: inline void msdos_call_xms_00h()
                   16079: {
1.1.1.29  root     16080: #if defined(HAS_I386)
                   16081:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     16082: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     16083:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   16084: #else
                   16085:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   16086:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   16087: #endif
                   16088: //     REG16(DX) = 0x0000; // HMA does not exist
                   16089:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     16090: }
                   16091: 
                   16092: inline void msdos_call_xms_01h()
                   16093: {
1.1.1.29  root     16094:        if(REG8(AL) == 0x40) {
                   16095:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   16096:                // DX=KB free extended memory returned by last call of function 08h
                   16097:                REG16(AX) = 0x0000;
                   16098:                REG8(BL) = 0x91;
                   16099:                REG16(DX) = xms_dx_after_call_08h;
                   16100:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16101:                REG16(AX) = 0x0000;
                   16102:                REG8(BL) = 0x81; // Vdisk was detected
                   16103: #ifdef SUPPORT_HMA
                   16104:        } else if(is_hma_used_by_int_2fh) {
                   16105:                REG16(AX) = 0x0000;
                   16106:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16107:        } else if(is_hma_used_by_xms) {
                   16108:                REG16(AX) = 0x0000;
                   16109:                REG8(BL) = 0x91; // HMA is already in use
                   16110:        } else {
                   16111:                REG16(AX) = 0x0001;
                   16112:                is_hma_used_by_xms = true;
                   16113: #else
                   16114:        } else {
                   16115:                REG16(AX) = 0x0000;
                   16116:                REG8(BL) = 0x91; // HMA is already in use
                   16117: #endif
                   16118:        }
1.1.1.19  root     16119: }
                   16120: 
                   16121: inline void msdos_call_xms_02h()
                   16122: {
1.1.1.29  root     16123:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16124:                REG16(AX) = 0x0000;
                   16125:                REG8(BL) = 0x81; // Vdisk was detected
                   16126: #ifdef SUPPORT_HMA
                   16127:        } else if(is_hma_used_by_int_2fh) {
                   16128:                REG16(AX) = 0x0000;
                   16129:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16130:        } else if(!is_hma_used_by_xms) {
                   16131:                REG16(AX) = 0x0000;
                   16132:                REG8(BL) = 0x93; // HMA is not allocated
                   16133:        } else {
                   16134:                REG16(AX) = 0x0001;
                   16135:                is_hma_used_by_xms = false;
                   16136:                // restore first free mcb in high memory area
                   16137:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16138: #else
                   16139:        } else {
                   16140:                REG16(AX) = 0x0000;
                   16141:                REG8(BL) = 0x91; // HMA is already in use
                   16142: #endif
                   16143:        }
1.1.1.19  root     16144: }
                   16145: 
                   16146: inline void msdos_call_xms_03h()
                   16147: {
                   16148:        i386_set_a20_line(1);
                   16149:        REG16(AX) = 0x0001;
                   16150:        REG8(BL) = 0x00;
                   16151: }
                   16152: 
                   16153: inline void msdos_call_xms_04h()
                   16154: {
1.1.1.21  root     16155:        i386_set_a20_line(0);
                   16156:        REG16(AX) = 0x0001;
                   16157:        REG8(BL) = 0x00;
1.1.1.19  root     16158: }
                   16159: 
                   16160: inline void msdos_call_xms_05h()
                   16161: {
                   16162:        i386_set_a20_line(1);
                   16163:        REG16(AX) = 0x0001;
                   16164:        REG8(BL) = 0x00;
1.1.1.21  root     16165:        xms_a20_local_enb_count++;
1.1.1.19  root     16166: }
                   16167: 
                   16168: void msdos_call_xms_06h()
                   16169: {
1.1.1.21  root     16170:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16171:                if(--xms_a20_local_enb_count == 0) {
                   16172:                        i386_set_a20_line(0);
                   16173:                        REG16(AX) = 0x0001;
                   16174:                        REG8(BL) = 0x00;
                   16175:                } else {
                   16176:                        REG16(AX) = 0x0000;
                   16177:                        REG8(BL) = 0x94;
                   16178:                }
1.1.1.21  root     16179:        } else {
1.1.1.45  root     16180:                i386_set_a20_line(0);
1.1.1.21  root     16181:                REG16(AX) = 0x0001;
                   16182:                REG8(BL) = 0x00;
1.1.1.19  root     16183:        }
                   16184: }
                   16185: 
                   16186: inline void msdos_call_xms_07h()
                   16187: {
                   16188:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16189:        REG8(BL) = 0x00;
                   16190: }
                   16191: 
                   16192: inline void msdos_call_xms_08h()
                   16193: {
1.1.1.45  root     16194:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16195:        
1.1.1.30  root     16196:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16197:                if(emb_handle->handle == 0) {
1.1.1.45  root     16198:                        if(eax < emb_handle->size_kb) {
                   16199:                                eax = emb_handle->size_kb;
1.1.1.19  root     16200:                        }
1.1.1.45  root     16201:                        edx += emb_handle->size_kb;
1.1.1.19  root     16202:                }
                   16203:        }
1.1.1.45  root     16204:        if(eax > 65535) {
                   16205:                eax = 65535;
                   16206:        }
                   16207:        if(edx > 65535) {
                   16208:                edx = 65535;
                   16209:        }
                   16210:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16211:                REG8(BL) = 0xa0;
                   16212:        } else {
                   16213:                REG8(BL) = 0x00;
                   16214:        }
1.1.1.45  root     16215: #if defined(HAS_I386)
                   16216:        REG32(EAX) = eax;
                   16217:        REG32(EDX) = edx;
                   16218: #else
                   16219:        REG16(AX) = (UINT16)eax;
                   16220:        REG16(DX) = (UINT16)edx;
                   16221: #endif
1.1.1.29  root     16222:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16223: }
                   16224: 
1.1.1.30  root     16225: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16226: {
1.1.1.30  root     16227:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16228:        
                   16229:        if(emb_handle != NULL) {
                   16230:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16231:                
                   16232:                REG16(AX) = 0x0001;
                   16233:                REG16(DX) = emb_handle->handle;
                   16234:                REG8(BL) = 0x00;
                   16235:        } else {
                   16236:                REG16(AX) = REG16(DX) = 0x0000;
                   16237:                REG8(BL) = 0xa0;
1.1.1.19  root     16238:        }
1.1.1.30  root     16239: }
                   16240: 
                   16241: inline void msdos_call_xms_09h()
                   16242: {
                   16243:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16244: }
                   16245: 
                   16246: inline void msdos_call_xms_0ah()
                   16247: {
1.1.1.30  root     16248:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16249:        
                   16250:        if(emb_handle == NULL) {
1.1.1.19  root     16251:                REG16(AX) = 0x0000;
                   16252:                REG8(BL) = 0xa2;
1.1.1.45  root     16253: //     } else if(emb_handle->lock > 0) {
                   16254: //             REG16(AX) = 0x0000;
                   16255: //             REG8(BL) = 0xab;
1.1.1.19  root     16256:        } else {
1.1.1.30  root     16257:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16258:                
                   16259:                REG16(AX) = 0x0001;
                   16260:                REG8(BL) = 0x00;
                   16261:        }
                   16262: }
                   16263: 
                   16264: inline void msdos_call_xms_0bh()
                   16265: {
                   16266:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16267:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16268:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16269:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16270:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16271:        
                   16272:        UINT8 *src_buffer, *dest_buffer;
                   16273:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16274:        emb_handle_t *emb_handle;
1.1.1.19  root     16275:        
                   16276:        if(src_handle == 0) {
                   16277:                src_buffer = mem;
                   16278:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16279:                src_addr_max = MAX_MEM;
                   16280:        } else {
1.1.1.30  root     16281:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16282:                        REG16(AX) = 0x0000;
                   16283:                        REG8(BL) = 0xa3;
                   16284:                        return;
                   16285:                }
1.1.1.30  root     16286:                src_buffer = mem + emb_handle->address;
                   16287:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16288:        }
                   16289:        if(dest_handle == 0) {
                   16290:                dest_buffer = mem;
                   16291:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16292:                dest_addr_max = MAX_MEM;
                   16293:        } else {
1.1.1.30  root     16294:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16295:                        REG16(AX) = 0x0000;
                   16296:                        REG8(BL) = 0xa5;
                   16297:                        return;
                   16298:                }
1.1.1.30  root     16299:                dest_buffer = mem + emb_handle->address;
                   16300:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16301:        }
                   16302:        for(int i = 0; i < copy_length; i++) {
                   16303:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16304:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16305:                } else {
                   16306:                        break;
                   16307:                }
                   16308:        }
                   16309:        REG16(AX) = 0x0001;
                   16310:        REG8(BL) = 0x00;
                   16311: }
                   16312: 
                   16313: inline void msdos_call_xms_0ch()
                   16314: {
1.1.1.30  root     16315:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16316:        
                   16317:        if(emb_handle == NULL) {
1.1.1.19  root     16318:                REG16(AX) = 0x0000;
                   16319:                REG8(BL) = 0xa2;
                   16320:        } else {
1.1.1.45  root     16321:                if(emb_handle->lock < 255) {
                   16322:                        emb_handle->lock++;
                   16323:                }
1.1.1.19  root     16324:                REG16(AX) = 0x0001;
1.1.1.30  root     16325:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16326:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16327:        }
                   16328: }
                   16329: 
                   16330: inline void msdos_call_xms_0dh()
                   16331: {
1.1.1.30  root     16332:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16333:        
                   16334:        if(emb_handle == NULL) {
1.1.1.19  root     16335:                REG16(AX) = 0x0000;
                   16336:                REG8(BL) = 0xa2;
1.1.1.30  root     16337:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16338:                REG16(AX) = 0x0000;
                   16339:                REG8(BL) = 0xaa;
                   16340:        } else {
1.1.1.30  root     16341:                emb_handle->lock--;
1.1.1.19  root     16342:                REG16(AX) = 0x0001;
                   16343:                REG8(BL) = 0x00;
                   16344:        }
                   16345: }
                   16346: 
                   16347: inline void msdos_call_xms_0eh()
                   16348: {
1.1.1.30  root     16349:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16350:        
                   16351:        if(emb_handle == NULL) {
1.1.1.19  root     16352:                REG16(AX) = 0x0000;
                   16353:                REG8(BL) = 0xa2;
                   16354:        } else {
                   16355:                REG16(AX) = 0x0001;
1.1.1.30  root     16356:                REG8(BH) = emb_handle->lock;
                   16357:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16358:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16359:        }
                   16360: }
                   16361: 
1.1.1.30  root     16362: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16363: {
1.1.1.30  root     16364:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16365:        
                   16366:        if(emb_handle == NULL) {
1.1.1.19  root     16367:                REG16(AX) = 0x0000;
                   16368:                REG8(BL) = 0xa2;
1.1.1.30  root     16369:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16370:                REG16(AX) = 0x0000;
                   16371:                REG8(BL) = 0xab;
                   16372:        } else {
1.1.1.30  root     16373:                if(emb_handle->size_kb < size_kb) {
                   16374:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16375:                                msdos_xms_combine_emb_handles(emb_handle);
                   16376:                                if(emb_handle->size_kb > size_kb) {
                   16377:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16378:                                }
                   16379:                        } else {
                   16380:                                int old_handle = emb_handle->handle;
                   16381:                                int old_size_kb = emb_handle->size_kb;
                   16382:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16383:                                
                   16384:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16385:                                msdos_xms_free_emb_handle(emb_handle);
                   16386:                                
                   16387:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16388:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16389:                                }
                   16390:                                emb_handle->handle = old_handle;
                   16391:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16392:                                free(buffer);
                   16393:                        }
                   16394:                } else if(emb_handle->size_kb > size_kb) {
                   16395:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16396:                }
                   16397:                if(emb_handle->size_kb != size_kb) {
                   16398:                        REG16(AX) = 0x0000;
                   16399:                        REG8(BL) = 0xa0;
                   16400:                } else {
                   16401:                        REG16(AX) = 0x0001;
                   16402:                        REG8(BL) = 0x00;
                   16403:                }
1.1.1.19  root     16404:        }
                   16405: }
                   16406: 
1.1.1.30  root     16407: inline void msdos_call_xms_0fh()
                   16408: {
                   16409:        msdos_call_xms_0fh(REG16(BX));
                   16410: }
                   16411: 
1.1.1.19  root     16412: inline void msdos_call_xms_10h()
                   16413: {
                   16414:        int seg;
                   16415:        
                   16416:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16417:                REG16(AX) = 0x0001;
                   16418:                REG16(BX) = seg;
                   16419:        } else {
                   16420:                REG16(AX) = 0x0000;
                   16421:                REG8(BL) = 0xb0;
                   16422:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16423:        }
                   16424: }
                   16425: 
                   16426: inline void msdos_call_xms_11h()
                   16427: {
                   16428:        int mcb_seg = REG16(DX) - 1;
                   16429:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16430:        
                   16431:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16432:                msdos_mem_free(REG16(DX));
                   16433:                REG16(AX) = 0x0001;
                   16434:                REG8(BL) = 0x00;
                   16435:        } else {
                   16436:                REG16(AX) = 0x0000;
                   16437:                REG8(BL) = 0xb2;
                   16438:        }
                   16439: }
                   16440: 
                   16441: inline void msdos_call_xms_12h()
                   16442: {
                   16443:        int mcb_seg = REG16(DX) - 1;
                   16444:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16445:        int max_paragraphs;
                   16446:        
                   16447:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16448:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16449:                        REG16(AX) = 0x0001;
                   16450:                        REG8(BL) = 0x00;
                   16451:                } else {
                   16452:                        REG16(AX) = 0x0000;
                   16453:                        REG8(BL) = 0xb0;
                   16454:                        REG16(DX) = max_paragraphs;
                   16455:                }
                   16456:        } else {
                   16457:                REG16(AX) = 0x0000;
                   16458:                REG8(BL) = 0xb2;
                   16459:        }
                   16460: }
                   16461: 
1.1.1.29  root     16462: #if defined(HAS_I386)
                   16463: 
                   16464: inline void msdos_call_xms_88h()
                   16465: {
                   16466:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16467:        
1.1.1.30  root     16468:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16469:                if(emb_handle->handle == 0) {
                   16470:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16471:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16472:                        }
1.1.1.30  root     16473:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16474:                }
                   16475:        }
                   16476:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16477:                REG8(BL) = 0xa0;
                   16478:        } else {
                   16479:                REG8(BL) = 0x00;
                   16480:        }
                   16481:        REG32(ECX) = EMB_END - 1;
                   16482: }
                   16483: 
                   16484: inline void msdos_call_xms_89h()
                   16485: {
1.1.1.30  root     16486:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16487: }
                   16488: 
                   16489: inline void msdos_call_xms_8eh()
                   16490: {
1.1.1.30  root     16491:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16492:        
                   16493:        if(emb_handle == NULL) {
1.1.1.29  root     16494:                REG16(AX) = 0x0000;
                   16495:                REG8(BL) = 0xa2;
                   16496:        } else {
                   16497:                REG16(AX) = 0x0001;
1.1.1.30  root     16498:                REG8(BH) = emb_handle->lock;
                   16499:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16500:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16501:        }
                   16502: }
                   16503: 
                   16504: inline void msdos_call_xms_8fh()
                   16505: {
1.1.1.30  root     16506:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16507: }
                   16508: 
                   16509: #endif
1.1.1.19  root     16510: #endif
                   16511: 
1.1.1.26  root     16512: UINT16 msdos_get_equipment()
                   16513: {
                   16514:        static UINT16 equip = 0;
                   16515:        
                   16516:        if(equip == 0) {
                   16517: #ifdef SUPPORT_FPU
                   16518:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   16519: #endif
                   16520:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   16521:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   16522: //             equip |= (1 << 8);      // 0 if DMA installed
                   16523:                equip |= (2 << 9);      // number of serial ports
                   16524:                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     16525:                
                   16526:                // check only A: and B: if it is floppy drive
                   16527:                int n = 0;
                   16528:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     16529:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   16530:                                n++;
1.1.1.28  root     16531:                        }
                   16532:                }
                   16533:                if(n != 0) {
                   16534:                        equip |= (1 << 0);      // floppy disk(s) installed
                   16535:                        n--;
                   16536:                        equip |= (n << 6);      // number of floppies installed less 1
                   16537:                }
                   16538: //             if(joyGetNumDevs() != 0) {
                   16539: //                     equip |= (1 << 12);     // game port installed
                   16540: //             }
1.1.1.26  root     16541:        }
                   16542:        return(equip);
                   16543: }
                   16544: 
1.1       root     16545: void msdos_syscall(unsigned num)
                   16546: {
1.1.1.22  root     16547: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     16548:        if(num == 0x08 || num == 0x1c) {
                   16549:                // don't log the timer interrupts
1.1.1.45  root     16550: //             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     16551:        } else if(num == 0x30) {
                   16552:                // dummy interrupt for call 0005h (call near)
                   16553:                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     16554:        } else if(num == 0x68) {
1.1.1.22  root     16555:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     16556:                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     16557:        } else if(num == 0x69) {
                   16558:                // dummy interrupt for XMS (call far)
1.1.1.33  root     16559:                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     16560:        } else if(num >= 0x6a && num <= 0x6f) {
1.1.1.45  root     16561:                // dummy interrupt
1.1.1.22  root     16562:        } else {
1.1.1.33  root     16563:                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     16564:        }
                   16565: #endif
1.1.1.36  root     16566:        // update cursor position
                   16567:        if(cursor_moved) {
                   16568:                pcbios_update_cursor_position();
                   16569:                cursor_moved = false;
                   16570:        }
1.1.1.50  root     16571: #ifdef USE_SERVICE_THREAD
                   16572:        // this is called from dummy loop to wait until a serive that waits input is done
                   16573:        if(!in_service)
                   16574: #endif
1.1.1.33  root     16575:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     16576:        
1.1       root     16577:        switch(num) {
                   16578:        case 0x00:
1.1.1.28  root     16579:                try {
                   16580:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16581:                        error("division by zero\n");
                   16582:                } catch(...) {
                   16583:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   16584:                }
1.1       root     16585:                break;
                   16586:        case 0x04:
1.1.1.28  root     16587:                try {
                   16588:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16589:                        error("overflow\n");
                   16590:                } catch(...) {
                   16591:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   16592:                }
1.1       root     16593:                break;
                   16594:        case 0x06:
                   16595:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     16596:                if(!ignore_illegal_insn) {
1.1.1.28  root     16597:                        try {
                   16598:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16599:                                error("illegal instruction\n");
                   16600:                        } catch(...) {
                   16601:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   16602:                        }
1.1.1.14  root     16603:                } else {
                   16604: #if defined(HAS_I386)
1.1.1.39  root     16605:                        m_eip = m_int6h_skip_eip;
                   16606: #elif defined(HAS_I286)
                   16607:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     16608: #else
1.1.1.39  root     16609:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     16610: #endif
                   16611:                }
1.1       root     16612:                break;
1.1.1.33  root     16613:        case 0x09:
                   16614:                // ctrl-break is pressed
                   16615:                if(raise_int_1bh) {
                   16616: #if defined(HAS_I386)
                   16617:                        m_ext = 0; // not an external interrupt
                   16618:                        i386_trap(0x1b, 1, 0);
                   16619:                        m_ext = 1;
                   16620: #else
                   16621:                        PREFIX86(_interrupt)(0x1b);
                   16622: #endif
                   16623:                        raise_int_1bh = false;
                   16624:                }
1.1.1.8   root     16625:        case 0x08:
1.1.1.14  root     16626: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     16627:        case 0x0b:
                   16628:        case 0x0c:
                   16629:        case 0x0d:
                   16630:        case 0x0e:
                   16631:        case 0x0f:
                   16632:                // EOI
                   16633:                pic[0].isr &= ~(1 << (num - 0x08));
                   16634:                pic_update();
                   16635:                break;
1.1       root     16636:        case 0x10:
                   16637:                // PC BIOS - Video
1.1.1.14  root     16638:                if(!restore_console_on_exit) {
1.1.1.15  root     16639:                        change_console_size(scr_width, scr_height);
1.1       root     16640:                }
1.1.1.3   root     16641:                m_CF = 0;
1.1       root     16642:                switch(REG8(AH)) {
1.1.1.16  root     16643:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     16644:                case 0x01: pcbios_int_10h_01h(); break;
                   16645:                case 0x02: pcbios_int_10h_02h(); break;
                   16646:                case 0x03: pcbios_int_10h_03h(); break;
                   16647:                case 0x05: pcbios_int_10h_05h(); break;
                   16648:                case 0x06: pcbios_int_10h_06h(); break;
                   16649:                case 0x07: pcbios_int_10h_07h(); break;
                   16650:                case 0x08: pcbios_int_10h_08h(); break;
                   16651:                case 0x09: pcbios_int_10h_09h(); break;
                   16652:                case 0x0a: pcbios_int_10h_0ah(); break;
                   16653:                case 0x0b: break;
1.1.1.40  root     16654:                case 0x0c: pcbios_int_10h_0ch(); break;
                   16655:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     16656:                case 0x0e: pcbios_int_10h_0eh(); break;
                   16657:                case 0x0f: pcbios_int_10h_0fh(); break;
                   16658:                case 0x10: break;
1.1.1.14  root     16659:                case 0x11: pcbios_int_10h_11h(); break;
                   16660:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     16661:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     16662:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     16663:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     16664:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   16665:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     16666:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     16667:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   16668:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     16669:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     16670:                case 0x6f: break;
1.1.1.22  root     16671:                case 0x80: m_CF = 1; break; // unknown
                   16672:                case 0x81: m_CF = 1; break; // unknown
1.1       root     16673:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     16674:                case 0x83: pcbios_int_10h_83h(); break;
                   16675:                case 0x8b: break;
                   16676:                case 0x8c: m_CF = 1; break; // unknown
                   16677:                case 0x8d: m_CF = 1; break; // unknown
                   16678:                case 0x8e: m_CF = 1; break; // unknown
                   16679:                case 0x90: pcbios_int_10h_90h(); break;
                   16680:                case 0x91: pcbios_int_10h_91h(); break;
                   16681:                case 0x92: break;
                   16682:                case 0x93: break;
                   16683:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     16684:                case 0xfa: break; // ega register interface library is not installed
1.1       root     16685:                case 0xfe: pcbios_int_10h_feh(); break;
                   16686:                case 0xff: pcbios_int_10h_ffh(); break;
                   16687:                default:
1.1.1.22  root     16688:                        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));
                   16689:                        m_CF = 1;
1.1       root     16690:                        break;
                   16691:                }
                   16692:                break;
                   16693:        case 0x11:
                   16694:                // PC BIOS - Get Equipment List
1.1.1.26  root     16695:                REG16(AX) = msdos_get_equipment();
1.1       root     16696:                break;
                   16697:        case 0x12:
                   16698:                // PC BIOS - Get Memory Size
1.1.1.33  root     16699:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     16700:                break;
                   16701:        case 0x13:
1.1.1.42  root     16702:                // PC BIOS - Disk I/O
                   16703:                {
                   16704:                        static UINT8 last = 0x00;
                   16705:                        switch(REG8(AH)) {
                   16706:                        case 0x00: pcbios_int_13h_00h(); break;
                   16707:                        case 0x01: // get last status
                   16708:                                REG8(AH) = last;
                   16709:                                break;
                   16710:                        case 0x02: pcbios_int_13h_02h(); break;
                   16711:                        case 0x03: pcbios_int_13h_03h(); break;
                   16712:                        case 0x04: pcbios_int_13h_04h(); break;
                   16713:                        case 0x08: pcbios_int_13h_08h(); break;
                   16714:                        case 0x0a: pcbios_int_13h_02h(); break;
                   16715:                        case 0x0b: pcbios_int_13h_03h(); break;
                   16716:                        case 0x0d: pcbios_int_13h_00h(); break;
                   16717:                        case 0x10: pcbios_int_13h_10h(); break;
                   16718:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     16719:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     16720:                        case 0x05: // format
                   16721:                        case 0x06:
                   16722:                        case 0x07:
                   16723:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   16724:                                m_CF = 1;
                   16725:                                break;
                   16726:                        case 0x09:
                   16727:                        case 0x0c: // seek
                   16728:                        case 0x11: // recalib
                   16729:                        case 0x14:
                   16730:                        case 0x17:
                   16731:                                REG8(AH) = 0x00; // successful completion
                   16732:                                break;
1.1.1.43  root     16733:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   16734:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   16735:                                REG8(AH) = 0x01; // invalid function
                   16736:                                m_CF = 1;
                   16737:                                break;
1.1.1.42  root     16738:                        default:
                   16739:                                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));
                   16740:                                REG8(AH) = 0x01; // invalid function
                   16741:                                m_CF = 1;
                   16742:                                break;
                   16743:                        }
                   16744:                        last = REG8(AH);
                   16745:                }
1.1       root     16746:                break;
                   16747:        case 0x14:
                   16748:                // PC BIOS - Serial I/O
1.1.1.25  root     16749:                switch(REG8(AH)) {
                   16750:                case 0x00: pcbios_int_14h_00h(); break;
                   16751:                case 0x01: pcbios_int_14h_01h(); break;
                   16752:                case 0x02: pcbios_int_14h_02h(); break;
                   16753:                case 0x03: pcbios_int_14h_03h(); break;
                   16754:                case 0x04: pcbios_int_14h_04h(); break;
                   16755:                case 0x05: pcbios_int_14h_05h(); break;
                   16756:                default:
                   16757:                        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));
                   16758:                        break;
                   16759:                }
1.1       root     16760:                break;
                   16761:        case 0x15:
                   16762:                // PC BIOS
1.1.1.3   root     16763:                m_CF = 0;
1.1       root     16764:                switch(REG8(AH)) {
1.1.1.14  root     16765:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     16766:                case 0x23: pcbios_int_15h_23h(); break;
                   16767:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     16768:                case 0x41: break;
1.1       root     16769:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     16770:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     16771:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     16772:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     16773:                case 0x86: pcbios_int_15h_86h(); break;
                   16774:                case 0x87: pcbios_int_15h_87h(); break;
                   16775:                case 0x88: pcbios_int_15h_88h(); break;
                   16776:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     16777:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     16778:                case 0xc0: // PS/2 ???
1.1.1.54  root     16779: #ifndef EXT_BIOS_TOP
1.1.1.22  root     16780:                case 0xc1:
1.1.1.54  root     16781: #endif
1.1.1.30  root     16782:                case 0xc3: // PS50+ ???
                   16783:                case 0xc4:
1.1.1.22  root     16784:                        REG8(AH) = 0x86;
                   16785:                        m_CF = 1;
                   16786:                        break;
1.1.1.54  root     16787: #ifdef EXT_BIOS_TOP
                   16788:                case 0xc1: pcbios_int_15h_c1h(); break;
                   16789: #endif
                   16790:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     16791: #if defined(HAS_I386)
1.1       root     16792:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     16793: #endif
1.1       root     16794:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     16795:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     16796:                default:
1.1.1.22  root     16797:                        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));
                   16798:                        REG8(AH) = 0x86;
1.1.1.3   root     16799:                        m_CF = 1;
1.1       root     16800:                        break;
                   16801:                }
                   16802:                break;
                   16803:        case 0x16:
                   16804:                // PC BIOS - Keyboard
1.1.1.3   root     16805:                m_CF = 0;
1.1       root     16806:                switch(REG8(AH)) {
                   16807:                case 0x00: pcbios_int_16h_00h(); break;
                   16808:                case 0x01: pcbios_int_16h_01h(); break;
                   16809:                case 0x02: pcbios_int_16h_02h(); break;
                   16810:                case 0x03: pcbios_int_16h_03h(); break;
                   16811:                case 0x05: pcbios_int_16h_05h(); break;
                   16812:                case 0x10: pcbios_int_16h_00h(); break;
                   16813:                case 0x11: pcbios_int_16h_01h(); break;
                   16814:                case 0x12: pcbios_int_16h_12h(); break;
                   16815:                case 0x13: pcbios_int_16h_13h(); break;
                   16816:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     16817:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     16818:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     16819:                case 0xda: break; // unknown
1.1.1.43  root     16820:                case 0xdb: break; // unknown
1.1.1.22  root     16821:                case 0xff: break; // unknown
1.1       root     16822:                default:
1.1.1.22  root     16823:                        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     16824:                        break;
                   16825:                }
                   16826:                break;
                   16827:        case 0x17:
                   16828:                // PC BIOS - Printer
1.1.1.37  root     16829:                m_CF = 0;
                   16830:                switch(REG8(AH)) {
                   16831:                case 0x00: pcbios_int_17h_00h(); break;
                   16832:                case 0x01: pcbios_int_17h_01h(); break;
                   16833:                case 0x02: pcbios_int_17h_02h(); break;
                   16834:                case 0x03: pcbios_int_17h_03h(); break;
                   16835:                case 0x50: pcbios_int_17h_50h(); break;
                   16836:                case 0x51: pcbios_int_17h_51h(); break;
                   16837:                case 0x52: pcbios_int_17h_52h(); break;
                   16838:                case 0x84: pcbios_int_17h_84h(); break;
                   16839:                case 0x85: pcbios_int_17h_85h(); break;
                   16840:                default:
                   16841:                        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));
                   16842:                        break;
                   16843:                }
1.1       root     16844:                break;
                   16845:        case 0x1a:
                   16846:                // PC BIOS - Timer
1.1.1.3   root     16847:                m_CF = 0;
1.1       root     16848:                switch(REG8(AH)) {
                   16849:                case 0x00: pcbios_int_1ah_00h(); break;
                   16850:                case 0x01: break;
                   16851:                case 0x02: pcbios_int_1ah_02h(); break;
                   16852:                case 0x03: break;
                   16853:                case 0x04: pcbios_int_1ah_04h(); break;
                   16854:                case 0x05: break;
                   16855:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   16856:                case 0x0b: break;
1.1.1.14  root     16857:                case 0x35: break; // Word Perfect Third Party Interface?
                   16858:                case 0x36: break; // Word Perfect Third Party Interface
                   16859:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     16860:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     16861:                case 0xb1: break; // PCI BIOS v2.0c+
                   16862:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     16863:                default:
1.1.1.22  root     16864:                        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     16865:                        break;
                   16866:                }
                   16867:                break;
1.1.1.33  root     16868:        case 0x1b:
                   16869:                mem[0x471] = 0x00;
                   16870:                break;
1.1       root     16871:        case 0x20:
1.1.1.28  root     16872:                try {
                   16873:                        msdos_process_terminate(SREG(CS), retval, 1);
                   16874:                } catch(...) {
                   16875:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   16876:                }
1.1       root     16877:                break;
1.1.1.49  root     16878:        case 0x30:
1.1.1.46  root     16879:                // dummy interrupt for case map routine pointed in the country info
                   16880: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   16881: //                     REG8(AL) = 0x00;
                   16882: //                     break;
                   16883: //             }
1.1       root     16884:        case 0x21:
                   16885:                // MS-DOS System Call
1.1.1.3   root     16886:                m_CF = 0;
1.1.1.28  root     16887:                try {
1.1.1.46  root     16888:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     16889:                        case 0x00: msdos_int_21h_00h(); break;
                   16890:                        case 0x01: msdos_int_21h_01h(); break;
                   16891:                        case 0x02: msdos_int_21h_02h(); break;
                   16892:                        case 0x03: msdos_int_21h_03h(); break;
                   16893:                        case 0x04: msdos_int_21h_04h(); break;
                   16894:                        case 0x05: msdos_int_21h_05h(); break;
                   16895:                        case 0x06: msdos_int_21h_06h(); break;
                   16896:                        case 0x07: msdos_int_21h_07h(); break;
                   16897:                        case 0x08: msdos_int_21h_08h(); break;
                   16898:                        case 0x09: msdos_int_21h_09h(); break;
                   16899:                        case 0x0a: msdos_int_21h_0ah(); break;
                   16900:                        case 0x0b: msdos_int_21h_0bh(); break;
                   16901:                        case 0x0c: msdos_int_21h_0ch(); break;
                   16902:                        case 0x0d: msdos_int_21h_0dh(); break;
                   16903:                        case 0x0e: msdos_int_21h_0eh(); break;
                   16904:                        case 0x0f: msdos_int_21h_0fh(); break;
                   16905:                        case 0x10: msdos_int_21h_10h(); break;
                   16906:                        case 0x11: msdos_int_21h_11h(); break;
                   16907:                        case 0x12: msdos_int_21h_12h(); break;
                   16908:                        case 0x13: msdos_int_21h_13h(); break;
                   16909:                        case 0x14: msdos_int_21h_14h(); break;
                   16910:                        case 0x15: msdos_int_21h_15h(); break;
                   16911:                        case 0x16: msdos_int_21h_16h(); break;
                   16912:                        case 0x17: msdos_int_21h_17h(); break;
                   16913:                        case 0x18: msdos_int_21h_18h(); break;
                   16914:                        case 0x19: msdos_int_21h_19h(); break;
                   16915:                        case 0x1a: msdos_int_21h_1ah(); break;
                   16916:                        case 0x1b: msdos_int_21h_1bh(); break;
                   16917:                        case 0x1c: msdos_int_21h_1ch(); break;
                   16918:                        case 0x1d: msdos_int_21h_1dh(); break;
                   16919:                        case 0x1e: msdos_int_21h_1eh(); break;
                   16920:                        case 0x1f: msdos_int_21h_1fh(); break;
                   16921:                        case 0x20: msdos_int_21h_20h(); break;
                   16922:                        case 0x21: msdos_int_21h_21h(); break;
                   16923:                        case 0x22: msdos_int_21h_22h(); break;
                   16924:                        case 0x23: msdos_int_21h_23h(); break;
                   16925:                        case 0x24: msdos_int_21h_24h(); break;
                   16926:                        case 0x25: msdos_int_21h_25h(); break;
                   16927:                        case 0x26: msdos_int_21h_26h(); break;
                   16928:                        case 0x27: msdos_int_21h_27h(); break;
                   16929:                        case 0x28: msdos_int_21h_28h(); break;
                   16930:                        case 0x29: msdos_int_21h_29h(); break;
                   16931:                        case 0x2a: msdos_int_21h_2ah(); break;
                   16932:                        case 0x2b: msdos_int_21h_2bh(); break;
                   16933:                        case 0x2c: msdos_int_21h_2ch(); break;
                   16934:                        case 0x2d: msdos_int_21h_2dh(); break;
                   16935:                        case 0x2e: msdos_int_21h_2eh(); break;
                   16936:                        case 0x2f: msdos_int_21h_2fh(); break;
                   16937:                        case 0x30: msdos_int_21h_30h(); break;
                   16938:                        case 0x31: msdos_int_21h_31h(); break;
                   16939:                        case 0x32: msdos_int_21h_32h(); break;
                   16940:                        case 0x33: msdos_int_21h_33h(); break;
                   16941:                        case 0x34: msdos_int_21h_34h(); break;
                   16942:                        case 0x35: msdos_int_21h_35h(); break;
                   16943:                        case 0x36: msdos_int_21h_36h(); break;
                   16944:                        case 0x37: msdos_int_21h_37h(); break;
                   16945:                        case 0x38: msdos_int_21h_38h(); break;
                   16946:                        case 0x39: msdos_int_21h_39h(0); break;
                   16947:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   16948:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   16949:                        case 0x3c: msdos_int_21h_3ch(); break;
                   16950:                        case 0x3d: msdos_int_21h_3dh(); break;
                   16951:                        case 0x3e: msdos_int_21h_3eh(); break;
                   16952:                        case 0x3f: msdos_int_21h_3fh(); break;
                   16953:                        case 0x40: msdos_int_21h_40h(); break;
                   16954:                        case 0x41: msdos_int_21h_41h(0); break;
                   16955:                        case 0x42: msdos_int_21h_42h(); break;
                   16956:                        case 0x43: msdos_int_21h_43h(0); break;
                   16957:                        case 0x44: msdos_int_21h_44h(); break;
                   16958:                        case 0x45: msdos_int_21h_45h(); break;
                   16959:                        case 0x46: msdos_int_21h_46h(); break;
                   16960:                        case 0x47: msdos_int_21h_47h(0); break;
                   16961:                        case 0x48: msdos_int_21h_48h(); break;
                   16962:                        case 0x49: msdos_int_21h_49h(); break;
                   16963:                        case 0x4a: msdos_int_21h_4ah(); break;
                   16964:                        case 0x4b: msdos_int_21h_4bh(); break;
                   16965:                        case 0x4c: msdos_int_21h_4ch(); break;
                   16966:                        case 0x4d: msdos_int_21h_4dh(); break;
                   16967:                        case 0x4e: msdos_int_21h_4eh(); break;
                   16968:                        case 0x4f: msdos_int_21h_4fh(); break;
                   16969:                        case 0x50: msdos_int_21h_50h(); break;
                   16970:                        case 0x51: msdos_int_21h_51h(); break;
                   16971:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     16972:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     16973:                        case 0x54: msdos_int_21h_54h(); break;
                   16974:                        case 0x55: msdos_int_21h_55h(); break;
                   16975:                        case 0x56: msdos_int_21h_56h(0); break;
                   16976:                        case 0x57: msdos_int_21h_57h(); break;
                   16977:                        case 0x58: msdos_int_21h_58h(); break;
                   16978:                        case 0x59: msdos_int_21h_59h(); break;
                   16979:                        case 0x5a: msdos_int_21h_5ah(); break;
                   16980:                        case 0x5b: msdos_int_21h_5bh(); break;
                   16981:                        case 0x5c: msdos_int_21h_5ch(); break;
                   16982:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     16983:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     16984:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     16985:                        case 0x60: msdos_int_21h_60h(0); break;
                   16986:                        case 0x61: msdos_int_21h_61h(); break;
                   16987:                        case 0x62: msdos_int_21h_62h(); break;
                   16988:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     16989:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     16990:                        case 0x65: msdos_int_21h_65h(); break;
                   16991:                        case 0x66: msdos_int_21h_66h(); break;
                   16992:                        case 0x67: msdos_int_21h_67h(); break;
                   16993:                        case 0x68: msdos_int_21h_68h(); break;
                   16994:                        case 0x69: msdos_int_21h_69h(); break;
                   16995:                        case 0x6a: msdos_int_21h_6ah(); break;
                   16996:                        case 0x6b: msdos_int_21h_6bh(); break;
                   16997:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     16998:                        case 0x6d: // Find First ROM Program
                   16999:                        case 0x6e: // Find Next ROM Program
                   17000:                        case 0x6f: // Get/Set ROM Scan Start Address
                   17001:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   17002:                                break;
1.1.1.43  root     17003:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     17004:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     17005:                                switch(REG8(AL)) {
                   17006:                                case 0x0d: msdos_int_21h_710dh(); break;
                   17007:                                case 0x39: msdos_int_21h_39h(1); break;
                   17008:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   17009:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     17010:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     17011:                                case 0x43: msdos_int_21h_43h(1); break;
                   17012:                                case 0x47: msdos_int_21h_47h(1); break;
                   17013:                                case 0x4e: msdos_int_21h_714eh(); break;
                   17014:                                case 0x4f: msdos_int_21h_714fh(); break;
                   17015:                                case 0x56: msdos_int_21h_56h(1); break;
                   17016:                                case 0x60: msdos_int_21h_60h(1); break;
                   17017:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   17018:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   17019:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   17020:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   17021:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   17022:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     17023:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     17024:                                case 0xaa: msdos_int_21h_71aah(); break;
                   17025:                                default:
                   17026:                                        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));
                   17027:                                        REG16(AX) = 0x7100;
                   17028:                                        m_CF = 1;
                   17029:                                        break;
                   17030:                                }
                   17031:                                break;
1.1.1.48  root     17032:                        case 0x72: // Windows95 beta - LFN FindClose
                   17033: //                             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));
                   17034:                                REG16(AX) = 0x7200;
                   17035:                                m_CF = 1;
                   17036:                                break;
                   17037:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     17038:                                switch(REG8(AL)) {
                   17039:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     17040:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     17041:                                case 0x02: msdos_int_21h_7302h(); break;
                   17042:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     17043:                                // 0x04: Set DPB to Use for Formatting
                   17044:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     17045:                                default:
                   17046:                                        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));
                   17047:                                        REG16(AX) = 0x7300;
                   17048:                                        m_CF = 1;
                   17049:                                        break;
                   17050:                                }
1.1       root     17051:                                break;
1.1.1.30  root     17052:                        case 0xdb: msdos_int_21h_dbh(); break;
                   17053:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     17054:                        default:
1.1.1.22  root     17055:                                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     17056:                                REG16(AX) = 0x01;
1.1.1.3   root     17057:                                m_CF = 1;
1.1       root     17058:                                break;
                   17059:                        }
1.1.1.28  root     17060:                } catch(int error) {
                   17061:                        REG16(AX) = error;
                   17062:                        m_CF = 1;
                   17063:                } catch(...) {
                   17064:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     17065:                        m_CF = 1;
1.1       root     17066:                }
1.1.1.3   root     17067:                if(m_CF) {
1.1.1.23  root     17068:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     17069:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     17070:                        sda->extended_error_code = REG16(AX);
                   17071:                        switch(sda->extended_error_code) {
                   17072:                        case  4: // Too many open files
                   17073:                        case  8: // Insufficient memory
                   17074:                                sda->error_class = 1; // Out of resource
                   17075:                                break;
                   17076:                        case  5: // Access denied
                   17077:                                sda->error_class = 3; // Authorization
                   17078:                                break;
                   17079:                        case  7: // Memory control block destroyed
                   17080:                                sda->error_class = 4; // Internal
                   17081:                                break;
                   17082:                        case  2: // File not found
                   17083:                        case  3: // Path not found
                   17084:                        case 15: // Invaid drive specified
                   17085:                        case 18: // No more files
                   17086:                                sda->error_class = 8; // Not found
                   17087:                                break;
                   17088:                        case 32: // Sharing violation
                   17089:                        case 33: // Lock violation
                   17090:                                sda->error_class = 10; // Locked
                   17091:                                break;
                   17092: //                     case 16: // Removal of current directory attempted
                   17093:                        case 19: // Attempted write on protected disk
                   17094:                        case 21: // Drive not ready
                   17095: //                     case 29: // Write failure
                   17096: //                     case 30: // Read failure
                   17097: //                     case 82: // Cannot create subdirectory
                   17098:                                sda->error_class = 11; // Media
                   17099:                                break;
                   17100:                        case 80: // File already exists
                   17101:                                sda->error_class = 12; // Already exist
                   17102:                                break;
                   17103:                        default:
                   17104:                                sda->error_class = 13; // Unknown
                   17105:                                break;
                   17106:                        }
                   17107:                        sda->suggested_action = 1; // Retry
                   17108:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     17109:                }
1.1.1.33  root     17110:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     17111:                        // raise int 23h
                   17112: #if defined(HAS_I386)
                   17113:                        m_ext = 0; // not an external interrupt
                   17114:                        i386_trap(0x23, 1, 0);
                   17115:                        m_ext = 1;
                   17116: #else
                   17117:                        PREFIX86(_interrupt)(0x23);
                   17118: #endif
                   17119:                }
1.1       root     17120:                break;
                   17121:        case 0x22:
                   17122:                fatalerror("int 22h (terminate address)\n");
                   17123:        case 0x23:
1.1.1.28  root     17124:                try {
                   17125:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   17126:                } catch(...) {
                   17127:                        fatalerror("failed to terminate the current process by int 23h\n");
                   17128:                }
1.1       root     17129:                break;
                   17130:        case 0x24:
1.1.1.32  root     17131: /*
1.1.1.28  root     17132:                try {
                   17133:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17134:                } catch(...) {
                   17135:                        fatalerror("failed to terminate the current process by int 24h\n");
                   17136:                }
1.1.1.32  root     17137: */
                   17138:                msdos_int_24h();
1.1       root     17139:                break;
                   17140:        case 0x25:
                   17141:                msdos_int_25h();
                   17142:                break;
                   17143:        case 0x26:
                   17144:                msdos_int_26h();
                   17145:                break;
                   17146:        case 0x27:
1.1.1.28  root     17147:                try {
                   17148:                        msdos_int_27h();
                   17149:                } catch(...) {
                   17150:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   17151:                }
1.1       root     17152:                break;
                   17153:        case 0x28:
                   17154:                Sleep(10);
1.1.1.35  root     17155:                REQUEST_HARDWRE_UPDATE();
1.1       root     17156:                break;
                   17157:        case 0x29:
                   17158:                msdos_int_29h();
                   17159:                break;
                   17160:        case 0x2e:
                   17161:                msdos_int_2eh();
                   17162:                break;
                   17163:        case 0x2f:
                   17164:                // multiplex interrupt
                   17165:                switch(REG8(AH)) {
1.1.1.22  root     17166:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     17167:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     17168:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     17169:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17170:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17171:                case 0x14: msdos_int_2fh_14h(); break;
                   17172:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17173:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17174:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17175:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17176:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17177:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17178:                case 0x46: msdos_int_2fh_46h(); break;
                   17179:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17180:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17181:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17182:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17183:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17184:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17185:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17186:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17187:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17188:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17189:                default:
1.1.1.30  root     17190:                        switch(REG8(AL)) {
                   17191:                        case 0x00:
                   17192:                                // This is not installed
                   17193: //                             REG8(AL) = 0x00;
                   17194:                                break;
1.1.1.33  root     17195:                        case 0x01:
1.1.1.42  root     17196:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17197:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17198:                                        break;
                   17199:                                }
1.1.1.33  root     17200:                                // Banyan VINES v4+ is not installed
                   17201:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17202:                                        break;
                   17203:                                }
1.1.1.42  root     17204:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17205:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17206:                                        break;
                   17207:                                }
1.1.1.30  root     17208:                        default:
1.1.1.42  root     17209:                                // NORTON UTILITIES 5.0+
                   17210:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17211:                                        break;
                   17212:                                }
1.1.1.30  root     17213:                                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     17214:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17215:                                m_CF = 1;
                   17216:                                break;
                   17217:                        }
                   17218:                        break;
1.1       root     17219:                }
                   17220:                break;
1.1.1.24  root     17221:        case 0x33:
                   17222:                switch(REG8(AH)) {
                   17223:                case 0x00:
                   17224:                        // Mouse
1.1.1.49  root     17225:                        switch(REG16(AX)) {
                   17226:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17227:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17228:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17229:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17230:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17231:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17232:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17233:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17234:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17235:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17236:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17237:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17238:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17239:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17240:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17241:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17242:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17243:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17244:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17245:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17246:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17247:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17248:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17249:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17250:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17251:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17252:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17253:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17254:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17255:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17256:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17257:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17258:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17259:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17260:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17261:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17262:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17263:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17264:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17265:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17266:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17267:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17268:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17269:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17270:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17271:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17272:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17273:                        case 0x002f: break; // Mouse Hardware Reset
                   17274:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17275:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17276:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17277:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17278:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17279:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17280:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17281:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17282:                        default:
                   17283:                                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));
                   17284:                                break;
                   17285:                        }
                   17286:                        break;
                   17287:                default:
                   17288:                        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));
                   17289:                        break;
                   17290:                }
                   17291:                break;
1.1.1.50  root     17292: /*
                   17293:        case 0x67:
                   17294:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 68h
                   17295:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   17296:                break;
                   17297: */
1.1.1.19  root     17298:        case 0x68:
                   17299:                // dummy interrupt for EMS (int 67h)
                   17300:                switch(REG8(AH)) {
                   17301:                case 0x40: msdos_int_67h_40h(); break;
                   17302:                case 0x41: msdos_int_67h_41h(); break;
                   17303:                case 0x42: msdos_int_67h_42h(); break;
                   17304:                case 0x43: msdos_int_67h_43h(); break;
                   17305:                case 0x44: msdos_int_67h_44h(); break;
                   17306:                case 0x45: msdos_int_67h_45h(); break;
                   17307:                case 0x46: msdos_int_67h_46h(); break;
                   17308:                case 0x47: msdos_int_67h_47h(); break;
                   17309:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17310:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17311:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17312:                case 0x4b: msdos_int_67h_4bh(); break;
                   17313:                case 0x4c: msdos_int_67h_4ch(); break;
                   17314:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17315:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17316:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17317:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17318:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17319:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17320:                case 0x53: msdos_int_67h_53h(); break;
                   17321:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17322:                case 0x55: msdos_int_67h_55h(); break;
                   17323:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17324:                case 0x57: msdos_int_67h_57h(); break;
                   17325:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17326:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17327:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17328:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17329:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17330:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17331:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17332:                // 0xde: VCPI
1.1.1.30  root     17333:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17334:                default:
1.1.1.22  root     17335:                        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     17336:                        REG8(AH) = 0x84;
                   17337:                        break;
                   17338:                }
                   17339:                break;
                   17340: #ifdef SUPPORT_XMS
                   17341:        case 0x69:
                   17342:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17343:                try {
                   17344:                        switch(REG8(AH)) {
                   17345:                        case 0x00: msdos_call_xms_00h(); break;
                   17346:                        case 0x01: msdos_call_xms_01h(); break;
                   17347:                        case 0x02: msdos_call_xms_02h(); break;
                   17348:                        case 0x03: msdos_call_xms_03h(); break;
                   17349:                        case 0x04: msdos_call_xms_04h(); break;
                   17350:                        case 0x05: msdos_call_xms_05h(); break;
                   17351:                        case 0x06: msdos_call_xms_06h(); break;
                   17352:                        case 0x07: msdos_call_xms_07h(); break;
                   17353:                        case 0x08: msdos_call_xms_08h(); break;
                   17354:                        case 0x09: msdos_call_xms_09h(); break;
                   17355:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17356:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17357:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17358:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17359:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17360:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17361:                        case 0x10: msdos_call_xms_10h(); break;
                   17362:                        case 0x11: msdos_call_xms_11h(); break;
                   17363:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17364: #if defined(HAS_I386)
                   17365:                        case 0x88: msdos_call_xms_88h(); break;
                   17366:                        case 0x89: msdos_call_xms_89h(); break;
                   17367:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17368:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17369: #endif
1.1.1.28  root     17370:                        default:
                   17371:                                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));
                   17372:                                REG16(AX) = 0x0000;
                   17373:                                REG8(BL) = 0x80; // function not implemented
                   17374:                                break;
                   17375:                        }
                   17376:                } catch(...) {
1.1.1.19  root     17377:                        REG16(AX) = 0x0000;
1.1.1.28  root     17378:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17379:                }
                   17380:                break;
                   17381: #endif
                   17382:        case 0x6a:
1.1.1.24  root     17383:                // irq12 (mouse)
                   17384:                mouse_push_ax = REG16(AX);
                   17385:                mouse_push_bx = REG16(BX);
                   17386:                mouse_push_cx = REG16(CX);
                   17387:                mouse_push_dx = REG16(DX);
                   17388:                mouse_push_si = REG16(SI);
                   17389:                mouse_push_di = REG16(DI);
                   17390:                
1.1.1.43  root     17391:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17392:                        REG16(AX) = mouse.status_irq;
                   17393:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17394:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17395:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17396:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17397:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17398:                        
1.1.1.49  root     17399:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17400:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17401:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17402:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17403:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     17404:                        break;
1.1.1.24  root     17405:                }
1.1.1.43  root     17406:                for(int i = 0; i < 8; i++) {
                   17407:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17408:                                REG16(AX) = mouse.status_irq_alt;
                   17409:                                REG16(BX) = mouse.get_buttons();
                   17410:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17411:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17412:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17413:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17414:                                
1.1.1.49  root     17415:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17416:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17417:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17418:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17419:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.43  root     17420:                                break;
                   17421:                        }
                   17422:                }
1.1.1.58! root     17423: /*
1.1.1.54  root     17424:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw) {
                   17425:                        UINT16 data_1st, data_2nd, data_3rd;
                   17426:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   17427:                        i386_push16(data_1st);
                   17428:                        i386_push16(data_2nd);
                   17429:                        i386_push16(data_3rd);
                   17430:                        i386_push16(0x0000);
                   17431:                        
                   17432:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17433:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
                   17434:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
                   17435:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
                   17436:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
                   17437:                        break;
                   17438:                }
1.1.1.58! root     17439: */
1.1.1.43  root     17440:                // invalid call addr :-(
1.1.1.49  root     17441:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17442:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17443:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17444:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17445:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.24  root     17446:                break;
                   17447:        case 0x6b:
                   17448:                // end of irq12 (mouse)
                   17449:                REG16(AX) = mouse_push_ax;
                   17450:                REG16(BX) = mouse_push_bx;
                   17451:                REG16(CX) = mouse_push_cx;
                   17452:                REG16(DX) = mouse_push_dx;
                   17453:                REG16(SI) = mouse_push_si;
                   17454:                REG16(DI) = mouse_push_di;
                   17455:                
                   17456:                // EOI
                   17457:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17458:                        pic[0].isr &= ~(1 << 2); // master
                   17459:                }
                   17460:                pic_update();
                   17461:                break;
                   17462:        case 0x6c:
1.1.1.19  root     17463:                // dummy interrupt for case map routine pointed in the country info
                   17464:                if(REG8(AL) >= 0x80) {
                   17465:                        char tmp[2] = {0};
                   17466:                        tmp[0] = REG8(AL);
                   17467:                        my_strupr(tmp);
                   17468:                        REG8(AL) = tmp[0];
                   17469:                }
                   17470:                break;
1.1.1.27  root     17471:        case 0x6d:
                   17472:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17473:                REG8(AL) = 0x86; // not supported
                   17474:                m_CF = 1;
                   17475:                break;
1.1.1.32  root     17476:        case 0x6e:
                   17477:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17478:                {
                   17479:                        UINT16 code = REG16(AX);
                   17480:                        if(code & 0xf0) {
                   17481:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17482:                        }
                   17483:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17484:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17485:                                        const char *message = NULL;
                   17486:                                        if(active_code_page == 932) {
                   17487:                                                message = param_error_table[i].message_japanese;
                   17488:                                        }
                   17489:                                        if(message == NULL) {
                   17490:                                                message = param_error_table[i].message_english;
                   17491:                                        }
                   17492:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   17493:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   17494:                                        
                   17495:                                        SREG(ES) = WORK_TOP >> 4;
                   17496:                                        i386_load_segment_descriptor(ES);
                   17497:                                        REG16(DI) = 0x0000;
                   17498:                                        break;
                   17499:                                }
                   17500:                        }
                   17501:                }
                   17502:                break;
1.1.1.49  root     17503:        case 0x6f:
                   17504:                // dummy interrupt for end of alter page map and call
                   17505:                {
                   17506:                        UINT16 handles[4], pages[4];
                   17507:                        
                   17508:                        // pop old mapping data in new mapping
                   17509:                        for(int i = 0; i < 4; i++) {
                   17510:                                pages  [3 - i] = i386_pop16();
                   17511:                                handles[3 - i] = i386_pop16();
                   17512:                        }
                   17513:                        
                   17514:                        // restore old mapping
                   17515:                        for(int i = 0; i < 4; i++) {
                   17516:                                UINT16 handle = handles[i];
                   17517:                                UINT16 page   = pages  [i];
                   17518:                                
                   17519:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   17520:                                        ems_map_page(i, handle, page);
                   17521:                                } else {
                   17522:                                        ems_unmap_page(i);
                   17523:                                }
                   17524:                        }
                   17525:                        // do ret_far (pop cs/ip) in old mapping
                   17526:                }
                   17527:                break;
1.1.1.8   root     17528:        case 0x70:
                   17529:        case 0x71:
                   17530:        case 0x72:
                   17531:        case 0x73:
                   17532:        case 0x74:
                   17533:        case 0x75:
                   17534:        case 0x76:
                   17535:        case 0x77:
                   17536:                // EOI
                   17537:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   17538:                        pic[0].isr &= ~(1 << 2); // master
                   17539:                }
                   17540:                pic_update();
                   17541:                break;
1.1       root     17542:        default:
1.1.1.22  root     17543: //             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     17544:                break;
                   17545:        }
                   17546:        
                   17547:        // update cursor position
                   17548:        if(cursor_moved) {
1.1.1.36  root     17549:                pcbios_update_cursor_position();
1.1       root     17550:                cursor_moved = false;
                   17551:        }
                   17552: }
                   17553: 
                   17554: // init
                   17555: 
                   17556: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   17557: {
                   17558:        // init file handler
                   17559:        memset(file_handler, 0, sizeof(file_handler));
                   17560:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   17561:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   17562:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     17563: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17564:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     17565: #else
                   17566:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   17567: #endif
                   17568:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     17569:        }
1.1.1.21  root     17570:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     17571: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   17572:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     17573:        }
1.1       root     17574:        _dup2(0, DUP_STDIN);
                   17575:        _dup2(1, DUP_STDOUT);
                   17576:        _dup2(2, DUP_STDERR);
1.1.1.21  root     17577:        _dup2(3, DUP_STDAUX);
                   17578:        _dup2(4, DUP_STDPRN);
1.1       root     17579:        
1.1.1.24  root     17580:        // init mouse
                   17581:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     17582:        mouse.enabled = true;   // from DOSBox
                   17583:        mouse.hidden = 1;       // hidden in default ???
                   17584:        mouse.old_hidden = 1;   // from DOSBox
                   17585:        mouse.max_position.x = 8 * (scr_width  - 1);
                   17586:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     17587:        mouse.mickey.x = 8;
                   17588:        mouse.mickey.y = 16;
                   17589:        
1.1.1.26  root     17590: #ifdef SUPPORT_XMS
                   17591:        // init xms
                   17592:        msdos_xms_init();
                   17593: #endif
                   17594:        
1.1       root     17595:        // init process
                   17596:        memset(process, 0, sizeof(process));
                   17597:        
1.1.1.13  root     17598:        // init dtainfo
                   17599:        msdos_dta_info_init();
                   17600:        
1.1       root     17601:        // init memory
                   17602:        memset(mem, 0, sizeof(mem));
                   17603:        
                   17604:        // bios data area
1.1.1.23  root     17605:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     17606:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   17607:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.58! root     17608: //     CONSOLE_FONT_INFO cfi;
1.1.1.57  root     17609: //     GetCurrentConsoleFont(hStdout, FALSE, &cfi);
1.1.1.58! root     17610:        int font_width = 10, font_height = 18; // default in english mode
        !          17611:        get_console_font_size(hStdout, &font_width, &font_height);
1.1.1.14  root     17612:        
                   17613:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   17614:        text_vram_top_address = TEXT_VRAM_TOP;
                   17615:        text_vram_end_address = text_vram_top_address + regen;
                   17616:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   17617:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     17618:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     17619:        
                   17620:        if(regen > 0x4000) {
                   17621:                regen = 0x8000;
                   17622:                vram_pages = 1;
                   17623:        } else if(regen > 0x2000) {
                   17624:                regen = 0x4000;
                   17625:                vram_pages = 2;
                   17626:        } else if(regen > 0x1000) {
                   17627:                regen = 0x2000;
                   17628:                vram_pages = 4;
                   17629:        } else {
                   17630:                regen = 0x1000;
                   17631:                vram_pages = 8;
                   17632:        }
1.1       root     17633:        
1.1.1.25  root     17634:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   17635:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     17636:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   17637:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     17638:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     17639:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   17640:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     17641: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17642:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     17643: #endif
1.1.1.26  root     17644:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     17645:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     17646:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   17647:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     17648:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     17649:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   17650:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     17651:        *(UINT16 *)(mem + 0x44e) = 0;
                   17652:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     17653:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     17654:        *(UINT8  *)(mem + 0x460) = 7;
                   17655:        *(UINT8  *)(mem + 0x461) = 7;
                   17656:        *(UINT8  *)(mem + 0x462) = 0;
                   17657:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     17658:        *(UINT8  *)(mem + 0x465) = 0x09;
                   17659:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     17660:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     17661:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   17662:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     17663:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
1.1.1.58! root     17664:        *(UINT16 *)(mem + 0x485) = font_height;
1.1.1.14  root     17665:        *(UINT8  *)(mem + 0x487) = 0x60;
                   17666:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     17667: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17668:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     17669: #endif
1.1.1.14  root     17670:        
                   17671:        // initial screen
                   17672:        SMALL_RECT rect;
                   17673:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   17674:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   17675:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   17676:                for(int x = 0; x < scr_width; x++) {
                   17677:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   17678:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   17679:                }
                   17680:        }
1.1       root     17681:        
1.1.1.19  root     17682:        // init mcb
1.1       root     17683:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     17684:        
                   17685:        // iret table
                   17686:        // note: int 2eh vector should address the routine in command.com,
                   17687:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   17688:        // so move iret table into allocated memory block
                   17689:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.58! root     17690:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, (IRET_SIZE + 5 * 128) >> 4);
1.1.1.19  root     17691:        IRET_TOP = seg << 4;
1.1.1.58! root     17692:        seg += (IRET_SIZE + 5 * 128) >> 4;
1.1.1.25  root     17693:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     17694:        
1.1.1.58! root     17695:        // note: SO1 checks int 21h vector and if it aims iret (cfh)
        !          17696:        // it is recognized SO1 is not running on MS-DOS environment
        !          17697:        for(int i = 0; i < 128; i++) {
        !          17698:                // jmp far (IRET_TOP >> 4):(interrupt number)
        !          17699:                *(UINT8  *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 0) = 0xea;
        !          17700:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 1) = i;
        !          17701:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 3) = IRET_TOP >> 4;
        !          17702:        }
        !          17703:        
1.1.1.19  root     17704:        // dummy xms/ems device
1.1.1.33  root     17705:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     17706:        XMS_TOP = seg << 4;
                   17707:        seg += XMS_SIZE >> 4;
                   17708:        
                   17709:        // environment
1.1.1.33  root     17710:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     17711:        int env_seg = seg;
                   17712:        int ofs = 0;
1.1.1.32  root     17713:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   17714:        char comspec_added = 0;
1.1.1.33  root     17715:        char lastdrive_added = 0;
1.1.1.32  root     17716:        char env_msdos_path[ENV_SIZE] = {0};
                   17717:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     17718:        char prompt_added = 0;
1.1.1.32  root     17719:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     17720:        char tz_added = 0;
1.1.1.45  root     17721:        const char *path, *short_path;
1.1.1.32  root     17722:        
                   17723:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   17724:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17725:                        strcpy(env_append, short_path);
                   17726:                }
                   17727:        }
                   17728:        if((path = getenv("APPEND")) != NULL) {
                   17729:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17730:                        if(env_append[0] != '\0') {
                   17731:                                strcat(env_append, ";");
                   17732:                        }
                   17733:                        strcat(env_append, short_path);
                   17734:                }
                   17735:        }
                   17736:        
                   17737:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   17738:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17739:                        strcpy(comspec_path, short_path);
                   17740:                }
                   17741:        }
                   17742:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   17743:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17744:                        strcpy(comspec_path, short_path);
                   17745:                }
                   17746:        }
1.1       root     17747:        
1.1.1.28  root     17748:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     17749:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17750:                        strcpy(env_msdos_path, short_path);
                   17751:                        strcpy(env_path, short_path);
1.1.1.14  root     17752:                }
                   17753:        }
1.1.1.28  root     17754:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     17755:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17756:                        if(env_path[0] != '\0') {
                   17757:                                strcat(env_path, ";");
                   17758:                        }
                   17759:                        strcat(env_path, short_path);
1.1.1.9   root     17760:                }
                   17761:        }
1.1.1.32  root     17762:        
                   17763:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   17764:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     17765:        }
1.1.1.32  root     17766:        for(int i = 0; i < 4; i++) {
                   17767:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   17768:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   17769:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17770:                                strcpy(env_temp, short_path);
                   17771:                                break;
                   17772:                        }
                   17773:                }
1.1.1.24  root     17774:        }
1.1.1.32  root     17775:        
1.1.1.9   root     17776:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     17777:                // lower to upper
1.1.1.28  root     17778:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     17779:                strcpy(tmp, *p);
                   17780:                for(int i = 0;; i++) {
                   17781:                        if(tmp[i] == '=') {
                   17782:                                tmp[i] = '\0';
                   17783:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     17784:                                my_strupr(name);
1.1       root     17785:                                tmp[i] = '=';
                   17786:                                break;
                   17787:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     17788:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     17789:                        }
                   17790:                }
1.1.1.33  root     17791:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   17792:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   17793:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     17794:                        // ignore non standard environments
                   17795:                } else {
1.1.1.33  root     17796:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     17797:                                if(env_append[0] != '\0') {
                   17798:                                        sprintf(tmp, "APPEND=%s", env_append);
                   17799:                                } else {
                   17800:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   17801:                                }
                   17802:                                append_added = 1;
                   17803:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     17804:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     17805:                                comspec_added = 1;
1.1.1.33  root     17806:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   17807:                                char *env = getenv("MSDOS_LASTDRIVE");
                   17808:                                if(env != NULL) {
                   17809:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   17810:                                }
                   17811:                                lastdrive_added = 1;
                   17812:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     17813:                                if(env_msdos_path[0] != '\0') {
                   17814:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   17815:                                } else {
                   17816:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   17817:                                }
1.1.1.33  root     17818:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     17819:                                if(env_path[0] != '\0') {
                   17820:                                        sprintf(tmp, "PATH=%s", env_path);
                   17821:                                } else {
                   17822:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   17823:                                }
1.1.1.32  root     17824:                                path_added = 1;
1.1.1.33  root     17825:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   17826:                                prompt_added = 1;
1.1.1.28  root     17827:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   17828:                                if(env_temp[0] != '\0') {
                   17829:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   17830:                                } else {
                   17831:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   17832:                                }
1.1.1.32  root     17833:                                temp_added = 1;
1.1.1.33  root     17834:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     17835:                                if(env_temp[0] != '\0') {
                   17836:                                        sprintf(tmp, "TMP=%s", env_temp);
                   17837:                                } else {
                   17838:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     17839:                                }
1.1.1.32  root     17840:                                tmp_added = 1;
1.1.1.33  root     17841:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   17842:                                char *env = getenv("MSDOS_TZ");
                   17843:                                if(env != NULL) {
                   17844:                                        sprintf(tmp, "TZ=%s", env);
                   17845:                                }
                   17846:                                tz_added = 1;
1.1       root     17847:                        }
                   17848:                        int len = strlen(tmp);
1.1.1.14  root     17849:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     17850:                                fatalerror("too many environments\n");
                   17851:                        }
                   17852:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   17853:                        ofs += len + 1;
                   17854:                }
                   17855:        }
1.1.1.32  root     17856:        if(!append_added && env_append[0] != '\0') {
                   17857:                #define SET_ENV(name, value) { \
                   17858:                        char tmp[ENV_SIZE]; \
                   17859:                        sprintf(tmp, "%s=%s", name, value); \
                   17860:                        int len = strlen(tmp); \
                   17861:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   17862:                                fatalerror("too many environments\n"); \
                   17863:                        } \
                   17864:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   17865:                        ofs += len + 1; \
                   17866:                }
                   17867:                SET_ENV("APPEND", env_append);
                   17868:        }
                   17869:        if(!comspec_added) {
                   17870:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   17871:        }
1.1.1.33  root     17872:        if(!lastdrive_added) {
                   17873:                SET_ENV("LASTDRIVE", "Z");
                   17874:        }
1.1.1.32  root     17875:        if(!path_added) {
                   17876:                SET_ENV("PATH", env_path);
                   17877:        }
1.1.1.33  root     17878:        if(!prompt_added) {
                   17879:                SET_ENV("PROMPT", "$P$G");
                   17880:        }
1.1.1.32  root     17881:        if(!temp_added) {
                   17882:                SET_ENV("TEMP", env_temp);
                   17883:        }
                   17884:        if(!tmp_added) {
                   17885:                SET_ENV("TMP", env_temp);
                   17886:        }
1.1.1.33  root     17887:        if(!tz_added) {
                   17888:                TIME_ZONE_INFORMATION tzi;
                   17889:                HKEY hKey, hSubKey;
                   17890:                char tzi_std_name[64];
                   17891:                char tz_std[8] = "GMT";
                   17892:                char tz_dlt[8] = "GST";
                   17893:                char tz_value[32];
                   17894:                
                   17895:                // timezone name from GetTimeZoneInformation may not be english
                   17896:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   17897:                setlocale(LC_CTYPE, "");
                   17898:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   17899:                
                   17900:                // get english timezone name from registry
                   17901:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   17902:                        for(DWORD i = 0; !tz_added; i++) {
                   17903:                                char reg_name[256], sub_key[1024], std_name[256];
                   17904:                                DWORD size;
                   17905:                                FILETIME ftTime;
                   17906:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   17907:                                
                   17908:                                if(result == ERROR_SUCCESS) {
                   17909:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   17910:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   17911:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   17912:                                                        // search english timezone name from table
1.1.1.37  root     17913:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     17914:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   17915:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   17916:                                                                                if(tz_table[j].std != NULL) {
                   17917:                                                                                        strcpy(tz_std, tz_table[j].std);
                   17918:                                                                                }
                   17919:                                                                                if(tz_table[j].dlt != NULL) {
                   17920:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   17921:                                                                                }
                   17922:                                                                                tz_added = 1;
                   17923:                                                                                break;
                   17924:                                                                        }
                   17925:                                                                }
                   17926:                                                        }
                   17927:                                                }
                   17928:                                                RegCloseKey(hSubKey);
                   17929:                                        }
                   17930:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   17931:                                        break;
                   17932:                                }
                   17933:                        }
                   17934:                        RegCloseKey(hKey);
                   17935:                }
                   17936:                if((tzi.Bias % 60) != 0) {
                   17937:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   17938:                } else {
                   17939:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   17940:                }
                   17941:                if(daylight) {
                   17942:                        strcat(tz_value, tz_dlt);
                   17943:                }
                   17944:                SET_ENV("TZ", tz_value);
                   17945:        }
1.1       root     17946:        seg += (ENV_SIZE >> 4);
                   17947:        
                   17948:        // psp
1.1.1.33  root     17949:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     17950:        current_psp = seg;
1.1.1.35  root     17951:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   17952:        psp->parent_psp = current_psp;
1.1       root     17953:        seg += (PSP_SIZE >> 4);
                   17954:        
1.1.1.19  root     17955:        // first free mcb in conventional memory
1.1.1.33  root     17956:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     17957:        first_mcb = seg;
                   17958:        
1.1.1.19  root     17959:        // dummy mcb to link to umb
1.1.1.33  root     17960: #if 0
1.1.1.39  root     17961:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     17962: #else
1.1.1.39  root     17963:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     17964: #endif
1.1.1.19  root     17965:        
                   17966:        // first free mcb in upper memory block
1.1.1.8   root     17967:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     17968:        
1.1.1.29  root     17969: #ifdef SUPPORT_HMA
                   17970:        // first free mcb in high memory area
                   17971:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   17972: #endif
                   17973:        
1.1.1.26  root     17974:        // interrupt vector
1.1.1.58! root     17975:        for(int i = 0; i < 256; i++) {
        !          17976:                // 00-07: CPU exception handler
        !          17977:                // 08-0F: IRQ 0-7
        !          17978:                // 10-1F: PC BIOS
        !          17979:                // 20-3F: MS-DOS system call
        !          17980:                // 70-77: IRQ 8-15
        !          17981:                *(UINT16 *)(mem + 4 * i + 0) = (i <= 0x3f || (i >= 0x70 && i <= 0x77)) ? (IRET_SIZE + 5 * i) : i;
1.1.1.26  root     17982:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   17983:        }
1.1.1.49  root     17984:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   17985:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17986:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   17987:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   17988:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   17989:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     17990:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   17991:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17992:        
1.1.1.29  root     17993:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     17994:        static const struct {
                   17995:                UINT16 attributes;
                   17996:                char *dev_name;
                   17997:        } dummy_devices[] = {
                   17998:                {0x8013, "CON     "},
                   17999:                {0x8000, "AUX     "},
                   18000:                {0xa0c0, "PRN     "},
                   18001:                {0x8008, "CLOCK$  "},
                   18002:                {0x8000, "COM1    "},
                   18003:                {0xa0c0, "LPT1    "},
                   18004:                {0xa0c0, "LPT2    "},
                   18005:                {0xa0c0, "LPT3    "},
                   18006:                {0x8000, "COM2    "},
                   18007:                {0x8000, "COM3    "},
                   18008:                {0x8000, "COM4    "},
1.1.1.30  root     18009: //             {0xc000, "CONFIG$ "},
                   18010:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     18011:        };
                   18012:        static const UINT8 dummy_device_routine[] = {
                   18013:                // from NUL device of Windows 98 SE
                   18014:                // or word ptr ES:[BX+03],0100
                   18015:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   18016:                // retf
                   18017:                0xcb,
                   18018:        };
1.1.1.29  root     18019:        device_t *last = NULL;
1.1.1.32  root     18020:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     18021:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     18022:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   18023:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18024:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     18025:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   18026:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     18027:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     18028:                last = device;
                   18029:        }
                   18030:        if(last != NULL) {
                   18031:                last->next_driver.w.l = 0;
                   18032:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     18033:        }
1.1.1.29  root     18034:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     18035:        
1.1.1.25  root     18036:        // dos info
                   18037:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   18038:        dos_info->magic_word = 1;
                   18039:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   18040:        dos_info->first_dpb.w.l = 0;
                   18041:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   18042:        dos_info->first_sft.w.l = 0;
                   18043:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     18044:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     18045:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18046:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     18047:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   18048:        dos_info->max_sector_len = 512;
                   18049:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   18050:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   18051:        dos_info->cds.w.l = 0;
                   18052:        dos_info->cds.w.h = CDS_TOP >> 4;
                   18053:        dos_info->fcb_table.w.l = 0;
                   18054:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   18055:        dos_info->last_drive = 'Z' - 'A' + 1;
                   18056:        dos_info->buffers_x = 20;
                   18057:        dos_info->buffers_y = 0;
                   18058:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     18059:        dos_info->nul_device.next_driver.w.l = 22;
                   18060:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     18061:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     18062:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   18063:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18064:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   18065:        dos_info->disk_buf_heads.w.l = 0;
                   18066:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     18067:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     18068:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   18069:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     18070:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     18071:        
                   18072:        char *env;
                   18073:        if((env = getenv("LASTDRIVE")) != NULL) {
                   18074:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18075:                        dos_info->last_drive = env[0] - 'A' + 1;
                   18076:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18077:                        dos_info->last_drive = env[0] - 'a' + 1;
                   18078:                }
                   18079:        }
                   18080:        if((env = getenv("windir")) != NULL) {
                   18081:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18082:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   18083:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18084:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   18085:                }
                   18086:        }
                   18087: #if defined(HAS_I386)
                   18088:        dos_info->i386_or_later = 1;
                   18089: #else
                   18090:        dos_info->i386_or_later = 0;
                   18091: #endif
                   18092:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   18093:        
1.1.1.27  root     18094:        // ems (int 67h) and xms
1.1.1.25  root     18095:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   18096:        xms_device->next_driver.w.l = 0xffff;
                   18097:        xms_device->next_driver.w.h = 0xffff;
                   18098:        xms_device->attributes = 0xc000;
1.1.1.29  root     18099:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   18100:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18101:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   18102:        
1.1.1.26  root     18103:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   18104:        mem[XMS_TOP + 0x13] = 0x68;
                   18105:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     18106: #ifdef SUPPORT_XMS
                   18107:        if(support_xms) {
1.1.1.26  root     18108:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   18109:                mem[XMS_TOP + 0x16] = 0x69;
                   18110:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     18111:        } else
                   18112: #endif
1.1.1.26  root     18113:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     18114:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     18115:        
1.1.1.26  root     18116:        // irq12 routine (mouse)
1.1.1.49  root     18117:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 6ah (dummy)
                   18118:        mem[DUMMY_TOP + 0x01] = 0x6a;
                   18119:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   18120:        mem[DUMMY_TOP + 0x03] = 0xff;
                   18121:        mem[DUMMY_TOP + 0x04] = 0xff;
                   18122:        mem[DUMMY_TOP + 0x05] = 0xff;
                   18123:        mem[DUMMY_TOP + 0x06] = 0xff;
                   18124:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   18125:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   18126:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     18127:        
1.1.1.27  root     18128:        // case map routine
1.1.1.49  root     18129:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   18130:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   18131:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     18132:        
                   18133:        // font read routine
1.1.1.49  root     18134:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   18135:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   18136:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     18137:        
1.1.1.32  root     18138:        // error message read routine
1.1.1.49  root     18139:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   18140:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   18141:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     18142:        
1.1.1.35  root     18143:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     18144:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   18145:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   18146:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   18147:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   18148:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     18149:        
1.1.1.50  root     18150:        // irq0 routine (system timer)
1.1.1.49  root     18151:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   18152:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   18153:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   18154:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   18155:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   18156:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   18157:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   18158:        
                   18159:        // alter page map and call routine
                   18160:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   18161:        mem[DUMMY_TOP + 0x20] = 0xff;
                   18162:        mem[DUMMY_TOP + 0x21] = 0xff;
                   18163:        mem[DUMMY_TOP + 0x22] = 0xff;
                   18164:        mem[DUMMY_TOP + 0x23] = 0xff;
                   18165:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   18166:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   18167:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     18168:        
1.1.1.50  root     18169:        // call int 29h routine
                   18170:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   18171:        mem[DUMMY_TOP + 0x28] = 0x29;
                   18172:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   18173:        
1.1.1.26  root     18174:        // boot routine
1.1.1.49  root     18175:        mem[0xffff0 + 0x00] = 0xf4;     // halt
                   18176:        mem[0xffff0 + 0x05] = '0';      // rom date
                   18177:        mem[0xffff0 + 0x06] = '2';
                   18178:        mem[0xffff0 + 0x07] = '/';
                   18179:        mem[0xffff0 + 0x08] = '2';
                   18180:        mem[0xffff0 + 0x09] = '2';
                   18181:        mem[0xffff0 + 0x0a] = '/';
                   18182:        mem[0xffff0 + 0x0b] = '0';
                   18183:        mem[0xffff0 + 0x0c] = '6';
                   18184:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id
                   18185:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.24  root     18186:        
1.1       root     18187:        // param block
                   18188:        // + 0: param block (22bytes)
                   18189:        // +24: fcb1/2 (20bytes)
                   18190:        // +44: command tail (128bytes)
                   18191:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18192:        param->env_seg = 0;
                   18193:        param->cmd_line.w.l = 44;
                   18194:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18195:        param->fcb1.w.l = 24;
                   18196:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18197:        param->fcb2.w.l = 24;
                   18198:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18199:        
                   18200:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18201:        
                   18202:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18203:        if(argc > 1) {
                   18204:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18205:                for(int i = 2; i < argc; i++) {
                   18206:                        char tmp[128];
                   18207:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18208:                        strcpy(cmd_line->cmd, tmp);
                   18209:                }
                   18210:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18211:        } else {
                   18212:                cmd_line->len = 0;
                   18213:        }
                   18214:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18215:        
                   18216:        // system file table
1.1.1.21  root     18217:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18218:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18219:        
1.1.1.19  root     18220:        // disk buffer header (from DOSBox)
                   18221:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18222:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18223:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18224:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18225:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18226:        
1.1       root     18227:        // fcb table
                   18228:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18229:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18230:        
1.1.1.41  root     18231:        // drive parameter block
1.1.1.42  root     18232:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18233:                // may be a floppy drive
1.1.1.44  root     18234:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18235:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18236:                cds->drive_attrib = 0x4000;     // physical drive
                   18237:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18238:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18239:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18240:                cds->bs_offset = 2;
                   18241:                
1.1.1.41  root     18242:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18243:                dpb->drive_num = i;
                   18244:                dpb->unit_num = i;
1.1.1.43  root     18245:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18246:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18247:        }
                   18248:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18249:                msdos_cds_update(i);
1.1.1.42  root     18250:                UINT16 seg, ofs;
                   18251:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18252:        }
                   18253:        
1.1.1.17  root     18254:        // nls stuff
                   18255:        msdos_nls_tables_init();
1.1       root     18256:        
                   18257:        // execute command
1.1.1.28  root     18258:        try {
1.1.1.52  root     18259:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18260:                        fatalerror("'%s' not found\n", argv[0]);
                   18261:                }
                   18262:        } catch(...) {
                   18263:                // we should not reach here :-(
                   18264:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18265:        }
                   18266:        retval = 0;
                   18267:        return(0);
                   18268: }
                   18269: 
                   18270: #define remove_std_file(path) { \
                   18271:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18272:        if(fd != -1) { \
                   18273:                _lseek(fd, 0, SEEK_END); \
                   18274:                int size = _tell(fd); \
                   18275:                _close(fd); \
                   18276:                if(size == 0) { \
                   18277:                        remove(path); \
                   18278:                } \
                   18279:        } \
                   18280: }
                   18281: 
                   18282: void msdos_finish()
                   18283: {
                   18284:        for(int i = 0; i < MAX_FILES; i++) {
                   18285:                if(file_handler[i].valid) {
                   18286:                        _close(i);
                   18287:                }
                   18288:        }
1.1.1.21  root     18289: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18290:        remove_std_file("stdaux.txt");
1.1.1.21  root     18291: #endif
1.1.1.30  root     18292: #ifdef SUPPORT_XMS
                   18293:        msdos_xms_finish();
                   18294: #endif
1.1       root     18295:        msdos_dbcs_table_finish();
                   18296: }
                   18297: 
                   18298: /* ----------------------------------------------------------------------------
                   18299:        PC/AT hardware emulation
                   18300: ---------------------------------------------------------------------------- */
                   18301: 
                   18302: void hardware_init()
                   18303: {
1.1.1.3   root     18304:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18305:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18306:        m_IF = 1;
1.1.1.3   root     18307: #if defined(HAS_I386)
1.1       root     18308:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18309:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18310: #endif
                   18311:        i386_set_a20_line(0);
1.1.1.14  root     18312:        
1.1.1.19  root     18313:        ems_init();
1.1.1.25  root     18314:        dma_init();
1.1       root     18315:        pic_init();
1.1.1.25  root     18316:        pio_init();
1.1.1.8   root     18317: #ifdef PIT_ALWAYS_RUNNING
                   18318:        pit_init();
                   18319: #else
1.1       root     18320:        pit_active = 0;
                   18321: #endif
1.1.1.25  root     18322:        sio_init();
1.1.1.8   root     18323:        cmos_init();
                   18324:        kbd_init();
1.1       root     18325: }
                   18326: 
1.1.1.10  root     18327: void hardware_finish()
                   18328: {
                   18329: #if defined(HAS_I386)
                   18330:        vtlb_free(m_vtlb);
                   18331: #endif
1.1.1.19  root     18332:        ems_finish();
1.1.1.37  root     18333:        pio_finish();
1.1.1.25  root     18334:        sio_finish();
1.1.1.10  root     18335: }
                   18336: 
1.1.1.28  root     18337: void hardware_release()
                   18338: {
                   18339:        // release hardware resources when this program will be terminated abnormally
                   18340: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18341:        if(fp_debug_log != NULL) {
                   18342:                fclose(fp_debug_log);
                   18343:                fp_debug_log = NULL;
1.1.1.28  root     18344:        }
                   18345: #endif
                   18346: #if defined(HAS_I386)
                   18347:        vtlb_free(m_vtlb);
                   18348: #endif
                   18349:        ems_release();
1.1.1.37  root     18350:        pio_release();
1.1.1.28  root     18351:        sio_release();
                   18352: }
                   18353: 
1.1       root     18354: void hardware_run()
                   18355: {
1.1.1.22  root     18356: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18357:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18358:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18359: #endif
1.1.1.51  root     18360: #ifdef USE_DEBUGGER
                   18361:        m_int_num = -1;
                   18362: #endif
1.1.1.54  root     18363:        while(!m_exit) {
1.1.1.50  root     18364:                hardware_run_cpu();
1.1       root     18365:        }
1.1.1.22  root     18366: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18367:        if(fp_debug_log != NULL) {
                   18368:                fclose(fp_debug_log);
                   18369:                fp_debug_log = NULL;
1.1.1.28  root     18370:        }
1.1.1.22  root     18371: #endif
1.1       root     18372: }
                   18373: 
1.1.1.50  root     18374: inline void hardware_run_cpu()
                   18375: {
                   18376: #if defined(HAS_I386)
                   18377:        CPU_EXECUTE_CALL(i386);
                   18378:        if(m_eip != m_prev_eip) {
                   18379:                idle_ops++;
                   18380:        }
                   18381: #else
                   18382:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18383:        if(m_pc != m_prevpc) {
                   18384:                idle_ops++;
                   18385:        }
                   18386: #endif
                   18387: #ifdef USE_DEBUGGER
                   18388:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18389:        if(m_int_num >= 0) {
                   18390:                unsigned num = (unsigned)m_int_num;
                   18391:                m_int_num = -1;
                   18392:                msdos_syscall(num);
                   18393:        }
                   18394: #endif
                   18395:        if(++update_ops == UPDATE_OPS) {
                   18396:                update_ops = 0;
                   18397:                hardware_update();
                   18398:        }
                   18399: }
                   18400: 
1.1       root     18401: void hardware_update()
                   18402: {
1.1.1.8   root     18403:        static UINT32 prev_time = 0;
                   18404:        UINT32 cur_time = timeGetTime();
                   18405:        
                   18406:        if(prev_time != cur_time) {
                   18407:                // update pit and raise irq0
                   18408: #ifndef PIT_ALWAYS_RUNNING
                   18409:                if(pit_active)
                   18410: #endif
                   18411:                {
                   18412:                        if(pit_run(0, cur_time)) {
                   18413:                                pic_req(0, 0, 1);
                   18414:                        }
                   18415:                        pit_run(1, cur_time);
                   18416:                        pit_run(2, cur_time);
                   18417:                }
1.1.1.24  root     18418:                
1.1.1.25  root     18419:                // update sio and raise irq4/3
1.1.1.29  root     18420:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18421:                        sio_update(c);
                   18422:                }
                   18423:                
1.1.1.24  root     18424:                // update keyboard and mouse
1.1.1.14  root     18425:                static UINT32 prev_tick = 0;
                   18426:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     18427:                
1.1.1.14  root     18428:                if(prev_tick != cur_tick) {
                   18429:                        // update keyboard flags
                   18430:                        UINT8 state;
1.1.1.24  root     18431:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   18432:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   18433:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   18434:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   18435:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   18436:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   18437:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   18438:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     18439:                        mem[0x417] = state;
                   18440:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   18441:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   18442:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   18443:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     18444: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   18445: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     18446:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   18447:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   18448:                        mem[0x418] = state;
                   18449:                        
1.1.1.24  root     18450:                        // update console input if needed
1.1.1.34  root     18451:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     18452:                                update_console_input();
                   18453:                        }
1.1.1.57  root     18454:                        if(!(kbd_status & 1)) {
                   18455:                                if(key_buf_data != NULL) {
                   18456: #ifdef USE_SERVICE_THREAD
                   18457:                                        EnterCriticalSection(&key_buf_crit_sect);
                   18458: #endif
                   18459:                                        if(!key_buf_data->empty()) {
                   18460:                                                kbd_data = key_buf_data->read();
                   18461:                                                kbd_status |= 1;
                   18462:                                                key_changed = true;
                   18463:                                        }
                   18464: #ifdef USE_SERVICE_THREAD
                   18465:                                        LeaveCriticalSection(&key_buf_crit_sect);
                   18466: #endif
                   18467:                                }
                   18468:                        }
1.1.1.24  root     18469:                        
1.1.1.57  root     18470:                        // raise irq1 if key is pressed/released or key buffer is not empty
1.1.1.56  root     18471:                        if(!key_changed) {
1.1.1.55  root     18472: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18473:                                EnterCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18474: #endif
1.1.1.57  root     18475:                                if(!pcbios_is_key_buffer_empty()) {
                   18476: /*
                   18477:                                        if(!(kbd_status & 1)) {
                   18478:                                                UINT16 head = *(UINT16 *)(mem + 0x41a);
                   18479:                                                UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   18480:                                                if(head != tail) {
                   18481:                                                        int key_char = mem[0x400 + (head++)];
                   18482:                                                        int key_scan = mem[0x400 + (head++)];
                   18483:                                                        kbd_data = key_char ? key_char : key_scan;
                   18484:                                                        kbd_status |= 1;
                   18485:                                                }
                   18486:                                        }
                   18487: */
                   18488:                                        key_changed = true;
                   18489:                                }
1.1.1.55  root     18490: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18491:                                LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18492: #endif
1.1.1.56  root     18493:                        }
                   18494:                        if(key_changed) {
1.1.1.8   root     18495:                                pic_req(0, 1, 1);
1.1.1.56  root     18496:                                key_changed = false;
1.1.1.24  root     18497:                        }
                   18498:                        
                   18499:                        // raise irq12 if mouse status is changed
1.1.1.58! root     18500: /*
1.1.1.54  root     18501:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw) {
                   18502:                                mouse.status_irq = 0; // ???
                   18503:                                mouse.status_irq_alt = 0; // ???
                   18504:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
                   18505:                                mouse.status = 0;
                   18506:                                pic_req(1, 4, 1);
1.1.1.58! root     18507:                        } else
        !          18508: */
        !          18509:                        if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     18510:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   18511:                                mouse.status_irq_alt = 0; // ???
1.1.1.54  root     18512:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     18513:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     18514:                                pic_req(1, 4, 1);
                   18515:                        } else {
                   18516:                                for(int i = 0; i < 8; i++) {
                   18517:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   18518:                                                mouse.status_irq = 0; // ???
                   18519:                                                mouse.status_irq_alt = 0;
1.1.1.54  root     18520:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     18521:                                                for(int j = 0; j < 8; j++) {
                   18522:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   18523:                                                                mouse.status_irq_alt |=  (1 << j);
                   18524:                                                                mouse.status_alt     &= ~(1 << j);
                   18525:                                                        }
                   18526:                                                }
                   18527:                                                pic_req(1, 4, 1);
                   18528:                                                break;
                   18529:                                        }
                   18530:                                }
1.1.1.8   root     18531:                        }
1.1.1.24  root     18532:                        
1.1.1.58! root     18533:                        // update cursor position by crtc
        !          18534:                        if(crtc_changed[14] != 0 || crtc_changed[15] != 0) {
        !          18535:                                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
        !          18536:                                int position = crtc_regs[14] * 256 + crtc_regs[15];
        !          18537:                                int width = *(UINT16 *)(mem + 0x44a);
        !          18538:                                COORD co;
        !          18539:                                co.X = position % width;
        !          18540:                                co.Y = position / width + scr_top;
        !          18541:                                SetConsoleCursorPosition(hStdout, co);
        !          18542:                                
        !          18543:                                crtc_changed[14] = crtc_changed[15] = 0;
        !          18544:                        }
1.1.1.14  root     18545:                        prev_tick = cur_tick;
1.1.1.8   root     18546:                }
1.1.1.24  root     18547:                
1.1.1.19  root     18548:                // update daily timer counter
                   18549:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     18550:                
1.1.1.8   root     18551:                prev_time = cur_time;
1.1       root     18552:        }
                   18553: }
                   18554: 
1.1.1.19  root     18555: // ems
                   18556: 
                   18557: void ems_init()
                   18558: {
                   18559:        memset(ems_handles, 0, sizeof(ems_handles));
                   18560:        memset(ems_pages, 0, sizeof(ems_pages));
                   18561:        free_ems_pages = MAX_EMS_PAGES;
                   18562: }
                   18563: 
                   18564: void ems_finish()
                   18565: {
1.1.1.28  root     18566:        ems_release();
                   18567: }
                   18568: 
                   18569: void ems_release()
                   18570: {
1.1.1.31  root     18571:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     18572:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     18573:                        free(ems_handles[i].buffer);
                   18574:                        ems_handles[i].buffer = NULL;
                   18575:                }
                   18576:        }
                   18577: }
                   18578: 
                   18579: void ems_allocate_pages(int handle, int pages)
                   18580: {
                   18581:        if(pages > 0) {
                   18582:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18583:        } else {
                   18584:                ems_handles[handle].buffer = NULL;
                   18585:        }
                   18586:        ems_handles[handle].pages = pages;
                   18587:        ems_handles[handle].allocated = true;
                   18588:        free_ems_pages -= pages;
                   18589: }
                   18590: 
                   18591: void ems_reallocate_pages(int handle, int pages)
                   18592: {
                   18593:        if(ems_handles[handle].allocated) {
                   18594:                if(ems_handles[handle].pages != pages) {
                   18595:                        UINT8 *new_buffer = NULL;
                   18596:                        
                   18597:                        if(pages > 0) {
                   18598:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18599:                        }
1.1.1.32  root     18600:                        if(ems_handles[handle].buffer != NULL) {
                   18601:                                if(new_buffer != NULL) {
1.1.1.19  root     18602:                                        if(pages > ems_handles[handle].pages) {
                   18603:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   18604:                                        } else {
                   18605:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   18606:                                        }
                   18607:                                }
                   18608:                                free(ems_handles[handle].buffer);
                   18609:                                ems_handles[handle].buffer = NULL;
                   18610:                        }
                   18611:                        free_ems_pages += ems_handles[handle].pages;
                   18612:                        
                   18613:                        ems_handles[handle].buffer = new_buffer;
                   18614:                        ems_handles[handle].pages = pages;
                   18615:                        free_ems_pages -= pages;
                   18616:                }
                   18617:        } else {
                   18618:                ems_allocate_pages(handle, pages);
                   18619:        }
                   18620: }
                   18621: 
                   18622: void ems_release_pages(int handle)
                   18623: {
                   18624:        if(ems_handles[handle].allocated) {
1.1.1.32  root     18625:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     18626:                        free(ems_handles[handle].buffer);
                   18627:                        ems_handles[handle].buffer = NULL;
                   18628:                }
                   18629:                free_ems_pages += ems_handles[handle].pages;
                   18630:                ems_handles[handle].allocated = false;
                   18631:        }
                   18632: }
                   18633: 
                   18634: void ems_map_page(int physical, int handle, int logical)
                   18635: {
                   18636:        if(ems_pages[physical].mapped) {
                   18637:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   18638:                        return;
                   18639:                }
                   18640:                ems_unmap_page(physical);
                   18641:        }
1.1.1.32  root     18642:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18643:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   18644:        }
                   18645:        ems_pages[physical].handle = handle;
                   18646:        ems_pages[physical].page = logical;
                   18647:        ems_pages[physical].mapped = true;
                   18648: }
                   18649: 
                   18650: void ems_unmap_page(int physical)
                   18651: {
                   18652:        if(ems_pages[physical].mapped) {
                   18653:                int handle = ems_pages[physical].handle;
                   18654:                int logical = ems_pages[physical].page;
                   18655:                
1.1.1.32  root     18656:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18657:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   18658:                }
                   18659:                ems_pages[physical].mapped = false;
                   18660:        }
                   18661: }
                   18662: 
1.1.1.25  root     18663: // dma
1.1       root     18664: 
1.1.1.25  root     18665: void dma_init()
1.1       root     18666: {
1.1.1.26  root     18667:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     18668:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     18669: //             for(int ch = 0; ch < 4; ch++) {
                   18670: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   18671: //             }
1.1.1.25  root     18672:                dma_reset(c);
                   18673:        }
1.1       root     18674: }
                   18675: 
1.1.1.25  root     18676: void dma_reset(int c)
1.1       root     18677: {
1.1.1.25  root     18678:        dma[c].low_high = false;
                   18679:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   18680:        dma[c].mask = 0xff;
                   18681: }
                   18682: 
                   18683: void dma_write(int c, UINT32 addr, UINT8 data)
                   18684: {
                   18685:        int ch = (addr >> 1) & 3;
                   18686:        UINT8 bit = 1 << (data & 3);
                   18687:        
                   18688:        switch(addr & 0x0f) {
                   18689:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18690:                if(dma[c].low_high) {
                   18691:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     18692:                } else {
1.1.1.25  root     18693:                        dma[c].ch[ch].bareg.b.l = data;
                   18694:                }
                   18695:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18696:                dma[c].low_high = !dma[c].low_high;
                   18697:                break;
                   18698:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18699:                if(dma[c].low_high) {
                   18700:                        dma[c].ch[ch].bcreg.b.h = data;
                   18701:                } else {
                   18702:                        dma[c].ch[ch].bcreg.b.l = data;
                   18703:                }
                   18704:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18705:                dma[c].low_high = !dma[c].low_high;
                   18706:                break;
                   18707:        case 0x08:
                   18708:                // command register
                   18709:                dma[c].cmd = data;
                   18710:                break;
                   18711:        case 0x09:
                   18712:                // dma[c].request register
                   18713:                if(data & 4) {
                   18714:                        if(!(dma[c].req & bit)) {
                   18715:                                dma[c].req |= bit;
                   18716: //                             dma_run(c, ch);
                   18717:                        }
                   18718:                } else {
                   18719:                        dma[c].req &= ~bit;
                   18720:                }
                   18721:                break;
                   18722:        case 0x0a:
                   18723:                // single mask register
                   18724:                if(data & 4) {
                   18725:                        dma[c].mask |= bit;
                   18726:                } else {
                   18727:                        dma[c].mask &= ~bit;
                   18728:                }
                   18729:                break;
                   18730:        case 0x0b:
                   18731:                // mode register
                   18732:                dma[c].ch[data & 3].mode = data;
                   18733:                break;
                   18734:        case 0x0c:
                   18735:                dma[c].low_high = false;
                   18736:                break;
                   18737:        case 0x0d:
                   18738:                // clear master
                   18739:                dma_reset(c);
                   18740:                break;
                   18741:        case 0x0e:
                   18742:                // clear mask register
                   18743:                dma[c].mask = 0;
                   18744:                break;
                   18745:        case 0x0f:
                   18746:                // all mask register
                   18747:                dma[c].mask = data & 0x0f;
                   18748:                break;
                   18749:        }
                   18750: }
                   18751: 
                   18752: UINT8 dma_read(int c, UINT32 addr)
                   18753: {
                   18754:        int ch = (addr >> 1) & 3;
                   18755:        UINT8 val = 0xff;
                   18756:        
                   18757:        switch(addr & 0x0f) {
                   18758:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18759:                if(dma[c].low_high) {
                   18760:                        val = dma[c].ch[ch].areg.b.h;
                   18761:                } else {
                   18762:                        val = dma[c].ch[ch].areg.b.l;
                   18763:                }
                   18764:                dma[c].low_high = !dma[c].low_high;
                   18765:                return(val);
                   18766:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18767:                if(dma[c].low_high) {
                   18768:                        val = dma[c].ch[ch].creg.b.h;
                   18769:                } else {
                   18770:                        val = dma[c].ch[ch].creg.b.l;
                   18771:                }
                   18772:                dma[c].low_high = !dma[c].low_high;
                   18773:                return(val);
                   18774:        case 0x08:
                   18775:                // status register
                   18776:                val = (dma[c].req << 4) | dma[c].tc;
                   18777:                dma[c].tc = 0;
                   18778:                return(val);
                   18779:        case 0x0d:
1.1.1.26  root     18780:                // temporary register (intel 82374 does not support)
1.1.1.25  root     18781:                return(dma[c].tmp & 0xff);
1.1.1.26  root     18782:        case 0x0f:
                   18783:                // mask register (intel 82374 does support)
                   18784:                return(dma[c].mask);
1.1.1.25  root     18785:        }
                   18786:        return(0xff);
                   18787: }
                   18788: 
                   18789: void dma_page_write(int c, int ch, UINT8 data)
                   18790: {
                   18791:        dma[c].ch[ch].pagereg = data;
                   18792: }
                   18793: 
                   18794: UINT8 dma_page_read(int c, int ch)
                   18795: {
                   18796:        return(dma[c].ch[ch].pagereg);
                   18797: }
                   18798: 
                   18799: void dma_run(int c, int ch)
                   18800: {
                   18801:        UINT8 bit = 1 << ch;
                   18802:        
                   18803:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   18804:                // execute dma
                   18805:                while(dma[c].req & bit) {
                   18806:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   18807:                                // memory -> memory
                   18808:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   18809:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   18810:                                
                   18811:                                if(c == 0) {
                   18812:                                        dma[c].tmp = read_byte(saddr);
                   18813:                                        write_byte(daddr, dma[c].tmp);
                   18814:                                } else {
                   18815:                                        dma[c].tmp = read_word(saddr << 1);
                   18816:                                        write_word(daddr << 1, dma[c].tmp);
                   18817:                                }
                   18818:                                if(!(dma[c].cmd & 0x02)) {
                   18819:                                        if(dma[c].ch[0].mode & 0x20) {
                   18820:                                                dma[c].ch[0].areg.w--;
                   18821:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   18822:                                                        dma[c].ch[0].pagereg--;
                   18823:                                                }
                   18824:                                        } else {
                   18825:                                                dma[c].ch[0].areg.w++;
                   18826:                                                if(dma[c].ch[0].areg.w == 0) {
                   18827:                                                        dma[c].ch[0].pagereg++;
                   18828:                                                }
                   18829:                                        }
                   18830:                                }
                   18831:                                if(dma[c].ch[1].mode & 0x20) {
                   18832:                                        dma[c].ch[1].areg.w--;
                   18833:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   18834:                                                dma[c].ch[1].pagereg--;
                   18835:                                        }
                   18836:                                } else {
                   18837:                                        dma[c].ch[1].areg.w++;
                   18838:                                        if(dma[c].ch[1].areg.w == 0) {
                   18839:                                                dma[c].ch[1].pagereg++;
                   18840:                                        }
                   18841:                                }
                   18842:                                
                   18843:                                // check dma condition
                   18844:                                if(dma[c].ch[0].creg.w-- == 0) {
                   18845:                                        if(dma[c].ch[0].mode & 0x10) {
                   18846:                                                // self initialize
                   18847:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   18848:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   18849:                                        } else {
                   18850: //                                             dma[c].mask |= bit;
                   18851:                                        }
                   18852:                                }
                   18853:                                if(dma[c].ch[1].creg.w-- == 0) {
                   18854:                                        // terminal count
                   18855:                                        if(dma[c].ch[1].mode & 0x10) {
                   18856:                                                // self initialize
                   18857:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   18858:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   18859:                                        } else {
                   18860:                                                dma[c].mask |= bit;
                   18861:                                        }
                   18862:                                        dma[c].req &= ~bit;
                   18863:                                        dma[c].tc |= bit;
                   18864:                                }
                   18865:                        } else {
                   18866:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   18867:                                
                   18868:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   18869:                                        // verify
                   18870:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   18871:                                        // io -> memory
                   18872:                                        if(c == 0) {
                   18873:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   18874:                                                write_byte(addr, dma[c].tmp);
                   18875:                                        } else {
                   18876:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   18877:                                                write_word(addr << 1, dma[c].tmp);
                   18878:                                        }
                   18879:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   18880:                                        // memory -> io
                   18881:                                        if(c == 0) {
                   18882:                                                dma[c].tmp = read_byte(addr);
                   18883:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   18884:                                        } else {
                   18885:                                                dma[c].tmp = read_word(addr << 1);
                   18886:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   18887:                                        }
                   18888:                                }
                   18889:                                if(dma[c].ch[ch].mode & 0x20) {
                   18890:                                        dma[c].ch[ch].areg.w--;
                   18891:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   18892:                                                dma[c].ch[ch].pagereg--;
                   18893:                                        }
                   18894:                                } else {
                   18895:                                        dma[c].ch[ch].areg.w++;
                   18896:                                        if(dma[c].ch[ch].areg.w == 0) {
                   18897:                                                dma[c].ch[ch].pagereg++;
                   18898:                                        }
                   18899:                                }
                   18900:                                
                   18901:                                // check dma condition
                   18902:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   18903:                                        // terminal count
                   18904:                                        if(dma[c].ch[ch].mode & 0x10) {
                   18905:                                                // self initialize
                   18906:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18907:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18908:                                        } else {
                   18909:                                                dma[c].mask |= bit;
                   18910:                                        }
                   18911:                                        dma[c].req &= ~bit;
                   18912:                                        dma[c].tc |= bit;
                   18913:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   18914:                                        // single mode
                   18915:                                        break;
                   18916:                                }
                   18917:                        }
                   18918:                }
                   18919:        }
                   18920: }
                   18921: 
                   18922: // pic
                   18923: 
                   18924: void pic_init()
                   18925: {
                   18926:        memset(pic, 0, sizeof(pic));
                   18927:        pic[0].imr = pic[1].imr = 0xff;
                   18928:        
                   18929:        // from bochs bios
                   18930:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   18931:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   18932:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   18933:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   18934:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   18935:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   18936:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   18937:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   18938:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   18939: }
                   18940: 
                   18941: void pic_write(int c, UINT32 addr, UINT8 data)
                   18942: {
                   18943:        if(addr & 1) {
                   18944:                if(pic[c].icw2_r) {
                   18945:                        // icw2
                   18946:                        pic[c].icw2 = data;
                   18947:                        pic[c].icw2_r = 0;
                   18948:                } else if(pic[c].icw3_r) {
                   18949:                        // icw3
                   18950:                        pic[c].icw3 = data;
                   18951:                        pic[c].icw3_r = 0;
                   18952:                } else if(pic[c].icw4_r) {
                   18953:                        // icw4
                   18954:                        pic[c].icw4 = data;
                   18955:                        pic[c].icw4_r = 0;
                   18956:                } else {
                   18957:                        // ocw1
1.1       root     18958:                        pic[c].imr = data;
                   18959:                }
                   18960:        } else {
                   18961:                if(data & 0x10) {
                   18962:                        // icw1
                   18963:                        pic[c].icw1 = data;
                   18964:                        pic[c].icw2_r = 1;
                   18965:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   18966:                        pic[c].icw4_r = data & 1;
                   18967:                        pic[c].irr = 0;
                   18968:                        pic[c].isr = 0;
                   18969:                        pic[c].imr = 0;
                   18970:                        pic[c].prio = 0;
                   18971:                        if(!(pic[c].icw1 & 1)) {
                   18972:                                pic[c].icw4 = 0;
                   18973:                        }
                   18974:                        pic[c].ocw3 = 0;
                   18975:                } else if(data & 8) {
                   18976:                        // ocw3
                   18977:                        if(!(data & 2)) {
                   18978:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   18979:                        }
                   18980:                        if(!(data & 0x40)) {
                   18981:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   18982:                        }
                   18983:                        pic[c].ocw3 = data;
                   18984:                } else {
                   18985:                        // ocw2
                   18986:                        int level = 0;
                   18987:                        if(data & 0x40) {
                   18988:                                level = data & 7;
                   18989:                        } else {
                   18990:                                if(!pic[c].isr) {
                   18991:                                        return;
                   18992:                                }
                   18993:                                level = pic[c].prio;
                   18994:                                while(!(pic[c].isr & (1 << level))) {
                   18995:                                        level = (level + 1) & 7;
                   18996:                                }
                   18997:                        }
                   18998:                        if(data & 0x80) {
                   18999:                                pic[c].prio = (level + 1) & 7;
                   19000:                        }
                   19001:                        if(data & 0x20) {
                   19002:                                pic[c].isr &= ~(1 << level);
                   19003:                        }
                   19004:                }
                   19005:        }
                   19006:        pic_update();
                   19007: }
                   19008: 
                   19009: UINT8 pic_read(int c, UINT32 addr)
                   19010: {
                   19011:        if(addr & 1) {
                   19012:                return(pic[c].imr);
                   19013:        } else {
                   19014:                // polling mode is not supported...
                   19015:                //if(pic[c].ocw3 & 4) {
                   19016:                //      return ???;
                   19017:                //}
                   19018:                if(pic[c].ocw3 & 1) {
                   19019:                        return(pic[c].isr);
                   19020:                } else {
                   19021:                        return(pic[c].irr);
                   19022:                }
                   19023:        }
                   19024: }
                   19025: 
                   19026: void pic_req(int c, int level, int signal)
                   19027: {
                   19028:        if(signal) {
                   19029:                pic[c].irr |= (1 << level);
                   19030:        } else {
                   19031:                pic[c].irr &= ~(1 << level);
                   19032:        }
                   19033:        pic_update();
                   19034: }
                   19035: 
                   19036: int pic_ack()
                   19037: {
                   19038:        // ack (INTA=L)
                   19039:        pic[pic_req_chip].isr |= pic_req_bit;
                   19040:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   19041:        if(pic_req_chip > 0) {
                   19042:                // update isr and irr of master
                   19043:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   19044:                pic[pic_req_chip - 1].isr |= slave;
                   19045:                pic[pic_req_chip - 1].irr &= ~slave;
                   19046:        }
                   19047:        //if(pic[pic_req_chip].icw4 & 1) {
                   19048:                // 8086 mode
                   19049:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   19050:        //} else {
                   19051:        //      // 8080 mode
                   19052:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   19053:        //      if(pic[pic_req_chip].icw1 & 4) {
                   19054:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   19055:        //      } else {
                   19056:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   19057:        //      }
                   19058:        //      vector = 0xcd | (addr << 8);
                   19059:        //}
                   19060:        if(pic[pic_req_chip].icw4 & 2) {
                   19061:                // auto eoi
                   19062:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   19063:        }
                   19064:        return(vector);
                   19065: }
                   19066: 
                   19067: void pic_update()
                   19068: {
                   19069:        for(int c = 0; c < 2; c++) {
                   19070:                UINT8 irr = pic[c].irr;
                   19071:                if(c + 1 < 2) {
                   19072:                        // this is master
                   19073:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   19074:                                // request from slave
                   19075:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   19076:                        }
                   19077:                }
                   19078:                irr &= (~pic[c].imr);
                   19079:                if(!irr) {
                   19080:                        break;
                   19081:                }
                   19082:                if(!(pic[c].ocw3 & 0x20)) {
                   19083:                        irr |= pic[c].isr;
                   19084:                }
                   19085:                int level = pic[c].prio;
                   19086:                UINT8 bit = 1 << level;
                   19087:                while(!(irr & bit)) {
                   19088:                        level = (level + 1) & 7;
                   19089:                        bit = 1 << level;
                   19090:                }
                   19091:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   19092:                        // check slave
                   19093:                        continue;
                   19094:                }
                   19095:                if(pic[c].isr & bit) {
                   19096:                        break;
                   19097:                }
                   19098:                // interrupt request
                   19099:                pic_req_chip = c;
                   19100:                pic_req_level = level;
                   19101:                pic_req_bit = bit;
1.1.1.3   root     19102:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     19103:                return;
                   19104:        }
1.1.1.3   root     19105:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     19106: }
1.1       root     19107: 
1.1.1.25  root     19108: // pio
                   19109: 
                   19110: void pio_init()
                   19111: {
1.1.1.38  root     19112: //     bool conv_mode = (GetConsoleCP() == 932);
                   19113:        
1.1.1.26  root     19114:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     19115:        
1.1.1.25  root     19116:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     19117:                pio[c].stat = 0xdf;
1.1.1.25  root     19118:                pio[c].ctrl = 0x0c;
1.1.1.38  root     19119: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     19120:        }
                   19121: }
                   19122: 
1.1.1.37  root     19123: void pio_finish()
                   19124: {
                   19125:        pio_release();
                   19126: }
                   19127: 
                   19128: void pio_release()
                   19129: {
                   19130:        for(int c = 0; c < 2; c++) {
                   19131:                if(pio[c].fp != NULL) {
1.1.1.38  root     19132:                        if(pio[c].jis_mode) {
                   19133:                                fputc(0x1c, pio[c].fp);
                   19134:                                fputc(0x2e, pio[c].fp);
                   19135:                        }
1.1.1.37  root     19136:                        fclose(pio[c].fp);
                   19137:                        pio[c].fp = NULL;
                   19138:                }
                   19139:        }
                   19140: }
                   19141: 
1.1.1.25  root     19142: void pio_write(int c, UINT32 addr, UINT8 data)
                   19143: {
                   19144:        switch(addr & 3) {
                   19145:        case 0:
                   19146:                pio[c].data = data;
                   19147:                break;
                   19148:        case 2:
1.1.1.37  root     19149:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   19150:                        // strobe H -> L
                   19151:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   19152:                                // auto feed
                   19153:                                printer_out(c, 0x0d);
                   19154:                                printer_out(c, 0x0a);
                   19155:                        } else {
                   19156:                                printer_out(c, pio[c].data);
                   19157:                        }
                   19158:                        pio[c].stat &= ~0x40; // set ack
                   19159:                }
1.1.1.25  root     19160:                pio[c].ctrl = data;
                   19161:                break;
                   19162:        }
                   19163: }
                   19164: 
                   19165: UINT8 pio_read(int c, UINT32 addr)
                   19166: {
                   19167:        switch(addr & 3) {
                   19168:        case 0:
1.1.1.37  root     19169:                if(pio[c].ctrl & 0x20) {
                   19170:                        // input mode
                   19171:                        return(0xff);
                   19172:                }
1.1.1.25  root     19173:                return(pio[c].data);
                   19174:        case 1:
1.1.1.37  root     19175:                {
                   19176:                        UINT8 stat = pio[c].stat;
                   19177:                        pio[c].stat |= 0x40; // clear ack
                   19178:                        return(stat);
                   19179:                }
1.1.1.25  root     19180:        case 2:
                   19181:                return(pio[c].ctrl);
                   19182:        }
                   19183:        return(0xff);
                   19184: }
                   19185: 
1.1.1.37  root     19186: void printer_out(int c, UINT8 data)
                   19187: {
                   19188:        SYSTEMTIME time;
1.1.1.38  root     19189:        bool jis_mode = false;
1.1.1.37  root     19190:        
                   19191:        GetLocalTime(&time);
                   19192:        
                   19193:        if(pio[c].fp != NULL) {
                   19194:                // if at least 1000ms passed from last written, close the current file
                   19195:                FILETIME ftime1;
                   19196:                FILETIME ftime2;
                   19197:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   19198:                SystemTimeToFileTime(&time, &ftime2);
                   19199:                INT64 *time1 = (INT64 *)&ftime1;
                   19200:                INT64 *time2 = (INT64 *)&ftime2;
                   19201:                INT64 msec = (*time2 - *time1) / 10000;
                   19202:                
                   19203:                if(msec >= 1000) {
1.1.1.38  root     19204:                        if(pio[c].jis_mode) {
                   19205:                                fputc(0x1c, pio[c].fp);
                   19206:                                fputc(0x2e, pio[c].fp);
                   19207:                                jis_mode = true;
                   19208:                        }
1.1.1.37  root     19209:                        fclose(pio[c].fp);
                   19210:                        pio[c].fp = NULL;
                   19211:                }
                   19212:        }
                   19213:        if(pio[c].fp == NULL) {
                   19214:                // create a new file in the temp folder
                   19215:                char file_name[MAX_PATH];
                   19216:                
                   19217:                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);
                   19218:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   19219:                        strcat(pio[c].path, file_name);
                   19220:                } else {
                   19221:                        strcpy(pio[c].path, file_name);
                   19222:                }
1.1.1.38  root     19223:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     19224:        }
                   19225:        if(pio[c].fp != NULL) {
1.1.1.38  root     19226:                if(jis_mode) {
                   19227:                        fputc(0x1c, pio[c].fp);
                   19228:                        fputc(0x26, pio[c].fp);
                   19229:                }
1.1.1.37  root     19230:                fputc(data, pio[c].fp);
1.1.1.38  root     19231:                
                   19232:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   19233:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   19234:                        UINT8 buffer[4];
                   19235:                        fseek(pio[c].fp, 0, SEEK_SET);
                   19236:                        fread(buffer, 4, 1, pio[c].fp);
                   19237:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   19238:                                fclose(pio[c].fp);
                   19239:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   19240:                        }
                   19241:                }
1.1.1.37  root     19242:                pio[c].time = time;
                   19243:        }
                   19244: }
                   19245: 
1.1       root     19246: // pit
                   19247: 
1.1.1.22  root     19248: #define PIT_FREQ 1193182ULL
1.1       root     19249: #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)
                   19250: 
                   19251: void pit_init()
                   19252: {
1.1.1.8   root     19253:        memset(pit, 0, sizeof(pit));
1.1       root     19254:        for(int ch = 0; ch < 3; ch++) {
                   19255:                pit[ch].count = 0x10000;
                   19256:                pit[ch].ctrl_reg = 0x34;
                   19257:                pit[ch].mode = 3;
                   19258:        }
                   19259:        
                   19260:        // from bochs bios
                   19261:        pit_write(3, 0x34);
                   19262:        pit_write(0, 0x00);
                   19263:        pit_write(0, 0x00);
                   19264: }
                   19265: 
                   19266: void pit_write(int ch, UINT8 val)
                   19267: {
1.1.1.8   root     19268: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19269:        if(!pit_active) {
                   19270:                pit_active = 1;
                   19271:                pit_init();
                   19272:        }
1.1.1.8   root     19273: #endif
1.1       root     19274:        switch(ch) {
                   19275:        case 0:
                   19276:        case 1:
                   19277:        case 2:
                   19278:                // write count register
                   19279:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19280:                        if(pit[ch].ctrl_reg & 0x10) {
                   19281:                                pit[ch].low_write = 1;
                   19282:                        }
                   19283:                        if(pit[ch].ctrl_reg & 0x20) {
                   19284:                                pit[ch].high_write = 1;
                   19285:                        }
                   19286:                }
                   19287:                if(pit[ch].low_write) {
                   19288:                        pit[ch].count_reg = val;
                   19289:                        pit[ch].low_write = 0;
                   19290:                } else if(pit[ch].high_write) {
                   19291:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19292:                                pit[ch].count_reg = val << 8;
                   19293:                        } else {
                   19294:                                pit[ch].count_reg |= val << 8;
                   19295:                        }
                   19296:                        pit[ch].high_write = 0;
                   19297:                }
                   19298:                // start count
1.1.1.8   root     19299:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19300:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19301:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19302:                                pit[ch].prev_time = timeGetTime();
                   19303:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19304:                        }
                   19305:                }
                   19306:                break;
                   19307:        case 3: // ctrl reg
                   19308:                if((val & 0xc0) == 0xc0) {
                   19309:                        // i8254 read-back command
                   19310:                        for(ch = 0; ch < 3; ch++) {
                   19311:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19312:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19313:                                        pit[ch].status_latched = 1;
                   19314:                                }
                   19315:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19316:                                        pit_latch_count(ch);
                   19317:                                }
                   19318:                        }
                   19319:                        break;
                   19320:                }
                   19321:                ch = (val >> 6) & 3;
                   19322:                if(val & 0x30) {
1.1.1.35  root     19323:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19324:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19325:                        pit[ch].count_latched = 0;
                   19326:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19327:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19328:                        pit[ch].ctrl_reg = val;
                   19329:                        // stop count
1.1.1.8   root     19330:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19331:                        pit[ch].count_reg = 0;
                   19332:                } else if(!pit[ch].count_latched) {
                   19333:                        pit_latch_count(ch);
                   19334:                }
                   19335:                break;
                   19336:        }
                   19337: }
                   19338: 
                   19339: UINT8 pit_read(int ch)
                   19340: {
1.1.1.8   root     19341: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19342:        if(!pit_active) {
                   19343:                pit_active = 1;
                   19344:                pit_init();
                   19345:        }
1.1.1.8   root     19346: #endif
1.1       root     19347:        switch(ch) {
                   19348:        case 0:
                   19349:        case 1:
                   19350:        case 2:
                   19351:                if(pit[ch].status_latched) {
                   19352:                        pit[ch].status_latched = 0;
                   19353:                        return(pit[ch].status);
                   19354:                }
                   19355:                // if not latched, through current count
                   19356:                if(!pit[ch].count_latched) {
                   19357:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19358:                                pit_latch_count(ch);
                   19359:                        }
                   19360:                }
                   19361:                // return latched count
                   19362:                if(pit[ch].low_read) {
                   19363:                        pit[ch].low_read = 0;
                   19364:                        if(!pit[ch].high_read) {
                   19365:                                pit[ch].count_latched = 0;
                   19366:                        }
                   19367:                        return(pit[ch].latch & 0xff);
                   19368:                } else if(pit[ch].high_read) {
                   19369:                        pit[ch].high_read = 0;
                   19370:                        pit[ch].count_latched = 0;
                   19371:                        return((pit[ch].latch >> 8) & 0xff);
                   19372:                }
                   19373:        }
                   19374:        return(0xff);
                   19375: }
                   19376: 
1.1.1.8   root     19377: int pit_run(int ch, UINT32 cur_time)
1.1       root     19378: {
1.1.1.8   root     19379:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19380:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19381:                pit[ch].prev_time = pit[ch].expired_time;
                   19382:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19383:                if(cur_time >= pit[ch].expired_time) {
                   19384:                        pit[ch].prev_time = cur_time;
                   19385:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19386:                }
1.1.1.8   root     19387:                return(1);
1.1       root     19388:        }
1.1.1.8   root     19389:        return(0);
1.1       root     19390: }
                   19391: 
                   19392: void pit_latch_count(int ch)
                   19393: {
1.1.1.8   root     19394:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19395:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19396:                pit_run(ch, cur_time);
                   19397:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19398:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19399:                
                   19400:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19401:                        // decrement counter in 1msec period
                   19402:                        if(pit[ch].next_latch == 0) {
                   19403:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   19404:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19405:                        }
                   19406:                        if(pit[ch].latch > pit[ch].next_latch) {
                   19407:                                pit[ch].latch--;
                   19408:                        }
                   19409:                } else {
                   19410:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   19411:                        pit[ch].next_latch = 0;
                   19412:                }
1.1.1.8   root     19413:        } else {
                   19414:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     19415:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     19416:        }
                   19417:        pit[ch].count_latched = 1;
                   19418:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   19419:                // lower byte
                   19420:                pit[ch].low_read = 1;
                   19421:                pit[ch].high_read = 0;
                   19422:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19423:                // upper byte
                   19424:                pit[ch].low_read = 0;
                   19425:                pit[ch].high_read = 1;
                   19426:        } else {
                   19427:                // lower -> upper
1.1.1.14  root     19428:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     19429:        }
                   19430: }
                   19431: 
1.1.1.8   root     19432: int pit_get_expired_time(int ch)
1.1       root     19433: {
1.1.1.22  root     19434:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   19435:        UINT64 val = pit[ch].accum >> 10;
                   19436:        pit[ch].accum -= val << 10;
                   19437:        return((val != 0) ? val : 1);
1.1.1.8   root     19438: }
                   19439: 
1.1.1.25  root     19440: // sio
                   19441: 
                   19442: void sio_init()
                   19443: {
1.1.1.26  root     19444:        memset(sio, 0, sizeof(sio));
                   19445:        memset(sio_mt, 0, sizeof(sio_mt));
                   19446:        
1.1.1.29  root     19447:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19448:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19449:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19450:                
                   19451:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   19452:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     19453:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   19454:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     19455:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   19456:                sio[c].irq_identify = 0x01;     // no pending irq
                   19457:                
                   19458:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   19459:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   19460:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   19461:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   19462:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   19463:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   19464:                
1.1.1.26  root     19465:                if(sio_port_number[c] != 0) {
1.1.1.25  root     19466:                        sio[c].channel = c;
                   19467:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   19468:                }
                   19469:        }
                   19470: }
                   19471: 
                   19472: void sio_finish()
                   19473: {
1.1.1.29  root     19474:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19475:                if(sio_mt[c].hThread != NULL) {
                   19476:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   19477:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     19478:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     19479:                }
                   19480:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   19481:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   19482:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   19483:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   19484:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   19485:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     19486:        }
                   19487:        sio_release();
                   19488: }
                   19489: 
                   19490: void sio_release()
                   19491: {
1.1.1.29  root     19492:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     19493:                // sio_thread() may access the resources :-(
1.1.1.32  root     19494:                bool running = (sio_mt[c].hThread != NULL);
                   19495:                
                   19496:                if(running) {
                   19497:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   19498:                }
                   19499:                if(sio[c].send_buffer != NULL) {
                   19500:                        sio[c].send_buffer->release();
                   19501:                        delete sio[c].send_buffer;
                   19502:                        sio[c].send_buffer = NULL;
                   19503:                }
                   19504:                if(running) {
                   19505:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19506:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   19507:                }
                   19508:                if(sio[c].recv_buffer != NULL) {
                   19509:                        sio[c].recv_buffer->release();
                   19510:                        delete sio[c].recv_buffer;
                   19511:                        sio[c].recv_buffer = NULL;
                   19512:                }
                   19513:                if(running) {
                   19514:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     19515:                }
1.1.1.25  root     19516:        }
                   19517: }
                   19518: 
                   19519: void sio_write(int c, UINT32 addr, UINT8 data)
                   19520: {
                   19521:        switch(addr & 7) {
                   19522:        case 0:
                   19523:                if(sio[c].selector & 0x80) {
                   19524:                        if(sio[c].divisor.b.l != data) {
                   19525:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19526:                                sio[c].divisor.b.l = data;
                   19527:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19528:                        }
                   19529:                } else {
                   19530:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19531:                        if(sio[c].send_buffer != NULL) {
                   19532:                                sio[c].send_buffer->write(data);
                   19533:                        }
1.1.1.25  root     19534:                        // transmitter holding/shift registers are not empty
                   19535:                        sio[c].line_stat_buf &= ~0x60;
                   19536:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19537:                        
                   19538:                        if(sio[c].irq_enable & 0x02) {
                   19539:                                sio_update_irq(c);
                   19540:                        }
                   19541:                }
                   19542:                break;
                   19543:        case 1:
                   19544:                if(sio[c].selector & 0x80) {
                   19545:                        if(sio[c].divisor.b.h != data) {
                   19546:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19547:                                sio[c].divisor.b.h = data;
                   19548:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19549:                        }
                   19550:                } else {
                   19551:                        if(sio[c].irq_enable != data) {
                   19552:                                sio[c].irq_enable = data;
                   19553:                                sio_update_irq(c);
                   19554:                        }
                   19555:                }
                   19556:                break;
                   19557:        case 3:
                   19558:                {
                   19559:                        UINT8 line_ctrl = data & 0x3f;
                   19560:                        bool set_brk = ((data & 0x40) != 0);
                   19561:                        
                   19562:                        if(sio[c].line_ctrl != line_ctrl) {
                   19563:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19564:                                sio[c].line_ctrl = line_ctrl;
                   19565:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19566:                        }
                   19567:                        if(sio[c].set_brk != set_brk) {
                   19568:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   19569:                                sio[c].set_brk = set_brk;
                   19570:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19571:                        }
                   19572:                }
                   19573:                sio[c].selector = data;
                   19574:                break;
                   19575:        case 4:
                   19576:                {
                   19577:                        bool set_dtr = ((data & 0x01) != 0);
                   19578:                        bool set_rts = ((data & 0x02) != 0);
                   19579:                        
                   19580:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     19581: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     19582:                                sio[c].set_dtr = set_dtr;
                   19583:                                sio[c].set_rts = set_rts;
1.1.1.26  root     19584: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19585:                                
                   19586:                                bool state_changed = false;
                   19587:                                
                   19588:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19589:                                if(set_dtr) {
                   19590:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   19591:                                } else {
                   19592:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   19593:                                }
                   19594:                                if(set_rts) {
                   19595:                                        sio[c].modem_stat |= 0x10;      // cts on
                   19596:                                } else {
                   19597:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   19598:                                }
                   19599:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   19600:                                        if(!(sio[c].modem_stat & 0x02)) {
                   19601:                                                if(sio[c].irq_enable & 0x08) {
                   19602:                                                        state_changed = true;
                   19603:                                                }
                   19604:                                                sio[c].modem_stat |= 0x02;
                   19605:                                        }
                   19606:                                }
                   19607:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   19608:                                        if(!(sio[c].modem_stat & 0x01)) {
                   19609:                                                if(sio[c].irq_enable & 0x08) {
                   19610:                                                        state_changed = true;
                   19611:                                                }
                   19612:                                                sio[c].modem_stat |= 0x01;
                   19613:                                        }
                   19614:                                }
                   19615:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19616:                                
                   19617:                                if(state_changed) {
                   19618:                                        sio_update_irq(c);
                   19619:                                }
1.1.1.25  root     19620:                        }
                   19621:                }
                   19622:                sio[c].modem_ctrl = data;
                   19623:                break;
                   19624:        case 7:
                   19625:                sio[c].scratch = data;
                   19626:                break;
                   19627:        }
                   19628: }
                   19629: 
                   19630: UINT8 sio_read(int c, UINT32 addr)
                   19631: {
                   19632:        switch(addr & 7) {
                   19633:        case 0:
                   19634:                if(sio[c].selector & 0x80) {
                   19635:                        return(sio[c].divisor.b.l);
                   19636:                } else {
                   19637:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19638:                        UINT8 data = 0;
                   19639:                        if(sio[c].recv_buffer != NULL) {
                   19640:                                data = sio[c].recv_buffer->read();
                   19641:                        }
1.1.1.25  root     19642:                        // data is not ready
                   19643:                        sio[c].line_stat_buf &= ~0x01;
                   19644:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19645:                        
                   19646:                        if(sio[c].irq_enable & 0x01) {
                   19647:                                sio_update_irq(c);
                   19648:                        }
                   19649:                        return(data);
                   19650:                }
                   19651:        case 1:
                   19652:                if(sio[c].selector & 0x80) {
                   19653:                        return(sio[c].divisor.b.h);
                   19654:                } else {
                   19655:                        return(sio[c].irq_enable);
                   19656:                }
                   19657:        case 2:
                   19658:                return(sio[c].irq_identify);
                   19659:        case 3:
                   19660:                return(sio[c].selector);
                   19661:        case 4:
                   19662:                return(sio[c].modem_ctrl);
                   19663:        case 5:
                   19664:                {
                   19665:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   19666:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   19667:                        sio[c].line_stat_err = 0x00;
                   19668:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19669:                        
                   19670:                        bool state_changed = false;
                   19671:                        
                   19672:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19673:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19674:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19675:                                        // transmitter holding register will be empty first
                   19676:                                        if(sio[c].irq_enable & 0x02) {
                   19677:                                                state_changed = true;
                   19678:                                        }
                   19679:                                        sio[c].line_stat_buf |= 0x20;
                   19680:                                }
                   19681:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19682:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19683:                                // transmitter shift register will be empty later
                   19684:                                sio[c].line_stat_buf |= 0x40;
                   19685:                        }
                   19686:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   19687:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19688:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19689:                                        // data is ready
                   19690:                                        if(sio[c].irq_enable & 0x01) {
                   19691:                                                state_changed = true;
                   19692:                                        }
                   19693:                                        sio[c].line_stat_buf |= 0x01;
                   19694:                                }
                   19695:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19696:                        }
                   19697:                        if(state_changed) {
                   19698:                                sio_update_irq(c);
                   19699:                        }
                   19700:                        return(val);
                   19701:                }
                   19702:        case 6:
                   19703:                {
                   19704:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   19705:                        UINT8 val = sio[c].modem_stat;
                   19706:                        sio[c].modem_stat &= 0xf0;
                   19707:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   19708:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19709:                        
                   19710:                        if(sio[c].modem_ctrl & 0x10) {
                   19711:                                // loop-back
                   19712:                                val &= 0x0f;
                   19713:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   19714:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   19715:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   19716:                        }
                   19717:                        return(val);
                   19718:                }
                   19719:        case 7:
                   19720:                return(sio[c].scratch);
                   19721:        }
                   19722:        return(0xff);
                   19723: }
                   19724: 
                   19725: void sio_update(int c)
                   19726: {
                   19727:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19728:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19729:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19730:                        // transmitter holding/shift registers will be empty
                   19731:                        sio[c].line_stat_buf |= 0x60;
                   19732:                }
                   19733:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19734:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19735:                // transmitter shift register will be empty
                   19736:                sio[c].line_stat_buf |= 0x40;
                   19737:        }
                   19738:        if(!(sio[c].line_stat_buf & 0x01)) {
                   19739:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19740:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19741:                        // data is ready
                   19742:                        sio[c].line_stat_buf |= 0x01;
                   19743:                }
                   19744:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19745:        }
                   19746:        sio_update_irq(c);
                   19747: }
                   19748: 
                   19749: void sio_update_irq(int c)
                   19750: {
                   19751:        int level = -1;
                   19752:        
                   19753:        if(sio[c].irq_enable & 0x08) {
                   19754:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19755:                if((sio[c].modem_stat & 0x0f) != 0) {
                   19756:                        level = 0;
                   19757:                }
                   19758:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19759:        }
                   19760:        if(sio[c].irq_enable & 0x02) {
                   19761:                if(sio[c].line_stat_buf & 0x20) {
                   19762:                        level = 1;
                   19763:                }
                   19764:        }
                   19765:        if(sio[c].irq_enable & 0x01) {
                   19766:                if(sio[c].line_stat_buf & 0x01) {
                   19767:                        level = 2;
                   19768:                }
                   19769:        }
                   19770:        if(sio[c].irq_enable & 0x04) {
                   19771:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   19772:                if(sio[c].line_stat_err != 0) {
                   19773:                        level = 3;
                   19774:                }
                   19775:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19776:        }
1.1.1.29  root     19777:        
                   19778:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     19779:        if(level != -1) {
                   19780:                sio[c].irq_identify = level << 1;
1.1.1.29  root     19781:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     19782:        } else {
                   19783:                sio[c].irq_identify = 1;
1.1.1.29  root     19784:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     19785:        }
                   19786: }
                   19787: 
                   19788: DWORD WINAPI sio_thread(void *lpx)
                   19789: {
                   19790:        volatile sio_t *p = (sio_t *)lpx;
                   19791:        sio_mt_t *q = &sio_mt[p->channel];
                   19792:        
                   19793:        char name[] = "COM1";
1.1.1.26  root     19794:        name[3] = '0' + sio_port_number[p->channel];
                   19795:        HANDLE hComm = NULL;
                   19796:        COMMPROP commProp;
                   19797:        DCB dcb;
                   19798:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   19799:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   19800:        
                   19801:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   19802:                if(GetCommProperties(hComm, &commProp)) {
                   19803:                        dwSettableBaud = commProp.dwSettableBaud;
                   19804:                }
1.1.1.25  root     19805:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     19806: //             EscapeCommFunction(hComm, SETRTS);
                   19807: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     19808:                
1.1.1.54  root     19809:                while(!m_exit) {
1.1.1.25  root     19810:                        // setup comm port
                   19811:                        bool comm_state_changed = false;
                   19812:                        
                   19813:                        EnterCriticalSection(&q->csLineCtrl);
                   19814:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   19815:                                p->prev_divisor = p->divisor.w;
                   19816:                                p->prev_line_ctrl = p->line_ctrl;
                   19817:                                comm_state_changed = true;
                   19818:                        }
                   19819:                        LeaveCriticalSection(&q->csLineCtrl);
                   19820:                        
                   19821:                        if(comm_state_changed) {
1.1.1.26  root     19822:                                if(GetCommState(hComm, &dcb)) {
                   19823: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   19824:                                        DWORD baud = 115200 / p->prev_divisor;
                   19825:                                        dcb.BaudRate = 9600; // default
                   19826:                                        
                   19827:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   19828:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   19829:                                        // 134.5bps is not supported ???
                   19830: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   19831:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   19832:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   19833:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   19834:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   19835:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   19836:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   19837:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   19838:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   19839:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   19840: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   19841: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   19842: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   19843:                                        
                   19844:                                        switch(p->prev_line_ctrl & 0x03) {
                   19845:                                        case 0x00: dcb.ByteSize = 5; break;
                   19846:                                        case 0x01: dcb.ByteSize = 6; break;
                   19847:                                        case 0x02: dcb.ByteSize = 7; break;
                   19848:                                        case 0x03: dcb.ByteSize = 8; break;
                   19849:                                        }
                   19850:                                        switch(p->prev_line_ctrl & 0x04) {
                   19851:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   19852:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   19853:                                        }
                   19854:                                        switch(p->prev_line_ctrl & 0x38) {
                   19855:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   19856:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   19857:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   19858:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   19859:                                        default:   dcb.Parity = NOPARITY;    break;
                   19860:                                        }
                   19861:                                        dcb.fBinary = TRUE;
                   19862:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   19863:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   19864:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   19865:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   19866:                                        dcb.fTXContinueOnXoff = TRUE;
                   19867:                                        dcb.fOutX = dcb.fInX = FALSE;
                   19868:                                        dcb.fErrorChar = FALSE;
                   19869:                                        dcb.fNull = FALSE;
                   19870:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   19871:                                        dcb.fAbortOnError = FALSE;
                   19872:                                        
                   19873:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     19874:                                }
                   19875:                                
                   19876:                                // check again to apply all comm state changes
                   19877:                                Sleep(10);
                   19878:                                continue;
                   19879:                        }
                   19880:                        
                   19881:                        // set comm pins
                   19882:                        bool change_brk = false;
1.1.1.26  root     19883: //                     bool change_rts = false;
                   19884: //                     bool change_dtr = false;
1.1.1.25  root     19885:                        
                   19886:                        EnterCriticalSection(&q->csModemCtrl);
                   19887:                        if(p->prev_set_brk != p->set_brk) {
                   19888:                                p->prev_set_brk = p->set_brk;
                   19889:                                change_brk = true;
                   19890:                        }
1.1.1.26  root     19891: //                     if(p->prev_set_rts != p->set_rts) {
                   19892: //                             p->prev_set_rts = p->set_rts;
                   19893: //                             change_rts = true;
                   19894: //                     }
                   19895: //                     if(p->prev_set_dtr != p->set_dtr) {
                   19896: //                             p->prev_set_dtr = p->set_dtr;
                   19897: //                             change_dtr = true;
                   19898: //                     }
1.1.1.25  root     19899:                        LeaveCriticalSection(&q->csModemCtrl);
                   19900:                        
                   19901:                        if(change_brk) {
1.1.1.26  root     19902:                                static UINT32 clear_time = 0;
                   19903:                                if(p->prev_set_brk) {
                   19904:                                        EscapeCommFunction(hComm, SETBREAK);
                   19905:                                        clear_time = timeGetTime() + 200;
                   19906:                                } else {
                   19907:                                        // keep break for at least 200msec
                   19908:                                        UINT32 cur_time = timeGetTime();
                   19909:                                        if(clear_time > cur_time) {
                   19910:                                                Sleep(clear_time - cur_time);
                   19911:                                        }
                   19912:                                        EscapeCommFunction(hComm, CLRBREAK);
                   19913:                                }
1.1.1.25  root     19914:                        }
1.1.1.26  root     19915: //                     if(change_rts) {
                   19916: //                             if(p->prev_set_rts) {
                   19917: //                                     EscapeCommFunction(hComm, SETRTS);
                   19918: //                             } else {
                   19919: //                                     EscapeCommFunction(hComm, CLRRTS);
                   19920: //                             }
                   19921: //                     }
                   19922: //                     if(change_dtr) {
                   19923: //                             if(p->prev_set_dtr) {
                   19924: //                                     EscapeCommFunction(hComm, SETDTR);
                   19925: //                             } else {
                   19926: //                                     EscapeCommFunction(hComm, CLRDTR);
                   19927: //                             }
                   19928: //                     }
1.1.1.25  root     19929:                        
                   19930:                        // get comm pins
                   19931:                        DWORD dwModemStat = 0;
                   19932:                        
                   19933:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   19934:                                EnterCriticalSection(&q->csModemStat);
                   19935:                                if(dwModemStat & MS_RLSD_ON) {
                   19936:                                        p->modem_stat |= 0x80;
                   19937:                                } else {
                   19938:                                        p->modem_stat &= ~0x80;
                   19939:                                }
                   19940:                                if(dwModemStat & MS_RING_ON) {
                   19941:                                        p->modem_stat |= 0x40;
                   19942:                                } else {
                   19943:                                        p->modem_stat &= ~0x40;
                   19944:                                }
1.1.1.26  root     19945: //                             if(dwModemStat & MS_DSR_ON) {
                   19946: //                                     p->modem_stat |= 0x20;
                   19947: //                             } else {
                   19948: //                                     p->modem_stat &= ~0x20;
                   19949: //                             }
                   19950: //                             if(dwModemStat & MS_CTS_ON) {
                   19951: //                                     p->modem_stat |= 0x10;
                   19952: //                             } else {
                   19953: //                                     p->modem_stat &= ~0x10;
                   19954: //                             }
1.1.1.25  root     19955:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   19956:                                        p->modem_stat |= 0x08;
                   19957:                                }
                   19958:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   19959:                                        p->modem_stat |= 0x04;
                   19960:                                }
1.1.1.26  root     19961: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   19962: //                                     p->modem_stat |= 0x02;
                   19963: //                             }
                   19964: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   19965: //                                     p->modem_stat |= 0x01;
                   19966: //                             }
1.1.1.25  root     19967:                                LeaveCriticalSection(&q->csModemStat);
                   19968:                        }
                   19969:                        
                   19970:                        // send data
                   19971:                        DWORD dwSend = 0;
                   19972:                        
                   19973:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     19974:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     19975:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   19976:                        }
                   19977:                        LeaveCriticalSection(&q->csSendData);
                   19978:                        
                   19979:                        if(dwSend != 0) {
                   19980:                                DWORD dwWritten = 0;
                   19981:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   19982:                        }
                   19983:                        
                   19984:                        // get line status and recv data
                   19985:                        DWORD dwLineStat = 0;
                   19986:                        COMSTAT comStat;
                   19987:                        
                   19988:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   19989:                                EnterCriticalSection(&q->csLineStat);
                   19990:                                if(dwLineStat & CE_BREAK) {
                   19991:                                        p->line_stat_err |= 0x10;
                   19992:                                }
                   19993:                                if(dwLineStat & CE_FRAME) {
                   19994:                                        p->line_stat_err |= 0x08;
                   19995:                                }
                   19996:                                if(dwLineStat & CE_RXPARITY) {
                   19997:                                        p->line_stat_err |= 0x04;
                   19998:                                }
                   19999:                                if(dwLineStat & CE_OVERRUN) {
                   20000:                                        p->line_stat_err |= 0x02;
                   20001:                                }
                   20002:                                LeaveCriticalSection(&q->csLineStat);
                   20003:                                
                   20004:                                if(comStat.cbInQue != 0) {
                   20005:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20006:                                        DWORD dwRecv = 0;
                   20007:                                        if(p->recv_buffer != NULL) {
                   20008:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   20009:                                        }
1.1.1.25  root     20010:                                        LeaveCriticalSection(&q->csRecvData);
                   20011:                                        
                   20012:                                        if(dwRecv != 0) {
                   20013:                                                DWORD dwRead = 0;
                   20014:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   20015:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20016:                                                        if(p->recv_buffer != NULL) {
                   20017:                                                                for(int i = 0; i < dwRead; i++) {
                   20018:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   20019:                                                                }
1.1.1.25  root     20020:                                                        }
                   20021:                                                        LeaveCriticalSection(&q->csRecvData);
                   20022:                                                }
                   20023:                                        }
                   20024:                                }
                   20025:                        }
                   20026:                        Sleep(10);
                   20027:                }
                   20028:                CloseHandle(hComm);
                   20029:        }
                   20030:        return 0;
                   20031: }
                   20032: 
1.1.1.8   root     20033: // cmos
                   20034: 
                   20035: void cmos_init()
                   20036: {
                   20037:        memset(cmos, 0, sizeof(cmos));
                   20038:        cmos_addr = 0;
1.1       root     20039:        
1.1.1.8   root     20040:        // from DOSBox
                   20041:        cmos_write(0x0a, 0x26);
                   20042:        cmos_write(0x0b, 0x02);
                   20043:        cmos_write(0x0d, 0x80);
1.1       root     20044: }
                   20045: 
1.1.1.8   root     20046: void cmos_write(int addr, UINT8 val)
1.1       root     20047: {
1.1.1.8   root     20048:        cmos[addr & 0x7f] = val;
                   20049: }
                   20050: 
                   20051: #define CMOS_GET_TIME() { \
                   20052:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   20053:        if(prev_sec != cur_sec) { \
                   20054:                GetLocalTime(&time); \
                   20055:                prev_sec = cur_sec; \
                   20056:        } \
1.1       root     20057: }
1.1.1.8   root     20058: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     20059: 
1.1.1.8   root     20060: UINT8 cmos_read(int addr)
1.1       root     20061: {
1.1.1.8   root     20062:        static SYSTEMTIME time;
                   20063:        static UINT32 prev_sec = 0;
1.1       root     20064:        
1.1.1.8   root     20065:        switch(addr & 0x7f) {
                   20066:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   20067:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   20068:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   20069:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   20070:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   20071:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   20072:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   20073: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   20074:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   20075:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   20076:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   20077:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20078:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20079:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20080:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20081:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     20082:        }
1.1.1.8   root     20083:        return(cmos[addr & 0x7f]);
1.1       root     20084: }
                   20085: 
1.1.1.7   root     20086: // kbd (a20)
                   20087: 
                   20088: void kbd_init()
                   20089: {
1.1.1.8   root     20090:        kbd_data = kbd_command = 0;
1.1.1.7   root     20091:        kbd_status = 0x18;
                   20092: }
                   20093: 
                   20094: UINT8 kbd_read_data()
                   20095: {
1.1.1.57  root     20096:        UINT8 data = kbd_data;
                   20097:        kbd_data = 0;
1.1.1.8   root     20098:        kbd_status &= ~1;
1.1.1.57  root     20099:        return(data);
1.1.1.7   root     20100: }
                   20101: 
                   20102: void kbd_write_data(UINT8 val)
                   20103: {
                   20104:        switch(kbd_command) {
                   20105:        case 0xd1:
                   20106:                i386_set_a20_line((val >> 1) & 1);
                   20107:                break;
                   20108:        }
                   20109:        kbd_command = 0;
1.1.1.8   root     20110:        kbd_status &= ~8;
1.1.1.7   root     20111: }
                   20112: 
                   20113: UINT8 kbd_read_status()
                   20114: {
                   20115:        return(kbd_status);
                   20116: }
                   20117: 
                   20118: void kbd_write_command(UINT8 val)
                   20119: {
                   20120:        switch(val) {
                   20121:        case 0xd0:
                   20122:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     20123:                kbd_status |= 1;
1.1.1.7   root     20124:                break;
                   20125:        case 0xdd:
                   20126:                i386_set_a20_line(0);
                   20127:                break;
                   20128:        case 0xdf:
                   20129:                i386_set_a20_line(1);
                   20130:                break;
1.1.1.26  root     20131:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   20132:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     20133:                if(!(val & 1)) {
1.1.1.8   root     20134:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     20135:                                // reset pic
                   20136:                                pic_init();
                   20137:                                pic[0].irr = pic[1].irr = 0x00;
                   20138:                                pic[0].imr = pic[1].imr = 0xff;
                   20139:                        }
                   20140:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     20141:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   20142:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   20143:                        i386_jmp_far(selector, address);
1.1.1.7   root     20144:                }
                   20145:                i386_set_a20_line((val >> 1) & 1);
                   20146:                break;
                   20147:        }
                   20148:        kbd_command = val;
1.1.1.8   root     20149:        kbd_status |= 8;
1.1.1.7   root     20150: }
                   20151: 
1.1.1.9   root     20152: // vga
                   20153: 
                   20154: UINT8 vga_read_status()
                   20155: {
                   20156:        // 60hz
                   20157:        static const int period[3] = {16, 17, 17};
                   20158:        static int index = 0;
                   20159:        UINT32 time = timeGetTime() % period[index];
                   20160:        
                   20161:        index = (index + 1) % 3;
1.1.1.14  root     20162:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     20163: }
                   20164: 
1.1       root     20165: // i/o bus
                   20166: 
1.1.1.29  root     20167: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   20168: //#define SW1US_PATCH
                   20169: 
1.1.1.25  root     20170: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     20171: #ifdef USE_DEBUGGER
1.1.1.25  root     20172: {
1.1.1.33  root     20173:        if(now_debugging) {
                   20174:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20175:                        if(in_break_point.table[i].status == 1) {
                   20176:                                if(addr == in_break_point.table[i].addr) {
                   20177:                                        in_break_point.hit = i + 1;
                   20178:                                        now_suspended = true;
                   20179:                                        break;
                   20180:                                }
                   20181:                        }
                   20182:                }
1.1.1.25  root     20183:        }
1.1.1.33  root     20184:        return(debugger_read_io_byte(addr));
1.1.1.25  root     20185: }
1.1.1.33  root     20186: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     20187: #endif
1.1       root     20188: {
1.1.1.33  root     20189:        UINT8 val = 0xff;
                   20190:        
1.1       root     20191:        switch(addr) {
1.1.1.29  root     20192: #ifdef SW1US_PATCH
                   20193:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20194:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     20195:                val = sio_read(0, addr - 1);
                   20196:                break;
1.1.1.29  root     20197: #else
1.1.1.25  root     20198:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20199:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     20200:                val = dma_read(0, addr);
                   20201:                break;
1.1.1.29  root     20202: #endif
1.1.1.25  root     20203:        case 0x20: case 0x21:
1.1.1.33  root     20204:                val = pic_read(0, addr);
                   20205:                break;
1.1.1.25  root     20206:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     20207:                val = pit_read(addr & 0x03);
                   20208:                break;
1.1.1.7   root     20209:        case 0x60:
1.1.1.33  root     20210:                val = kbd_read_data();
                   20211:                break;
1.1.1.9   root     20212:        case 0x61:
1.1.1.33  root     20213:                val = system_port;
                   20214:                break;
1.1.1.7   root     20215:        case 0x64:
1.1.1.33  root     20216:                val = kbd_read_status();
                   20217:                break;
1.1       root     20218:        case 0x71:
1.1.1.33  root     20219:                val = cmos_read(cmos_addr);
                   20220:                break;
1.1.1.25  root     20221:        case 0x81:
1.1.1.33  root     20222:                val = dma_page_read(0, 2);
                   20223:                break;
1.1.1.25  root     20224:        case 0x82:
1.1.1.33  root     20225:                val = dma_page_read(0, 3);
                   20226:                break;
1.1.1.25  root     20227:        case 0x83:
1.1.1.33  root     20228:                val = dma_page_read(0, 1);
                   20229:                break;
1.1.1.25  root     20230:        case 0x87:
1.1.1.33  root     20231:                val = dma_page_read(0, 0);
                   20232:                break;
1.1.1.25  root     20233:        case 0x89:
1.1.1.33  root     20234:                val = dma_page_read(1, 2);
                   20235:                break;
1.1.1.25  root     20236:        case 0x8a:
1.1.1.33  root     20237:                val = dma_page_read(1, 3);
                   20238:                break;
1.1.1.25  root     20239:        case 0x8b:
1.1.1.33  root     20240:                val = dma_page_read(1, 1);
                   20241:                break;
1.1.1.25  root     20242:        case 0x8f:
1.1.1.33  root     20243:                val = dma_page_read(1, 0);
                   20244:                break;
1.1       root     20245:        case 0x92:
1.1.1.33  root     20246:                val = (m_a20_mask >> 19) & 2;
                   20247:                break;
1.1.1.25  root     20248:        case 0xa0: case 0xa1:
1.1.1.33  root     20249:                val = pic_read(1, addr);
                   20250:                break;
1.1.1.25  root     20251:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20252:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20253:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20254:                break;
1.1.1.37  root     20255:        case 0x278: case 0x279: case 0x27a:
                   20256:                val = pio_read(1, addr);
                   20257:                break;
1.1.1.29  root     20258:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20259:                val = sio_read(3, addr);
                   20260:                break;
1.1.1.25  root     20261:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20262:                val = sio_read(1, addr);
                   20263:                break;
1.1.1.25  root     20264:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20265:                val = pio_read(0, addr);
                   20266:                break;
1.1.1.25  root     20267:        case 0x3ba: case 0x3da:
1.1.1.33  root     20268:                val = vga_read_status();
                   20269:                break;
1.1.1.37  root     20270:        case 0x3bc: case 0x3bd: case 0x3be:
                   20271:                val = pio_read(2, addr);
                   20272:                break;
1.1.1.58! root     20273:        case 0x3d5:
        !          20274:                if(crtc_addr < 16) {
        !          20275:                        val = crtc_regs[crtc_addr];
        !          20276:                }
        !          20277:                break;
1.1.1.29  root     20278:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20279:                val = sio_read(2, addr);
                   20280:                break;
1.1.1.25  root     20281:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20282:                val = sio_read(0, addr);
                   20283:                break;
1.1       root     20284:        default:
1.1.1.33  root     20285: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20286:                break;
                   20287:        }
1.1.1.33  root     20288: #ifdef ENABLE_DEBUG_IOPORT
                   20289:        if(fp_debug_log != NULL) {
                   20290:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20291:        }
                   20292: #endif
                   20293:        return(val);
1.1       root     20294: }
                   20295: 
                   20296: UINT16 read_io_word(offs_t addr)
                   20297: {
                   20298:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20299: }
                   20300: 
1.1.1.33  root     20301: #ifdef USE_DEBUGGER
                   20302: UINT16 debugger_read_io_word(offs_t addr)
                   20303: {
                   20304:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20305: }
                   20306: #endif
                   20307: 
1.1       root     20308: UINT32 read_io_dword(offs_t addr)
                   20309: {
                   20310:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20311: }
                   20312: 
1.1.1.33  root     20313: #ifdef USE_DEBUGGER
                   20314: UINT32 debugger_read_io_dword(offs_t addr)
                   20315: {
                   20316:        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));
                   20317: }
                   20318: #endif
                   20319: 
1.1       root     20320: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20321: #ifdef USE_DEBUGGER
                   20322: {
                   20323:        if(now_debugging) {
                   20324:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20325:                        if(out_break_point.table[i].status == 1) {
                   20326:                                if(addr == out_break_point.table[i].addr) {
                   20327:                                        out_break_point.hit = i + 1;
                   20328:                                        now_suspended = true;
                   20329:                                        break;
                   20330:                                }
                   20331:                        }
                   20332:                }
                   20333:        }
                   20334:        debugger_write_io_byte(addr, val);
                   20335: }
                   20336: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20337: #endif
1.1       root     20338: {
1.1.1.25  root     20339: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20340:        if(fp_debug_log != NULL) {
1.1.1.43  root     20341: #ifdef USE_SERVICE_THREAD
                   20342:                if(addr != 0xf7)
                   20343: #endif
1.1.1.33  root     20344:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20345:        }
                   20346: #endif
1.1       root     20347:        switch(addr) {
1.1.1.29  root     20348: #ifdef SW1US_PATCH
                   20349:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20350:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20351:                sio_write(0, addr - 1, val);
                   20352:                break;
                   20353: #else
1.1.1.25  root     20354:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20355:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20356:                dma_write(0, addr, val);
                   20357:                break;
1.1.1.29  root     20358: #endif
1.1.1.25  root     20359:        case 0x20: case 0x21:
1.1       root     20360:                pic_write(0, addr, val);
                   20361:                break;
1.1.1.25  root     20362:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20363:                pit_write(addr & 0x03, val);
                   20364:                break;
1.1.1.7   root     20365:        case 0x60:
                   20366:                kbd_write_data(val);
                   20367:                break;
1.1.1.9   root     20368:        case 0x61:
                   20369:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20370:                        // beep on
                   20371: //                     MessageBeep(-1);
                   20372:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20373:                        // beep off
                   20374:                }
                   20375:                system_port = val;
                   20376:                break;
1.1       root     20377:        case 0x64:
1.1.1.7   root     20378:                kbd_write_command(val);
1.1       root     20379:                break;
                   20380:        case 0x70:
                   20381:                cmos_addr = val;
                   20382:                break;
                   20383:        case 0x71:
1.1.1.8   root     20384:                cmos_write(cmos_addr, val);
1.1       root     20385:                break;
1.1.1.25  root     20386:        case 0x81:
                   20387:                dma_page_write(0, 2, val);
                   20388:        case 0x82:
                   20389:                dma_page_write(0, 3, val);
                   20390:        case 0x83:
                   20391:                dma_page_write(0, 1, val);
                   20392:        case 0x87:
                   20393:                dma_page_write(0, 0, val);
                   20394:        case 0x89:
                   20395:                dma_page_write(1, 2, val);
                   20396:        case 0x8a:
                   20397:                dma_page_write(1, 3, val);
                   20398:        case 0x8b:
                   20399:                dma_page_write(1, 1, val);
                   20400:        case 0x8f:
                   20401:                dma_page_write(1, 0, val);
1.1       root     20402:        case 0x92:
1.1.1.7   root     20403:                i386_set_a20_line((val >> 1) & 1);
1.1       root     20404:                break;
1.1.1.25  root     20405:        case 0xa0: case 0xa1:
1.1       root     20406:                pic_write(1, addr, val);
                   20407:                break;
1.1.1.25  root     20408:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20409:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     20410:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     20411:                break;
1.1.1.35  root     20412: #ifdef USE_SERVICE_THREAD
                   20413:        case 0xf7:
                   20414:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     20415:                if(in_service && cursor_moved) {
                   20416:                        // update cursor position before service is done
                   20417:                        pcbios_update_cursor_position();
                   20418:                        cursor_moved = false;
                   20419:                }
1.1.1.35  root     20420:                finish_service_loop();
                   20421:                break;
                   20422: #endif
1.1.1.37  root     20423:        case 0x278: case 0x279: case 0x27a:
                   20424:                pio_write(1, addr, val);
                   20425:                break;
1.1.1.29  root     20426:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   20427:                sio_write(3, addr, val);
                   20428:                break;
1.1.1.25  root     20429:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   20430:                sio_write(1, addr, val);
                   20431:                break;
                   20432:        case 0x378: case 0x379: case 0x37a:
                   20433:                pio_write(0, addr, val);
                   20434:                break;
1.1.1.37  root     20435:        case 0x3bc: case 0x3bd: case 0x3be:
                   20436:                pio_write(2, addr, val);
                   20437:                break;
1.1.1.58! root     20438:        case 0x3d4:
        !          20439:                crtc_addr = val;
        !          20440:                break;
        !          20441:        case 0x3d5:
        !          20442:                if(crtc_addr < 16) {
        !          20443:                        if(crtc_regs[crtc_addr] != val) {
        !          20444:                                crtc_regs[crtc_addr] = val;
        !          20445:                                crtc_changed[crtc_addr] = 1;
        !          20446:                        }
        !          20447:                }
        !          20448:                break;
1.1.1.29  root     20449:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   20450:                sio_write(2, addr, val);
                   20451:                break;
1.1.1.25  root     20452:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   20453:                sio_write(0, addr, val);
                   20454:                break;
1.1       root     20455:        default:
1.1.1.33  root     20456: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     20457:                break;
                   20458:        }
                   20459: }
                   20460: 
                   20461: void write_io_word(offs_t addr, UINT16 val)
                   20462: {
                   20463:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20464:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20465: }
                   20466: 
1.1.1.33  root     20467: #ifdef USE_DEBUGGER
                   20468: void debugger_write_io_word(offs_t addr, UINT16 val)
                   20469: {
                   20470:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20471:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20472: }
                   20473: #endif
                   20474: 
1.1       root     20475: void write_io_dword(offs_t addr, UINT32 val)
                   20476: {
                   20477:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20478:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20479:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20480:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20481: }
1.1.1.33  root     20482: 
                   20483: #ifdef USE_DEBUGGER
                   20484: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   20485: {
                   20486:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20487:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20488:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20489:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20490: }
                   20491: #endif

unix.superglobalmegacorp.com

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