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

1.1       root        1: /*
                      2:        MS-DOS Player for Win32 console
                      3: 
                      4:        Author : Takeda.Toshiya
                      5:        Date   : 2009.11.09-
                      6: */
                      7: 
                      8: #include "msdos.h"
                      9: 
1.1.1.28  root       10: void exit_handler();
                     11: 
1.1       root       12: #define fatalerror(...) { \
                     13:        fprintf(stderr, __VA_ARGS__); \
1.1.1.28  root       14:        exit_handler(); \
1.1       root       15:        exit(1); \
                     16: }
                     17: #define error(...) fprintf(stderr, "error: " __VA_ARGS__)
1.1.1.22  root       18: #define nolog(...)
                     19: 
1.1.1.33  root       20: //#define ENABLE_DEBUG_LOG
                     21: #ifdef ENABLE_DEBUG_LOG
1.1.1.22  root       22:        #define EXPORT_DEBUG_TO_FILE
                     23:        #define ENABLE_DEBUG_SYSCALL
                     24:        #define ENABLE_DEBUG_UNIMPLEMENTED
1.1.1.25  root       25:        #define ENABLE_DEBUG_IOPORT
1.1.1.22  root       26:        
                     27:        #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root       28:                FILE* fp_debug_log = NULL;
1.1.1.22  root       29:        #else
1.1.1.33  root       30:                #define fp_debug_log stderr
1.1.1.22  root       31:        #endif
                     32:        #ifdef ENABLE_DEBUG_UNIMPLEMENTED
                     33:                #define unimplemented_10h fatalerror
1.1.1.42  root       34:                #define unimplemented_13h fatalerror
1.1.1.25  root       35:                #define unimplemented_14h fatalerror
1.1.1.22  root       36:                #define unimplemented_15h fatalerror
                     37:                #define unimplemented_16h fatalerror
1.1.1.37  root       38:                #define unimplemented_17h fatalerror
1.1.1.22  root       39:                #define unimplemented_1ah fatalerror
                     40:                #define unimplemented_21h fatalerror
                     41:                #define unimplemented_2fh fatalerror
1.1.1.24  root       42:                #define unimplemented_33h fatalerror
1.1.1.22  root       43:                #define unimplemented_67h fatalerror
                     44:                #define unimplemented_xms fatalerror
                     45:        #endif
                     46: #endif
                     47: #ifndef unimplemented_10h
                     48:        #define unimplemented_10h nolog
                     49: #endif
1.1.1.42  root       50: #ifndef unimplemented_13h
                     51:        #define unimplemented_13h nolog
                     52: #endif
1.1.1.25  root       53: #ifndef unimplemented_14h
                     54:        #define unimplemented_14h nolog
                     55: #endif
1.1.1.22  root       56: #ifndef unimplemented_15h
                     57:        #define unimplemented_15h nolog
                     58: #endif
                     59: #ifndef unimplemented_16h
                     60:        #define unimplemented_16h nolog
                     61: #endif
1.1.1.37  root       62: #ifndef unimplemented_17h
                     63:        #define unimplemented_17h nolog
                     64: #endif
1.1.1.22  root       65: #ifndef unimplemented_1ah
                     66:        #define unimplemented_1ah nolog
                     67: #endif
                     68: #ifndef unimplemented_21h
                     69:        #define unimplemented_21h nolog
                     70: #endif
                     71: #ifndef unimplemented_2fh
                     72:        #define unimplemented_2fh nolog
                     73: #endif
1.1.1.24  root       74: #ifndef unimplemented_33h
                     75:        #define unimplemented_33h nolog
                     76: #endif
1.1.1.22  root       77: #ifndef unimplemented_67h
                     78:        #define unimplemented_67h nolog
                     79: #endif
                     80: #ifndef unimplemented_xms
                     81:        #define unimplemented_xms nolog
                     82: #endif
                     83: 
1.1.1.32  root       84: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1.1.22  root       85: #define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
                     86: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
                     87: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
1.1       root       88: 
1.1.1.12  root       89: #if defined(__MINGW32__)
                     90: extern "C" int _CRT_glob = 0;
                     91: #endif
                     92: 
                     93: /*
                     94:        kludge for "more-standardized" C++
                     95: */
                     96: #if !defined(_MSC_VER)
                     97: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
                     98: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
                     99: #define min(a,b) kludge_min(a,b)
                    100: #define max(a,b) kludge_max(a,b)
1.1.1.14  root      101: #elif _MSC_VER >= 1400
                    102: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
                    103: {
                    104: }
                    105: #endif
                    106: 
1.1.1.35  root      107: #define USE_VRAM_THREAD
1.1.1.14  root      108: 
1.1.1.35  root      109: #ifdef USE_VRAM_THREAD
1.1.1.14  root      110: static CRITICAL_SECTION vram_crit_sect;
                    111: #else
                    112: #define vram_flush()
1.1.1.12  root      113: #endif
                    114: 
1.1.1.14  root      115: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
                    116: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
                    117: 
                    118: void change_console_size(int width, int height);
                    119: void clear_scr_buffer(WORD attr);
                    120: 
                    121: static UINT32 vram_length_char = 0, vram_length_attr = 0;
                    122: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
                    123: static COORD vram_coord_char, vram_coord_attr;
                    124: 
1.1.1.28  root      125: char temp_file_path[MAX_PATH];
                    126: bool temp_file_created = false;
                    127: 
1.1.1.14  root      128: bool ignore_illegal_insn = false;
                    129: bool limit_max_memory = false;
                    130: bool no_windows = false;
                    131: bool stay_busy = false;
1.1.1.19  root      132: bool support_ems = false;
                    133: #ifdef SUPPORT_XMS
                    134: bool support_xms = false;
                    135: #endif
1.1.1.29  root      136: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14  root      137: 
1.1.1.54  root      138: BOOL is_xp_64_or_later;
1.1.1.14  root      139: BOOL is_vista_or_later;
                    140: 
1.1.1.35  root      141: #define UPDATE_OPS 16384
                    142: #define REQUEST_HARDWRE_UPDATE() { \
                    143:        update_ops = UPDATE_OPS - 1; \
                    144: }
                    145: UINT32 update_ops = 0;
                    146: UINT32 idle_ops = 0;
                    147: 
1.1.1.54  root      148: inline BOOL is_sse2_ready()
                    149: {
                    150:        static int result = -1;
                    151:        int cpu_info[4];
                    152:        
                    153:        if(result == -1) {
                    154:                result = 0;
                    155:                __cpuid(cpu_info, 0);
                    156:                if(cpu_info[0] >= 1){
                    157:                        __cpuid(cpu_info, 1);
                    158:                        if(cpu_info[3] & (1 << 26)) {
                    159:                                result = 1;
                    160:                        }
                    161:                }
                    162:        }
                    163:        return(result == 1);
                    164: }
                    165: 
1.1.1.14  root      166: inline void maybe_idle()
                    167: {
                    168:        // if it appears to be in a tight loop, assume waiting for input
                    169:        // allow for one updated video character, for a spinning cursor
1.1.1.35  root      170:        if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.54  root      171:                if(is_sse2_ready()) {
                    172:                        _mm_pause();    // SSE2 pause
                    173:                } else if(is_xp_64_or_later) {
                    174:                        Sleep(0);       // switch to other thread that is ready to run, without checking priority
                    175:                } else {
                    176:                        Sleep(1);
                    177:                        REQUEST_HARDWRE_UPDATE();
                    178:                }
1.1.1.14  root      179:        }
1.1.1.35  root      180:        idle_ops = 0;
1.1.1.14  root      181: }
1.1.1.12  root      182: 
1.1       root      183: /* ----------------------------------------------------------------------------
1.1.1.3   root      184:        MAME i86/i386
1.1       root      185: ---------------------------------------------------------------------------- */
                    186: 
1.1.1.10  root      187: #ifndef __BIG_ENDIAN__
1.1       root      188: #define LSB_FIRST
1.1.1.10  root      189: #endif
1.1       root      190: 
                    191: #ifndef INLINE
                    192: #define INLINE inline
                    193: #endif
                    194: #define U64(v) UINT64(v)
                    195: 
                    196: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    197: #define logerror(...)
                    198: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    199: #define popmessage(...)
                    200: 
                    201: /*****************************************************************************/
1.1.1.10  root      202: /* src/emu/devcpu.h */
                    203: 
                    204: // CPU interface functions
                    205: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    206: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    207: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    208: 
                    209: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    210: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    211: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    212: 
                    213: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    214: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    215: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    216: 
                    217: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    218: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    219: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    220: 
                    221: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    222: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    223: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    224: 
1.1.1.14  root      225: #define CPU_MODEL_STR(name)                    #name
                    226: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    227: 
1.1.1.10  root      228: /*****************************************************************************/
                    229: /* src/emu/didisasm.h */
                    230: 
                    231: // Disassembler constants
                    232: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    233: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    234: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    235: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    236: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    237: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    238: 
                    239: /*****************************************************************************/
1.1       root      240: /* src/emu/diexec.h */
                    241: 
                    242: // I/O line states
                    243: enum line_state
                    244: {
                    245:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    246:        ASSERT_LINE,                            // assert an interrupt immediately
                    247:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    248:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    249: };
                    250: 
                    251: // I/O line definitions
                    252: enum
                    253: {
                    254:        INPUT_LINE_IRQ = 0,
                    255:        INPUT_LINE_NMI
                    256: };
                    257: 
                    258: /*****************************************************************************/
1.1.1.10  root      259: /* src/emu/dimemory.h */
1.1       root      260: 
1.1.1.10  root      261: // Translation intentions
                    262: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    263: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    264: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    265: 
                    266: const int TRANSLATE_READ            = 0;        // translate for read
                    267: const int TRANSLATE_WRITE           = 1;        // translate for write
                    268: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    269: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    270: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    271: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    272: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    273: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    274: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      275: 
1.1.1.10  root      276: /*****************************************************************************/
                    277: /* src/emu/emucore.h */
1.1       root      278: 
1.1.1.10  root      279: // constants for expression endianness
                    280: enum endianness_t
                    281: {
                    282:        ENDIANNESS_LITTLE,
                    283:        ENDIANNESS_BIG
                    284: };
1.1       root      285: 
1.1.1.10  root      286: // declare native endianness to be one or the other
                    287: #ifdef LSB_FIRST
                    288: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    289: #else
                    290: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    291: #endif
                    292: 
                    293: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    294: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    295: 
                    296: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    297: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    298: 
                    299: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    300: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      301: 
                    302: /*****************************************************************************/
                    303: /* src/emu/memory.h */
                    304: 
1.1.1.10  root      305: // address spaces
                    306: enum address_spacenum
                    307: {
                    308:        AS_0,                           // first address space
                    309:        AS_1,                           // second address space
                    310:        AS_2,                           // third address space
                    311:        AS_3,                           // fourth address space
                    312:        ADDRESS_SPACES,                 // maximum number of address spaces
                    313: 
                    314:        // alternate address space names for common use
                    315:        AS_PROGRAM = AS_0,              // program address space
                    316:        AS_DATA = AS_1,                 // data address space
                    317:        AS_IO = AS_2                    // I/O address space
                    318: };
                    319: 
1.1       root      320: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      321: //typedef UINT32       offs_t;
1.1       root      322: 
                    323: // read accessors
                    324: UINT8 read_byte(offs_t byteaddress)
1.1.1.33  root      325: #ifdef USE_DEBUGGER
                    326: {
                    327:        if(now_debugging) {
                    328:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    329:                        if(rd_break_point.table[i].status == 1) {
                    330:                                if(byteaddress == rd_break_point.table[i].addr) {
                    331:                                        rd_break_point.hit = i + 1;
                    332:                                        now_suspended = true;
                    333:                                        break;
                    334:                                }
                    335:                        }
                    336:                }
                    337:        }
                    338:        return(debugger_read_byte(byteaddress));
                    339: }
                    340: UINT8 debugger_read_byte(offs_t byteaddress)
                    341: #endif
1.1       root      342: {
1.1.1.4   root      343: #if defined(HAS_I386)
1.1       root      344:        if(byteaddress < MAX_MEM) {
                    345:                return mem[byteaddress];
1.1.1.3   root      346: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    347: //             return read_byte(byteaddress & 0xfffff);
1.1       root      348:        }
                    349:        return 0;
1.1.1.4   root      350: #else
                    351:        return mem[byteaddress];
                    352: #endif
1.1       root      353: }
                    354: 
                    355: UINT16 read_word(offs_t byteaddress)
1.1.1.33  root      356: #ifdef USE_DEBUGGER
                    357: {
                    358:        if(now_debugging) {
                    359:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    360:                        if(rd_break_point.table[i].status == 1) {
                    361:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
                    362:                                        rd_break_point.hit = i + 1;
                    363:                                        now_suspended = true;
                    364:                                        break;
                    365:                                }
                    366:                        }
                    367:                }
                    368:        }
                    369:        return(debugger_read_word(byteaddress));
                    370: }
                    371: UINT16 debugger_read_word(offs_t byteaddress)
                    372: #endif
1.1       root      373: {
1.1.1.14  root      374:        if(byteaddress == 0x41c) {
                    375:                // pointer to first free slot in keyboard buffer
1.1.1.35  root      376:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                    377: #ifdef USE_SERVICE_THREAD
                    378:                        EnterCriticalSection(&key_buf_crit_sect);
                    379: #endif
1.1.1.55! root      380:                        bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root      381: #ifdef USE_SERVICE_THREAD
                    382:                        LeaveCriticalSection(&key_buf_crit_sect);
                    383: #endif
1.1.1.51  root      384:                        if(empty) maybe_idle();
1.1.1.14  root      385:                }
                    386:        }
1.1.1.4   root      387: #if defined(HAS_I386)
1.1       root      388:        if(byteaddress < MAX_MEM - 1) {
                    389:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      390: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    391: //             return read_word(byteaddress & 0xfffff);
1.1       root      392:        }
                    393:        return 0;
1.1.1.4   root      394: #else
                    395:        return *(UINT16 *)(mem + byteaddress);
                    396: #endif
1.1       root      397: }
                    398: 
                    399: UINT32 read_dword(offs_t byteaddress)
1.1.1.33  root      400: #ifdef USE_DEBUGGER
                    401: {
                    402:        if(now_debugging) {
                    403:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    404:                        if(rd_break_point.table[i].status == 1) {
                    405:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
                    406:                                        rd_break_point.hit = i + 1;
                    407:                                        now_suspended = true;
                    408:                                        break;
                    409:                                }
                    410:                        }
                    411:                }
                    412:        }
                    413:        return(debugger_read_dword(byteaddress));
                    414: }
                    415: UINT32 debugger_read_dword(offs_t byteaddress)
                    416: #endif
1.1       root      417: {
1.1.1.4   root      418: #if defined(HAS_I386)
1.1       root      419:        if(byteaddress < MAX_MEM - 3) {
                    420:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      421: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    422: //             return read_dword(byteaddress & 0xfffff);
1.1       root      423:        }
                    424:        return 0;
1.1.1.4   root      425: #else
                    426:        return *(UINT32 *)(mem + byteaddress);
                    427: #endif
1.1       root      428: }
                    429: 
                    430: // write accessors
1.1.1.35  root      431: #ifdef USE_VRAM_THREAD
1.1.1.14  root      432: void vram_flush_char()
                    433: {
                    434:        if(vram_length_char != 0) {
                    435:                DWORD num;
1.1.1.23  root      436:                WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      437:                vram_length_char = vram_last_length_char = 0;
                    438:        }
                    439: }
                    440: 
                    441: void vram_flush_attr()
                    442: {
                    443:        if(vram_length_attr != 0) {
                    444:                DWORD num;
1.1.1.23  root      445:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      446:                vram_length_attr = vram_last_length_attr = 0;
                    447:        }
                    448: }
                    449: 
                    450: void vram_flush()
                    451: {
                    452:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    453:                EnterCriticalSection(&vram_crit_sect);
                    454:                vram_flush_char();
                    455:                vram_flush_attr();
                    456:                LeaveCriticalSection(&vram_crit_sect);
                    457:        }
                    458: }
                    459: #endif
                    460: 
                    461: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      462: {
1.1.1.35  root      463: #ifdef USE_VRAM_THREAD
1.1.1.14  root      464:        static offs_t first_offset_char, last_offset_char;
                    465:        
                    466:        if(vram_length_char != 0) {
                    467:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    468:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    469:                        return;
                    470:                }
                    471:                if(offset != last_offset_char + 2) {
                    472:                        vram_flush_char();
                    473:                }
                    474:        }
                    475:        if(vram_length_char == 0) {
                    476:                first_offset_char = offset;
                    477:                vram_coord_char.X = (offset >> 1) % scr_width;
                    478:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    479:        }
                    480:        scr_char[vram_length_char++] = data;
                    481:        last_offset_char = offset;
                    482: #else
1.1.1.8   root      483:        COORD co;
                    484:        DWORD num;
                    485:        
1.1.1.14  root      486:        co.X = (offset >> 1) % scr_width;
                    487:        co.Y = (offset >> 1) / scr_width;
                    488:        scr_char[0] = data;
1.1.1.23  root      489:        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      490: #endif
                    491: }
                    492: 
                    493: void write_text_vram_attr(offs_t offset, UINT8 data)
                    494: {
1.1.1.35  root      495: #ifdef USE_VRAM_THREAD
1.1.1.14  root      496:        static offs_t first_offset_attr, last_offset_attr;
                    497:        
                    498:        if(vram_length_attr != 0) {
                    499:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    500:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    501:                        return;
                    502:                }
                    503:                if(offset != last_offset_attr + 2) {
                    504:                        vram_flush_attr();
                    505:                }
                    506:        }
                    507:        if(vram_length_attr == 0) {
                    508:                first_offset_attr = offset;
                    509:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    510:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    511:        }
                    512:        scr_attr[vram_length_attr++] = data;
                    513:        last_offset_attr = offset;
                    514: #else
                    515:        COORD co;
                    516:        DWORD num;
1.1.1.8   root      517:        
1.1.1.14  root      518:        co.X = (offset >> 1) % scr_width;
                    519:        co.Y = (offset >> 1) / scr_width;
                    520:        scr_attr[0] = data;
1.1.1.23  root      521:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      522: #endif
                    523: }
                    524: 
                    525: void write_text_vram_byte(offs_t offset, UINT8 data)
                    526: {
1.1.1.35  root      527: #ifdef USE_VRAM_THREAD
1.1.1.14  root      528:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      529: #endif
1.1.1.8   root      530:        if(offset & 1) {
1.1.1.14  root      531:                write_text_vram_attr(offset, data);
1.1.1.8   root      532:        } else {
1.1.1.14  root      533:                write_text_vram_char(offset, data);
1.1.1.8   root      534:        }
1.1.1.35  root      535: #ifdef USE_VRAM_THREAD
1.1.1.14  root      536:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      537: #endif
1.1.1.8   root      538: }
                    539: 
                    540: void write_text_vram_word(offs_t offset, UINT16 data)
                    541: {
1.1.1.35  root      542: #ifdef USE_VRAM_THREAD
1.1.1.14  root      543:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      544: #endif
1.1.1.8   root      545:        if(offset & 1) {
1.1.1.14  root      546:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    547:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      548:        } else {
1.1.1.14  root      549:                write_text_vram_char(offset    , (data     ) & 0xff);
                    550:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      551:        }
1.1.1.35  root      552: #ifdef USE_VRAM_THREAD
1.1.1.14  root      553:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      554: #endif
1.1.1.8   root      555: }
                    556: 
                    557: void write_text_vram_dword(offs_t offset, UINT32 data)
                    558: {
1.1.1.35  root      559: #ifdef USE_VRAM_THREAD
1.1.1.14  root      560:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      561: #endif
1.1.1.8   root      562:        if(offset & 1) {
1.1.1.14  root      563:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    564:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    565:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    566:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    567:        } else {
                    568:                write_text_vram_char(offset    , (data      ) & 0xff);
                    569:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    570:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    571:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      572:        }
1.1.1.35  root      573: #ifdef USE_VRAM_THREAD
1.1.1.14  root      574:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      575: #endif
1.1.1.8   root      576: }
                    577: 
1.1       root      578: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      579: #ifdef USE_DEBUGGER
                    580: {
                    581:        if(now_debugging) {
                    582:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    583:                        if(wr_break_point.table[i].status == 1) {
                    584:                                if(byteaddress == wr_break_point.table[i].addr) {
                    585:                                        wr_break_point.hit = i + 1;
                    586:                                        now_suspended = true;
                    587:                                        break;
                    588:                                }
                    589:                        }
                    590:                }
                    591:        }
                    592:        debugger_write_byte(byteaddress, data);
                    593: }
                    594: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    595: #endif
1.1       root      596: {
1.1.1.8   root      597:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      598:                mem[byteaddress] = data;
1.1.1.8   root      599:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      600:                if(!restore_console_on_exit) {
                    601:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      602:                }
1.1.1.8   root      603:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    604:                mem[byteaddress] = data;
                    605:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    606:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    607:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      608:                }
                    609:                mem[byteaddress] = data;
1.1.1.4   root      610: #if defined(HAS_I386)
1.1.1.3   root      611:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      612: #else
                    613:        } else {
                    614: #endif
1.1.1.3   root      615:                mem[byteaddress] = data;
1.1       root      616:        }
                    617: }
                    618: 
                    619: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      620: #ifdef USE_DEBUGGER
                    621: {
                    622:        if(now_debugging) {
                    623:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    624:                        if(wr_break_point.table[i].status == 1) {
                    625:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    626:                                        wr_break_point.hit = i + 1;
                    627:                                        now_suspended = true;
                    628:                                        break;
                    629:                                }
                    630:                        }
                    631:                }
                    632:        }
                    633:        debugger_write_word(byteaddress, data);
                    634: }
                    635: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    636: #endif
1.1       root      637: {
1.1.1.8   root      638:        if(byteaddress < MEMORY_END) {
1.1.1.51  root      639:                if(byteaddress == cursor_position_address) {
                    640:                        if(*(UINT16 *)(mem + byteaddress) != data) {
                    641:                                COORD co;
                    642:                                co.X = data & 0xff;
                    643:                                co.Y = (data >> 8) + scr_top;
                    644:                                SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
                    645:                        }
1.1.1.14  root      646:                }
1.1.1.3   root      647:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      648:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      649:                if(!restore_console_on_exit) {
                    650:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      651:                }
1.1.1.8   root      652:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    653:                *(UINT16 *)(mem + byteaddress) = data;
                    654:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    655:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    656:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      657:                }
                    658:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      659: #if defined(HAS_I386)
1.1.1.3   root      660:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      661: #else
                    662:        } else {
                    663: #endif
1.1.1.3   root      664:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      665:        }
                    666: }
                    667: 
                    668: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      669: #ifdef USE_DEBUGGER
                    670: {
                    671:        if(now_debugging) {
                    672:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    673:                        if(wr_break_point.table[i].status == 1) {
                    674:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    675:                                        wr_break_point.hit = i + 1;
                    676:                                        now_suspended = true;
                    677:                                        break;
                    678:                                }
                    679:                        }
                    680:                }
                    681:        }
                    682:        debugger_write_dword(byteaddress, data);
                    683: }
                    684: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    685: #endif
1.1       root      686: {
1.1.1.8   root      687:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      688:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      689:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      690:                if(!restore_console_on_exit) {
                    691:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      692:                }
1.1.1.8   root      693:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    694:                *(UINT32 *)(mem + byteaddress) = data;
                    695:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    696:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    697:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      698:                }
                    699:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      700: #if defined(HAS_I386)
1.1.1.3   root      701:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      702: #else
                    703:        } else {
                    704: #endif
1.1.1.3   root      705:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      706:        }
                    707: }
                    708: 
                    709: #define read_decrypted_byte read_byte
                    710: #define read_decrypted_word read_word
                    711: #define read_decrypted_dword read_dword
                    712: 
1.1.1.3   root      713: #define read_raw_byte read_byte
                    714: #define write_raw_byte write_byte
                    715: 
                    716: #define read_word_unaligned read_word
                    717: #define write_word_unaligned write_word
                    718: 
                    719: #define read_io_word_unaligned read_io_word
                    720: #define write_io_word_unaligned write_io_word
                    721: 
1.1       root      722: UINT8 read_io_byte(offs_t byteaddress);
                    723: UINT16 read_io_word(offs_t byteaddress);
                    724: UINT32 read_io_dword(offs_t byteaddress);
                    725: 
                    726: void write_io_byte(offs_t byteaddress, UINT8 data);
                    727: void write_io_word(offs_t byteaddress, UINT16 data);
                    728: void write_io_dword(offs_t byteaddress, UINT32 data);
                    729: 
                    730: /*****************************************************************************/
                    731: /* src/osd/osdcomm.h */
                    732: 
                    733: /* Highly useful macro for compile-time knowledge of an array size */
                    734: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    735: 
1.1.1.54  root      736: // flag to exit MS-DOS Player
                    737: // this is set when the first process is terminated and jump to FFFF:0000 HALT
                    738: int m_exit = 0;
                    739: 
1.1.1.3   root      740: #if defined(HAS_I386)
1.1.1.10  root      741:        static CPU_TRANSLATE(i386);
                    742:        #include "mame/lib/softfloat/softfloat.c"
                    743:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      744:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      745: #elif defined(HAS_I286)
1.1.1.10  root      746:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      747: #else
1.1.1.10  root      748:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      749: #endif
1.1.1.33  root      750: #ifdef USE_DEBUGGER
1.1.1.10  root      751:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      752: #endif
                    753: 
1.1.1.3   root      754: #if defined(HAS_I386)
                    755:        #define SREG(x)                         m_sreg[x].selector
                    756:        #define SREG_BASE(x)                    m_sreg[x].base
                    757:        int cpu_type, cpu_step;
                    758: #else
                    759:        #define REG8(x)                         m_regs.b[x]
                    760:        #define REG16(x)                        m_regs.w[x]
                    761:        #define SREG(x)                         m_sregs[x]
                    762:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      763:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      764:        #define m_CF                            m_CarryVal
                    765:        #define m_a20_mask                      AMASK
                    766:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    767:        #if defined(HAS_I286)
                    768:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    769:        #else
                    770:                #define i386_set_a20_line(x)
                    771:        #endif
                    772:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    773: #endif
1.1       root      774: 
                    775: void i386_jmp_far(UINT16 selector, UINT32 address)
                    776: {
1.1.1.3   root      777: #if defined(HAS_I386)
1.1       root      778:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      779:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      780:        } else {
1.1.1.3   root      781:                SREG(CS) = selector;
                    782:                m_performed_intersegment_jump = 1;
                    783:                i386_load_segment_descriptor(CS);
                    784:                m_eip = address;
                    785:                CHANGE_PC(m_eip);
1.1       root      786:        }
1.1.1.3   root      787: #elif defined(HAS_I286)
                    788:        i80286_code_descriptor(selector, address, 1);
                    789: #else
                    790:        SREG(CS) = selector;
                    791:        i386_load_segment_descriptor(CS);
                    792:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    793: #endif
1.1       root      794: }
                    795: 
1.1.1.24  root      796: void i386_call_far(UINT16 selector, UINT32 address)
                    797: {
                    798: #if defined(HAS_I386)
                    799:        if(PROTECTED_MODE && !V8086_MODE) {
                    800:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    801:        } else {
                    802:                PUSH16(SREG(CS));
                    803:                PUSH16(m_eip);
                    804:                SREG(CS) = selector;
                    805:                m_performed_intersegment_jump = 1;
                    806:                i386_load_segment_descriptor(CS);
                    807:                m_eip = address;
                    808:                CHANGE_PC(m_eip);
                    809:        }
                    810: #else
                    811:        UINT16 ip = m_pc - SREG_BASE(CS);
                    812:        UINT16 cs = SREG(CS);
                    813: #if defined(HAS_I286)
                    814:        i80286_code_descriptor(selector, address, 2);
                    815: #else
                    816:        SREG(CS) = selector;
                    817:        i386_load_segment_descriptor(CS);
                    818:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    819: #endif
                    820:        PUSH(cs);
                    821:        PUSH(ip);
                    822:        CHANGE_PC(m_pc);
                    823: #endif
                    824: }
1.1.1.49  root      825: 
                    826: void i386_push16(UINT16 value)
                    827: {
                    828: #if defined(HAS_I386)
                    829:        PUSH16(value);
                    830: #else
                    831:        PUSH(value);
1.1.1.35  root      832: #endif
1.1.1.49  root      833: }
                    834: 
                    835: UINT16 i386_pop16()
                    836: {
                    837: #if defined(HAS_I386)
                    838:        return POP16();
                    839: #else
                    840:        UINT16 value;
                    841:        POP(value);
                    842:        return value;
                    843: #endif
                    844: }
1.1.1.24  root      845: 
1.1.1.29  root      846: UINT16 i386_read_stack()
                    847: {
                    848: #if defined(HAS_I386)
                    849:        UINT32 ea, new_esp;
                    850:        if( STACK_32BIT ) {
                    851:                new_esp = REG32(ESP) + 2;
                    852:                ea = i386_translate(SS, new_esp - 2, 0);
                    853:        } else {
                    854:                new_esp = REG16(SP) + 2;
                    855:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    856:        }
                    857:        return READ16(ea);
                    858: #else
                    859:        UINT16 sp = m_regs.w[SP] + 2;
                    860:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    861: #endif
                    862: }
                    863: 
1.1.1.53  root      864: void i386_write_stack(UINT16 value)
                    865: {
                    866: #if defined(HAS_I386)
                    867:        UINT32 ea, new_esp;
                    868:        if( STACK_32BIT ) {
                    869:                new_esp = REG32(ESP) + 2;
                    870:                ea = i386_translate(SS, new_esp - 2, 0);
                    871:        } else {
                    872:                new_esp = REG16(SP) + 2;
                    873:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    874:        }
                    875:        WRITE16(ea, value);
                    876: #else
                    877:        UINT16 sp = m_regs.w[SP] + 2;
                    878:        WriteWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK), value);
                    879: #endif
                    880: }
                    881: 
1.1       root      882: /* ----------------------------------------------------------------------------
1.1.1.33  root      883:        debugger
                    884: ---------------------------------------------------------------------------- */
                    885: 
                    886: #ifdef USE_DEBUGGER
                    887: #define TELNET_BLUE      0x0004 // text color contains blue.
                    888: #define TELNET_GREEN     0x0002 // text color contains green.
                    889: #define TELNET_RED       0x0001 // text color contains red.
                    890: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    891: 
                    892: int svr_socket = 0;
                    893: int cli_socket = 0;
                    894: 
1.1.1.55! root      895: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
1.1.1.33  root      896: 
                    897: void debugger_init()
                    898: {
                    899:        now_debugging = false;
                    900:        now_going = false;
                    901:        now_suspended = false;
                    902:        force_suspend = false;
                    903:        
                    904:        memset(&break_point, 0, sizeof(break_point_t));
                    905:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    906:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    907:        memset(&in_break_point, 0, sizeof(break_point_t));
                    908:        memset(&out_break_point, 0, sizeof(break_point_t));
                    909:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    910: }
                    911: 
1.1.1.45  root      912: void telnet_send(const char *string)
1.1.1.33  root      913: {
                    914:        char buffer[8192], *ptr;
                    915:        strcpy(buffer, string);
                    916:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    917:                char tmp[8192];
                    918:                *ptr = '\0';
                    919:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    920:                strcpy(buffer, tmp);
                    921:        }
                    922:        
                    923:        int len = strlen(buffer), res;
                    924:        ptr = buffer;
                    925:        while(len > 0) {
                    926:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    927:                        len -= res;
                    928:                        ptr += res;
                    929:                }
                    930:        }
                    931: }
                    932: 
                    933: void telnet_command(const char *format, ...)
                    934: {
                    935:        char buffer[1024];
                    936:        va_list ap;
                    937:        va_start(ap, format);
                    938:        vsprintf(buffer, format, ap);
                    939:        va_end(ap);
                    940:        
                    941:        telnet_send(buffer);
                    942: }
                    943: 
                    944: void telnet_printf(const char *format, ...)
                    945: {
                    946:        char buffer[1024];
                    947:        va_list ap;
                    948:        va_start(ap, format);
                    949:        vsprintf(buffer, format, ap);
                    950:        va_end(ap);
                    951:        
                    952:        if(fp_debugger != NULL) {
                    953:                fprintf(fp_debugger, "%s", buffer);
                    954:        }
                    955:        telnet_send(buffer);
                    956: }
                    957: 
                    958: bool telnet_gets(char *str, int n)
                    959: {
                    960:        char buffer[1024];
                    961:        int ptr = 0;
                    962:        
                    963:        telnet_command("\033[12l"); // local echo on
                    964:        telnet_command("\033[2l");  // key unlock
                    965:        
1.1.1.54  root      966:        while(!m_exit) {
1.1.1.33  root      967:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    968:                
                    969:                if(len > 0 && buffer[0] != 0xff) {
                    970:                        for(int i = 0; i < len; i++) {
                    971:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    972:                                        str[ptr] = 0;
                    973:                                        telnet_command("\033[2h");  // key lock
                    974:                                        telnet_command("\033[12h"); // local echo off
1.1.1.54  root      975:                                        return(!m_exit);
1.1.1.33  root      976:                                } else if(buffer[i] == 0x08) {
                    977:                                        if(ptr > 0) {
                    978:                                                telnet_command("\033[0K"); // erase from cursor position
                    979:                                                ptr--;
                    980:                                        } else {
                    981:                                                telnet_command("\033[1C"); // move cursor forward
                    982:                                        }
                    983:                                } else if(ptr < n - 1) {
1.1.1.37  root      984:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root      985:                                                str[ptr++] = buffer[i];
                    986:                                        }
                    987:                                } else {
                    988:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                    989:                                }
                    990:                        }
                    991:                } else if(len == -1) {
                    992:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    993:                                return(false);
                    994:                        }
                    995:                } else if(len == 0) {
                    996:                        return(false);
                    997:                }
                    998:                Sleep(10);
                    999:        }
1.1.1.54  root     1000:        return(!m_exit);
1.1.1.33  root     1001: }
                   1002: 
                   1003: bool telnet_kbhit()
                   1004: {
                   1005:        char buffer[1024];
                   1006:        
1.1.1.54  root     1007:        if(!m_exit) {
1.1.1.33  root     1008:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1009:                
                   1010:                if(len > 0) {
                   1011:                        for(int i = 0; i < len; i++) {
                   1012:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1013:                                        return(true);
                   1014:                                }
                   1015:                        }
                   1016:                } else if(len == 0) {
                   1017:                        return(true); // disconnected
                   1018:                }
                   1019:        }
                   1020:        return(false);
                   1021: }
                   1022: 
                   1023: bool telnet_disconnected()
                   1024: {
                   1025:        char buffer[1024];
                   1026:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1027:        
                   1028:        if(len == 0) {
                   1029:                return(true);
                   1030:        } else if(len == -1) {
                   1031:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1032:                        return(true);
                   1033:                }
                   1034:        }
                   1035:        return(false);
                   1036: }
                   1037: 
                   1038: void telnet_set_color(int color)
                   1039: {
                   1040:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                   1041: }
                   1042: 
                   1043: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                   1044: {
                   1045: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                   1046:        UINT8 ops[16];
                   1047:        for(int i = 0; i < 16; i++) {
                   1048:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                   1049:        }
                   1050:        UINT8 *oprom = ops;
                   1051:        
                   1052: #if defined(HAS_I386)
                   1053:        if(m_operand_size) {
                   1054:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                   1055:        } else
                   1056: #endif
                   1057:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1058: }
                   1059: 
                   1060: void debugger_regs_info(char *buffer)
                   1061: {
                   1062: #if defined(HAS_I386)
                   1063:        UINT32 flags = get_flags();
                   1064: #else
                   1065:        UINT32 flags = CompressFlags();
                   1066: #endif
                   1067: #if defined(HAS_I386)
                   1068:        if(m_operand_size) {
                   1069:                sprintf(buffer, "EAX=%08X  EBX=%08X  ECX=%08X  EDX=%08X\nESP=%08X  EBP=%08X  ESI=%08X  EDI=%08X\nEIP=%08X  DS=%04X  ES=%04X  SS=%04X  CS=%04X  FLAG=[%c %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n",
                   1070:                REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SP), REG16(BP), REG16(SI), REG16(DI), m_eip, SREG(DS), SREG(ES), SREG(SS), SREG(CS),
                   1071:                PROTECTED_MODE ? "PE" : "--",
                   1072:                (flags & 0x40000) ? 'A' : '-',
                   1073:                (flags & 0x20000) ? 'V' : '-',
                   1074:                (flags & 0x10000) ? 'R' : '-',
                   1075:                (flags & 0x04000) ? 'N' : '-',
                   1076:                (flags & 0x02000) ? '1' : '0',
                   1077:                (flags & 0x01000) ? '1' : '0',
                   1078:                (flags & 0x00800) ? 'O' : '-',
                   1079:                (flags & 0x00400) ? 'D' : '-',
                   1080:                (flags & 0x00200) ? 'I' : '-',
                   1081:                (flags & 0x00100) ? 'T' : '-',
                   1082:                (flags & 0x00080) ? 'S' : '-',
                   1083:                (flags & 0x00040) ? 'Z' : '-',
                   1084:                (flags & 0x00010) ? 'A' : '-',
                   1085:                (flags & 0x00004) ? 'P' : '-',
                   1086:                (flags & 0x00001) ? 'C' : '-');
                   1087:        } else {
                   1088: #endif
                   1089:                sprintf(buffer, "AX=%04X  BX=%04X  CX=%04X  DX=%04X  SP=%04X  BP=%04X  SI=%04X  DI=%04X\nIP=%04X  DS=%04X  ES=%04X  SS=%04X  CS=%04X  FLAG=[%c %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n",
                   1090:                REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SP), REG16(BP), REG16(SI), REG16(DI), m_eip, SREG(DS), SREG(ES), SREG(SS), SREG(CS),
                   1091: #if defined(HAS_I386)
                   1092:                PROTECTED_MODE ? "PE" : "--",
                   1093: #else
                   1094:                "--",
                   1095: #endif
                   1096:                (flags & 0x40000) ? 'A' : '-',
                   1097:                (flags & 0x20000) ? 'V' : '-',
                   1098:                (flags & 0x10000) ? 'R' : '-',
                   1099:                (flags & 0x04000) ? 'N' : '-',
                   1100:                (flags & 0x02000) ? '1' : '0',
                   1101:                (flags & 0x01000) ? '1' : '0',
                   1102:                (flags & 0x00800) ? 'O' : '-',
                   1103:                (flags & 0x00400) ? 'D' : '-',
                   1104:                (flags & 0x00200) ? 'I' : '-',
                   1105:                (flags & 0x00100) ? 'T' : '-',
                   1106:                (flags & 0x00080) ? 'S' : '-',
                   1107:                (flags & 0x00040) ? 'Z' : '-',
                   1108:                (flags & 0x00010) ? 'A' : '-',
                   1109:                (flags & 0x00004) ? 'P' : '-',
                   1110:                (flags & 0x00001) ? 'C' : '-');
                   1111: #if defined(HAS_I386)
                   1112:        }
                   1113: #endif
                   1114: }
                   1115: 
                   1116: void debugger_process_info(char *buffer)
                   1117: {
                   1118:        UINT16 psp_seg = current_psp;
                   1119:        process_t *process;
                   1120:        bool check[0x10000] = {0};
                   1121:        
                   1122:        buffer[0] = '\0';
                   1123:        
                   1124:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1125:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1126:                char *file = process->module_path, *s;
                   1127:                char tmp[8192];
                   1128:                
                   1129:                while((s = strstr(file, "\\")) != NULL) {
                   1130:                        file = s + 1;
                   1131:                }
                   1132:                sprintf(tmp, "PSP=%04X  ENV=%04X  RETURN=%04X:%04X  PROGRAM=%s\n", psp_seg, psp->env_seg, psp->int_22h.w.h, psp->int_22h.w.l, my_strupr(file));
                   1133:                strcat(tmp, buffer);
                   1134:                strcpy(buffer, tmp);
                   1135:                
                   1136:                check[psp_seg] = true;
                   1137:                psp_seg = psp->parent_psp;
                   1138:        }
                   1139: }
                   1140: 
                   1141: UINT32 debugger_get_val(const char *str)
                   1142: {
                   1143:        char tmp[1024];
                   1144:        
                   1145:        if(str == NULL || strlen(str) == 0) {
                   1146:                return(0);
                   1147:        }
                   1148:        strcpy(tmp, str);
                   1149:        
                   1150:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1151:                // ank
                   1152:                return(tmp[1] & 0xff);
                   1153:        } else if(tmp[0] == '%') {
                   1154:                // decimal
                   1155:                return(strtoul(tmp + 1, NULL, 10));
                   1156:        }
                   1157:        return(strtoul(tmp, NULL, 16));
                   1158: }
                   1159: 
                   1160: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1161: {
                   1162:        char tmp[1024], *s;
                   1163:        
                   1164:        if(str == NULL || strlen(str) == 0) {
                   1165:                return(val);
                   1166:        }
                   1167:        strcpy(tmp, str);
                   1168:        
                   1169:        if((s = strstr(tmp, ":")) != NULL) {
                   1170:                // 0000:0000
                   1171:                *s = '\0';
                   1172:                return(debugger_get_val(tmp));
                   1173:        }
                   1174:        return(val);
                   1175: }
                   1176: 
                   1177: UINT32 debugger_get_ofs(const char *str)
                   1178: {
                   1179:        char tmp[1024], *s;
                   1180:        
                   1181:        if(str == NULL || strlen(str) == 0) {
                   1182:                return(0);
                   1183:        }
                   1184:        strcpy(tmp, str);
                   1185:        
                   1186:        if((s = strstr(tmp, ":")) != NULL) {
                   1187:                // 0000:0000
                   1188:                return(debugger_get_val(s + 1));
                   1189:        }
                   1190:        return(debugger_get_val(tmp));
                   1191: }
                   1192: 
                   1193: void debugger_main()
                   1194: {
                   1195:        telnet_command("\033[20h"); // cr-lf
                   1196:        
                   1197:        force_suspend = true;
                   1198:        now_going = false;
                   1199:        now_debugging = true;
                   1200:        Sleep(100);
                   1201:        
1.1.1.54  root     1202:        if(!m_exit && !now_suspended) {
1.1.1.33  root     1203:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1204:                telnet_printf("waiting until cpu is suspended...\n");
                   1205:        }
1.1.1.54  root     1206:        while(!m_exit && !now_suspended) {
1.1.1.33  root     1207:                if(telnet_disconnected()) {
                   1208:                        break;
                   1209:                }
                   1210:                Sleep(10);
                   1211:        }
                   1212:        
                   1213:        char buffer[8192];
                   1214:        
                   1215:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1216:        debugger_process_info(buffer);
                   1217:        telnet_printf("%s", buffer);
                   1218:        debugger_regs_info(buffer);
                   1219:        telnet_printf("%s", buffer);
                   1220:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1221:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1222:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1223:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1224:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1225:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1226:        
                   1227:        #define MAX_COMMAND_LEN 64
                   1228:        
                   1229:        char command[MAX_COMMAND_LEN + 1];
                   1230:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1231:        
                   1232:        UINT32 data_seg = SREG(DS);
                   1233:        UINT32 data_ofs = 0;
                   1234:        UINT32 dasm_seg = SREG(CS);
                   1235:        UINT32 dasm_ofs = m_eip;
                   1236:        
1.1.1.54  root     1237:        while(!m_exit) {
1.1.1.33  root     1238:                telnet_printf("- ");
                   1239:                command[0] = '\0';
                   1240:                
                   1241:                if(fi_debugger != NULL) {
                   1242:                        while(command[0] == '\0') {
                   1243:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1244:                                        break;
                   1245:                                }
                   1246:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1247:                                        command[strlen(command) - 1] = '\0';
                   1248:                                }
                   1249:                        }
                   1250:                        if(command[0] != '\0') {
                   1251:                                telnet_command("%s\n", command);
                   1252:                        }
                   1253:                }
                   1254:                if(command[0] == '\0') {
                   1255:                        if(!telnet_gets(command, sizeof(command))) {
                   1256:                                break;
                   1257:                        }
                   1258:                }
                   1259:                if(command[0] == '\0') {
                   1260:                        strcpy(command, prev_command);
                   1261:                } else {
                   1262:                        strcpy(prev_command, command);
                   1263:                }
                   1264:                if(fp_debugger != NULL) {
                   1265:                        fprintf(fp_debugger, "%s\n", command);
                   1266:                }
                   1267:                
1.1.1.54  root     1268:                if(!m_exit && command[0] != 0) {
1.1.1.33  root     1269:                        char *params[32], *token = NULL;
                   1270:                        int num = 0;
                   1271:                        
                   1272:                        if((token = strtok(command, " ")) != NULL) {
                   1273:                                params[num++] = token;
                   1274:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1275:                                        params[num++] = token;
                   1276:                                }
                   1277:                        }
                   1278:                        if(stricmp(params[0], "D") == 0) {
                   1279:                                if(num <= 3) {
                   1280:                                        if(num >= 2) {
                   1281:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1282:                                                data_ofs = debugger_get_ofs(params[1]);
                   1283:                                        }
                   1284:                                        UINT32 end_seg = data_seg;
                   1285:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1286:                                        if(num == 3) {
                   1287:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1288:                                                end_ofs = debugger_get_ofs(params[2]);
                   1289:                                        }
                   1290:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1291:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1292: //                                     bool is_sjis = false;
1.1.1.33  root     1293:                                        
                   1294:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1295:                                                if((addr & 0x0f) == 0) {
                   1296:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1297:                                                                data_seg += 0x1000;
                   1298:                                                                data_ofs -= 0x10000;
                   1299:                                                        }
                   1300:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1301:                                                        memset(buffer, 0, sizeof(buffer));
                   1302:                                                }
                   1303:                                                if(addr < start_addr || addr > end_addr) {
                   1304:                                                        telnet_printf("   ");
                   1305:                                                        buffer[addr & 0x0f] = ' ';
                   1306:                                                } else {
                   1307:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1308:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1309: //                                                     if(is_sjis) {
1.1.1.33  root     1310: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1311: //                                                             is_sjis = false;
1.1.1.33  root     1312: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1313: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1314: //                                                             is_sjis = true;
1.1.1.33  root     1315: //                                                     } else
                   1316:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1317:                                                                buffer[addr & 0x0f] = data;
                   1318:                                                        } else {
                   1319:                                                                buffer[addr & 0x0f] = '.';
                   1320:                                                        }
                   1321:                                                }
                   1322:                                                if((addr & 0x0f) == 0x0f) {
                   1323:                                                        telnet_printf("  %s\n", buffer);
                   1324:                                                }
                   1325:                                        }
                   1326:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1327:                                                data_seg += 0x1000;
                   1328:                                                data_ofs -= 0x10000;
                   1329:                                        }
                   1330:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1331:                                } else {
                   1332:                                        telnet_printf("invalid parameter number\n");
                   1333:                                }
                   1334:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1335:                                if(num >= 3) {
                   1336:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1337:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1338:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1339:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1340:                                        }
                   1341:                                } else {
                   1342:                                        telnet_printf("invalid parameter number\n");
                   1343:                                }
                   1344:                        } else if(stricmp(params[0], "EW") == 0) {
                   1345:                                if(num >= 3) {
                   1346:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1347:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1348:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1349:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1350:                                        }
                   1351:                                } else {
                   1352:                                        telnet_printf("invalid parameter number\n");
                   1353:                                }
                   1354:                        } else if(stricmp(params[0], "ED") == 0) {
                   1355:                                if(num >= 3) {
                   1356:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1357:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1358:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1359:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1360:                                        }
                   1361:                                } else {
                   1362:                                        telnet_printf("invalid parameter number\n");
                   1363:                                }
                   1364:                        } else if(stricmp(params[0], "EA") == 0) {
                   1365:                                if(num >= 3) {
                   1366:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1367:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1368:                                        strcpy(buffer, prev_command);
                   1369:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1370:                                                int len = strlen(token);
                   1371:                                                for(int i = 0; i < len; i++) {
                   1372:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1373:                                                }
                   1374:                                        } else {
                   1375:                                                telnet_printf("invalid parameter\n");
                   1376:                                        }
                   1377:                                } else {
                   1378:                                        telnet_printf("invalid parameter number\n");
                   1379:                                }
                   1380:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1381:                                if(num == 2) {
                   1382:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1383:                                } else {
                   1384:                                        telnet_printf("invalid parameter number\n");
                   1385:                                }
                   1386:                        } else if(stricmp(params[0], "IW") == 0) {
                   1387:                                if(num == 2) {
                   1388:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1389:                                } else {
                   1390:                                        telnet_printf("invalid parameter number\n");
                   1391:                                }
                   1392:                        } else if(stricmp(params[0], "ID") == 0) {
                   1393:                                if(num == 2) {
                   1394:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1395:                                } else {
                   1396:                                        telnet_printf("invalid parameter number\n");
                   1397:                                }
                   1398:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1399:                                if(num == 3) {
                   1400:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1401:                                } else {
                   1402:                                        telnet_printf("invalid parameter number\n");
                   1403:                                }
                   1404:                        } else if(stricmp(params[0], "OW") == 0) {
                   1405:                                if(num == 3) {
                   1406:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1407:                                } else {
                   1408:                                        telnet_printf("invalid parameter number\n");
                   1409:                                }
                   1410:                        } else if(stricmp(params[0], "OD") == 0) {
                   1411:                                if(num == 3) {
                   1412:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1413:                                } else {
                   1414:                                        telnet_printf("invalid parameter number\n");
                   1415:                                }
                   1416:                        } else if(stricmp(params[0], "R") == 0) {
                   1417:                                if(num == 1) {
                   1418:                                        debugger_regs_info(buffer);
                   1419:                                        telnet_printf("%s", buffer);
                   1420:                                } else if(num == 3) {
                   1421: #if defined(HAS_I386)
                   1422:                                        if(stricmp(params[1], "EAX") == 0) {
                   1423:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1424:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1425:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1426:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1427:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1428:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1429:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1430:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1431:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1432:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1433:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1434:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1435:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1436:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1437:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1438:                                        } else
                   1439: #endif
                   1440:                                        if(stricmp(params[1], "AX") == 0) {
                   1441:                                                REG16(AX) = debugger_get_val(params[2]);
                   1442:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1443:                                                REG16(BX) = debugger_get_val(params[2]);
                   1444:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1445:                                                REG16(CX) = debugger_get_val(params[2]);
                   1446:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1447:                                                REG16(DX) = debugger_get_val(params[2]);
                   1448:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1449:                                                REG16(SP) = debugger_get_val(params[2]);
                   1450:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1451:                                                REG16(BP) = debugger_get_val(params[2]);
                   1452:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1453:                                                REG16(SI) = debugger_get_val(params[2]);
                   1454:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1455:                                                REG16(DI) = debugger_get_val(params[2]);
                   1456:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1457: #if defined(HAS_I386)
                   1458:                                                if(m_operand_size) {
                   1459:                                                        m_eip = debugger_get_val(params[2]);
                   1460:                                                } else {
                   1461:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1462:                                                }
                   1463:                                                CHANGE_PC(m_eip);
                   1464: #else
                   1465:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1466:                                                CHANGE_PC(m_pc);
                   1467: #endif
                   1468:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1469:                                                REG8(AL) = debugger_get_val(params[2]);
                   1470:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1471:                                                REG8(AH) = debugger_get_val(params[2]);
                   1472:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1473:                                                REG8(BL) = debugger_get_val(params[2]);
                   1474:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1475:                                                REG8(BH) = debugger_get_val(params[2]);
                   1476:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1477:                                                REG8(CL) = debugger_get_val(params[2]);
                   1478:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1479:                                                REG8(CH) = debugger_get_val(params[2]);
                   1480:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1481:                                                REG8(DL) = debugger_get_val(params[2]);
                   1482:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1483:                                                REG8(DH) = debugger_get_val(params[2]);
                   1484:                                        } else {
                   1485:                                                telnet_printf("unknown register %s\n", params[1]);
                   1486:                                        }
                   1487:                                } else {
                   1488:                                        telnet_printf("invalid parameter number\n");
                   1489:                                }
                   1490:                        } else if(_tcsicmp(params[0], "S") == 0) {
                   1491:                                if(num >= 4) {
                   1492:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1493:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1494:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1495:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1496:                                        UINT8 list[32];
                   1497:                                        
                   1498:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1499:                                                list[j] = debugger_get_val(params[i]);
                   1500:                                        }
                   1501:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1502:                                                bool found = true;
                   1503:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1504:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1505:                                                                found = false;
                   1506:                                                                break;
                   1507:                                                        }
                   1508:                                                }
                   1509:                                                if(found) {
                   1510:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1511:                                                }
                   1512:                                                if((cur_ofs += 1) > 0xffff) {
                   1513:                                                        cur_seg += 0x1000;
                   1514:                                                        cur_ofs -= 0x10000;
                   1515:                                                }
                   1516:                                        }
                   1517:                                } else {
                   1518:                                        telnet_printf("invalid parameter number\n");
                   1519:                                }
                   1520:                        } else if(stricmp(params[0], "U") == 0) {
                   1521:                                if(num <= 3) {
                   1522:                                        if(num >= 2) {
                   1523:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1524:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1525:                                        }
                   1526:                                        if(num == 3) {
                   1527:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1528:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1529:                                                
                   1530:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1531:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1532:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1533:                                                        for(int i = 0; i < len; i++) {
                   1534:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1535:                                                        }
                   1536:                                                        for(int i = len; i < 8; i++) {
                   1537:                                                                telnet_printf("  ");
                   1538:                                                        }
                   1539:                                                        telnet_printf("  %s\n", buffer);
                   1540:                                                        if((dasm_ofs += len) > 0xffff) {
                   1541:                                                                dasm_seg += 0x1000;
                   1542:                                                                dasm_ofs -= 0x10000;
                   1543:                                                        }
                   1544:                                                }
                   1545:                                        } else {
                   1546:                                                for(int i = 0; i < 16; i++) {
                   1547:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1548:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1549:                                                        for(int i = 0; i < len; i++) {
                   1550:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1551:                                                        }
                   1552:                                                        for(int i = len; i < 8; i++) {
                   1553:                                                                telnet_printf("  ");
                   1554:                                                        }
                   1555:                                                        telnet_printf("  %s\n", buffer);
                   1556:                                                        if((dasm_ofs += len) > 0xffff) {
                   1557:                                                                dasm_seg += 0x1000;
                   1558:                                                                dasm_ofs -= 0x10000;
                   1559:                                                        }
                   1560:                                                }
                   1561:                                        }
                   1562:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1563:                                } else {
                   1564:                                        telnet_printf("invalid parameter number\n");
                   1565:                                }
                   1566:                        } else if(stricmp(params[0], "H") == 0) {
                   1567:                                if(num == 3) {
                   1568:                                        UINT32 l = debugger_get_val(params[1]);
                   1569:                                        UINT32 r = debugger_get_val(params[2]);
                   1570:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1571:                                } else {
                   1572:                                        telnet_printf("invalid parameter number\n");
                   1573:                                }
                   1574:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1575:                                break_point_t *break_point_ptr;
                   1576:                                #define GET_BREAK_POINT_PTR() { \
                   1577:                                        if(params[0][0] == 'R') { \
                   1578:                                                break_point_ptr = &rd_break_point; \
                   1579:                                        } else if(params[0][0] == 'W') { \
                   1580:                                                break_point_ptr = &wr_break_point; \
                   1581:                                        } else if(params[0][0] == 'I') { \
                   1582:                                                break_point_ptr = &in_break_point; \
                   1583:                                        } else if(params[0][0] == 'O') { \
                   1584:                                                break_point_ptr = &out_break_point; \
                   1585:                                        } else { \
                   1586:                                                break_point_ptr = &break_point; \
                   1587:                                        } \
                   1588:                                }
                   1589:                                GET_BREAK_POINT_PTR();
                   1590:                                if(num == 2) {
                   1591:                                        UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1592:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1593:                                        bool found = false;
                   1594:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1595:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1596:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1597:                                                        break_point_ptr->table[i].seg = seg;
                   1598:                                                        break_point_ptr->table[i].ofs = ofs;
                   1599:                                                        break_point_ptr->table[i].status = 1;
                   1600:                                                        found = true;
                   1601:                                                }
                   1602:                                        }
                   1603:                                        if(!found) {
                   1604:                                                telnet_printf("too many break points\n");
                   1605:                                        }
                   1606:                                } else {
                   1607:                                        telnet_printf("invalid parameter number\n");
                   1608:                                }
                   1609:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1610:                                break_point_t *break_point_ptr;
                   1611:                                GET_BREAK_POINT_PTR();
                   1612:                                if(num == 2) {
                   1613:                                        UINT32 addr = debugger_get_val(params[1]);
                   1614:                                        bool found = false;
                   1615:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1616:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1617:                                                        break_point_ptr->table[i].addr = addr;
                   1618:                                                        break_point_ptr->table[i].status = 1;
                   1619:                                                        found = true;
                   1620:                                                }
                   1621:                                        }
                   1622:                                        if(!found) {
                   1623:                                                telnet_printf("too many break points\n");
                   1624:                                        }
                   1625:                                } else {
                   1626:                                        telnet_printf("invalid parameter number\n");
                   1627:                                }
                   1628:                        } else if(stricmp(params[0], "BC") == 0 || stricmp(params[0], "RBC") == 0 || stricmp(params[0], "WBC") == 0 || stricmp(params[0], "IBC") == 0 || stricmp(params[0], "OBC") == 0) {
                   1629:                                break_point_t *break_point_ptr;
                   1630:                                GET_BREAK_POINT_PTR();
                   1631:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1632:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1633:                                } else if(num >= 2) {
                   1634:                                        for(int i = 1; i < num; i++) {
                   1635:                                                int index = debugger_get_val(params[i]);
                   1636:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1637:                                                        telnet_printf("invalid index %x\n", index);
                   1638:                                                } else {
                   1639:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1640:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1641:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1642:                                                        break_point_ptr->table[index - 1].status = 0;
                   1643:                                                }
                   1644:                                        }
                   1645:                                } else {
                   1646:                                        telnet_printf("invalid parameter number\n");
                   1647:                                }
                   1648:                        } else if(stricmp(params[0], "BD") == 0 || stricmp(params[0], "RBD") == 0 || stricmp(params[0], "WBD") == 0 || stricmp(params[0], "IBD") == 0 || stricmp(params[0], "OBD") == 0 ||
                   1649:                                  stricmp(params[0], "BE") == 0 || stricmp(params[0], "RBE") == 0 || stricmp(params[0], "WBE") == 0 || stricmp(params[0], "IBE") == 0 || stricmp(params[0], "OBE") == 0) {
                   1650:                                break_point_t *break_point_ptr;
                   1651:                                GET_BREAK_POINT_PTR();
                   1652:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1653:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1654:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1655:                                                if(break_point_ptr->table[i].status != 0) {
                   1656:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1657:                                                }
                   1658:                                        }
                   1659:                                } else if(num >= 2) {
                   1660:                                        for(int i = 1; i < num; i++) {
                   1661:                                                int index = debugger_get_val(params[i]);
                   1662:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1663:                                                        telnet_printf("invalid index %x\n", index);
                   1664:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1665:                                                        telnet_printf("break point %x is null\n", index);
                   1666:                                                } else {
                   1667:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1668:                                                }
                   1669:                                        }
                   1670:                                } else {
                   1671:                                        telnet_printf("invalid parameter number\n");
                   1672:                                }
                   1673:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1674:                                break_point_t *break_point_ptr;
                   1675:                                GET_BREAK_POINT_PTR();
                   1676:                                if(num == 1) {
                   1677:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1678:                                                if(break_point_ptr->table[i].status) {
                   1679:                                                        telnet_printf("%d %c %04X:%04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].seg, break_point_ptr->table[i].ofs);
                   1680:                                                }
                   1681:                                        }
                   1682:                                } else {
                   1683:                                        telnet_printf("invalid parameter number\n");
                   1684:                                }
                   1685:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1686:                                break_point_t *break_point_ptr;
                   1687:                                GET_BREAK_POINT_PTR();
                   1688:                                if(num == 1) {
                   1689:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1690:                                                if(break_point_ptr->table[i].status) {
                   1691:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1692:                                                }
                   1693:                                        }
                   1694:                                } else {
                   1695:                                        telnet_printf("invalid parameter number\n");
                   1696:                                }
                   1697:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1698:                                if(num >= 2 && num <= 4) {
                   1699:                                        int int_num = debugger_get_val(params[1]);
                   1700:                                        UINT8 ah = 0, ah_registered = 0;
                   1701:                                        UINT8 al = 0, al_registered = 0;
                   1702:                                        if(num >= 3) {
                   1703:                                                ah = debugger_get_val(params[2]);
                   1704:                                                ah_registered = 1;
                   1705:                                        }
                   1706:                                        if(num == 4) {
                   1707:                                                al = debugger_get_val(params[3]);
                   1708:                                                al_registered = 1;
                   1709:                                        }
                   1710:                                        bool found = false;
                   1711:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1712:                                                if(int_break_point.table[i].status == 0 || (
                   1713:                                                   int_break_point.table[i].int_num == int_num &&
                   1714:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1715:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1716:                                                        int_break_point.table[i].int_num = int_num;
                   1717:                                                        int_break_point.table[i].ah = ah;
                   1718:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1719:                                                        int_break_point.table[i].al = al;
                   1720:                                                        int_break_point.table[i].al_registered = al_registered;
                   1721:                                                        int_break_point.table[i].status = 1;
                   1722:                                                        found = true;
                   1723:                                                }
                   1724:                                        }
                   1725:                                        if(!found) {
                   1726:                                                telnet_printf("too many break points\n");
                   1727:                                        }
                   1728:                                } else {
                   1729:                                        telnet_printf("invalid parameter number\n");
                   1730:                                }
                   1731:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1732:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1733:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1734:                                } else if(num >= 2) {
                   1735:                                        for(int i = 1; i < num; i++) {
                   1736:                                                int index = debugger_get_val(params[i]);
                   1737:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1738:                                                        telnet_printf("invalid index %x\n", index);
                   1739:                                                } else {
                   1740:                                                        int_break_point.table[index - 1].int_num = 0;
                   1741:                                                        int_break_point.table[index - 1].ah = 0;
                   1742:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1743:                                                        int_break_point.table[index - 1].al = 0;
                   1744:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1745:                                                        int_break_point.table[index - 1].status = 0;
                   1746:                                                }
                   1747:                                        }
                   1748:                                } else {
                   1749:                                        telnet_printf("invalid parameter number\n");
                   1750:                                }
                   1751:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1752:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1753:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1754:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1755:                                                if(int_break_point.table[i].status != 0) {
                   1756:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1757:                                                }
                   1758:                                        }
                   1759:                                } else if(num >= 2) {
                   1760:                                        for(int i = 1; i < num; i++) {
                   1761:                                                int index = debugger_get_val(params[i]);
                   1762:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1763:                                                        telnet_printf("invalid index %x\n", index);
                   1764:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1765:                                                        telnet_printf("break point %x is null\n", index);
                   1766:                                                } else {
                   1767:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1768:                                                }
                   1769:                                        }
                   1770:                                } else {
                   1771:                                        telnet_printf("invalid parameter number\n");
                   1772:                                }
                   1773:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1774:                                if(num == 1) {
                   1775:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1776:                                                if(int_break_point.table[i].status) {
                   1777:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1778:                                                        if(int_break_point.table[i].ah_registered) {
                   1779:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1780:                                                        }
                   1781:                                                        if(int_break_point.table[i].al_registered) {
                   1782:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1783:                                                        }
                   1784:                                                        telnet_printf("\n");
                   1785:                                                }
                   1786:                                        }
                   1787:                                } else {
                   1788:                                        telnet_printf("invalid parameter number\n");
                   1789:                                }
                   1790:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1791:                                if(num == 1 || num == 2) {
                   1792:                                        break_point_t break_point_stored;
                   1793:                                        bool break_points_stored = false;
                   1794:                                        
                   1795:                                        if(stricmp(params[0], "P") == 0) {
                   1796:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1797:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1798:                                                break_points_stored = true;
                   1799:                                                
                   1800:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1801:                                                break_point.table[0].status = 1;
                   1802:                                        } else if(num >= 2) {
                   1803:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1804:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1805:                                                break_points_stored = true;
                   1806:                                                
                   1807:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1808:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1809:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1810:                                                break_point.table[0].seg = seg;
                   1811:                                                break_point.table[0].ofs = ofs;
                   1812:                                                break_point.table[0].status = 1;
                   1813:                                        }
                   1814:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1815:                                        now_going = true;
                   1816:                                        now_suspended = false;
                   1817:                                        
                   1818:                                        telnet_command("\033[2l"); // key unlock
1.1.1.54  root     1819:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1820:                                                if(telnet_kbhit()) {
                   1821:                                                        break;
                   1822:                                                }
                   1823:                                                Sleep(10);
                   1824:                                        }
                   1825:                                        now_going = false;
                   1826:                                        telnet_command("\033[2h"); // key lock
                   1827:                                        
                   1828:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1829:                                                Sleep(100);
1.1.1.54  root     1830:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1831:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1832:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1833:                                                }
                   1834:                                        }
1.1.1.54  root     1835:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1836:                                                if(telnet_disconnected()) {
                   1837:                                                        break;
                   1838:                                                }
                   1839:                                                Sleep(10);
                   1840:                                        }
                   1841:                                        dasm_seg = SREG(CS);
                   1842:                                        dasm_ofs = m_eip;
                   1843:                                        
                   1844:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1845:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1846:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1847:                                        
                   1848:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1849:                                        debugger_regs_info(buffer);
                   1850:                                        telnet_printf("%s", buffer);
                   1851:                                        
                   1852:                                        if(break_point.hit) {
                   1853:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1854:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1855:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1856:                                                }
                   1857:                                        } else if(rd_break_point.hit) {
                   1858:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1859:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1860:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1861:                                                m_prev_cs, m_prev_eip);
                   1862:                                        } else if(wr_break_point.hit) {
                   1863:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1864:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1865:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1866:                                                m_prev_cs, m_prev_eip);
                   1867:                                        } else if(in_break_point.hit) {
                   1868:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1869:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1870:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1871:                                                m_prev_cs, m_prev_eip);
                   1872:                                        } else if(out_break_point.hit) {
                   1873:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1874:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1875:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1876:                                                m_prev_cs, m_prev_eip);
                   1877:                                        } else if(int_break_point.hit) {
                   1878:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1879:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1880:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1881:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1882:                                                }
                   1883:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1884:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1885:                                                }
                   1886:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1887:                                        } else {
                   1888:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1889:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1890:                                        }
                   1891:                                        if(break_points_stored) {
                   1892:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1893:                                        }
                   1894:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1895:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1896:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1897:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1898:                                } else {
                   1899:                                        telnet_printf("invalid parameter number\n");
                   1900:                                }
                   1901:                        } else if(stricmp(params[0], "T") == 0) {
                   1902:                                if(num == 1 || num == 2) {
                   1903:                                        int steps = 1;
                   1904:                                        if(num >= 2) {
                   1905:                                                steps = debugger_get_val(params[1]);
                   1906:                                        }
                   1907:                                        
                   1908:                                        telnet_command("\033[2l"); // key unlock
                   1909:                                        while(steps-- > 0) {
                   1910:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1911:                                                now_going = false;
                   1912:                                                now_suspended = false;
                   1913:                                                
1.1.1.54  root     1914:                                                while(!m_exit && !now_suspended) {
1.1.1.33  root     1915:                                                        if(telnet_disconnected()) {
                   1916:                                                                break;
                   1917:                                                        }
                   1918:                                                        Sleep(10);
                   1919:                                                }
                   1920:                                                dasm_seg = SREG(CS);
                   1921:                                                dasm_ofs = m_eip;
                   1922:                                                
                   1923:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1924:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1925:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1926:                                                
                   1927:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1928:                                                debugger_regs_info(buffer);
                   1929:                                                telnet_printf("%s", buffer);
                   1930:                                                
                   1931:                                                if(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit || telnet_kbhit()) {
                   1932:                                                        break;
                   1933:                                                }
                   1934:                                        }
                   1935:                                        telnet_command("\033[2h"); // key lock
                   1936:                                        
                   1937:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1938:                                                Sleep(100);
1.1.1.54  root     1939:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1940:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1941:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1942:                                                }
                   1943:                                        }
1.1.1.54  root     1944:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1945:                                                if(telnet_disconnected()) {
                   1946:                                                        break;
                   1947:                                                }
                   1948:                                                Sleep(10);
                   1949:                                        }
                   1950:                                        if(break_point.hit) {
                   1951:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1952:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1953:                                        } else if(rd_break_point.hit) {
                   1954:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1955:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1956:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1957:                                                m_prev_cs, m_prev_eip);
                   1958:                                        } else if(wr_break_point.hit) {
                   1959:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1960:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1961:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1962:                                                m_prev_cs, m_prev_eip);
                   1963:                                        } else if(in_break_point.hit) {
                   1964:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1965:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1966:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1967:                                                m_prev_cs, m_prev_eip);
                   1968:                                        } else if(out_break_point.hit) {
                   1969:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1970:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1971:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1972:                                                m_prev_cs, m_prev_eip);
                   1973:                                        } else if(int_break_point.hit) {
                   1974:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1975:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1976:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1977:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1978:                                                }
                   1979:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1980:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1981:                                                }
                   1982:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1983:                                        } else if(steps > 0) {
                   1984:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1985:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1986:                                        }
                   1987:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1988:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1989:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1990:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1991:                                } else {
                   1992:                                        telnet_printf("invalid parameter number\n");
                   1993:                                }
                   1994:                        } else if(stricmp(params[0], "Q") == 0) {
                   1995:                                break;
                   1996:                        } else if(stricmp(params[0], "X") == 0) {
                   1997:                                debugger_process_info(buffer);
                   1998:                                telnet_printf("%s", buffer);
                   1999:                        } else if(stricmp(params[0], ">") == 0) {
                   2000:                                if(num == 2) {
                   2001:                                        if(fp_debugger != NULL) {
                   2002:                                                fclose(fp_debugger);
                   2003:                                                fp_debugger = NULL;
                   2004:                                        }
                   2005:                                        fp_debugger = fopen(params[1], "w");
                   2006:                                } else {
                   2007:                                        telnet_printf("invalid parameter number\n");
                   2008:                                }
                   2009:                        } else if(stricmp(params[0], "<") == 0) {
                   2010:                                if(num == 2) {
                   2011:                                        if(fi_debugger != NULL) {
                   2012:                                                fclose(fi_debugger);
                   2013:                                                fi_debugger = NULL;
                   2014:                                        }
                   2015:                                        fi_debugger = fopen(params[1], "r");
                   2016:                                } else {
                   2017:                                        telnet_printf("invalid parameter number\n");
                   2018:                                }
                   2019:                        } else if(stricmp(params[0], "?") == 0) {
                   2020:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   2021:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   2022:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   2023:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   2024:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   2025:                                
                   2026:                                telnet_printf("R - show registers\n");
                   2027:                                telnet_printf("R <reg> <value> - edit register\n");
                   2028:                                telnet_printf("S <start> <end> <list> - search\n");
                   2029:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   2030:                                
                   2031:                                telnet_printf("H <value> <value> - hexadd\n");
                   2032:                                
                   2033:                                telnet_printf("BP <address> - set breakpoint\n");
                   2034:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   2035:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   2036:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   2037:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   2038:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   2039:                                
                   2040:                                telnet_printf("G - go (press enter key to break)\n");
                   2041:                                telnet_printf("G <address> - go and break at address\n");
                   2042:                                telnet_printf("P - trace one opcode (step over)\n");
                   2043:                                telnet_printf("T [<count>] - trace (step in)\n");
                   2044:                                telnet_printf("Q - quit\n");
                   2045:                                telnet_printf("X - show dos process info\n");
                   2046:                                
                   2047:                                telnet_printf("> <filename> - output logfile\n");
                   2048:                                telnet_printf("< <filename> - input commands from file\n");
                   2049:                                
                   2050:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   2051:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   2052:                        } else {
                   2053:                                telnet_printf("unknown command %s\n", params[0]);
                   2054:                        }
                   2055:                }
                   2056:        }
                   2057:        if(fp_debugger != NULL) {
                   2058:                fclose(fp_debugger);
                   2059:                fp_debugger = NULL;
                   2060:        }
                   2061:        if(fi_debugger != NULL) {
                   2062:                fclose(fi_debugger);
                   2063:                fi_debugger = NULL;
                   2064:        }
                   2065:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2066:        closesocket(cli_socket);
                   2067: }
                   2068: 
                   2069: const char *debugger_get_ttermpro_path()
                   2070: {
                   2071:        static char path[MAX_PATH] = {0};
                   2072:        
                   2073:        if(getenv("ProgramFiles")) {
                   2074:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2075:        }
                   2076:        return(path);
                   2077: }
                   2078: 
                   2079: const char *debugger_get_ttermpro_x86_path()
                   2080: {
                   2081:        static char path[MAX_PATH] = {0};
                   2082:        
                   2083:        if(getenv("ProgramFiles(x86)")) {
                   2084:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2085:        }
                   2086:        return(path);
                   2087: }
                   2088: 
                   2089: const char *debugger_get_putty_path()
                   2090: {
                   2091:        static char path[MAX_PATH] = {0};
                   2092:        
                   2093:        if(getenv("ProgramFiles")) {
                   2094:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2095:        }
                   2096:        return(path);
                   2097: }
                   2098: 
                   2099: const char *debugger_get_putty_x86_path()
                   2100: {
                   2101:        static char path[MAX_PATH] = {0};
                   2102:        
                   2103:        if(getenv("ProgramFiles(x86)")) {
                   2104:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2105:        }
                   2106:        return(path);
                   2107: }
                   2108: 
                   2109: const char *debugger_get_telnet_path()
                   2110: {
                   2111:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2112:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2113:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2114:        // and 64bit version of telnet.exe will be installed in System32.
                   2115:        static char path[MAX_PATH] = {0};
                   2116:        
                   2117:        if(getenv("windir") != NULL) {
                   2118:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2119:        }
                   2120:        return(path);
                   2121: }
                   2122: 
                   2123: DWORD WINAPI debugger_thread(LPVOID)
                   2124: {
                   2125:        WSADATA was_data;
                   2126:        struct sockaddr_in svr_addr;
                   2127:        struct sockaddr_in cli_addr;
                   2128:        int cli_addr_len = sizeof(cli_addr);
                   2129:        int port = 23;
                   2130:        int bind_stat = SOCKET_ERROR;
                   2131:        struct timeval timeout;
                   2132:        
                   2133:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2134:        
                   2135:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2136:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2137:                svr_addr.sin_family = AF_INET;
                   2138:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2139:                
1.1.1.54  root     2140:                while(!m_exit && port < 10000) {
1.1.1.33  root     2141:                        svr_addr.sin_port = htons(port);
                   2142:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2143:                                break;
                   2144:                        } else {
                   2145:                                port = (port == 23) ? 9000 : (port + 1);
                   2146:                        }
                   2147:                }
                   2148:                if(bind_stat == 0) {
                   2149:                        timeout.tv_sec = 1;
                   2150:                        timeout.tv_usec = 0;
1.1.1.45  root     2151:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2152:                        
                   2153:                        listen(svr_socket, 1);
                   2154:                        
                   2155:                        char command[MAX_PATH] = {0};
                   2156:                        STARTUPINFO si;
                   2157:                        PROCESS_INFORMATION pi;
                   2158:                        
                   2159:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2160:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2161:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2162:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2163:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2164:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2165:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2166:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2167:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2168:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2169:                        }
                   2170:                        if(command[0] != '\0') {
                   2171:                                memset(&si, 0, sizeof(STARTUPINFO));
                   2172:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
                   2173:                                CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
                   2174:                        }
                   2175:                        
1.1.1.54  root     2176:                        while(!m_exit) {
1.1.1.33  root     2177:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2178:                                        u_long val = 1;
                   2179:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2180:                                        debugger_main();
                   2181:                                }
                   2182:                        }
                   2183:                }
                   2184:        }
                   2185:        WSACleanup();
                   2186:        return(0);
                   2187: }
                   2188: #endif
                   2189: 
                   2190: /* ----------------------------------------------------------------------------
1.1       root     2191:        main
                   2192: ---------------------------------------------------------------------------- */
                   2193: 
1.1.1.28  root     2194: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2195: {
                   2196:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2197:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2198: #ifdef USE_SERVICE_THREAD
                   2199:                        EnterCriticalSection(&key_buf_crit_sect);
                   2200: #endif
1.1.1.51  root     2201:                        pcbios_clear_key_buffer();
1.1.1.35  root     2202: #ifdef USE_SERVICE_THREAD
                   2203:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2204: #endif
1.1.1.33  root     2205:                }
                   2206: //             key_code = key_recv = 0;
1.1.1.28  root     2207:                return TRUE;
                   2208:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2209:                return TRUE;
                   2210:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2211:                // this program will be terminated abnormally, do minimum end process
                   2212:                exit_handler();
                   2213:                exit(1);
                   2214:        }
                   2215:        return FALSE;
                   2216: }
                   2217: 
                   2218: void exit_handler()
                   2219: {
                   2220:        if(temp_file_created) {
                   2221:                DeleteFile(temp_file_path);
                   2222:                temp_file_created = false;
                   2223:        }
                   2224:        if(key_buf_char != NULL) {
                   2225:                key_buf_char->release();
                   2226:                delete key_buf_char;
                   2227:                key_buf_char = NULL;
                   2228:        }
                   2229:        if(key_buf_scan != NULL) {
                   2230:                key_buf_scan->release();
                   2231:                delete key_buf_scan;
                   2232:                key_buf_scan = NULL;
                   2233:        }
1.1.1.32  root     2234: #ifdef SUPPORT_XMS
                   2235:        msdos_xms_release();
                   2236: #endif
1.1.1.28  root     2237:        hardware_release();
                   2238: }
                   2239: 
1.1.1.35  root     2240: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2241: DWORD WINAPI vram_thread(LPVOID)
                   2242: {
1.1.1.54  root     2243:        while(!m_exit) {
1.1.1.28  root     2244:                EnterCriticalSection(&vram_crit_sect);
                   2245:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2246:                        vram_flush_char();
                   2247:                }
                   2248:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2249:                        vram_flush_attr();
                   2250:                }
                   2251:                vram_last_length_char = vram_length_char;
                   2252:                vram_last_length_attr = vram_length_attr;
                   2253:                LeaveCriticalSection(&vram_crit_sect);
                   2254:                // this is about half the maximum keyboard repeat rate - any
                   2255:                // lower tends to be jerky, any higher misses updates
                   2256:                Sleep(15);
                   2257:        }
                   2258:        return 0;
                   2259: }
                   2260: #endif
                   2261: 
1.1.1.45  root     2262: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2263: {
                   2264:        UINT8 header[0x400];
                   2265:        
                   2266:        long position = ftell(fp);
                   2267:        fseek(fp, 0, SEEK_SET);
                   2268:        fread(header, sizeof(header), 1, fp);
                   2269:        fseek(fp, position, SEEK_SET);
                   2270:        
                   2271:        try {
                   2272:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2273:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2274:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2275:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2276:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2277:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2278:                
                   2279:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2280:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2281:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2282:                                return(sectionHeader->PointerToRawData);
                   2283:                        }
                   2284:                }
                   2285:        } catch(...) {
                   2286:        }
                   2287:        return(0);
                   2288: }
                   2289: 
1.1.1.10  root     2290: bool is_started_from_command_prompt()
                   2291: {
1.1.1.18  root     2292:        bool ret = false;
                   2293:        
                   2294:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2295:        if(hLibrary) {
                   2296:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2297:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2298:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                   2299:                if(lpfnGetConsoleProcessList) {
                   2300:                        DWORD pl;
                   2301:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                   2302:                        FreeLibrary(hLibrary);
                   2303:                        return(ret);
                   2304:                }
                   2305:                FreeLibrary(hLibrary);
                   2306:        }
                   2307:        
                   2308:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2309:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2310:                DWORD dwParentProcessID = 0;
                   2311:                PROCESSENTRY32 pe32;
                   2312:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2313:                if(Process32First(hSnapshot, &pe32)) {
                   2314:                        do {
                   2315:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2316:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2317:                                        break;
                   2318:                                }
                   2319:                        } while(Process32Next(hSnapshot, &pe32));
                   2320:                }
                   2321:                CloseHandle(hSnapshot);
                   2322:                if(dwParentProcessID != 0) {
                   2323:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2324:                        if(hProcess != NULL) {
                   2325:                                HMODULE hMod;
                   2326:                                DWORD cbNeeded;
                   2327:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2328:                                        char module_name[MAX_PATH];
                   2329:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                   2330:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                   2331:                                        }
                   2332:                                }
                   2333:                                CloseHandle(hProcess);
                   2334:                        }
                   2335:                }
                   2336:        }
                   2337:        return(ret);
1.1.1.14  root     2338: }
                   2339: 
                   2340: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2341: {
1.1.1.24  root     2342:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2343:        OSVERSIONINFOEX osvi;
                   2344:        DWORDLONG dwlConditionMask = 0;
                   2345:        int op = VER_GREATER_EQUAL;
                   2346:        
                   2347:        // Initialize the OSVERSIONINFOEX structure.
                   2348:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2349:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2350:        osvi.dwMajorVersion = dwMajorVersion;
                   2351:        osvi.dwMinorVersion = dwMinorVersion;
                   2352:        osvi.wServicePackMajor = wServicePackMajor;
                   2353:        osvi.wServicePackMinor = wServicePackMinor;
                   2354:        
                   2355:         // Initialize the condition mask.
                   2356:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2357:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2358:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2359:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2360:        
                   2361:        // Perform the test.
                   2362:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2363: }
                   2364: 
1.1.1.27  root     2365: void get_sio_port_numbers()
                   2366: {
                   2367:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2368:        HDEVINFO hDevInfo = 0;
                   2369:        HKEY hKey = 0;
                   2370:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2371:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2372:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2373:                                char chData[256];
                   2374:                                DWORD dwType = 0;
                   2375:                                DWORD dwSize = sizeof(chData);
                   2376:                                int port_number = 0;
                   2377:                                
                   2378:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2379:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2380:                                                port_number = atoi(chData + 3);
                   2381:                                        }
                   2382:                                }
                   2383:                                RegCloseKey(hKey);
                   2384:                                
1.1.1.29  root     2385:                                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     2386:                                        continue;
                   2387:                                }
                   2388:                                if(sio_port_number[0] == 0) {
                   2389:                                        sio_port_number[0] = port_number;
                   2390:                                } else if(sio_port_number[1] == 0) {
                   2391:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2392:                                } else if(sio_port_number[2] == 0) {
                   2393:                                        sio_port_number[2] = port_number;
                   2394:                                } else if(sio_port_number[3] == 0) {
                   2395:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2396:                                }
1.1.1.29  root     2397:                                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     2398:                                        break;
                   2399:                                }
                   2400:                        }
                   2401:                }
                   2402:        }
                   2403: }
                   2404: 
1.1.1.28  root     2405: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2406: 
1.1       root     2407: int main(int argc, char *argv[], char *envp[])
                   2408: {
1.1.1.9   root     2409:        int arg_offset = 0;
                   2410:        int standard_env = 0;
1.1.1.14  root     2411:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2412:        bool get_console_info_success = false;
                   2413:        bool screen_size_changed = false;
                   2414:        
                   2415:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2416:        GetModuleFileName(NULL, path, MAX_PATH);
                   2417:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2418:        
1.1.1.27  root     2419:        char dummy_argv_0[] = "msdos.exe";
                   2420:        char dummy_argv_1[MAX_PATH];
                   2421:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2422:        char new_exec_file[MAX_PATH];
                   2423:        bool convert_cmd_file = false;
1.1.1.28  root     2424:        unsigned int code_page = 0;
1.1.1.27  root     2425:        
                   2426:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2427:                // check if command file is embedded to this execution file
                   2428:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2429:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2430:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2431:                if(offset != 0) {
1.1.1.30  root     2432:                        UINT8 buffer[16];
1.1.1.28  root     2433:                        fseek(fp, offset, SEEK_SET);
                   2434:                        fread(buffer, sizeof(buffer), 1, fp);
                   2435:                        
                   2436:                        // restore flags
                   2437:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2438:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2439:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2440:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2441:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2442:                        if((buffer[0] & 0x20) != 0) {
                   2443:                                get_sio_port_numbers();
                   2444:                        }
                   2445:                        if((buffer[0] & 0x40) != 0) {
                   2446:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2447:                                support_ems = true;
1.1.1.30  root     2448:                        }
1.1.1.27  root     2449: #ifdef SUPPORT_XMS
1.1.1.30  root     2450:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2451:                                support_xms = true;
                   2452:                        }
1.1.1.30  root     2453: #endif
1.1.1.28  root     2454:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2455:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2456:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2457:                        }
                   2458:                        if(buffer[5] != 0) {
1.1.1.30  root     2459:                                dos_major_version = buffer[5];
                   2460:                                dos_minor_version = buffer[6];
                   2461:                        }
                   2462:                        if(buffer[7] != 0) {
                   2463:                                win_major_version = buffer[7];
                   2464:                                win_minor_version = buffer[8];
1.1.1.28  root     2465:                        }
1.1.1.30  root     2466:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2467:                                SetConsoleCP(code_page);
                   2468:                                SetConsoleOutputCP(code_page);
                   2469:                        }
1.1.1.30  root     2470:                        int name_len = buffer[11];
                   2471:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2472:                        
                   2473:                        // restore command file name
                   2474:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2475:                        fread(dummy_argv_1, name_len, 1, fp);
                   2476:                        
                   2477:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2478:                                // if original command file exists, create a temporary file name
                   2479:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2480:                                        // create a temporary command file in the current director
                   2481:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2482:                                } else {
1.1.1.28  root     2483:                                        // create a temporary command file in the temporary folder
                   2484:                                        GetTempPath(MAX_PATH, path);
                   2485:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2486:                                                DeleteFile(dummy_argv_1);
                   2487:                                        } else {
                   2488:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2489:                                        }
1.1.1.27  root     2490:                                }
1.1.1.28  root     2491:                                // check the command file type
                   2492:                                fread(buffer, 2, 1, fp);
                   2493:                                fseek(fp, -2, SEEK_CUR);
                   2494:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2495:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2496:                                } else {
                   2497:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2498:                                }
                   2499:                        }
1.1.1.28  root     2500:                        
                   2501:                        // restore command file
                   2502:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2503:                        for(int i = 0; i < file_len; i++) {
                   2504:                                fputc(fgetc(fp), fo);
                   2505:                        }
                   2506:                        fclose(fo);
                   2507:                        
                   2508:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2509:                        temp_file_created = true;
                   2510:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2511:                        
                   2512:                        // adjust argc/argv
                   2513:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2514:                                dummy_argv[i + 1] = argv[i];
                   2515:                        }
                   2516:                        argc++;
                   2517:                        argv = dummy_argv;
1.1.1.27  root     2518:                }
                   2519:                fclose(fp);
                   2520:        }
1.1.1.9   root     2521:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2522:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2523:                        stay_busy = true;
                   2524:                        arg_offset++;
1.1.1.27  root     2525:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2526:                        if(argv[i][2] != '\0') {
                   2527:                                strcpy(new_exec_file, &argv[i][2]);
                   2528:                        } else {
                   2529:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2530:                        }
                   2531:                        convert_cmd_file = true;
                   2532:                        arg_offset++;
1.1.1.28  root     2533:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2534:                        if(IS_NUMERIC(argv[i][2])) {
                   2535:                                code_page = atoi(&argv[i][2]);
                   2536:                        } else {
                   2537:                                code_page = GetConsoleCP();
                   2538:                        }
                   2539:                        arg_offset++;
1.1.1.25  root     2540:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2541:                        no_windows = true;
                   2542:                        arg_offset++;
                   2543:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2544:                        standard_env = 1;
                   2545:                        arg_offset++;
1.1.1.14  root     2546:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2547:                        ignore_illegal_insn = true;
                   2548:                        arg_offset++;
                   2549:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2550:                        limit_max_memory = true;
                   2551:                        arg_offset++;
                   2552:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51  root     2553:                        int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
                   2554:                        if(result == 1) {
                   2555:                                buf_width = 0;
                   2556:                        } else if(result != 2) {
1.1.1.17  root     2557:                                buf_width = buf_height = 0;
                   2558:                        }
                   2559:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2560:                                buf_width = 80;
                   2561:                        }
                   2562:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2563:                                buf_height = 25;
                   2564:                        }
1.1.1.14  root     2565:                        arg_offset++;
1.1.1.25  root     2566:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2567:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2568:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2569:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2570:                                        sio_port_number[1] = atoi(p1 + 1);
                   2571:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2572:                                                sio_port_number[2] = atoi(p2 + 1);
                   2573:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2574:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2575:                                                }
                   2576:                                        }
1.1.1.25  root     2577:                                }
1.1.1.29  root     2578:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2579:                        }
1.1.1.29  root     2580:                        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     2581:                                get_sio_port_numbers();
1.1.1.25  root     2582:                        }
                   2583:                        arg_offset++;
1.1.1.9   root     2584:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2585:                        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     2586:                                dos_major_version = argv[i][2] - '0';
                   2587:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2588:                        }
                   2589:                        arg_offset++;
                   2590:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2591:                        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]))) {
                   2592:                                win_major_version = argv[i][2] - '0';
                   2593:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2594:                        }
                   2595:                        arg_offset++;
1.1.1.25  root     2596:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2597:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2598:                        support_ems = true;
                   2599: #ifdef SUPPORT_XMS
                   2600:                        support_xms = true;
                   2601: #endif
                   2602:                        arg_offset++;
1.1.1.9   root     2603:                } else {
                   2604:                        break;
                   2605:                }
                   2606:        }
                   2607:        if(argc < 2 + arg_offset) {
1.1       root     2608: #ifdef _WIN64
1.1.1.14  root     2609:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2610: #else
1.1.1.14  root     2611:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2612: #endif
1.1.1.25  root     2613:                fprintf(stderr,
1.1.1.28  root     2614:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2615:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2616:                        "\n"
                   2617:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2618: #ifdef _WIN64
1.1.1.27  root     2619:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2620: #else
1.1.1.27  root     2621:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2622: #endif
1.1.1.28  root     2623:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2624:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2625:                        "\t-e\tuse a reduced environment block\n"
                   2626:                        "\t-i\tignore invalid instructions\n"
                   2627:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2628:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2629:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2630:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2631:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2632: #ifdef SUPPORT_XMS
1.1.1.28  root     2633:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2634: #else
1.1.1.28  root     2635:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2636: #endif
                   2637:                );
1.1.1.10  root     2638:                
                   2639:                if(!is_started_from_command_prompt()) {
                   2640:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2641:                        while(!_kbhit()) {
                   2642:                                Sleep(10);
                   2643:                        }
                   2644:                }
1.1.1.20  root     2645: #ifdef _DEBUG
                   2646:                _CrtDumpMemoryLeaks();
                   2647: #endif
1.1       root     2648:                return(EXIT_FAILURE);
                   2649:        }
1.1.1.27  root     2650:        if(convert_cmd_file) {
                   2651:                retval = EXIT_FAILURE;
1.1.1.28  root     2652:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2653:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2654:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2655:                        
1.1.1.28  root     2656:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2657:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2658:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2659:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2660:                        } else {
1.1.1.28  root     2661:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2662:                                if(offset != 0) {
                   2663:                                        UINT8 buffer[14];
                   2664:                                        fseek(fp, offset, SEEK_SET);
                   2665:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2666:                                        memset(path, 0, sizeof(path));
                   2667:                                        fread(path, buffer[9], 1, fp);
                   2668:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2669:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2670:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2671:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2672:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2673:                                } else {
                   2674:                                        // read pe header of msdos.exe
                   2675:                                        UINT8 header[0x400];
                   2676:                                        fseek(fp, 0, SEEK_SET);
                   2677:                                        fread(header, sizeof(header), 1, fp);
                   2678:                                        
                   2679:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2680:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2681:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2682:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2683:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2684:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2685:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2686:                                        
                   2687:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2688:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2689:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2690:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2691:                                        if(dwExtraLastSectionBytes != 0) {
                   2692:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2693:                                                dwLastSectionSize += dwRemain;
                   2694:                                        }
                   2695:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2696:                                        
                   2697:                                        // store msdos.exe
                   2698:                                        fseek(fp, 0, SEEK_SET);
                   2699:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2700:                                                if((data = fgetc(fp)) != EOF) {
                   2701:                                                        fputc(data, fo);
                   2702:                                                } else {
                   2703:                                                        // we should not reach here :-(
                   2704:                                                        fputc(0, fo);
                   2705:                                                }
                   2706:                                        }
                   2707:                                        
                   2708:                                        // store options
                   2709:                                        UINT8 flags = 0;
                   2710:                                        if(stay_busy) {
                   2711:                                                flags |= 0x01;
                   2712:                                        }
                   2713:                                        if(no_windows) {
                   2714:                                                flags |= 0x02;
                   2715:                                        }
                   2716:                                        if(standard_env) {
                   2717:                                                flags |= 0x04;
                   2718:                                        }
                   2719:                                        if(ignore_illegal_insn) {
                   2720:                                                flags |= 0x08;
                   2721:                                        }
                   2722:                                        if(limit_max_memory) {
                   2723:                                                flags |= 0x10;
                   2724:                                        }
1.1.1.29  root     2725:                                        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     2726:                                                flags |= 0x20;
                   2727:                                        }
                   2728:                                        if(support_ems) {
                   2729:                                                flags |= 0x40;
                   2730:                                        }
1.1.1.30  root     2731: #ifdef SUPPORT_XMS
                   2732:                                        if(support_xms) {
                   2733:                                                flags |= 0x80;
                   2734:                                        }
                   2735: #endif
1.1.1.28  root     2736:                                        
                   2737:                                        fputc(flags, fo);
                   2738:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2739:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2740:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2741:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2742:                                        fputc(dos_major_version, fo);
                   2743:                                        fputc(dos_minor_version, fo);
                   2744:                                        fputc(win_major_version, fo);
                   2745:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2746:                                        fputc((code_page >> 0) & 0xff, fo);
                   2747:                                        fputc((code_page >> 8) & 0xff, fo);
                   2748:                                        
                   2749:                                        // store command file info
                   2750:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2751:                                        int name_len = strlen(name);
                   2752:                                        fseek(fs, 0, SEEK_END);
                   2753:                                        long file_size = ftell(fs);
                   2754:                                        
                   2755:                                        fputc(name_len, fo);
                   2756:                                        fputc((file_size >>  0) & 0xff, fo);
                   2757:                                        fputc((file_size >>  8) & 0xff, fo);
                   2758:                                        fputc((file_size >> 16) & 0xff, fo);
                   2759:                                        fputc((file_size >> 24) & 0xff, fo);
                   2760:                                        fwrite(name, name_len, 1, fo);
                   2761:                                        
                   2762:                                        // store command file
                   2763:                                        fseek(fs, 0, SEEK_SET);
                   2764:                                        for(int i = 0; i < file_size; i++) {
                   2765:                                                if((data = fgetc(fs)) != EOF) {
                   2766:                                                        fputc(data, fo);
                   2767:                                                } else {
                   2768:                                                        // we should not reach here :-(
                   2769:                                                        fputc(0, fo);
                   2770:                                                }
                   2771:                                        }
                   2772:                                        
                   2773:                                        // store padding data and update pe header
1.1.1.29  root     2774:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2775:                                        coffHeader->NumberOfSections++;
                   2776:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2777:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2778:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2779:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2780:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2781:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2782:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2783:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2784:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2785:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2786:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2787:                                                        if(i < 2) {
                   2788:                                                                fputc(padding[i & 15], fo);
                   2789:                                                        } else {
                   2790:                                                                fputc(padding[(i - 2) & 15], fo);
                   2791:                                                        }
1.1.1.28  root     2792:                                                }
                   2793:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2794:                                        }
                   2795:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2796:                                        
                   2797:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2798:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2799:                                        if(dwExtraNewSectionBytes != 0) {
                   2800:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2801:                                                dwNewSectionSize += dwRemain;
                   2802:                                        }
                   2803:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2804:                                        
                   2805:                                        fseek(fo, 0, SEEK_SET);
                   2806:                                        fwrite(header, sizeof(header), 1, fo);
                   2807:                                        
                   2808:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2809:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2810:                                }
                   2811:                        }
                   2812:                        if(fp != NULL) {
                   2813:                                fclose(fp);
                   2814:                        }
                   2815:                        if(fs != NULL) {
                   2816:                                fclose(fs);
                   2817:                        }
                   2818:                        if(fo != NULL) {
                   2819:                                fclose(fo);
                   2820:                        }
                   2821:                }
                   2822: #ifdef _DEBUG
                   2823:                _CrtDumpMemoryLeaks();
                   2824: #endif
                   2825:                return(retval);
                   2826:        }
1.1       root     2827:        
1.1.1.54  root     2828:        is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14  root     2829:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2830:        
1.1.1.23  root     2831:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2832:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2833:        CONSOLE_CURSOR_INFO ci;
1.1.1.23  root     2834:        
1.1.1.28  root     2835:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2836:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     2837:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1       root     2838:        
1.1.1.14  root     2839:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2840:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2841:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2842:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2843:                }
                   2844:        }
1.1.1.28  root     2845:        if(get_console_info_success) {
1.1.1.12  root     2846:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2847:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2848:                
1.1.1.28  root     2849:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2850:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2851:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2852:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2853:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2854:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2855:                                scr_width = 80;
                   2856:                                scr_height = 25;
                   2857:                        }
1.1.1.28  root     2858:                        screen_size_changed = true;
1.1.1.14  root     2859:                }
1.1.1.12  root     2860:        } else {
                   2861:                // for a proof (not a console)
                   2862:                scr_width = 80;
                   2863:                scr_height = 25;
                   2864:        }
1.1.1.14  root     2865:        scr_buf_size.X = scr_width;
                   2866:        scr_buf_size.Y = scr_height;
                   2867:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2868:        scr_top = csbi.srWindow.Top;
1.1       root     2869:        cursor_moved = false;
                   2870:        
1.1.1.54  root     2871:        SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
                   2872:        
1.1.1.35  root     2873: #ifdef USE_SERVICE_THREAD
                   2874:        InitializeCriticalSection(&input_crit_sect);
                   2875:        InitializeCriticalSection(&key_buf_crit_sect);
                   2876:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     2877:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     2878: #endif
1.1.1.50  root     2879:        
1.1.1.25  root     2880:        key_buf_char = new FIFO(256);
                   2881:        key_buf_scan = new FIFO(256);
1.1       root     2882:        
                   2883:        hardware_init();
                   2884:        
1.1.1.33  root     2885: #ifdef USE_DEBUGGER
                   2886:        debugger_init();
                   2887: #endif
                   2888:        
1.1.1.9   root     2889:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     2890:                retval = EXIT_FAILURE;
                   2891:        } else {
1.1.1.27  root     2892: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     2893:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   2894: #endif
                   2895:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   2896:                
1.1.1.28  root     2897:                if(screen_size_changed) {
1.1.1.24  root     2898:                        change_console_size(scr_width, scr_height);
                   2899:                }
1.1.1.8   root     2900:                TIMECAPS caps;
                   2901:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   2902:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     2903: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2904:                InitializeCriticalSection(&vram_crit_sect);
                   2905:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   2906: #endif
1.1.1.33  root     2907: #ifdef USE_DEBUGGER
                   2908:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   2909:                // wait until telnet client starts and connects to me
                   2910:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   2911:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   2912:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   2913:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   2914:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   2915:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   2916:                                Sleep(100);
                   2917:                        }
                   2918:                }
                   2919: #endif
1.1       root     2920:                hardware_run();
1.1.1.35  root     2921: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2922:                vram_flush();
                   2923:                DeleteCriticalSection(&vram_crit_sect);
                   2924: #endif
1.1.1.24  root     2925:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     2926:                
1.1.1.24  root     2927:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28  root     2928:                if(get_console_info_success) {
1.1.1.23  root     2929:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2930:                        if(restore_console_on_exit) {
1.1.1.14  root     2931:                                // window can't be bigger than buffer,
                   2932:                                // buffer can't be smaller than window,
                   2933:                                // so make a tiny window,
                   2934:                                // set the required buffer,
                   2935:                                // then set the required window
                   2936:                                SMALL_RECT rect;
                   2937:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   2938:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     2939:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     2940:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     2941:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2942:                        }
1.1.1.14  root     2943:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   2944:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     2945:                }
1.1.1.24  root     2946:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   2947:                
1.1       root     2948:                msdos_finish();
1.1.1.14  root     2949:                
                   2950:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     2951:        }
1.1.1.35  root     2952:        if(temp_file_created) {
                   2953:                DeleteFile(temp_file_path);
                   2954:                temp_file_created = false;
                   2955:        }
1.1.1.10  root     2956:        hardware_finish();
                   2957:        
1.1.1.28  root     2958:        if(key_buf_char != NULL) {
                   2959:                key_buf_char->release();
                   2960:                delete key_buf_char;
                   2961:                key_buf_char = NULL;
                   2962:        }
                   2963:        if(key_buf_scan != NULL) {
                   2964:                key_buf_scan->release();
                   2965:                delete key_buf_scan;
                   2966:                key_buf_scan = NULL;
                   2967:        }
1.1.1.35  root     2968: #ifdef USE_SERVICE_THREAD
                   2969:        DeleteCriticalSection(&input_crit_sect);
                   2970:        DeleteCriticalSection(&key_buf_crit_sect);
                   2971:        DeleteCriticalSection(&putch_crit_sect);
                   2972: #endif
1.1.1.20  root     2973: #ifdef _DEBUG
                   2974:        _CrtDumpMemoryLeaks();
                   2975: #endif
1.1       root     2976:        return(retval);
                   2977: }
                   2978: 
1.1.1.20  root     2979: /* ----------------------------------------------------------------------------
                   2980:        console
                   2981: ---------------------------------------------------------------------------- */
                   2982: 
1.1.1.14  root     2983: void change_console_size(int width, int height)
1.1.1.12  root     2984: {
1.1.1.23  root     2985:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2986:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2987:        SMALL_RECT rect;
                   2988:        COORD co;
                   2989:        
                   2990:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     2991:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   2992:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   2993:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   2994:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2995:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2996:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   2997:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   2998:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2999:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3000:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     3001:                }
                   3002:        }
1.1.1.14  root     3003:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     3004:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     3005:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     3006:                SetConsoleCursorPosition(hStdout, co);
                   3007:                cursor_moved = true;
                   3008:        }
1.1.1.14  root     3009:        
                   3010:        // window can't be bigger than buffer,
                   3011:        // buffer can't be smaller than window,
                   3012:        // so make a tiny window,
                   3013:        // set the required buffer,
                   3014:        // then set the required window
                   3015:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     3016:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     3017:        co.X = width;
                   3018:        co.Y = height;
1.1.1.12  root     3019:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     3020:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3021:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3022:        
                   3023:        scr_width = scr_buf_size.X = width;
                   3024:        scr_height = scr_buf_size.Y = height;
                   3025:        scr_top = 0;
                   3026:        
                   3027:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3028:        
                   3029:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     3030:        text_vram_end_address = text_vram_top_address + regen;
                   3031:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   3032:        
1.1.1.14  root     3033:        if(regen > 0x4000) {
                   3034:                regen = 0x8000;
                   3035:                vram_pages = 1;
                   3036:        } else if(regen > 0x2000) {
                   3037:                regen = 0x4000;
                   3038:                vram_pages = 2;
                   3039:        } else if(regen > 0x1000) {
                   3040:                regen = 0x2000;
                   3041:                vram_pages = 4;
                   3042:        } else {
                   3043:                regen = 0x1000;
                   3044:                vram_pages = 8;
                   3045:        }
1.1.1.15  root     3046:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3047:        *(UINT16 *)(mem + 0x44c) = regen;
                   3048:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3049:        
1.1.1.24  root     3050:        mouse.min_position.x = 0;
                   3051:        mouse.min_position.y = 0;
1.1.1.34  root     3052:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3053:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3054:        
1.1.1.15  root     3055:        restore_console_on_exit = true;
1.1.1.14  root     3056: }
                   3057: 
                   3058: void clear_scr_buffer(WORD attr)
                   3059: {
                   3060:        for(int y = 0; y < scr_height; y++) {
                   3061:                for(int x = 0; x < scr_width; x++) {
                   3062:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3063:                        SCR_BUF(y,x).Attributes = attr;
                   3064:                }
                   3065:        }
1.1.1.12  root     3066: }
                   3067: 
1.1.1.24  root     3068: bool update_console_input()
1.1       root     3069: {
1.1.1.35  root     3070: #ifdef USE_SERVICE_THREAD
                   3071:        EnterCriticalSection(&input_crit_sect);
                   3072: #endif
1.1.1.23  root     3073:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3074:        DWORD dwNumberOfEvents = 0;
1.1       root     3075:        DWORD dwRead;
                   3076:        INPUT_RECORD ir[16];
1.1.1.24  root     3077:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3078:        bool result = false;
1.1       root     3079:        
1.1.1.8   root     3080:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3081:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3082:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3083:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34  root     3084:                                        if(mouse.hidden == 0) {
                   3085:                                                // NOTE: if restore_console_on_exit, console is not scrolled
                   3086:                                                if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3087:                                                        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3088:                                                }
                   3089:                                                // FIXME: character size is always 8x8 ???
                   3090:                                                int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3091:                                                int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3092:                                                
                   3093:                                                if(mouse.position.x != x || mouse.position.y != y) {
                   3094:                                                        mouse.position.x = x;
                   3095:                                                        mouse.position.y = y;
                   3096:                                                        mouse.status |= 1;
1.1.1.43  root     3097:                                                        mouse.status_alt |= 1;
1.1.1.34  root     3098:                                                }
                   3099:                                        }
                   3100:                                        if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3101:                                                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   3102:                                                        static const DWORD bits[] = {
                   3103:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3104:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3105:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3106:                                                        };
                   3107:                                                        bool prev_status = mouse.buttons[i].status;
                   3108:                                                        mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   3109:                                                        
                   3110:                                                        if(!prev_status && mouse.buttons[i].status) {
                   3111:                                                                mouse.buttons[i].pressed_times++;
                   3112:                                                                mouse.buttons[i].pressed_position.x = mouse.position.x;
                   3113:                                                                mouse.buttons[i].pressed_position.y = mouse.position.x;
1.1.1.43  root     3114:                                                                if(i < 2) {
                   3115:                                                                        mouse.status_alt |= 2 << (i * 2);
                   3116:                                                                }
1.1.1.34  root     3117:                                                                mouse.status |= 2 << (i * 2);
                   3118:                                                        } else if(prev_status && !mouse.buttons[i].status) {
                   3119:                                                                mouse.buttons[i].released_times++;
                   3120:                                                                mouse.buttons[i].released_position.x = mouse.position.x;
                   3121:                                                                mouse.buttons[i].released_position.y = mouse.position.x;
1.1.1.43  root     3122:                                                                if(i < 2) {
                   3123:                                                                        mouse.status_alt |= 4 << (i * 2);
                   3124:                                                                }
1.1.1.34  root     3125:                                                                mouse.status |= 4 << (i * 2);
1.1.1.14  root     3126:                                                        }
                   3127:                                                }
                   3128:                                        }
1.1.1.24  root     3129:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3130:                                        // update keyboard flags in bios data area
1.1.1.35  root     3131:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3132:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3133:                                        } else {
1.1.1.35  root     3134:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3135:                                        }
1.1.1.35  root     3136:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3137:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3138:                                        } else {
1.1.1.35  root     3139:                                                mem[0x417] &= ~0x20;
                   3140:                                        }
                   3141:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3142:                                                mem[0x417] |= 0x10;
                   3143:                                        } else {
                   3144:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3145:                                        }
                   3146:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3147:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3148:                                                        mouse.status_alt |= 0x80;
                   3149:                                                }
1.1.1.33  root     3150:                                                mem[0x417] |= 0x08;
                   3151:                                        } else {
                   3152:                                                mem[0x417] &= ~0x08;
                   3153:                                        }
1.1.1.35  root     3154:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3155:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3156:                                                        mouse.status_alt |= 0x40;
                   3157:                                                }
1.1.1.35  root     3158:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3159:                                        } else {
1.1.1.35  root     3160:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3161:                                        }
                   3162:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3163:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3164:                                                        mouse.status_alt |= 0x20;
                   3165:                                                }
1.1.1.33  root     3166:                                                if(!(mem[0x417] & 0x03)) {
                   3167:                                                        mem[0x417] |= 0x02; // left shift
                   3168:                                                }
                   3169:                                        } else {
                   3170:                                                mem[0x417] &= ~0x03;
                   3171:                                        }
1.1.1.35  root     3172:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3173:                                                mem[0x418] |= 0x02;
                   3174:                                        } else {
                   3175:                                                mem[0x418] &= ~0x02;
                   3176:                                        }
                   3177:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3178:                                                mem[0x418] |= 0x01;
                   3179:                                        } else {
                   3180:                                                mem[0x418] &= ~0x01;
                   3181:                                        }
1.1.1.33  root     3182:                                        
1.1.1.28  root     3183:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24  root     3184:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3185:                                        kbd_status |= 1;
                   3186:                                        
                   3187:                                        // update dos key buffer
                   3188:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3189:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3190:                                        UINT8 scn_old = scn;
1.1.1.33  root     3191:                                        
                   3192:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3193:                                                // make
1.1.1.24  root     3194:                                                kbd_data &= 0x7f;
                   3195:                                                
1.1.1.33  root     3196:                                                if(chr == 0x00) {
1.1.1.24  root     3197:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3198:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3199:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3200:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3201:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3202:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3203:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3204:                                                                } else if(scn == 0x35) {
                   3205:                                                                        scn = 0xa4;             // keypad /
                   3206:                                                                }
                   3207:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3208:                                                                if(scn == 0x07) {
                   3209:                                                                        chr = 0x1e;     // Ctrl+^
                   3210:                                                                } else if(scn == 0x0c) {
                   3211:                                                                        chr = 0x1f;     // Ctrl+_
                   3212:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3213:                                                                        static const UINT8 ctrl_map[] = {
                   3214:                                                                                0x95,   // keypad /
                   3215:                                                                                0,
                   3216:                                                                                0x96,   // keypad *
                   3217:                                                                                0, 0, 0,
                   3218:                                                                                0x5e,   // F1
                   3219:                                                                                0x5f,   // F2
                   3220:                                                                                0x60,   // F3
                   3221:                                                                                0x61,   // F4
                   3222:                                                                                0x62,   // F5
                   3223:                                                                                0x63,   // F6
                   3224:                                                                                0x64,   // F7
                   3225:                                                                                0x65,   // F8
                   3226:                                                                                0x66,   // F9
                   3227:                                                                                0x67,   // F10
                   3228:                                                                                0,
                   3229:                                                                                0,
                   3230:                                                                                0x77,   // Home
                   3231:                                                                                0x8d,   // Up
                   3232:                                                                                0x84,   // PgUp
                   3233:                                                                                0x8e,   // keypad -
                   3234:                                                                                0x73,   // Left
                   3235:                                                                                0x8f,   // keypad center
                   3236:                                                                                0x74,   // Right
                   3237:                                                                                0x90,   // keyapd +
                   3238:                                                                                0x75,   // End
                   3239:                                                                                0x91,   // Down
                   3240:                                                                                0x76,   // PgDn
                   3241:                                                                                0x92,   // Insert
                   3242:                                                                                0x93,   // Delete
                   3243:                                                                                0, 0, 0,
                   3244:                                                                                0x89,   // F11
                   3245:                                                                                0x8a,   // F12
                   3246:                                                                        };
                   3247:                                                                        scn = ctrl_map[scn - 0x35];
                   3248:                                                                }
                   3249:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3250:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3251:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3252:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3253:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3254:                                                                }
                   3255:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3256:                                                                scn += 0x85 - 0x57;
                   3257:                                                        }
                   3258:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3259:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3260:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3261: #ifdef USE_SERVICE_THREAD
                   3262:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3263: #endif
1.1.1.32  root     3264:                                                                        if(chr == 0) {
1.1.1.51  root     3265:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3266:                                                                        }
1.1.1.51  root     3267:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3268: #ifdef USE_SERVICE_THREAD
                   3269:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3270: #endif
1.1.1.24  root     3271:                                                                }
                   3272:                                                        }
                   3273:                                                } else {
                   3274:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3275:                                                                chr = 0;
                   3276:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3277:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3278:                                                                }
                   3279:                                                        }
1.1.1.32  root     3280:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3281: #ifdef USE_SERVICE_THREAD
                   3282:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3283: #endif
1.1.1.51  root     3284:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3285: #ifdef USE_SERVICE_THREAD
                   3286:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3287: #endif
1.1.1.32  root     3288:                                                        }
1.1.1.24  root     3289:                                                }
1.1.1.33  root     3290:                                        } else if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3291:                                                // ctrl-break, ctrl-c
                   3292:                                                if(scn == 0x46) {
                   3293:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3294: #ifdef USE_SERVICE_THREAD
                   3295:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3296: #endif
1.1.1.51  root     3297:                                                                pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3298: #ifdef USE_SERVICE_THREAD
                   3299:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3300: #endif
1.1.1.33  root     3301:                                                        }
                   3302:                                                        ctrl_break_pressed = true;
                   3303:                                                        mem[0x471] = 0x80;
                   3304:                                                        raise_int_1bh = true;
                   3305:                                                } else {
                   3306:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3307: #ifdef USE_SERVICE_THREAD
                   3308:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3309: #endif
1.1.1.51  root     3310:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3311: #ifdef USE_SERVICE_THREAD
                   3312:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3313: #endif
1.1.1.33  root     3314:                                                        }
                   3315:                                                        ctrl_c_pressed = (scn == 0x2e);
                   3316:                                                }
                   3317:                                        } else {
                   3318:                                                // break
                   3319:                                                kbd_data |= 0x80;
1.1       root     3320:                                        }
1.1.1.24  root     3321:                                        result = key_changed = true;
1.1.1.36  root     3322:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3323:                                        cursor_moved = true;
1.1       root     3324:                                }
                   3325:                        }
                   3326:                }
                   3327:        }
1.1.1.35  root     3328: #ifdef USE_SERVICE_THREAD
                   3329:        LeaveCriticalSection(&input_crit_sect);
                   3330: #endif
1.1.1.24  root     3331:        return(result);
1.1.1.8   root     3332: }
                   3333: 
1.1.1.14  root     3334: bool update_key_buffer()
1.1.1.8   root     3335: {
1.1.1.35  root     3336:        if(update_console_input()) {
                   3337:                return(true);
                   3338:        }
                   3339:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3340: #ifdef USE_SERVICE_THREAD
                   3341:                EnterCriticalSection(&key_buf_crit_sect);
                   3342: #endif
1.1.1.55! root     3343:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     3344: #ifdef USE_SERVICE_THREAD
                   3345:                LeaveCriticalSection(&key_buf_crit_sect);
                   3346: #endif
                   3347:                if(!empty) return(true);
                   3348:        }
                   3349:        return(false);
1.1.1.8   root     3350: }
                   3351: 
1.1.1.20  root     3352: /* ----------------------------------------------------------------------------
                   3353:        MS-DOS virtual machine
                   3354: ---------------------------------------------------------------------------- */
                   3355: 
1.1.1.32  root     3356: static const struct {
1.1.1.33  root     3357:        char *name;
                   3358:        DWORD lcid;
                   3359:        char *std;
                   3360:        char *dlt;
                   3361: } tz_table[] = {
                   3362:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3363: //     0       GMT             Greenwich Mean Time             GMT0
                   3364:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3365:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3366:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3367:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3368: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3369:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3370:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3371: //     3       BST             Brazil Standard Time            BST3
                   3372:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3373:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3374:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3375: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3376:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3377: //     3       GST             Greenland Standard Time         GST3
                   3378:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3379: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3380:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3381: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3382:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3383: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3384:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3385:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3386: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3387:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3388:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3389:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3390: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3391:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3392: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3393:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3394: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3395:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3396: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3397:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3398:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3399:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3400: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3401:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3402: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3403:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3404:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3405:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3406: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3407:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3408:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3409: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3410: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3411:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3412: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3413:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3414:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3415: //     11      SST             Samoa Standard Time             SST11
                   3416:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3417: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3418:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3419: //     -10     GST             Guam Standard Time              GST-10
                   3420:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3421: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3422:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3423:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3424:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3425: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3426:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3427:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3428: //     -9      JST             Japan Standard Time             JST-9
                   3429:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3430: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3431:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3432:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3433: //     -8      HKT             Hong Kong Time                  HKT-8
                   3434:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3435: //     -8      CCT             China Coast Time                CCT-8
                   3436:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3437:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3438: //     -8      SST             Singapore Standard Time         SST-8
                   3439:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3440: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3441:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3442:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3443: //     -7:30   JT              Java Standard Time              JST-7:30
                   3444: //     -7      NST             North Sumatra Time              NST-7
                   3445:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3446: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3447:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3448: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3449:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3450: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3451:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3452:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3453: //     -2      EET             Eastern Europe Time             EET-2
                   3454:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3455:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3456:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3457:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3458: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3459:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3460: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3461: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3462: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3463: //     -1      CET     CES     Central European Time           CET-1CES
                   3464:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3465:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3466:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3467:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3468: //     -1      WAT             West African Time               WAT-1
                   3469:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3470:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3471: //     0       UTC             Universal Coordinated Time      UTC0
                   3472:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3473:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3474:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3475:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3476:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3477:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3478: };
                   3479: 
1.1.1.53  root     3480: // FIXME: consider to build on non-Japanese environment :-(
                   3481: // message_japanese string must be in shift-jis
                   3482: 
1.1.1.33  root     3483: static const struct {
1.1.1.32  root     3484:        UINT16 code;
                   3485:        char *message_english;
                   3486:        char *message_japanese;
                   3487: } standard_error_table[] = {
                   3488:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3489:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3490:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3491:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3492:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3493:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3494:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3495:        {0x08,  "Insufficient memory", "������������܂���."},
                   3496:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3497:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3498:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3499:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3500:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3501:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3502:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3503:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3504:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3505:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3506:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3507:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3508:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3509:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3510:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3511:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3512:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3513:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3514:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3515:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3516:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3517:        {0x1F,  "General failure", "�G���[�ł�."},
                   3518:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3519:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3520:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3521:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3522:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3523:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3524:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3525:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3526: /*
                   3527:        {0x32,  "Network request not supported", NULL},
                   3528:        {0x33,  "Remote computer not listening", NULL},
                   3529:        {0x34,  "Duplicate name on network", NULL},
                   3530:        {0x35,  "Network name not found", NULL},
                   3531:        {0x36,  "Network busy", NULL},
                   3532:        {0x37,  "Network device no longer exists", NULL},
                   3533:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3534:        {0x39,  "Network adapter hardware error", NULL},
                   3535:        {0x3A,  "Incorrect response from network", NULL},
                   3536:        {0x3B,  "Unexpected network error", NULL},
                   3537:        {0x3C,  "Incompatible remote adapter", NULL},
                   3538:        {0x3D,  "Print queue full", NULL},
                   3539:        {0x3E,  "Queue not full", NULL},
                   3540:        {0x3F,  "Not enough space to print file", NULL},
                   3541:        {0x40,  "Network name was deleted", NULL},
                   3542:        {0x41,  "Network: Access denied", NULL},
                   3543:        {0x42,  "Network device type incorrect", NULL},
                   3544:        {0x43,  "Network name not found", NULL},
                   3545:        {0x44,  "Network name limit exceeded", NULL},
                   3546:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3547:        {0x46,  "Temporarily paused", NULL},
                   3548:        {0x47,  "Network request not accepted", NULL},
                   3549:        {0x48,  "Network print/disk redirection paused", NULL},
                   3550:        {0x49,  "Network software not installed", NULL},
                   3551:        {0x4A,  "Unexpected adapter close", NULL},
                   3552: */
                   3553:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3554:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3555:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3556:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3557:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3558:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3559:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3560:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3561:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3562:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53  root     3563: #ifdef SUPPORT_MSCDEX
1.1.1.32  root     3564:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3565:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3566:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3567:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3568:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
1.1.1.53  root     3569: #endif
1.1.1.32  root     3570:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3571:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3572:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3573:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3574:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3575:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3576: };
                   3577: 
                   3578: static const struct {
                   3579:        UINT16 code;
                   3580:        char *message_english;
                   3581:        char *message_japanese;
                   3582: } param_error_table[] = {
                   3583:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3584:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3585:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3586:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3587:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3588:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3589:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3590:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3591:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3592:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3593:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3594: };
                   3595: 
                   3596: static const struct {
                   3597:        UINT16 code;
                   3598:        char *message_english;
                   3599:        char *message_japanese;
                   3600: } critical_error_table[] = {
                   3601:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3602:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3603:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3604:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3605:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3606:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3607:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3608:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3609:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3610:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3611:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3612:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3613:        {0x0C,  "General failure", "�G���[�ł�."},
                   3614:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3615:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3616:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3617:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3618:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3619:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3620:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3621:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3622:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3623: };
                   3624: 
1.1.1.20  root     3625: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3626: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3627: void msdos_putch(UINT8 data);
1.1.1.50  root     3628: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3629: #ifdef USE_SERVICE_THREAD
                   3630: void msdos_putch_tmp(UINT8 data);
                   3631: #endif
1.1.1.45  root     3632: const char *msdos_short_path(const char *path);
1.1.1.44  root     3633: bool msdos_is_valid_drive(int drv);
                   3634: bool msdos_is_removable_drive(int drv);
                   3635: bool msdos_is_cdrom_drive(int drv);
                   3636: bool msdos_is_remote_drive(int drv);
                   3637: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3638: 
1.1       root     3639: // process info
                   3640: 
                   3641: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3642: {
                   3643:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3644:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3645:                        memset(&process[i], 0, sizeof(process_t));
                   3646:                        process[i].psp = psp_seg;
                   3647:                        return(&process[i]);
                   3648:                }
                   3649:        }
                   3650:        fatalerror("too many processes\n");
                   3651:        return(NULL);
                   3652: }
                   3653: 
1.1.1.52  root     3654: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3655: {
                   3656:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3657:                if(process[i].psp == psp_seg) {
                   3658:                        return(&process[i]);
                   3659:                }
                   3660:        }
1.1.1.33  root     3661:        if(show_error) {
                   3662:                fatalerror("invalid psp address\n");
                   3663:        }
1.1       root     3664:        return(NULL);
                   3665: }
                   3666: 
1.1.1.23  root     3667: void msdos_sda_update(int psp_seg)
                   3668: {
                   3669:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3670:        
                   3671:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3672:                if(process[i].psp == psp_seg) {
                   3673:                        sda->switchar = process[i].switchar;
                   3674:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3675:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3676:                        sda->current_psp = process[i].psp;
                   3677:                        break;
                   3678:                }
                   3679:        }
                   3680:        sda->malloc_strategy = malloc_strategy;
                   3681:        sda->return_code = retval;
                   3682:        sda->current_drive = _getdrive();
                   3683: }
                   3684: 
1.1.1.13  root     3685: // dta info
                   3686: 
                   3687: void msdos_dta_info_init()
                   3688: {
1.1.1.14  root     3689:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3690:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3691:        }
                   3692: }
                   3693: 
                   3694: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3695: {
                   3696:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3697:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3698:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3699:                        if(free_dta == NULL) {
1.1.1.13  root     3700:                                free_dta = &dtalist[i];
                   3701:                        }
1.1.1.14  root     3702:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3703:                        return(&dtalist[i]);
                   3704:                }
                   3705:        }
1.1.1.14  root     3706:        if(free_dta) {
1.1.1.13  root     3707:                free_dta->psp = psp_seg;
                   3708:                free_dta->dta = dta_laddr;
                   3709:                return(free_dta);
                   3710:        }
                   3711:        fatalerror("too many dta\n");
                   3712:        return(NULL);
                   3713: }
                   3714: 
                   3715: void msdos_dta_info_free(UINT16 psp_seg)
                   3716: {
1.1.1.14  root     3717:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3718:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3719:                        FindClose(dtalist[i].find_handle);
                   3720:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3721:                }
                   3722:        }
                   3723: }
                   3724: 
1.1       root     3725: void msdos_cds_update(int drv)
                   3726: {
1.1.1.44  root     3727:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     3728:        
1.1.1.44  root     3729:        memset(cds, 0, 88);
                   3730:        
                   3731:        if(msdos_is_valid_drive(drv)) {
                   3732:                char path[MAX_PATH];
                   3733:                if(msdos_is_remote_drive(drv)) {
                   3734:                        cds->drive_attrib = 0xc000;     // network drive
                   3735:                } else if(msdos_is_subst_drive(drv)) {
                   3736:                        cds->drive_attrib = 0x5000;     // subst drive
                   3737:                } else {
                   3738:                        cds->drive_attrib = 0x4000;     // physical drive
                   3739:                }
                   3740:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   3741:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   3742:                }
                   3743:        }
                   3744:        if(cds->path_name[0] == '\0') {
                   3745:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3746:        }
                   3747:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   3748:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   3749:        cds->word_1 = cds->word_2 = 0xffff;
                   3750:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   3751:        cds->bs_offset = 2;
                   3752: }
                   3753: 
1.1.1.45  root     3754: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     3755: {
                   3756:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   3757:        
                   3758:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     3759: }
                   3760: 
1.1.1.17  root     3761: // nls information tables
                   3762: 
                   3763: // uppercase table (func 6502h)
                   3764: void msdos_upper_table_update()
                   3765: {
                   3766:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3767:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3768:                UINT8 c[4];
1.1.1.33  root     3769:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3770:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3771:                c[0] = 0x80 + i;
                   3772:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3773:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3774:        }
                   3775: }
                   3776: 
1.1.1.23  root     3777: // lowercase table (func 6503h)
                   3778: void msdos_lower_table_update()
                   3779: {
                   3780:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3781:        for(unsigned i = 0; i < 0x80; ++i) {
                   3782:                UINT8 c[4];
1.1.1.33  root     3783:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3784:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3785:                c[0] = 0x80 + i;
                   3786:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3787:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3788:        }
                   3789: }
                   3790: 
1.1.1.17  root     3791: // filename uppercase table (func 6504h)
                   3792: void msdos_filename_upper_table_init()
                   3793: {
                   3794:        // depended on (file)system, not on active codepage
                   3795:        // temporary solution: just filling data
                   3796:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3797:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3798:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3799:        }
                   3800: }
                   3801: 
                   3802: // filaname terminator table (func 6505h)
                   3803: void msdos_filename_terminator_table_init()
                   3804: {
                   3805:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3806:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3807:        
                   3808:        data[2] = 1;            // marker? (permissible character value)
                   3809:        data[3] = 0x00;         // 00h...FFh
                   3810:        data[4] = 0xff;
                   3811:        data[5] = 0;            // marker? (excluded character)
                   3812:        data[6] = 0x00;         // 00h...20h
                   3813:        data[7] = 0x20;
                   3814:        data[8] = 2;            // marker? (illegal characters for filename)
                   3815:        data[9] = (UINT8)strlen(illegal_chars);
                   3816:        memcpy(data + 10, illegal_chars, data[9]);
                   3817:        
                   3818:        // total length
                   3819:        *(UINT16 *)data = (10 - 2) + data[9];
                   3820: }
                   3821: 
                   3822: // collating table (func 6506h)
                   3823: void msdos_collating_table_update()
                   3824: {
                   3825:        // temporary solution: just filling data
                   3826:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3827:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3828:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3829:        }
                   3830: }
                   3831: 
1.1       root     3832: // dbcs
                   3833: 
                   3834: void msdos_dbcs_table_update()
                   3835: {
                   3836:        UINT8 dbcs_data[DBCS_SIZE];
                   3837:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3838:        
                   3839:        CPINFO info;
                   3840:        GetCPInfo(active_code_page, &info);
                   3841:        
                   3842:        if(info.MaxCharSize != 1) {
                   3843:                for(int i = 0;; i += 2) {
                   3844:                        UINT8 lo = info.LeadByte[i + 0];
                   3845:                        UINT8 hi = info.LeadByte[i + 1];
                   3846:                        dbcs_data[2 + i + 0] = lo;
                   3847:                        dbcs_data[2 + i + 1] = hi;
                   3848:                        if(lo == 0 && hi == 0) {
                   3849:                                dbcs_data[0] = i + 2;
                   3850:                                break;
                   3851:                        }
                   3852:                }
                   3853:        } else {
                   3854:                dbcs_data[0] = 2;       // ???
                   3855:        }
                   3856:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   3857: }
                   3858: 
1.1.1.17  root     3859: void msdos_dbcs_table_finish()
                   3860: {
1.1.1.32  root     3861:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     3862:                _setmbcp(system_code_page);
                   3863:        }
1.1.1.32  root     3864:        if(console_code_page != GetConsoleCP()) {
                   3865:                SetConsoleCP(console_code_page);
                   3866:                SetConsoleOutputCP(console_code_page);
                   3867:        }
1.1.1.17  root     3868: }
                   3869: 
                   3870: void msdos_nls_tables_init()
1.1       root     3871: {
1.1.1.32  root     3872:        active_code_page = console_code_page = GetConsoleCP();
                   3873:        system_code_page = _getmbcp();
                   3874:        
                   3875:        if(active_code_page != system_code_page) {
                   3876:                if(_setmbcp(active_code_page) != 0) {
                   3877:                        active_code_page = system_code_page;
                   3878:                }
                   3879:        }
                   3880:        
1.1.1.17  root     3881:        msdos_upper_table_update();
1.1.1.23  root     3882:        msdos_lower_table_update();
1.1.1.17  root     3883:        msdos_filename_terminator_table_init();
                   3884:        msdos_filename_upper_table_init();
                   3885:        msdos_collating_table_update();
1.1       root     3886:        msdos_dbcs_table_update();
                   3887: }
                   3888: 
1.1.1.17  root     3889: void msdos_nls_tables_update()
1.1       root     3890: {
1.1.1.17  root     3891:        msdos_dbcs_table_update();
                   3892:        msdos_upper_table_update();
1.1.1.23  root     3893:        msdos_lower_table_update();
                   3894: //     msdos_collating_table_update();
1.1       root     3895: }
                   3896: 
                   3897: int msdos_lead_byte_check(UINT8 code)
                   3898: {
                   3899:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   3900:        
                   3901:        for(int i = 0;; i += 2) {
                   3902:                UINT8 lo = dbcs_table[i + 0];
                   3903:                UINT8 hi = dbcs_table[i + 1];
                   3904:                if(lo == 0 && hi == 0) {
                   3905:                        break;
                   3906:                }
                   3907:                if(lo <= code && code <= hi) {
                   3908:                        return(1);
                   3909:                }
                   3910:        }
                   3911:        return(0);
                   3912: }
                   3913: 
1.1.1.20  root     3914: int msdos_ctrl_code_check(UINT8 code)
                   3915: {
1.1.1.22  root     3916:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     3917: }
                   3918: 
1.1.1.36  root     3919: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   3920: {
                   3921:        int is_kanji_1st = 0;
                   3922:        int is_kanji_2nd = 0;
                   3923:        
                   3924:        for(int p = 0;; p++) {
                   3925:                if(is_kanji_1st) {
                   3926:                        is_kanji_1st = 0;
                   3927:                        is_kanji_2nd = 1;
                   3928:                } else if(msdos_lead_byte_check(buf[p])) {
                   3929:                        is_kanji_1st = 1;
                   3930:                }
                   3931:                if(p == n) {
                   3932:                        return(is_kanji_2nd);
                   3933:                }
                   3934:                is_kanji_2nd = 0;
                   3935:        }
                   3936: }
                   3937: 
1.1       root     3938: // file control
                   3939: 
1.1.1.45  root     3940: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     3941: {
                   3942:        static char tmp[MAX_PATH];
                   3943:        
                   3944:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     3945:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     3946:                memcpy(tmp, path + 1, strlen(path) - 2);
                   3947:        } else {
                   3948:                strcpy(tmp, path);
                   3949:        }
                   3950:        return(tmp);
                   3951: }
                   3952: 
1.1.1.45  root     3953: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     3954: {
                   3955:        static char tmp[MAX_PATH];
                   3956:        
                   3957:        strcpy(tmp, path);
1.1.1.45  root     3958:        
                   3959:        // for example "C:\" case, the end separator should not be removed
                   3960:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   3961:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     3962:        }
                   3963:        return(tmp);
                   3964: }
                   3965: 
1.1.1.45  root     3966: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     3967: {
                   3968:        static char tmp[MAX_PATH];
1.1.1.45  root     3969:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     3970:        
                   3971:        if(strlen(tmp_dir) == 0) {
                   3972:                strcpy(tmp, file);
                   3973:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   3974:                sprintf(tmp, "%s%s", tmp_dir, file);
                   3975:        } else {
                   3976:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   3977:        }
                   3978:        return(tmp);
                   3979: }
                   3980: 
1.1.1.45  root     3981: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     3982: {
                   3983:        static char tmp[MAX_PATH];
                   3984:        
                   3985:        if(lfn) {
                   3986:                strcpy(tmp, path);
                   3987:        } else {
                   3988:                // remove space in the path
1.1.1.45  root     3989:                const char *src = path;
                   3990:                char *dst = tmp;
1.1       root     3991:                
                   3992:                while(*src != '\0') {
                   3993:                        if(msdos_lead_byte_check(*src)) {
                   3994:                                *dst++ = *src++;
                   3995:                                *dst++ = *src++;
                   3996:                        } else if(*src != ' ') {
                   3997:                                *dst++ = *src++;
                   3998:                        } else {
                   3999:                                src++;  // skip space
                   4000:                        }
                   4001:                }
                   4002:                *dst = '\0';
                   4003:        }
1.1.1.14  root     4004:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   4005:                // redirect C:\COMMAND.COM to comspec_path
                   4006:                strcpy(tmp, comspec_path);
                   4007:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   4008:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   4009:                static int root_drive_protected = -1;
                   4010:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   4011:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   4012:                
                   4013:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   4014:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   4015:                        strcpy(name, name_temp);
                   4016:                        name_temp[0] = '\0';
                   4017:                        
                   4018:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   4019:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   4020:                                if(root_drive_protected == -1) {
                   4021:                                        FILE *fp = NULL;
                   4022:                                        
                   4023:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   4024:                                        root_drive_protected = 1;
                   4025:                                        try {
                   4026:                                                if((fp = fopen(temp, "w")) != NULL) {
                   4027:                                                        if(fprintf(fp, "TEST") == 4) {
                   4028:                                                                root_drive_protected = 0;
                   4029:                                                        }
                   4030:                                                }
                   4031:                                        } catch(...) {
                   4032:                                        }
                   4033:                                        if(fp != NULL) {
                   4034:                                                fclose(fp);
                   4035:                                        }
                   4036:                                        if(_access(temp, 0) == 0) {
                   4037:                                                remove(temp);
                   4038:                                        }
                   4039:                                }
                   4040:                                if(root_drive_protected == 1) {
                   4041:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   4042:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   4043:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4044:                                        }
                   4045:                                }
                   4046:                        }
                   4047:                }
                   4048:        }
1.1       root     4049:        return(tmp);
                   4050: }
                   4051: 
1.1.1.45  root     4052: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4053: {
1.1.1.32  root     4054:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4055:        static char env_path[ENV_SIZE];
                   4056:        char tmp[ENV_SIZE], *token;
                   4057:        
                   4058:        memset(env_path, 0, sizeof(env_path));
                   4059:        strcpy(tmp, src);
                   4060:        token = my_strtok(tmp, ";");
                   4061:        
                   4062:        while(token != NULL) {
                   4063:                if(token[0] != '\0') {
1.1.1.45  root     4064:                        const char *path = msdos_remove_double_quote(token);
                   4065:                        char short_path[MAX_PATH];
1.1.1.32  root     4066:                        if(path != NULL && strlen(path) != 0) {
                   4067:                                if(env_path[0] != '\0') {
                   4068:                                        strcat(env_path, ";");
                   4069:                                }
1.1.1.28  root     4070:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4071:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4072:                                } else {
                   4073:                                        my_strupr(short_path);
1.1.1.32  root     4074:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4075:                                }
                   4076:                        }
                   4077:                }
                   4078:                token = my_strtok(NULL, ";");
                   4079:        }
                   4080:        return(env_path);
                   4081: }
                   4082: 
1.1.1.45  root     4083: bool match(const char *text, const char *pattern)
1.1       root     4084: {
1.1.1.24  root     4085:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4086:        switch(*pattern) {
1.1       root     4087:        case '\0':
                   4088:                return !*text;
                   4089:        case '*':
1.1.1.14  root     4090:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4091:        case '?':
                   4092:                return *text && match(text + 1, pattern + 1);
                   4093:        default:
                   4094:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4095:        }
                   4096: }
                   4097: 
1.1.1.45  root     4098: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4099: {
1.1.1.45  root     4100:        const char *p = NULL;
1.1       root     4101:        
1.1.1.14  root     4102:        if(!*volume) {
                   4103:                return false;
                   4104:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4105:                return msdos_match_volume_label(p + 1, volume);
                   4106:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4107:                return msdos_match_volume_label(p + 1, volume);
                   4108:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4109:                char tmp[MAX_PATH];
                   4110:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4111:                return match(volume, tmp);
1.1       root     4112:        } else {
                   4113:                return match(volume, path);
                   4114:        }
                   4115: }
                   4116: 
1.1.1.45  root     4117: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4118: {
                   4119:        static char tmp[MAX_PATH];
                   4120:        char name[9], ext[4];
                   4121:        
                   4122:        memset(name, 0, sizeof(name));
                   4123:        memcpy(name, fcb->file_name, 8);
                   4124:        strcpy(name, msdos_trimmed_path(name, 0));
                   4125:        
                   4126:        memset(ext, 0, sizeof(ext));
                   4127:        memcpy(ext, fcb->file_name + 8, 3);
                   4128:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4129:        
                   4130:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4131:                strcpy(name, "*");
                   4132:        }
                   4133:        if(ext[0] == '\0') {
                   4134:                strcpy(tmp, name);
                   4135:        } else {
                   4136:                if(strcmp(ext, "???") == 0) {
                   4137:                        strcpy(ext, "*");
                   4138:                }
                   4139:                sprintf(tmp, "%s.%s", name, ext);
                   4140:        }
                   4141:        return(tmp);
                   4142: }
                   4143: 
1.1.1.45  root     4144: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4145: {
                   4146:        char *ext = my_strchr(path, '.');
                   4147:        
                   4148:        memset(fcb->file_name, 0x20, 8 + 3);
                   4149:        if(ext != NULL && path[0] != '.') {
                   4150:                *ext = '\0';
                   4151:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4152:        }
                   4153:        memcpy(fcb->file_name, path, strlen(path));
                   4154: }
                   4155: 
1.1.1.45  root     4156: const char *msdos_short_path(const char *path)
1.1       root     4157: {
                   4158:        static char tmp[MAX_PATH];
                   4159:        
1.1.1.24  root     4160:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4161:                strcpy(tmp, path);
                   4162:        }
1.1       root     4163:        my_strupr(tmp);
                   4164:        return(tmp);
                   4165: }
                   4166: 
1.1.1.45  root     4167: const char *msdos_short_name(WIN32_FIND_DATA *fd)
1.1.1.13  root     4168: {
                   4169:        static char tmp[MAX_PATH];
1.1.1.45  root     4170:        
1.1.1.14  root     4171:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4172:                strcpy(tmp, fd->cAlternateFileName);
                   4173:        } else {
                   4174:                strcpy(tmp, fd->cFileName);
                   4175:        }
                   4176:        my_strupr(tmp);
                   4177:        return(tmp);
                   4178: }
                   4179: 
1.1.1.45  root     4180: const char *msdos_short_full_path(const char *path)
1.1       root     4181: {
                   4182:        static char tmp[MAX_PATH];
                   4183:        char full[MAX_PATH], *name;
                   4184:        
1.1.1.14  root     4185:        // Full works with non-existent files, but Short does not
1.1       root     4186:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4187:        *tmp = '\0';
                   4188:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4189:                name[-1] = '\0';
                   4190:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4191:                if(len == 0) {
                   4192:                        strcpy(tmp, full);
                   4193:                } else {
                   4194:                        tmp[len++] = '\\';
                   4195:                        strcpy(tmp + len, name);
                   4196:                }
                   4197:        }
1.1       root     4198:        my_strupr(tmp);
                   4199:        return(tmp);
                   4200: }
                   4201: 
1.1.1.45  root     4202: const char *msdos_short_full_dir(const char *path)
1.1       root     4203: {
                   4204:        static char tmp[MAX_PATH];
                   4205:        char full[MAX_PATH], *name;
                   4206:        
                   4207:        GetFullPathName(path, MAX_PATH, full, &name);
                   4208:        name[-1] = '\0';
1.1.1.24  root     4209:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4210:                strcpy(tmp, full);
                   4211:        }
1.1       root     4212:        my_strupr(tmp);
                   4213:        return(tmp);
                   4214: }
                   4215: 
1.1.1.45  root     4216: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4217: {
1.1.1.45  root     4218:        static char trimmed[MAX_PATH];
                   4219:        
                   4220:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4221: #if 0
                   4222:        // I have forgotten the reason of this routine... :-(
1.1       root     4223:        if(_access(trimmed, 0) != 0) {
                   4224:                process_t *process = msdos_process_info_get(current_psp);
                   4225:                static char tmp[MAX_PATH];
                   4226:                
                   4227:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4228:                if(_access(tmp, 0) == 0) {
                   4229:                        return(tmp);
                   4230:                }
                   4231:        }
1.1.1.14  root     4232: #endif
1.1       root     4233:        return(trimmed);
                   4234: }
                   4235: 
1.1.1.45  root     4236: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4237: {
                   4238:        char full[MAX_PATH], *name;
                   4239:        
1.1.1.24  root     4240:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4241:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4242:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4243:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4244:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4245:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4246:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4247:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4248:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4249:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4250:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4251:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4252:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4253:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4254:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4255:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4256:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4257:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4258:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4259:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4260:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4261:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4262:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4263:                        return(true);
                   4264:                } else if(name != NULL) {
                   4265:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4266:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4267:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4268: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4269:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4270:                                return(true);
                   4271:                        }
                   4272:                }
1.1.1.24  root     4273:        }
                   4274:        return(false);
1.1.1.11  root     4275: }
                   4276: 
1.1.1.45  root     4277: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4278: {
1.1.1.14  root     4279:        char full[MAX_PATH], *name;
1.1.1.8   root     4280:        
1.1.1.24  root     4281:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4282:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4283:        }
                   4284:        return(false);
                   4285: }
                   4286: 
1.1.1.45  root     4287: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4288: {
                   4289:        char full[MAX_PATH], *name;
                   4290:        
                   4291:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4292:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4293:                        return(1);
                   4294:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4295:                        return(2);
                   4296:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4297:                        return(3);
                   4298:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4299:                        return(4);
1.1.1.24  root     4300:                }
                   4301:        }
1.1.1.29  root     4302:        return(0);
                   4303: }
                   4304: 
1.1.1.45  root     4305: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4306: {
                   4307:        // 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     4308:        const char *p = NULL;
1.1.1.37  root     4309:        
                   4310:        if((p = strstr(path, ":")) != NULL) {
                   4311:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4312:                
                   4313:                // baud rate
                   4314:                int baud = max(110, min(9600, atoi(p + 1)));
                   4315:                UINT16 divisor = 115200 / baud;
                   4316:                
                   4317:                if((p = strstr(p + 1, ",")) != NULL) {
                   4318:                        // parity
                   4319:                        if(p[1] == 'N' || p[1] == 'n') {
                   4320:                                selector = (selector & ~0x38) | 0x00;
                   4321:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4322:                                selector = (selector & ~0x38) | 0x08;
                   4323:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4324:                                selector = (selector & ~0x38) | 0x18;
                   4325:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4326:                                selector = (selector & ~0x38) | 0x28;
                   4327:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4328:                                selector = (selector & ~0x38) | 0x38;
                   4329:                        }
                   4330:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4331:                                // word length
                   4332:                                if(p[1] == '8') {
                   4333:                                        selector = (selector & ~0x03) | 0x03;
                   4334:                                } else if(p[1] == '7') {
                   4335:                                        selector = (selector & ~0x03) | 0x02;
                   4336:                                } else if(p[1] == '6') {
                   4337:                                        selector = (selector & ~0x03) | 0x01;
                   4338:                                } else if(p[1] == '5') {
                   4339:                                        selector = (selector & ~0x03) | 0x00;
                   4340:                                }
                   4341:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4342:                                        // stop bits
                   4343:                                        float bits = atof(p + 1);
                   4344:                                        if(bits > 1.0F) {
                   4345:                                                selector |= 0x04;
                   4346:                                        } else {
                   4347:                                                selector &= ~0x04;
                   4348:                                        }
                   4349:                                }
                   4350:                        }
                   4351:                }
                   4352:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4353:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4354:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4355:                sio_write(sio_port - 1, 3, selector);
                   4356:        }
                   4357: }
                   4358: 
1.1.1.45  root     4359: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4360: {
                   4361:        char full[MAX_PATH], *name;
                   4362:        
                   4363:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4364:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4365:                        return(1);
                   4366:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4367:                        return(1);
                   4368:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4369:                        return(2);
                   4370:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4371:                        return(3);
                   4372:                }
                   4373:        }
                   4374:        return(0);
                   4375: }
                   4376: 
1.1.1.44  root     4377: bool msdos_is_valid_drive(int drv)
                   4378: {
                   4379:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4380: }
                   4381: 
                   4382: bool msdos_is_removable_drive(int drv)
                   4383: {
                   4384:        char volume[] = "A:\\";
                   4385:        
                   4386:        volume[0] = 'A' + drv;
                   4387:        
                   4388:        return(GetDriveType(volume) == DRIVE_REMOVABLE);
                   4389: }
                   4390: 
                   4391: bool msdos_is_cdrom_drive(int drv)
                   4392: {
                   4393:        char volume[] = "A:\\";
                   4394:        
                   4395:        volume[0] = 'A' + drv;
                   4396:        
                   4397:        return(GetDriveType(volume) == DRIVE_CDROM);
                   4398: }
                   4399: 
                   4400: bool msdos_is_remote_drive(int drv)
                   4401: {
                   4402:        char volume[] = "A:\\";
                   4403:        
                   4404:        volume[0] = 'A' + drv;
                   4405:        
                   4406:        return(GetDriveType(volume) == DRIVE_REMOTE);
                   4407: }
                   4408: 
                   4409: bool msdos_is_subst_drive(int drv)
                   4410: {
                   4411:        char device[] = "A:", path[MAX_PATH];
                   4412:        
                   4413:        device[0] = 'A' + drv;
                   4414:        
                   4415:        if(QueryDosDevice(device, path, MAX_PATH)) {
                   4416:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4417:                        return(true);
                   4418:                }
                   4419:        }
                   4420:        return(false);
                   4421: }
                   4422: 
1.1.1.45  root     4423: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4424: {
                   4425:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4426:        WIN32_FIND_DATA FindData;
                   4427:        HANDLE hFind;
                   4428:        
                   4429:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4430:                FindClose(hFind);
                   4431:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4432:        }
                   4433:        return(false);
1.1.1.8   root     4434: }
                   4435: 
1.1.1.45  root     4436: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4437: {
                   4438:        static char tmp[MAX_PATH];
1.1.1.28  root     4439:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4440:        
1.1.1.28  root     4441:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4442:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4443:                sprintf(file_name, "COMMAND.COM");
                   4444:                if(_access(tmp, 0) == 0) {
                   4445:                        return(tmp);
                   4446:                }
                   4447:        }
1.1.1.28  root     4448:        
                   4449:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4450:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4451:                sprintf(file_name, "COMMAND.COM");
                   4452:                if(_access(tmp, 0) == 0) {
                   4453:                        return(tmp);
                   4454:                }
                   4455:        }
1.1.1.28  root     4456:        
                   4457:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4458:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4459:                if(_access(tmp, 0) == 0) {
                   4460:                        return(tmp);
                   4461:                }
                   4462:        }
1.1.1.28  root     4463:        
                   4464:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4465:        strcpy(path, env_path);
                   4466:        char *token = my_strtok(path, ";");
1.1.1.9   root     4467:        while(token != NULL) {
1.1.1.14  root     4468:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4469:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4470:                        if(_access(tmp, 0) == 0) {
                   4471:                                return(tmp);
                   4472:                        }
                   4473:                }
                   4474:                token = my_strtok(NULL, ";");
                   4475:        }
                   4476:        return(NULL);
                   4477: }
                   4478: 
1.1.1.14  root     4479: int msdos_drive_number(const char *path)
1.1       root     4480: {
                   4481:        char tmp[MAX_PATH], *name;
                   4482:        
1.1.1.45  root     4483:        if(GetFullPathName(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
                   4484:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4485:                        return(tmp[0] - 'a');
                   4486:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4487:                        return(tmp[0] - 'A');
                   4488:                }
1.1       root     4489:        }
1.1.1.45  root     4490: //     return(msdos_drive_number("."));
                   4491:        return(_getdrive() - 1);
1.1       root     4492: }
                   4493: 
1.1.1.45  root     4494: const char *msdos_volume_label(const char *path)
1.1       root     4495: {
                   4496:        static char tmp[MAX_PATH];
                   4497:        char volume[] = "A:\\";
                   4498:        
                   4499:        if(path[1] == ':') {
                   4500:                volume[0] = path[0];
                   4501:        } else {
                   4502:                volume[0] = 'A' + _getdrive() - 1;
                   4503:        }
                   4504:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4505:                memset(tmp, 0, sizeof(tmp));
                   4506:        }
                   4507:        return(tmp);
                   4508: }
                   4509: 
1.1.1.45  root     4510: const char *msdos_short_volume_label(const char *label)
1.1       root     4511: {
                   4512:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4513:        const char *src = label;
1.1       root     4514:        int remain = strlen(label);
                   4515:        char *dst_n = tmp;
                   4516:        char *dst_e = tmp + 9;
                   4517:        
                   4518:        strcpy(tmp, "        .   ");
                   4519:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4520:                if(msdos_lead_byte_check(*src)) {
                   4521:                        if(++i == 8) {
                   4522:                                break;
                   4523:                        }
                   4524:                        *dst_n++ = *src++;
                   4525:                        remain--;
                   4526:                }
                   4527:                *dst_n++ = *src++;
                   4528:                remain--;
                   4529:        }
                   4530:        if(remain > 0) {
                   4531:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4532:                        if(msdos_lead_byte_check(*src)) {
                   4533:                                if(++i == 3) {
                   4534:                                        break;
                   4535:                                }
                   4536:                                *dst_e++ = *src++;
                   4537:                                remain--;
                   4538:                        }
                   4539:                        *dst_e++ = *src++;
                   4540:                        remain--;
                   4541:                }
                   4542:                *dst_e = '\0';
                   4543:        } else {
                   4544:                *dst_n = '\0';
                   4545:        }
                   4546:        my_strupr(tmp);
                   4547:        return(tmp);
                   4548: }
                   4549: 
1.1.1.13  root     4550: errno_t msdos_maperr(unsigned long oserrno)
                   4551: {
                   4552:        _doserrno = oserrno;
1.1.1.14  root     4553:        switch(oserrno) {
1.1.1.13  root     4554:        case ERROR_FILE_NOT_FOUND:         // 2
                   4555:        case ERROR_PATH_NOT_FOUND:         // 3
                   4556:        case ERROR_INVALID_DRIVE:          // 15
                   4557:        case ERROR_NO_MORE_FILES:          // 18
                   4558:        case ERROR_BAD_NETPATH:            // 53
                   4559:        case ERROR_BAD_NET_NAME:           // 67
                   4560:        case ERROR_BAD_PATHNAME:           // 161
                   4561:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4562:                return ENOENT;
                   4563:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4564:                return EMFILE;
                   4565:        case ERROR_ACCESS_DENIED:          // 5
                   4566:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4567:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4568:        case ERROR_CANNOT_MAKE:            // 82
                   4569:        case ERROR_FAIL_I24:               // 83
                   4570:        case ERROR_DRIVE_LOCKED:           // 108
                   4571:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4572:        case ERROR_NOT_LOCKED:             // 158
                   4573:        case ERROR_LOCK_FAILED:            // 167
                   4574:                return EACCES;
                   4575:        case ERROR_INVALID_HANDLE:         // 6
                   4576:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4577:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4578:                return EBADF;
                   4579:        case ERROR_ARENA_TRASHED:          // 7
                   4580:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4581:        case ERROR_INVALID_BLOCK:          // 9
                   4582:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4583:                return ENOMEM;
                   4584:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4585:                return E2BIG;
                   4586:        case ERROR_BAD_FORMAT:             // 11
                   4587:                return ENOEXEC;
                   4588:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4589:                return EXDEV;
                   4590:        case ERROR_FILE_EXISTS:            // 80
                   4591:        case ERROR_ALREADY_EXISTS:         // 183
                   4592:                return EEXIST;
                   4593:        case ERROR_NO_PROC_SLOTS:          // 89
                   4594:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4595:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4596:                return EAGAIN;
                   4597:        case ERROR_BROKEN_PIPE:            // 109
                   4598:                return EPIPE;
                   4599:        case ERROR_DISK_FULL:              // 112
                   4600:                return ENOSPC;
                   4601:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4602:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4603:                return ECHILD;
                   4604:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4605:                return ENOTEMPTY;
                   4606:        }
1.1.1.14  root     4607:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4608:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4609:                return EACCES;
                   4610:        }
1.1.1.14  root     4611:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4612:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4613:                return ENOEXEC;
                   4614:        }
                   4615:        return EINVAL;
                   4616: }
                   4617: 
1.1.1.45  root     4618: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4619: {
1.1.1.14  root     4620:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4621:                return(_open(path, oflag));
1.1.1.13  root     4622:        }
1.1.1.14  root     4623:        
                   4624:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4625:        DWORD disposition;
1.1.1.14  root     4626:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4627:        default:
1.1.1.13  root     4628:        case _O_EXCL:
                   4629:                disposition = OPEN_EXISTING;
                   4630:                break;
                   4631:        case _O_CREAT:
                   4632:                disposition = OPEN_ALWAYS;
                   4633:                break;
                   4634:        case _O_CREAT | _O_EXCL:
                   4635:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4636:                disposition = CREATE_NEW;
                   4637:                break;
                   4638:        case _O_TRUNC:
                   4639:        case _O_TRUNC | _O_EXCL:
                   4640:                disposition = TRUNCATE_EXISTING;
                   4641:                break;
                   4642:        case _O_CREAT | _O_TRUNC:
                   4643:                disposition = CREATE_ALWAYS;
                   4644:                break;
                   4645:        }
1.1.1.14  root     4646:        
1.1.1.45  root     4647:        HANDLE h = CreateFile(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4648:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4649:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4650:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4651:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4652:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.45  root     4653:                h = CreateFile(path, GENERIC_READ,
1.1.1.13  root     4654:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4655:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4656:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4657:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4658:                        return(-1);
1.1.1.13  root     4659:                }
                   4660:        }
1.1.1.14  root     4661:        
1.1.1.13  root     4662:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4663:        if(fd == -1) {
1.1.1.13  root     4664:                CloseHandle(h);
                   4665:        }
1.1.1.45  root     4666:        return(fd);
                   4667: }
                   4668: 
                   4669: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4670: {
                   4671:        int fd = -1;
                   4672:        
                   4673:        *sio_port = *lpt_port = 0;
                   4674:        
                   4675:        if(msdos_is_con_path(path)) {
                   4676:                // MODE.COM opens CON device with read/write mode :-(
                   4677:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   4678:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   4679:                        oflag |= _O_RDONLY;
                   4680:                }
                   4681:                if((fd = msdos_open("CON", oflag)) == -1) {
                   4682: //                     fd = msdos_open("NUL", oflag);
                   4683:                }
                   4684:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   4685:                fd = msdos_open("NUL", oflag);
                   4686:                msdos_set_comm_params(*sio_port, path);
                   4687:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   4688:                fd = msdos_open("NUL", oflag);
                   4689:        } else if(msdos_is_device_path(path)) {
                   4690:                fd = msdos_open("NUL", oflag);
                   4691: //     } else if(oflag & _O_CREAT) {
                   4692: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   4693: //     } else {
                   4694: //             fd = _open(path, oflag);
                   4695:        }
                   4696:        return(fd);
                   4697: }
                   4698: 
                   4699: UINT16 msdos_device_info(const char *path)
                   4700: {
                   4701:        if(msdos_is_con_path(path)) {
                   4702:                return(0x80d3);
                   4703:        } else if(msdos_is_comm_path(path)) {
                   4704:                return(0x80a0);
                   4705:        } else if(msdos_is_prn_path(path)) {
                   4706: //             return(0xa8c0);
                   4707:                return(0x80a0);
                   4708:        } else if(msdos_is_device_path(path)) {
                   4709:                if(strstr(path, "EMMXXXX0") != NULL) {
                   4710:                        return(0xc0c0);
                   4711:                } else if(strstr(path, "MSCD001") != NULL) {
                   4712:                        return(0xc880);
                   4713:                } else {
                   4714:                        return(0x8084);
                   4715:                }
                   4716:        } else {
                   4717:                return(msdos_drive_number(path));
                   4718:        }
1.1.1.13  root     4719: }
                   4720: 
1.1.1.52  root     4721: 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     4722: {
                   4723:        static int id = 0;
                   4724:        char full[MAX_PATH], *name;
                   4725:        
                   4726:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4727:                strcpy(file_handler[fd].path, full);
                   4728:        } else {
                   4729:                strcpy(file_handler[fd].path, path);
                   4730:        }
1.1.1.14  root     4731:        // isatty makes no distinction between CON & NUL
                   4732:        // GetFileSize fails on CON, succeeds on NUL
                   4733:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     4734:                if(info == 0x80d3) {
                   4735:                        info = 0x8084;
                   4736:                }
1.1.1.14  root     4737:                atty = 0;
                   4738:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     4739: //             info = msdos_drive_number(".");
                   4740:                info = msdos_drive_number(path);
1.1.1.14  root     4741:        }
1.1       root     4742:        file_handler[fd].valid = 1;
                   4743:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     4744:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4745:        file_handler[fd].mode = mode;
                   4746:        file_handler[fd].info = info;
                   4747:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     4748:        file_handler[fd].sio_port = sio_port;
                   4749:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4750:        
                   4751:        // init system file table
                   4752:        if(fd < 20) {
                   4753:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4754:                
                   4755:                memset(sft, 0, 0x3b);
                   4756:                
                   4757:                *(UINT16 *)(sft + 0x00) = 1;
                   4758:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4759:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4760:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4761:                
                   4762:                if(!(file_handler[fd].info & 0x80)) {
                   4763:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4764:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4765:                        
                   4766:                        FILETIME time, local;
                   4767:                        HANDLE hHandle;
                   4768:                        WORD dos_date = 0, dos_time = 0;
                   4769:                        DWORD file_size = 0;
                   4770:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4771:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4772:                                        FileTimeToLocalFileTime(&time, &local);
                   4773:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4774:                                }
                   4775:                                file_size = GetFileSize(hHandle, NULL);
                   4776:                        }
                   4777:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4778:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4779:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4780:                }
                   4781:                
                   4782:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4783:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4784:                my_strupr(fname);
                   4785:                my_strupr(ext);
                   4786:                memset(sft + 0x20, 0x20, 11);
                   4787:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4788:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4789:                
                   4790:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4791:        }
1.1       root     4792: }
                   4793: 
                   4794: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4795: {
                   4796:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4797:        file_handler[dst].valid = 1;
                   4798:        file_handler[dst].id = file_handler[src].id;
                   4799:        file_handler[dst].atty = file_handler[src].atty;
                   4800:        file_handler[dst].mode = file_handler[src].mode;
                   4801:        file_handler[dst].info = file_handler[src].info;
                   4802:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     4803:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   4804:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4805: }
                   4806: 
1.1.1.20  root     4807: void msdos_file_handler_close(int fd)
1.1       root     4808: {
                   4809:        file_handler[fd].valid = 0;
1.1.1.21  root     4810:        
                   4811:        if(fd < 20) {
                   4812:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4813:        }
1.1       root     4814: }
                   4815: 
1.1.1.14  root     4816: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4817: {
1.1.1.14  root     4818:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4819:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4820:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4821: }
                   4822: 
                   4823: // find file
                   4824: 
                   4825: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4826: {
                   4827:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4828:                return(0);      // search directory only !!!
                   4829:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4830:                return(0);
                   4831:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4832:                return(0);
                   4833:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4834:                return(0);
                   4835:        } else if((attribute & required_mask) != required_mask) {
                   4836:                return(0);
                   4837:        } else {
                   4838:                return(1);
                   4839:        }
                   4840: }
                   4841: 
1.1.1.13  root     4842: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4843: {
1.1.1.14  root     4844:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     4845:                return(1);
1.1.1.13  root     4846:        }
                   4847:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4848:        if(len > 12) {
1.1.1.42  root     4849:                return(0);
1.1.1.13  root     4850:        }
                   4851:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4852:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     4853:                return(0);
1.1.1.13  root     4854:        }
1.1.1.42  root     4855:        return(1);
1.1.1.13  root     4856: }
                   4857: 
1.1       root     4858: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4859: {
                   4860:        FILETIME local;
                   4861:        
                   4862:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4863:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4864:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4865:        
                   4866:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4867:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4868:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4869:        
                   4870:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4871:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4872:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4873: }
                   4874: 
                   4875: // i/o
                   4876: 
                   4877: void msdos_stdio_reopen()
                   4878: {
                   4879:        if(!file_handler[0].valid) {
                   4880:                _dup2(DUP_STDIN, 0);
                   4881:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   4882:        }
                   4883:        if(!file_handler[1].valid) {
                   4884:                _dup2(DUP_STDOUT, 1);
                   4885:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   4886:        }
                   4887:        if(!file_handler[2].valid) {
                   4888:                _dup2(DUP_STDERR, 2);
                   4889:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   4890:        }
1.1.1.21  root     4891:        if(!file_handler[3].valid) {
                   4892:                _dup2(DUP_STDAUX, 3);
                   4893:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   4894:        }
                   4895:        if(!file_handler[4].valid) {
                   4896:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     4897: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   4898:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     4899:        }
                   4900:        for(int i = 0; i < 5; i++) {
                   4901:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   4902:                        msdos_psp_set_file_table(i, i, current_psp);
                   4903:                }
                   4904:        }
1.1       root     4905: }
                   4906: 
1.1.1.37  root     4907: int msdos_read(int fd, void *buffer, unsigned int count)
                   4908: {
                   4909:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   4910:                // read from serial port
                   4911:                int read = 0;
                   4912:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   4913:                        UINT8 *buf = (UINT8 *)buffer;
                   4914:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   4915:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     4916:                        DWORD timeout = timeGetTime() + 1000;
                   4917:                        while(read < count) {
                   4918:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   4919:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   4920:                                        timeout = timeGetTime() + 1000;
                   4921:                                } else {
                   4922:                                        if(timeGetTime() > timeout) {
                   4923:                                                break;
                   4924:                                        }
                   4925:                                        Sleep(10);
1.1.1.37  root     4926:                                }
                   4927:                        }
                   4928:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   4929:                }
                   4930:                return(read);
                   4931:        }
                   4932:        return(_read(fd, buffer, count));
                   4933: }
                   4934: 
1.1       root     4935: int msdos_kbhit()
                   4936: {
                   4937:        msdos_stdio_reopen();
                   4938:        
1.1.1.20  root     4939:        process_t *process = msdos_process_info_get(current_psp);
                   4940:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4941:        
                   4942:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4943:                // stdin is redirected to file
1.1.1.20  root     4944:                return(eof(fd) == 0);
1.1       root     4945:        }
                   4946:        
                   4947:        // check keyboard status
1.1.1.35  root     4948:        if(key_recv != 0) {
1.1       root     4949:                return(1);
                   4950:        }
1.1.1.35  root     4951:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4952: #ifdef USE_SERVICE_THREAD
                   4953:                EnterCriticalSection(&key_buf_crit_sect);
                   4954: #endif
1.1.1.55! root     4955:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     4956: #ifdef USE_SERVICE_THREAD
                   4957:                LeaveCriticalSection(&key_buf_crit_sect);
                   4958: #endif
                   4959:                if(!empty) return(1);
                   4960:        }
                   4961:        return(_kbhit());
1.1       root     4962: }
                   4963: 
                   4964: int msdos_getch_ex(int echo)
                   4965: {
                   4966:        static char prev = 0;
                   4967:        
                   4968:        msdos_stdio_reopen();
                   4969:        
1.1.1.20  root     4970:        process_t *process = msdos_process_info_get(current_psp);
                   4971:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4972:        
                   4973:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4974:                // stdin is redirected to file
                   4975: retry:
                   4976:                char data;
1.1.1.37  root     4977:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     4978:                        char tmp = data;
                   4979:                        if(data == 0x0a) {
                   4980:                                if(prev == 0x0d) {
                   4981:                                        goto retry; // CRLF -> skip LF
                   4982:                                } else {
                   4983:                                        data = 0x0d; // LF only -> CR
                   4984:                                }
                   4985:                        }
                   4986:                        prev = tmp;
                   4987:                        return(data);
                   4988:                }
                   4989:                return(EOF);
                   4990:        }
                   4991:        
                   4992:        // input from console
1.1.1.5   root     4993:        int key_char, key_scan;
1.1.1.33  root     4994:        if(key_recv != 0) {
1.1.1.5   root     4995:                key_char = (key_code >> 0) & 0xff;
                   4996:                key_scan = (key_code >> 8) & 0xff;
                   4997:                key_code >>= 16;
1.1.1.33  root     4998:                key_recv >>= 16;
1.1.1.5   root     4999:        } else {
1.1.1.54  root     5000:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35  root     5001:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5002: #ifdef USE_SERVICE_THREAD
                   5003:                                EnterCriticalSection(&key_buf_crit_sect);
                   5004: #endif
1.1.1.55! root     5005:                                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5006: #ifdef USE_SERVICE_THREAD
                   5007:                                LeaveCriticalSection(&key_buf_crit_sect);
                   5008: #endif
                   5009:                                if(!empty) break;
                   5010:                        }
1.1.1.23  root     5011:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   5012:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   5013:                                if(_kbhit()) {
1.1.1.32  root     5014:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5015: #ifdef USE_SERVICE_THREAD
                   5016:                                                EnterCriticalSection(&key_buf_crit_sect);
                   5017: #endif
1.1.1.51  root     5018:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     5019: #ifdef USE_SERVICE_THREAD
                   5020:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   5021: #endif
1.1.1.32  root     5022:                                        }
1.1.1.23  root     5023:                                } else {
                   5024:                                        Sleep(10);
                   5025:                                }
                   5026:                        } else {
                   5027:                                if(!update_key_buffer()) {
                   5028:                                        Sleep(10);
                   5029:                                }
1.1.1.14  root     5030:                        }
                   5031:                }
1.1.1.54  root     5032:                if(m_exit) {
1.1.1.33  root     5033:                        // insert CR to terminate input loops
1.1.1.14  root     5034:                        key_char = 0x0d;
                   5035:                        key_scan = 0;
1.1.1.32  root     5036:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5037: #ifdef USE_SERVICE_THREAD
                   5038:                        EnterCriticalSection(&key_buf_crit_sect);
                   5039: #endif
1.1.1.51  root     5040:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     5041: #ifdef USE_SERVICE_THREAD
                   5042:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5043: #endif
1.1.1.5   root     5044:                }
1.1       root     5045:        }
                   5046:        if(echo && key_char) {
                   5047:                msdos_putch(key_char);
                   5048:        }
                   5049:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5050: }
                   5051: 
                   5052: inline int msdos_getch()
                   5053: {
                   5054:        return(msdos_getch_ex(0));
                   5055: }
                   5056: 
                   5057: inline int msdos_getche()
                   5058: {
                   5059:        return(msdos_getch_ex(1));
                   5060: }
                   5061: 
                   5062: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5063: {
1.1.1.37  root     5064:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5065:                // write to serial port
1.1.1.38  root     5066:                int written = 0;
1.1.1.37  root     5067:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5068:                        UINT8 *buf = (UINT8 *)buffer;
                   5069:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5070:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5071:                        DWORD timeout = timeGetTime() + 1000;
                   5072:                        while(written < count) {
                   5073:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5074:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5075:                                        timeout = timeGetTime() + 1000;
                   5076:                                } else {
                   5077:                                        if(timeGetTime() > timeout) {
                   5078:                                                break;
                   5079:                                        }
                   5080:                                        Sleep(10);
                   5081:                                }
1.1.1.37  root     5082:                        }
                   5083:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5084:                }
1.1.1.38  root     5085:                return(written);
1.1.1.37  root     5086:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5087:                // write to printer port
                   5088:                UINT8 *buf = (UINT8 *)buffer;
                   5089:                for(unsigned int i = 0; i < count; i++) {
                   5090: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5091:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5092:                }
                   5093:                return(count);
                   5094:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5095:                // CR+LF -> LF
1.1.1.37  root     5096:                static int is_cr = 0;
1.1       root     5097:                UINT8 *buf = (UINT8 *)buffer;
                   5098:                for(unsigned int i = 0; i < count; i++) {
                   5099:                        UINT8 data = buf[i];
                   5100:                        if(is_cr) {
                   5101:                                if(data != 0x0a) {
                   5102:                                        UINT8 tmp = 0x0d;
                   5103:                                        _write(1, &tmp, 1);
                   5104:                                }
                   5105:                                _write(1, &data, 1);
                   5106:                                is_cr = 0;
                   5107:                        } else if(data == 0x0d) {
                   5108:                                is_cr = 1;
                   5109:                        } else {
                   5110:                                _write(1, &data, 1);
                   5111:                        }
                   5112:                }
                   5113:                return(count);
                   5114:        }
1.1.1.14  root     5115:        vram_flush();
1.1       root     5116:        return(_write(fd, buffer, count));
                   5117: }
                   5118: 
                   5119: void msdos_putch(UINT8 data)
1.1.1.50  root     5120: {
                   5121:        msdos_stdio_reopen();
                   5122:        
                   5123:        process_t *process = msdos_process_info_get(current_psp);
                   5124:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5125:        
                   5126:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5127:                // stdout is redirected to file
                   5128:                msdos_write(fd, &data, 1);
                   5129:                return;
                   5130:        }
                   5131:        
                   5132:        // call int 29h ?
                   5133:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
                   5134:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5135:                // int 29h is not hooked, no need to call int 29h
                   5136:                msdos_putch_fast(data);
                   5137: #ifdef USE_SERVICE_THREAD
                   5138:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5139:                // XXX: in usually we should not reach here
                   5140:                // this is called from service thread to echo the input
                   5141:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5142:                msdos_putch_fast(data);
                   5143: #endif
1.1.1.51  root     5144:        } else if(in_service_29h) {
1.1.1.50  root     5145:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5146:                msdos_putch_fast(data);
                   5147:        } else {
                   5148:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5149:                in_service_29h = true;
1.1.1.50  root     5150:                try {
                   5151:                        UINT32 tmp_pc = m_pc;
                   5152:                        UINT16 tmp_ax = REG16(AX);
                   5153:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5154:                        
                   5155:                        // call int 29h routine is at fffc:0027
                   5156:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5157:                        REG8(AL) = data;
                   5158:                        
                   5159:                        // run cpu until call int 29h routine is done
1.1.1.54  root     5160:                        while(!m_exit && tmp_pc != m_pc) {
1.1.1.50  root     5161:                                try {
                   5162:                                        hardware_run_cpu();
                   5163:                                } catch(...) {
                   5164:                                }
                   5165:                        }
                   5166:                        REG16(AX) = tmp_ax;
                   5167:                        REG16(BX) = tmp_bx;
                   5168:                } catch(...) {
                   5169:                }
1.1.1.51  root     5170:                in_service_29h = false;
1.1.1.50  root     5171:        }
                   5172: }
                   5173: 
                   5174: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5175: #ifdef USE_SERVICE_THREAD
                   5176: {
                   5177:        EnterCriticalSection(&putch_crit_sect);
                   5178:        msdos_putch_tmp(data);
                   5179:        LeaveCriticalSection(&putch_crit_sect);
                   5180: }
                   5181: void msdos_putch_tmp(UINT8 data)
                   5182: #endif
1.1       root     5183: {
1.1.1.34  root     5184:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5185:        SMALL_RECT rect;
                   5186:        COORD co;
1.1       root     5187:        static int p = 0;
                   5188:        static int is_kanji = 0;
                   5189:        static int is_esc = 0;
                   5190:        static int stored_x;
                   5191:        static int stored_y;
                   5192:        static WORD stored_a;
1.1.1.20  root     5193:        static char tmp[64], out[64];
1.1       root     5194:        
1.1.1.23  root     5195:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5196:        
                   5197:        // output to console
                   5198:        tmp[p++] = data;
                   5199:        
1.1.1.14  root     5200:        vram_flush();
                   5201:        
1.1       root     5202:        if(is_kanji) {
                   5203:                // kanji character
                   5204:                is_kanji = 0;
                   5205:        } else if(is_esc) {
                   5206:                // escape sequense
                   5207:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5208:                        p = is_esc = 0;
                   5209:                } else if(tmp[1] == '=' && p == 4) {
                   5210:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5211:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5212:                        SetConsoleCursorPosition(hStdout, co);
                   5213:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5214:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5215:                        cursor_moved = false;
                   5216:                        p = is_esc = 0;
                   5217:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   5218:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5219:                        co.X = csbi.dwCursorPosition.X;
                   5220:                        co.Y = csbi.dwCursorPosition.Y;
                   5221:                        WORD wAttributes = csbi.wAttributes;
                   5222:                        
                   5223:                        if(tmp[1] == 'D') {
                   5224:                                co.Y++;
                   5225:                        } else if(tmp[1] == 'E') {
                   5226:                                co.X = 0;
                   5227:                                co.Y++;
                   5228:                        } else if(tmp[1] == 'M') {
                   5229:                                co.Y--;
                   5230:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5231:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5232:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5233:                                co.X = 0;
                   5234:                                co.Y = csbi.srWindow.Top;
1.1       root     5235:                        } else if(tmp[1] == '[') {
                   5236:                                int param[256], params = 0;
                   5237:                                memset(param, 0, sizeof(param));
                   5238:                                for(int i = 2; i < p; i++) {
                   5239:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5240:                                                param[params] *= 10;
                   5241:                                                param[params] += tmp[i] - '0';
                   5242:                                        } else {
                   5243:                                                params++;
                   5244:                                        }
                   5245:                                }
                   5246:                                if(data == 'A') {
1.1.1.14  root     5247:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5248:                                } else if(data == 'B') {
1.1.1.14  root     5249:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5250:                                } else if(data == 'C') {
1.1.1.14  root     5251:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5252:                                } else if(data == 'D') {
1.1.1.14  root     5253:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5254:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5255:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5256:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5257:                                } else if(data == 'J') {
1.1.1.14  root     5258:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5259:                                        if(param[0] == 0) {
                   5260:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5261:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5262:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5263:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5264:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5265:                                                }
                   5266:                                        } else if(param[0] == 1) {
1.1.1.14  root     5267:                                                if(co.Y > csbi.srWindow.Top) {
                   5268:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5269:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5270:                                                }
                   5271:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5272:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5273:                                        } else if(param[0] == 2) {
1.1.1.14  root     5274:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5275:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5276:                                                co.X = co.Y = 0;
                   5277:                                        }
                   5278:                                } else if(data == 'K') {
1.1.1.14  root     5279:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5280:                                        if(param[0] == 0) {
                   5281:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5282:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5283:                                        } else if(param[0] == 1) {
                   5284:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5285:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5286:                                        } else if(param[0] == 2) {
                   5287:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5288:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5289:                                        }
                   5290:                                } else if(data == 'L') {
1.1.1.14  root     5291:                                        if(params == 0) {
                   5292:                                                param[0] = 1;
1.1       root     5293:                                        }
1.1.1.14  root     5294:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5295:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5296:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5297:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5298:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5299:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5300:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5301:                                        co.X = 0;
                   5302:                                } else if(data == 'M') {
1.1.1.14  root     5303:                                        if(params == 0) {
                   5304:                                                param[0] = 1;
                   5305:                                        }
                   5306:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5307:                                                clear_scr_buffer(csbi.wAttributes);
                   5308:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5309:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5310:                                        } else {
1.1.1.14  root     5311:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5312:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5313:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5314:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5315:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5316:                                        }
                   5317:                                        co.X = 0;
                   5318:                                } else if(data == 'h') {
                   5319:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5320:                                                CONSOLE_CURSOR_INFO cur;
                   5321:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5322:                                                if(cur.bVisible) {
                   5323:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5324: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5325:                                                }
                   5326:                                        }
                   5327:                                } else if(data == 'l') {
                   5328:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5329:                                                CONSOLE_CURSOR_INFO cur;
                   5330:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5331:                                                if(!cur.bVisible) {
                   5332:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5333: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5334:                                                }
                   5335:                                        }
                   5336:                                } else if(data == 'm') {
                   5337:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5338:                                        int reverse = 0, hidden = 0;
                   5339:                                        for(int i = 0; i < params; i++) {
                   5340:                                                if(param[i] == 1) {
                   5341:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5342:                                                } else if(param[i] == 4) {
                   5343:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5344:                                                } else if(param[i] == 7) {
                   5345:                                                        reverse = 1;
                   5346:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5347:                                                        hidden = 1;
                   5348:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5349:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5350:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5351:                                                                param[i] -= 16;
                   5352:                                                        } else {
                   5353:                                                                param[i] -= 30;
                   5354:                                                        }
                   5355:                                                        if(param[i] & 1) {
                   5356:                                                                wAttributes |= FOREGROUND_RED;
                   5357:                                                        }
                   5358:                                                        if(param[i] & 2) {
                   5359:                                                                wAttributes |= FOREGROUND_GREEN;
                   5360:                                                        }
                   5361:                                                        if(param[i] & 4) {
                   5362:                                                                wAttributes |= FOREGROUND_BLUE;
                   5363:                                                        }
                   5364:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5365:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5366:                                                        if((param[i] - 40) & 1) {
                   5367:                                                                wAttributes |= BACKGROUND_RED;
                   5368:                                                        }
                   5369:                                                        if((param[i] - 40) & 2) {
                   5370:                                                                wAttributes |= BACKGROUND_GREEN;
                   5371:                                                        }
                   5372:                                                        if((param[i] - 40) & 4) {
                   5373:                                                                wAttributes |= BACKGROUND_BLUE;
                   5374:                                                        }
                   5375:                                                }
                   5376:                                        }
                   5377:                                        if(reverse) {
                   5378:                                                wAttributes &= ~0xff;
                   5379:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5380:                                        }
                   5381:                                        if(hidden) {
                   5382:                                                wAttributes &= ~0x0f;
                   5383:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5384:                                        }
                   5385:                                } else if(data == 'n') {
                   5386:                                        if(param[0] == 6) {
                   5387:                                                char tmp[16];
                   5388:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5389:                                                int len = strlen(tmp);
1.1.1.32  root     5390:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5391: #ifdef USE_SERVICE_THREAD
                   5392:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5393: #endif
1.1.1.32  root     5394:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5395:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5396:                                                        }
1.1.1.35  root     5397: #ifdef USE_SERVICE_THREAD
                   5398:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5399: #endif
1.1       root     5400:                                                }
                   5401:                                        }
                   5402:                                } else if(data == 's') {
                   5403:                                        stored_x = co.X;
                   5404:                                        stored_y = co.Y;
                   5405:                                        stored_a = wAttributes;
                   5406:                                } else if(data == 'u') {
                   5407:                                        co.X = stored_x;
                   5408:                                        co.Y = stored_y;
                   5409:                                        wAttributes = stored_a;
                   5410:                                }
                   5411:                        }
                   5412:                        if(co.X < 0) {
                   5413:                                co.X = 0;
                   5414:                        } else if(co.X >= csbi.dwSize.X) {
                   5415:                                co.X = csbi.dwSize.X - 1;
                   5416:                        }
1.1.1.14  root     5417:                        if(co.Y < csbi.srWindow.Top) {
                   5418:                                co.Y = csbi.srWindow.Top;
                   5419:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5420:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5421:                        }
                   5422:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5423:                                SetConsoleCursorPosition(hStdout, co);
                   5424:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5425:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5426:                                cursor_moved = false;
                   5427:                        }
                   5428:                        if(wAttributes != csbi.wAttributes) {
                   5429:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5430:                        }
                   5431:                        p = is_esc = 0;
                   5432:                }
                   5433:                return;
                   5434:        } else {
                   5435:                if(msdos_lead_byte_check(data)) {
                   5436:                        is_kanji = 1;
                   5437:                        return;
                   5438:                } else if(data == 0x1b) {
                   5439:                        is_esc = 1;
                   5440:                        return;
                   5441:                }
                   5442:        }
1.1.1.20  root     5443:        
                   5444:        DWORD q = 0, num;
                   5445:        is_kanji = 0;
                   5446:        for(int i = 0; i < p; i++) {
                   5447:                UINT8 c = tmp[i];
                   5448:                if(is_kanji) {
                   5449:                        is_kanji = 0;
                   5450:                } else if(msdos_lead_byte_check(data)) {
                   5451:                        is_kanji = 1;
                   5452:                } else if(msdos_ctrl_code_check(data)) {
                   5453:                        out[q++] = '^';
                   5454:                        c += 'A' - 1;
                   5455:                }
                   5456:                out[q++] = c;
                   5457:        }
1.1.1.34  root     5458:        if(q == 1 && out[0] == 0x08) {
                   5459:                // back space
                   5460:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5461:                if(csbi.dwCursorPosition.X > 0) {
                   5462:                        co.X = csbi.dwCursorPosition.X - 1;
                   5463:                        co.Y = csbi.dwCursorPosition.Y;
                   5464:                        SetConsoleCursorPosition(hStdout, co);
                   5465:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5466:                        co.X = csbi.dwSize.X - 1;
                   5467:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5468:                        SetConsoleCursorPosition(hStdout, co);
                   5469:                } else {
                   5470:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5471:                }
                   5472:        } else {
                   5473:                WriteConsole(hStdout, out, q, &num, NULL);
                   5474:        }
1.1       root     5475:        p = 0;
1.1.1.14  root     5476:        
1.1.1.15  root     5477:        if(!restore_console_on_exit) {
                   5478:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5479:                scr_top = csbi.srWindow.Top;
                   5480:        }
1.1       root     5481:        cursor_moved = true;
                   5482: }
                   5483: 
                   5484: int msdos_aux_in()
                   5485: {
1.1.1.21  root     5486:        msdos_stdio_reopen();
                   5487:        
1.1.1.20  root     5488:        process_t *process = msdos_process_info_get(current_psp);
                   5489:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5490:        
                   5491:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5492:                char data = 0;
1.1.1.37  root     5493:                msdos_read(fd, &data, 1);
1.1       root     5494:                return(data);
                   5495:        } else {
                   5496:                return(EOF);
                   5497:        }
                   5498: }
                   5499: 
                   5500: void msdos_aux_out(char data)
                   5501: {
1.1.1.21  root     5502:        msdos_stdio_reopen();
                   5503:        
1.1.1.20  root     5504:        process_t *process = msdos_process_info_get(current_psp);
                   5505:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5506:        
                   5507:        if(fd < process->max_files && file_handler[fd].valid) {
                   5508:                msdos_write(fd, &data, 1);
1.1       root     5509:        }
                   5510: }
                   5511: 
                   5512: void msdos_prn_out(char data)
                   5513: {
1.1.1.21  root     5514:        msdos_stdio_reopen();
                   5515:        
1.1.1.20  root     5516:        process_t *process = msdos_process_info_get(current_psp);
                   5517:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5518:        
                   5519:        if(fd < process->max_files && file_handler[fd].valid) {
                   5520:                msdos_write(fd, &data, 1);
1.1       root     5521:        }
                   5522: }
                   5523: 
                   5524: // memory control
                   5525: 
1.1.1.52  root     5526: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5527: {
                   5528:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5529:        
                   5530:        mcb->mz = mz;
                   5531:        mcb->psp = psp;
1.1.1.30  root     5532:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5533:        
                   5534:        if(prog_name != NULL) {
                   5535:                memset(mcb->prog_name, 0, 8);
                   5536:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5537:        }
1.1       root     5538:        return(mcb);
                   5539: }
                   5540: 
                   5541: void msdos_mcb_check(mcb_t *mcb)
                   5542: {
                   5543:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5544:                #if 0
                   5545:                        // shutdown now !!!
                   5546:                        fatalerror("broken memory control block\n");
                   5547:                #else
                   5548:                        // return error code and continue
                   5549:                        throw(0x07); // broken memory control block
                   5550:                #endif
1.1       root     5551:        }
                   5552: }
                   5553: 
1.1.1.39  root     5554: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5555: {
                   5556:        int mcb_seg = seg - 1;
                   5557:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5558:        msdos_mcb_check(mcb);
                   5559:        
1.1.1.30  root     5560:        if(mcb->paragraphs > paragraphs) {
1.1       root     5561:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5562:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5563:                
                   5564:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5565:                mcb->mz = 'M';
1.1.1.30  root     5566:                mcb->paragraphs = paragraphs;
1.1       root     5567:        }
                   5568: }
                   5569: 
                   5570: void msdos_mem_merge(int seg)
                   5571: {
                   5572:        int mcb_seg = seg - 1;
                   5573:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5574:        msdos_mcb_check(mcb);
                   5575:        
                   5576:        while(1) {
                   5577:                if(mcb->mz == 'Z') {
                   5578:                        break;
                   5579:                }
1.1.1.30  root     5580:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5581:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5582:                msdos_mcb_check(next_mcb);
                   5583:                
                   5584:                if(next_mcb->psp != 0) {
                   5585:                        break;
                   5586:                }
                   5587:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5588:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5589:        }
                   5590: }
                   5591: 
1.1.1.8   root     5592: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5593: {
                   5594:        while(1) {
                   5595:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5596:                bool last_block;
1.1       root     5597:                
1.1.1.14  root     5598:                if(mcb->psp == 0) {
                   5599:                        msdos_mem_merge(mcb_seg + 1);
                   5600:                } else {
                   5601:                        msdos_mcb_check(mcb);
                   5602:                }
1.1.1.33  root     5603:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5604:                        // check if the next is dummy mcb to link to umb
                   5605:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5606:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5607:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5608:                }
                   5609:                if(!(new_process && !last_block)) {
1.1.1.30  root     5610:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5611:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5612:                                mcb->psp = current_psp;
                   5613:                                return(mcb_seg + 1);
                   5614:                        }
                   5615:                }
                   5616:                if(mcb->mz == 'Z') {
                   5617:                        break;
                   5618:                }
1.1.1.30  root     5619:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5620:        }
                   5621:        return(-1);
                   5622: }
                   5623: 
                   5624: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5625: {
                   5626:        int mcb_seg = seg - 1;
                   5627:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5628:        msdos_mcb_check(mcb);
1.1.1.30  root     5629:        int current_paragraphs = mcb->paragraphs;
1.1       root     5630:        
                   5631:        msdos_mem_merge(seg);
1.1.1.30  root     5632:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5633:                if(max_paragraphs) {
1.1.1.30  root     5634:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5635:                }
1.1       root     5636:                msdos_mem_split(seg, current_paragraphs);
                   5637:                return(-1);
                   5638:        }
                   5639:        msdos_mem_split(seg, paragraphs);
                   5640:        return(0);
                   5641: }
                   5642: 
                   5643: void msdos_mem_free(int seg)
                   5644: {
                   5645:        int mcb_seg = seg - 1;
                   5646:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5647:        msdos_mcb_check(mcb);
                   5648:        
                   5649:        mcb->psp = 0;
                   5650:        msdos_mem_merge(seg);
                   5651: }
                   5652: 
1.1.1.8   root     5653: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5654: {
                   5655:        int max_paragraphs = 0;
                   5656:        
                   5657:        while(1) {
                   5658:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5659:                bool last_block;
                   5660:                
1.1       root     5661:                msdos_mcb_check(mcb);
                   5662:                
1.1.1.33  root     5663:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5664:                        // check if the next is dummy mcb to link to umb
                   5665:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5666:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5667:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5668:                }
                   5669:                if(!(new_process && !last_block)) {
1.1.1.30  root     5670:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5671:                                max_paragraphs = mcb->paragraphs;
1.1       root     5672:                        }
                   5673:                }
                   5674:                if(mcb->mz == 'Z') {
                   5675:                        break;
                   5676:                }
1.1.1.30  root     5677:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5678:        }
1.1.1.14  root     5679:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5680: }
                   5681: 
1.1.1.8   root     5682: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5683: {
                   5684:        int last_seg = -1;
                   5685:        
                   5686:        while(1) {
                   5687:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5688:                msdos_mcb_check(mcb);
                   5689:                
1.1.1.14  root     5690:                if(mcb->psp == psp) {
1.1.1.8   root     5691:                        last_seg = mcb_seg;
                   5692:                }
1.1.1.14  root     5693:                if(mcb->mz == 'Z') {
                   5694:                        break;
                   5695:                }
1.1.1.30  root     5696:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5697:        }
                   5698:        return(last_seg);
                   5699: }
                   5700: 
1.1.1.19  root     5701: int msdos_mem_get_umb_linked()
                   5702: {
1.1.1.33  root     5703:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5704:        msdos_mcb_check(mcb);
1.1.1.19  root     5705:        
1.1.1.33  root     5706:        if(mcb->mz == 'M') {
                   5707:                return(-1);
1.1.1.19  root     5708:        }
                   5709:        return(0);
                   5710: }
                   5711: 
1.1.1.33  root     5712: void msdos_mem_link_umb()
1.1.1.19  root     5713: {
1.1.1.33  root     5714:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5715:        msdos_mcb_check(mcb);
1.1.1.19  root     5716:        
1.1.1.33  root     5717:        mcb->mz = 'M';
                   5718:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     5719:        
                   5720:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5721: }
                   5722: 
1.1.1.33  root     5723: void msdos_mem_unlink_umb()
1.1.1.19  root     5724: {
1.1.1.33  root     5725:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5726:        msdos_mcb_check(mcb);
1.1.1.19  root     5727:        
1.1.1.33  root     5728:        mcb->mz = 'Z';
                   5729:        mcb->paragraphs = 0;
1.1.1.39  root     5730:        
                   5731:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5732: }
                   5733: 
1.1.1.29  root     5734: #ifdef SUPPORT_HMA
                   5735: 
                   5736: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5737: {
                   5738:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5739:        
                   5740:        mcb->ms[0] = 'M';
                   5741:        mcb->ms[1] = 'S';
                   5742:        mcb->owner = owner;
                   5743:        mcb->size = size;
                   5744:        mcb->next = next;
                   5745:        return(mcb);
                   5746: }
                   5747: 
                   5748: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5749: {
                   5750:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5751: }
                   5752: 
                   5753: int msdos_hma_mem_split(int offset, int size)
                   5754: {
                   5755:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5756:        
                   5757:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5758:                return(-1);
                   5759:        }
                   5760:        if(mcb->size >= size + 0x10) {
                   5761:                int new_offset = offset + 0x10 + size;
                   5762:                int new_size = mcb->size - 0x10 - size;
                   5763:                
                   5764:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5765:                mcb->size = size;
                   5766:                mcb->next = new_offset;
                   5767:                return(0);
                   5768:        }
                   5769:        return(-1);
                   5770: }
                   5771: 
                   5772: void msdos_hma_mem_merge(int offset)
                   5773: {
                   5774:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5775:        
                   5776:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5777:                return;
                   5778:        }
                   5779:        while(1) {
                   5780:                if(mcb->next == 0) {
                   5781:                        break;
                   5782:                }
                   5783:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5784:                
                   5785:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5786:                        return;
                   5787:                }
                   5788:                if(next_mcb->owner != 0) {
                   5789:                        break;
                   5790:                }
                   5791:                mcb->size += 0x10 + next_mcb->size;
                   5792:                mcb->next = next_mcb->next;
                   5793:        }
                   5794: }
                   5795: 
                   5796: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5797: {
                   5798:        int offset = 0x10; // first mcb in HMA
                   5799:        
                   5800:        while(1) {
                   5801:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5802:                
                   5803:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5804:                        return(-1);
                   5805:                }
                   5806:                if(mcb->owner == 0) {
                   5807:                        msdos_hma_mem_merge(offset);
                   5808:                }
                   5809:                if(mcb->owner == 0 && mcb->size >= size) {
                   5810:                        msdos_hma_mem_split(offset, size);
                   5811:                        mcb->owner = owner;
                   5812:                        return(offset);
                   5813:                }
                   5814:                if(mcb->next == 0) {
                   5815:                        break;
                   5816:                }
                   5817:                offset = mcb->next;
                   5818:        }
                   5819:        return(-1);
                   5820: }
                   5821: 
                   5822: int msdos_hma_mem_realloc(int offset, int size)
                   5823: {
                   5824:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5825:        
                   5826:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5827:                return(-1);
                   5828:        }
                   5829:        if(mcb->size < size) {
                   5830:                return(-1);
                   5831:        }
                   5832:        msdos_hma_mem_split(offset, size);
                   5833:        return(0);
                   5834: }
                   5835: 
                   5836: void msdos_hma_mem_free(int offset)
                   5837: {
                   5838:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5839:        
                   5840:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5841:                return;
                   5842:        }
                   5843:        mcb->owner = 0;
                   5844:        msdos_hma_mem_merge(offset);
                   5845: }
                   5846: 
                   5847: int msdos_hma_mem_get_free(int *available_offset)
                   5848: {
                   5849:        int offset = 0x10; // first mcb in HMA
                   5850:        int size = 0;
                   5851:        
                   5852:        while(1) {
                   5853:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5854:                
                   5855:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5856:                        return(0);
                   5857:                }
                   5858:                if(mcb->owner == 0 && size < mcb->size) {
                   5859:                        if(available_offset != NULL) {
                   5860:                                *available_offset = offset;
                   5861:                        }
                   5862:                        size = mcb->size;
                   5863:                }
                   5864:                if(mcb->next == 0) {
                   5865:                        break;
                   5866:                }
                   5867:                offset = mcb->next;
                   5868:        }
                   5869:        return(size);
                   5870: }
                   5871: 
                   5872: #endif
                   5873: 
1.1       root     5874: // environment
                   5875: 
1.1.1.45  root     5876: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     5877: {
                   5878:        char *dst = (char *)(mem + (env_seg << 4));
                   5879:        
                   5880:        while(1) {
                   5881:                if(dst[0] == 0) {
                   5882:                        break;
                   5883:                }
                   5884:                dst += strlen(dst) + 1;
                   5885:        }
                   5886:        *dst++ = 0; // end of environment
                   5887:        *dst++ = 1; // top of argv[0]
                   5888:        *dst++ = 0;
                   5889:        memcpy(dst, argv, strlen(argv));
                   5890:        dst += strlen(argv);
                   5891:        *dst++ = 0;
                   5892:        *dst++ = 0;
                   5893: }
                   5894: 
1.1.1.45  root     5895: const char *msdos_env_get_argv(int env_seg)
1.1       root     5896: {
                   5897:        static char env[ENV_SIZE];
                   5898:        char *src = env;
                   5899:        
                   5900:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5901:        while(1) {
                   5902:                if(src[0] == 0) {
                   5903:                        if(src[1] == 1) {
                   5904:                                return(src + 3);
                   5905:                        }
                   5906:                        break;
                   5907:                }
                   5908:                src += strlen(src) + 1;
                   5909:        }
                   5910:        return(NULL);
                   5911: }
                   5912: 
1.1.1.45  root     5913: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     5914: {
                   5915:        static char env[ENV_SIZE];
                   5916:        char *src = env;
                   5917:        
                   5918:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5919:        while(1) {
                   5920:                if(src[0] == 0) {
                   5921:                        break;
                   5922:                }
                   5923:                int len = strlen(src);
                   5924:                char *n = my_strtok(src, "=");
                   5925:                char *v = src + strlen(n) + 1;
                   5926:                
                   5927:                if(_stricmp(name, n) == 0) {
                   5928:                        return(v);
                   5929:                }
                   5930:                src += len + 1;
                   5931:        }
                   5932:        return(NULL);
                   5933: }
                   5934: 
1.1.1.45  root     5935: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     5936: {
                   5937:        char env[ENV_SIZE];
                   5938:        char *src = env;
                   5939:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     5940:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     5941:        int done = 0;
                   5942:        
                   5943:        memcpy(src, dst, ENV_SIZE);
                   5944:        memset(dst, 0, ENV_SIZE);
                   5945:        while(1) {
                   5946:                if(src[0] == 0) {
                   5947:                        break;
                   5948:                }
                   5949:                int len = strlen(src);
                   5950:                char *n = my_strtok(src, "=");
                   5951:                char *v = src + strlen(n) + 1;
                   5952:                char tmp[1024];
                   5953:                
                   5954:                if(_stricmp(name, n) == 0) {
                   5955:                        sprintf(tmp, "%s=%s", n, value);
                   5956:                        done = 1;
                   5957:                } else {
                   5958:                        sprintf(tmp, "%s=%s", n, v);
                   5959:                }
                   5960:                memcpy(dst, tmp, strlen(tmp));
                   5961:                dst += strlen(tmp) + 1;
                   5962:                src += len + 1;
                   5963:        }
                   5964:        if(!done) {
                   5965:                char tmp[1024];
                   5966:                
                   5967:                sprintf(tmp, "%s=%s", name, value);
                   5968:                memcpy(dst, tmp, strlen(tmp));
                   5969:                dst += strlen(tmp) + 1;
                   5970:        }
                   5971:        if(argv) {
                   5972:                *dst++ = 0; // end of environment
                   5973:                *dst++ = 1; // top of argv[0]
                   5974:                *dst++ = 0;
                   5975:                memcpy(dst, argv, strlen(argv));
                   5976:                dst += strlen(argv);
                   5977:                *dst++ = 0;
                   5978:                *dst++ = 0;
                   5979:        }
                   5980: }
                   5981: 
                   5982: // process
                   5983: 
1.1.1.8   root     5984: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     5985: {
                   5986:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5987:        
                   5988:        memset(psp, 0, PSP_SIZE);
                   5989:        psp->exit[0] = 0xcd;
                   5990:        psp->exit[1] = 0x20;
1.1.1.8   root     5991:        psp->first_mcb = mcb_seg;
1.1.1.46  root     5992: #if 1
1.1.1.49  root     5993:        psp->call5[0] = 0xcd;   // int 30h
                   5994:        psp->call5[1] = 0x30;
1.1.1.46  root     5995:        psp->call5[2] = 0xc3;   // ret
                   5996: #else
                   5997:        psp->call5[0] = 0x8a;   // mov ah, cl
                   5998:        psp->call5[1] = 0xe1;
                   5999:        psp->call5[2] = 0xcd;   // int 21h
                   6000:        psp->call5[3] = 0x21;
                   6001:        psp->call5[4] = 0xc3;   // ret
                   6002: #endif
1.1       root     6003:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6004:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6005:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6006:        psp->parent_psp = parent_psp;
1.1.1.20  root     6007:        if(parent_psp == (UINT16)-1) {
                   6008:                for(int i = 0; i < 20; i++) {
                   6009:                        if(file_handler[i].valid) {
                   6010:                                psp->file_table[i] = i;
                   6011:                        } else {
                   6012:                                psp->file_table[i] = 0xff;
                   6013:                        }
1.1       root     6014:                }
1.1.1.20  root     6015:        } else {
                   6016:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     6017:        }
                   6018:        psp->env_seg = env_seg;
                   6019:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6020:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6021:        psp->file_table_size = 20;
                   6022:        psp->file_table_ptr.w.l = 0x18;
                   6023:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6024:        psp->service[0] = 0xcd;
                   6025:        psp->service[1] = 0x21;
                   6026:        psp->service[2] = 0xcb;
                   6027:        return(psp);
                   6028: }
                   6029: 
1.1.1.20  root     6030: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6031: {
                   6032:        if(psp_seg && fd < 20) {
                   6033:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6034:                psp->file_table[fd] = value;
                   6035:        }
                   6036: }
                   6037: 
                   6038: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6039: {
                   6040:        if(psp_seg && fd < 20) {
                   6041:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6042:                fd = psp->file_table[fd];
                   6043:        }
                   6044:        return fd;
                   6045: }
                   6046: 
1.1.1.52  root     6047: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     6048: {
                   6049:        // load command file
                   6050:        int fd = -1;
1.1.1.45  root     6051:        int sio_port = 0;
                   6052:        int lpt_port = 0;
1.1       root     6053:        int dos_command = 0;
1.1.1.24  root     6054:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6055:        char pipe_stdin_path[MAX_PATH] = {0};
                   6056:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6057:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6058:        
                   6059:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6060:        int opt_len = mem[opt_ofs];
                   6061:        memset(opt, 0, sizeof(opt));
                   6062:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6063:        
1.1.1.14  root     6064:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6065:                // this is a batch file, run command.com
                   6066:                char tmp[MAX_PATH];
                   6067:                if(opt_len != 0) {
                   6068:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6069:                } else {
                   6070:                        sprintf(tmp, "/C %s", cmd);
                   6071:                }
                   6072:                strcpy(opt, tmp);
                   6073:                opt_len = strlen(opt);
                   6074:                mem[opt_ofs] = opt_len;
                   6075:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6076:                strcpy(command, comspec_path);
                   6077:                strcpy(name_tmp, "COMMAND.COM");
                   6078:        } else {
                   6079:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6080:                        // redirect C:\COMMAND.COM to comspec_path
                   6081:                        strcpy(command, comspec_path);
                   6082:                } else {
                   6083:                        strcpy(command, cmd);
                   6084:                }
1.1.1.24  root     6085:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   6086:                        return(-1);
                   6087:                }
1.1.1.14  root     6088:                memset(name_tmp, 0, sizeof(name_tmp));
                   6089:                strcpy(name_tmp, name);
                   6090:                
                   6091:                // check command.com
1.1.1.38  root     6092:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6093:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6094:                        if(opt_len == 0) {
                   6095: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6096:                                process_t *current_process = NULL;
                   6097:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6098:                                        if(process[i].psp == current_psp) {
                   6099:                                                current_process = &process[i];
                   6100:                                                break;
                   6101:                                        }
                   6102:                                }
                   6103:                                if(current_process != NULL) {
                   6104:                                        param->cmd_line.dw = current_process->dta.dw;
                   6105:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6106:                                        opt_len = mem[opt_ofs];
                   6107:                                        memset(opt, 0, sizeof(opt));
                   6108:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6109:                                }
                   6110:                        }
                   6111:                        for(int i = 0; i < opt_len; i++) {
                   6112:                                if(opt[i] == ' ') {
                   6113:                                        continue;
                   6114:                                }
                   6115:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6116:                                        for(int j = i + 3; j < opt_len; j++) {
                   6117:                                                if(opt[j] == ' ') {
                   6118:                                                        continue;
                   6119:                                                }
                   6120:                                                char *token = my_strtok(opt + j, " ");
                   6121:                                                
1.1.1.38  root     6122:                                                strcpy(command, token);
                   6123:                                                char tmp[MAX_PATH];
                   6124:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6125:                                                strcpy(opt, "");
                   6126:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6127:                                                        if(tmp[i] != ' ') {
                   6128:                                                                strcpy(opt, tmp + i);
                   6129:                                                                break;
                   6130:                                                        }
                   6131:                                                }
                   6132:                                                strcpy(tmp, opt);
1.1.1.38  root     6133:                                                
                   6134:                                                if(al == 0x00) {
1.1.1.39  root     6135:                                                        #define GET_FILE_PATH() { \
                   6136:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6137:                                                                        token++; \
                   6138:                                                                } \
                   6139:                                                                token++; \
                   6140:                                                                while(*token == ' ') { \
                   6141:                                                                        token++; \
                   6142:                                                                } \
                   6143:                                                                char *ptr = token; \
                   6144:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6145:                                                                        ptr++; \
                   6146:                                                                } \
                   6147:                                                                *ptr = '\0'; \
                   6148:                                                        }
                   6149:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6150:                                                                GET_FILE_PATH();
1.1.1.38  root     6151:                                                                strcpy(pipe_stdin_path, token);
                   6152:                                                                strcpy(opt, tmp);
                   6153:                                                        }
1.1.1.39  root     6154:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6155:                                                                GET_FILE_PATH();
1.1.1.38  root     6156:                                                                strcpy(pipe_stdout_path, token);
                   6157:                                                                strcpy(opt, tmp);
                   6158:                                                        }
1.1.1.39  root     6159:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6160:                                                                GET_FILE_PATH();
                   6161:                                                                strcpy(pipe_stderr_path, token);
                   6162:                                                                strcpy(opt, tmp);
                   6163:                                                        }
                   6164:                                                        #undef GET_FILE_PATH
                   6165:                                                        
                   6166:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6167:                                                                *token = '\0';
                   6168:                                                        }
                   6169:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6170:                                                                *token = '\0';
                   6171:                                                        }
                   6172:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6173:                                                                *token = '\0';
                   6174:                                                        }
1.1.1.38  root     6175:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6176:                                                                *token = '\0';
                   6177:                                                        }
                   6178:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6179:                                                                *token = '\0';
                   6180:                                                        }
1.1.1.14  root     6181:                                                }
1.1.1.39  root     6182:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6183:                                                        opt[i] = '\0';
                   6184:                                                }
1.1.1.38  root     6185:                                                opt_len = strlen(opt);
                   6186:                                                mem[opt_ofs] = opt_len;
                   6187:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6188:                                                dos_command = 1;
1.1.1.14  root     6189:                                                break;
1.1       root     6190:                                        }
                   6191:                                }
1.1.1.14  root     6192:                                break;
1.1       root     6193:                        }
                   6194:                }
                   6195:        }
                   6196:        
                   6197:        // load command file
                   6198:        strcpy(path, command);
                   6199:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6200:                sprintf(path, "%s.COM", command);
                   6201:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6202:                        sprintf(path, "%s.EXE", command);
                   6203:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6204:                                sprintf(path, "%s.BAT", command);
                   6205:                                if(_access(path, 0) == 0) {
                   6206:                                        // this is a batch file, run command.com
                   6207:                                        char tmp[MAX_PATH];
                   6208:                                        if(opt_len != 0) {
                   6209:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6210:                                        } else {
                   6211:                                                sprintf(tmp, "/C %s", path);
                   6212:                                        }
                   6213:                                        strcpy(opt, tmp);
                   6214:                                        opt_len = strlen(opt);
                   6215:                                        mem[opt_ofs] = opt_len;
                   6216:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6217:                                        strcpy(path, comspec_path);
                   6218:                                        strcpy(name_tmp, "COMMAND.COM");
                   6219:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6220:                                } else {
                   6221:                                        // search path in parent environments
                   6222:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6223:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6224:                                        if(env != NULL) {
                   6225:                                                char env_path[4096];
                   6226:                                                strcpy(env_path, env);
                   6227:                                                char *token = my_strtok(env_path, ";");
                   6228:                                                
                   6229:                                                while(token != NULL) {
                   6230:                                                        if(strlen(token) != 0) {
                   6231:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6232:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6233:                                                                        break;
                   6234:                                                                }
                   6235:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6236:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6237:                                                                        break;
                   6238:                                                                }
                   6239:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6240:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6241:                                                                        break;
                   6242:                                                                }
                   6243:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6244:                                                                if(_access(path, 0) == 0) {
                   6245:                                                                        // this is a batch file, run command.com
                   6246:                                                                        char tmp[MAX_PATH];
                   6247:                                                                        if(opt_len != 0) {
                   6248:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6249:                                                                        } else {
                   6250:                                                                                sprintf(tmp, "/C %s", path);
                   6251:                                                                        }
                   6252:                                                                        strcpy(opt, tmp);
                   6253:                                                                        opt_len = strlen(opt);
                   6254:                                                                        mem[opt_ofs] = opt_len;
                   6255:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6256:                                                                        strcpy(path, comspec_path);
                   6257:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6258:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6259:                                                                        break;
                   6260:                                                                }
1.1.1.8   root     6261:                                                        }
1.1.1.14  root     6262:                                                        token = my_strtok(NULL, ";");
1.1       root     6263:                                                }
                   6264:                                        }
                   6265:                                }
                   6266:                        }
                   6267:                }
                   6268:        }
                   6269:        if(fd == -1) {
1.1.1.38  root     6270:                // we can not find command.com in the path, so open comspec_path
                   6271:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6272:                        strcpy(command, comspec_path);
                   6273:                        strcpy(path, command);
                   6274:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6275:                }
                   6276:        }
                   6277:        if(fd == -1) {
1.1.1.52  root     6278:                if(!first_process && al == 0 && dos_command) {
1.1       root     6279:                        // may be dos command
                   6280:                        char tmp[MAX_PATH];
1.1.1.52  root     6281:                        if(opt_len != 0) {
                   6282:                                sprintf(tmp, "%s %s", command, opt);
                   6283:                        } else {
                   6284:                                sprintf(tmp, "%s", command);
                   6285:                        }
                   6286:                        retval = system(tmp);
1.1       root     6287:                        return(0);
                   6288:                } else {
                   6289:                        return(-1);
                   6290:                }
                   6291:        }
1.1.1.52  root     6292:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6293:        _read(fd, file_buffer, sizeof(file_buffer));
                   6294:        _close(fd);
                   6295:        
1.1.1.52  root     6296:        // check if this is win32 program
                   6297:        if(!first_process && al == 0) {
                   6298:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
                   6299:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
                   6300:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
                   6301:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
                   6302:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
                   6303:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
                   6304:                                char tmp[MAX_PATH];
                   6305:                                if(opt_len != 0) {
                   6306:                                        sprintf(tmp, "\"%s\" %s", path, opt);
                   6307:                                } else {
                   6308:                                        sprintf(tmp, "\"%s\"", path);
                   6309:                                }
                   6310:                                retval = system(tmp);
                   6311:                                return(0);
                   6312:                        }
                   6313:                }
                   6314:        }
                   6315:        
1.1       root     6316:        // copy environment
1.1.1.29  root     6317:        int umb_linked, env_seg, psp_seg;
1.1       root     6318:        
1.1.1.29  root     6319:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6320:                msdos_mem_unlink_umb();
                   6321:        }
1.1.1.8   root     6322:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6323:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6324:                        if(umb_linked != 0) {
                   6325:                                msdos_mem_link_umb();
                   6326:                        }
                   6327:                        return(-1);
                   6328:                }
1.1       root     6329:        }
                   6330:        if(param->env_seg == 0) {
                   6331:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6332:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6333:        } else {
                   6334:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6335:        }
                   6336:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6337:        
                   6338:        // check exe header
                   6339:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6340:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6341:        UINT16 cs, ss, ip, sp;
                   6342:        
                   6343:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6344:                // memory allocation
                   6345:                int header_size = header->header_size * 16;
                   6346:                int load_size = header->pages * 512 - header_size;
                   6347:                if(header_size + load_size < 512) {
                   6348:                        load_size = 512 - header_size;
                   6349:                }
                   6350:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6351:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6352:                        msdos_mem_free(env_seg);
                   6353:                        return(-1);
                   6354:                }
                   6355:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6356:                if(paragraphs > free_paragraphs) {
                   6357:                        paragraphs = free_paragraphs;
                   6358:                }
1.1.1.8   root     6359:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6360:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6361:                                if(umb_linked != 0) {
                   6362:                                        msdos_mem_link_umb();
                   6363:                                }
                   6364:                                msdos_mem_free(env_seg);
                   6365:                                return(-1);
                   6366:                        }
1.1       root     6367:                }
                   6368:                // relocation
                   6369:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6370:                for(int i = 0; i < header->relocations; i++) {
                   6371:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6372:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6373:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6374:                }
                   6375:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6376:                // segments
                   6377:                cs = header->init_cs + start_seg;
                   6378:                ss = header->init_ss + start_seg;
                   6379:                ip = header->init_ip;
                   6380:                sp = header->init_sp - 2; // for symdeb
                   6381:        } else {
                   6382:                // memory allocation
                   6383:                paragraphs = free_paragraphs;
1.1.1.8   root     6384:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6385:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6386:                                if(umb_linked != 0) {
                   6387:                                        msdos_mem_link_umb();
                   6388:                                }
                   6389:                                msdos_mem_free(env_seg);
                   6390:                                return(-1);
                   6391:                        }
1.1       root     6392:                }
                   6393:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6394:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6395:                // segments
                   6396:                cs = ss = psp_seg;
                   6397:                ip = 0x100;
                   6398:                sp = 0xfffe;
                   6399:        }
1.1.1.29  root     6400:        if(umb_linked != 0) {
                   6401:                msdos_mem_link_umb();
                   6402:        }
1.1       root     6403:        
                   6404:        // create psp
1.1.1.3   root     6405:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6406:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6407:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6408:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6409:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6410:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6411:        
                   6412:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6413:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6414:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6415:        
1.1.1.4   root     6416:        for(int i = 0; i < 8; i++) {
                   6417:                if(name_tmp[i] == '.') {
                   6418:                        mcb_psp->prog_name[i] = '\0';
                   6419:                        break;
                   6420:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6421:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6422:                        i++;
                   6423:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6424:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6425:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6426:                } else {
                   6427:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6428:                }
                   6429:        }
                   6430:        
1.1       root     6431:        // process info
                   6432:        process_t *process = msdos_process_info_create(psp_seg);
                   6433:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6434: #ifdef USE_DEBUGGER
                   6435:        strcpy(process->module_path, path);
                   6436: #endif
1.1       root     6437:        process->dta.w.l = 0x80;
                   6438:        process->dta.w.h = psp_seg;
                   6439:        process->switchar = '/';
                   6440:        process->max_files = 20;
                   6441:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6442:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6443:        process->parent_ds = SREG(DS);
1.1.1.31  root     6444:        process->parent_es = SREG(ES);
1.1       root     6445:        
                   6446:        current_psp = psp_seg;
1.1.1.23  root     6447:        msdos_sda_update(current_psp);
1.1       root     6448:        
                   6449:        if(al == 0x00) {
                   6450:                int_10h_feh_called = int_10h_ffh_called = false;
                   6451:                
1.1.1.38  root     6452:                // pipe
                   6453:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6454: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6455:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6456:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6457:                        } else {
                   6458:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6459:                        }
                   6460:                        if(fd != -1) {
                   6461:                                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     6462:                                psp->file_table[0] = fd;
                   6463:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6464:                        }
                   6465:                }
                   6466:                if(pipe_stdout_path[0] != '\0') {
                   6467:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6468:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6469:                                DeleteFile(pipe_stdout_path);
                   6470:                        }
1.1.1.45  root     6471: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6472:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6473:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6474:                        } else {
                   6475:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6476:                        }
                   6477:                        if(fd != -1) {
                   6478:                                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     6479:                                psp->file_table[1] = fd;
                   6480:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6481:                        }
                   6482:                }
1.1.1.39  root     6483:                if(pipe_stderr_path[0] != '\0') {
                   6484:                        if(_access(pipe_stderr_path, 0) == 0) {
                   6485:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6486:                                DeleteFile(pipe_stderr_path);
                   6487:                        }
1.1.1.45  root     6488: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6489:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6490:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6491:                        } else {
                   6492:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6493:                        }
                   6494:                        if(fd != -1) {
                   6495:                                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     6496:                                psp->file_table[2] = fd;
                   6497:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6498:                        }
                   6499:                }
1.1.1.38  root     6500:                
1.1       root     6501:                // registers and segments
                   6502:                REG16(AX) = REG16(BX) = 0x00;
                   6503:                REG16(CX) = 0xff;
                   6504:                REG16(DX) = psp_seg;
                   6505:                REG16(SI) = ip;
                   6506:                REG16(DI) = sp;
                   6507:                REG16(SP) = sp;
1.1.1.3   root     6508:                SREG(DS) = SREG(ES) = psp_seg;
                   6509:                SREG(SS) = ss;
                   6510:                i386_load_segment_descriptor(DS);
                   6511:                i386_load_segment_descriptor(ES);
                   6512:                i386_load_segment_descriptor(SS);
1.1       root     6513:                
                   6514:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6515:                i386_jmp_far(cs, ip);
                   6516:        } else if(al == 0x01) {
                   6517:                // copy ss:sp and cs:ip to param block
                   6518:                param->sp = sp;
                   6519:                param->ss = ss;
                   6520:                param->ip = ip;
                   6521:                param->cs = cs;
1.1.1.31  root     6522:                
                   6523:                // the AX value to be passed to the child program is put on top of the child's stack
                   6524:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6525:        }
                   6526:        return(0);
                   6527: }
                   6528: 
                   6529: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6530: {
                   6531:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6532:        
                   6533:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6534:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6535:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6536:        
1.1.1.3   root     6537:        SREG(SS) = psp->stack.w.h;
                   6538:        i386_load_segment_descriptor(SS);
1.1       root     6539:        REG16(SP) = psp->stack.w.l;
                   6540:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6541:        
1.1.1.28  root     6542: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6543:        process_t *current_process = NULL;
                   6544:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6545:                if(process[i].psp == psp_seg) {
                   6546:                        current_process = &process[i];
                   6547:                        break;
                   6548:                }
                   6549:        }
                   6550:        if(current_process == NULL) {
                   6551:                throw(0x1f); // general failure
                   6552:        }
                   6553:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6554:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6555:        if(current_process->called_by_int2eh) {
                   6556:                REG16(AX) = ret;
                   6557:        }
                   6558:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6559:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6560:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6561:        i386_load_segment_descriptor(ES);
1.1       root     6562:        
                   6563:        if(mem_free) {
1.1.1.8   root     6564:                int mcb_seg;
                   6565:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6566:                        msdos_mem_free(mcb_seg + 1);
                   6567:                }
                   6568:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6569:                        msdos_mem_free(mcb_seg + 1);
                   6570:                }
1.1       root     6571:                
                   6572:                for(int i = 0; i < MAX_FILES; i++) {
                   6573:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6574:                                _close(i);
1.1.1.20  root     6575:                                msdos_file_handler_close(i);
                   6576:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6577:                        }
                   6578:                }
1.1.1.13  root     6579:                msdos_dta_info_free(psp_seg);
1.1       root     6580:        }
1.1.1.14  root     6581:        msdos_stdio_reopen();
1.1       root     6582:        
1.1.1.28  root     6583:        memset(current_process, 0, sizeof(process_t));
1.1       root     6584:        
                   6585:        current_psp = psp->parent_psp;
                   6586:        retval = ret;
1.1.1.23  root     6587:        msdos_sda_update(current_psp);
1.1       root     6588: }
                   6589: 
                   6590: // drive
                   6591: 
1.1.1.42  root     6592: int pcbios_update_drive_param(int drive_num, int force_update);
                   6593: 
1.1       root     6594: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6595: {
1.1.1.41  root     6596:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6597:                return(0);
                   6598:        }
1.1.1.42  root     6599:        pcbios_update_drive_param(drive_num, force_update);
                   6600:        
                   6601:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6602:        *seg = DPB_TOP >> 4;
                   6603:        *ofs = sizeof(dpb_t) * drive_num;
                   6604:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6605:        
                   6606:        memset(dpb, 0, sizeof(dpb_t));
                   6607:        
1.1.1.41  root     6608:        dpb->drive_num = drive_num;
                   6609:        dpb->unit_num = drive_num;
1.1.1.42  root     6610:        
                   6611:        if(drive_param->valid) {
                   6612:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6613:                
                   6614:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6615:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6616:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6617:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6618:                switch(geo->MediaType) {
                   6619:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6620:                        dpb->media_type = 0xff;
                   6621:                        break;
                   6622:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6623:                        dpb->media_type = 0xfe;
                   6624:                        break;
                   6625:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6626:                        dpb->media_type = 0xfd;
                   6627:                        break;
                   6628:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6629:                        dpb->media_type = 0xfc;
                   6630:                        break;
                   6631:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6632:                case F3_1Pt2_512:
                   6633:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6634:                case F5_720_512:
                   6635:                        dpb->media_type = 0xf9;
                   6636:                        break;
                   6637:                case FixedMedia:        // hard disk
                   6638:                case RemovableMedia:
                   6639:                case Unknown:
                   6640:                        dpb->media_type = 0xf8;
                   6641:                        break;
                   6642:                default:
                   6643:                        dpb->media_type = 0xf0;
                   6644:                        break;
                   6645:                }
                   6646:        }
1.1.1.41  root     6647:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6648:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6649:        dpb->info_sector = 0xffff;
                   6650:        dpb->backup_boot_sector = 0xffff;
                   6651:        dpb->free_clusters = 0xffff;
                   6652:        dpb->free_search_cluster = 0xffffffff;
                   6653:        
                   6654:        return(drive_param->valid);
1.1       root     6655: }
                   6656: 
                   6657: // pc bios
                   6658: 
1.1.1.35  root     6659: #ifdef USE_SERVICE_THREAD
                   6660: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6661: {
                   6662: #if defined(HAS_I386)
                   6663:        if(m_SF != 0) {
                   6664:                m_SF = 0;
1.1.1.49  root     6665:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6666:        } else {
                   6667:                m_SF = 1;
1.1.1.49  root     6668:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6669:        }
                   6670: #else
                   6671:        if(m_SignVal < 0) {
                   6672:                m_SignVal = 0;
1.1.1.49  root     6673:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6674:        } else {
                   6675:                m_SignVal = -1;
1.1.1.49  root     6676:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6677:        }
                   6678: #endif
1.1.1.49  root     6679:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     6680:        in_service = true;
                   6681:        service_exit = false;
                   6682:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6683: }
                   6684: 
                   6685: void finish_service_loop()
                   6686: {
                   6687:        if(in_service && service_exit) {
                   6688: #if defined(HAS_I386)
                   6689:                if(m_SF != 0) {
                   6690:                        m_SF = 0;
                   6691:                } else {
                   6692:                        m_SF = 1;
                   6693:                }
                   6694: #else
                   6695:                if(m_SignVal < 0) {
                   6696:                        m_SignVal = 0;
                   6697:                } else {
                   6698:                        m_SignVal = -1;
                   6699:                }
                   6700: #endif
                   6701:                in_service = false;
                   6702:        }
                   6703: }
                   6704: #endif
                   6705: 
1.1.1.19  root     6706: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6707: {
                   6708:        static unsigned __int64 start_msec_since_midnight = 0;
                   6709:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6710:        
                   6711:        if(start_msec_since_midnight == 0) {
                   6712:                SYSTEMTIME time;
                   6713:                GetLocalTime(&time);
                   6714:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6715:                start_msec_since_hostboot = cur_msec;
                   6716:        }
                   6717:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6718:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6719:        return (UINT32)tick;
                   6720: }
                   6721: 
                   6722: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6723: {
                   6724:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6725:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6726:        
                   6727:        if(prev_tick > next_tick) {
                   6728:                mem[0x470] = 1;
                   6729:        }
                   6730:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6731: }
                   6732: 
1.1.1.14  root     6733: inline void pcbios_irq0()
                   6734: {
                   6735:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6736:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6737: }
                   6738: 
1.1.1.16  root     6739: int pcbios_get_text_vram_address(int page)
1.1       root     6740: {
                   6741:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6742:                return TEXT_VRAM_TOP;
1.1       root     6743:        } else {
1.1.1.14  root     6744:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6745:        }
                   6746: }
                   6747: 
1.1.1.16  root     6748: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6749: {
1.1.1.14  root     6750:        if(!int_10h_feh_called) {
1.1.1.16  root     6751:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6752:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6753:                return SHADOW_BUF_TOP;
                   6754:        } else {
1.1.1.14  root     6755:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6756:        }
                   6757: }
                   6758: 
1.1.1.16  root     6759: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6760: {
1.1.1.16  root     6761:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6762: }
                   6763: 
1.1.1.16  root     6764: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6765: {
1.1.1.14  root     6766:        // clear the existing screen, not just the new one
                   6767:        int clr_height = max(height, scr_height);
                   6768:        
1.1.1.16  root     6769:        if(scr_width != width || scr_height != height) {
                   6770:                change_console_size(width, height);
1.1.1.14  root     6771:        }
                   6772:        mem[0x462] = 0;
                   6773:        *(UINT16 *)(mem + 0x44e) = 0;
                   6774:        
1.1.1.16  root     6775:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6776:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6777:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6778:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     6779:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     6780:        
1.1.1.23  root     6781:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6782:        if(clr_screen) {
1.1.1.14  root     6783:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6784:                        mem[ofs++] = 0x20;
                   6785:                        mem[ofs++] = 0x07;
                   6786:                }
                   6787:                
1.1.1.35  root     6788: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6789:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6790: #endif
1.1.1.14  root     6791:                for(int y = 0; y < clr_height; y++) {
                   6792:                        for(int x = 0; x < scr_width; x++) {
                   6793:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6794:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6795:                        }
                   6796:                }
                   6797:                SMALL_RECT rect;
1.1.1.14  root     6798:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6799:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6800:                vram_length_char = vram_last_length_char = 0;
                   6801:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6802: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6803:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6804: #endif
1.1       root     6805:        }
1.1.1.14  root     6806:        COORD co;
                   6807:        co.X = 0;
                   6808:        co.Y = scr_top;
                   6809:        SetConsoleCursorPosition(hStdout, co);
                   6810:        cursor_moved = true;
                   6811:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6812: }
                   6813: 
1.1.1.36  root     6814: void pcbios_update_cursor_position()
                   6815: {
                   6816:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6817:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6818:        if(!restore_console_on_exit) {
                   6819:                scr_top = csbi.srWindow.Top;
                   6820:        }
                   6821:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6822:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6823: }
                   6824: 
1.1.1.16  root     6825: inline void pcbios_int_10h_00h()
                   6826: {
                   6827:        switch(REG8(AL) & 0x7f) {
                   6828:        case 0x70: // v-text mode
                   6829:        case 0x71: // extended cga v-text mode
                   6830:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6831:                break;
                   6832:        default:
                   6833:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6834:                break;
                   6835:        }
                   6836:        if(REG8(AL) & 0x80) {
                   6837:                mem[0x487] |= 0x80;
                   6838:        } else {
                   6839:                mem[0x487] &= ~0x80;
                   6840:        }
                   6841:        mem[0x449] = REG8(AL) & 0x7f;
                   6842: }
                   6843: 
1.1       root     6844: inline void pcbios_int_10h_01h()
                   6845: {
1.1.1.13  root     6846:        mem[0x460] = REG8(CL);
                   6847:        mem[0x461] = REG8(CH);
1.1.1.14  root     6848:        
1.1.1.23  root     6849:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6850:        CONSOLE_CURSOR_INFO ci;
                   6851:        GetConsoleCursorInfo(hStdout, &ci);
                   6852: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6853: //     if(ci.bVisible) {
                   6854:                int lines = max(8, REG8(CL) + 1);
                   6855:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6856: //     }
                   6857:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6858: }
                   6859: 
                   6860: inline void pcbios_int_10h_02h()
                   6861: {
1.1.1.14  root     6862:        // continuously setting the cursor effectively stops it blinking
                   6863:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6864:                COORD co;
                   6865:                co.X = REG8(DL);
1.1.1.14  root     6866:                co.Y = REG8(DH) + scr_top;
                   6867:                
                   6868:                // some programs hide the cursor by moving it off screen
                   6869:                static bool hidden = false;
1.1.1.23  root     6870:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6871:                CONSOLE_CURSOR_INFO ci;
                   6872:                GetConsoleCursorInfo(hStdout, &ci);
                   6873:                
                   6874:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6875:                        if(ci.bVisible) {
                   6876:                                ci.bVisible = FALSE;
                   6877: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6878:                                hidden = true;
                   6879:                        }
                   6880:                } else if(hidden) {
                   6881:                        if(!ci.bVisible) {
                   6882:                                ci.bVisible = TRUE;
                   6883: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6884:                        }
                   6885:                        hidden = false;
                   6886:                }
1.1       root     6887:        }
1.1.1.14  root     6888:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6889:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6890: }
                   6891: 
                   6892: inline void pcbios_int_10h_03h()
                   6893: {
1.1.1.14  root     6894:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6895:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6896:        REG8(CL) = mem[0x460];
                   6897:        REG8(CH) = mem[0x461];
                   6898: }
                   6899: 
                   6900: inline void pcbios_int_10h_05h()
                   6901: {
1.1.1.14  root     6902:        if(REG8(AL) >= vram_pages) {
                   6903:                return;
                   6904:        }
                   6905:        if(mem[0x462] != REG8(AL)) {
                   6906:                vram_flush();
                   6907:                
1.1.1.23  root     6908:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6909:                SMALL_RECT rect;
1.1.1.14  root     6910:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6911:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6912:                
1.1.1.16  root     6913:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6914:                        for(int x = 0; x < scr_width; x++) {
                   6915:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6916:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6917:                        }
                   6918:                }
1.1.1.16  root     6919:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6920:                        for(int x = 0; x < scr_width; x++) {
                   6921:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6922:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6923:                        }
                   6924:                }
1.1.1.14  root     6925:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6926:                
                   6927:                COORD co;
1.1.1.14  root     6928:                co.X = mem[0x450 + REG8(AL) * 2];
                   6929:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6930:                if(co.Y < scr_top + scr_height) {
                   6931:                        SetConsoleCursorPosition(hStdout, co);
                   6932:                }
1.1       root     6933:        }
1.1.1.14  root     6934:        mem[0x462] = REG8(AL);
                   6935:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   6936:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     6937:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     6938:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     6939:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     6940:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     6941:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     6942: }
                   6943: 
                   6944: inline void pcbios_int_10h_06h()
                   6945: {
1.1.1.14  root     6946:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6947:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6948:                return;
                   6949:        }
                   6950:        vram_flush();
                   6951:        
1.1.1.23  root     6952:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6953:        SMALL_RECT rect;
1.1.1.14  root     6954:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6955:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6956:        
                   6957:        int right = min(REG8(DL), scr_width - 1);
                   6958:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6959:        
                   6960:        if(REG8(AL) == 0) {
1.1.1.14  root     6961:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6962:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6963:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6964:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6965:                        }
                   6966:                }
                   6967:        } else {
1.1.1.14  root     6968:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     6969:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6970:                                if(y2 <= bottom) {
                   6971:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6972:                                } else {
1.1.1.14  root     6973:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6974:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6975:                                }
1.1.1.14  root     6976:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6977:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6978:                        }
                   6979:                }
                   6980:        }
1.1.1.14  root     6981:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6982: }
                   6983: 
                   6984: inline void pcbios_int_10h_07h()
                   6985: {
1.1.1.14  root     6986:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6987:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6988:                return;
                   6989:        }
                   6990:        vram_flush();
                   6991:        
1.1.1.23  root     6992:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6993:        SMALL_RECT rect;
1.1.1.14  root     6994:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6995:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6996:        
                   6997:        int right = min(REG8(DL), scr_width - 1);
                   6998:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6999:        
                   7000:        if(REG8(AL) == 0) {
1.1.1.14  root     7001:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7002:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7003:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7004:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7005:                        }
                   7006:                }
                   7007:        } else {
1.1.1.14  root     7008:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     7009:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7010:                                if(y2 >= REG8(CH)) {
                   7011:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7012:                                } else {
1.1.1.14  root     7013:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7014:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7015:                                }
1.1.1.14  root     7016:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7017:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7018:                        }
                   7019:                }
                   7020:        }
1.1.1.14  root     7021:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7022: }
                   7023: 
                   7024: inline void pcbios_int_10h_08h()
                   7025: {
                   7026:        COORD co;
                   7027:        DWORD num;
                   7028:        
1.1.1.14  root     7029:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7030:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7031:        
                   7032:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7033:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7034:                co.Y += scr_top;
                   7035:                vram_flush();
1.1       root     7036:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   7037:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   7038:                REG8(AL) = scr_char[0];
                   7039:                REG8(AH) = scr_attr[0];
                   7040:        } else {
1.1.1.16  root     7041:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     7042:        }
                   7043: }
                   7044: 
                   7045: inline void pcbios_int_10h_09h()
                   7046: {
                   7047:        COORD co;
                   7048:        
1.1.1.14  root     7049:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7050:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7051:        
1.1.1.16  root     7052:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7053:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7054:        
                   7055:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7056: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7057:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7058: #endif
1.1.1.16  root     7059:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7060:                while(dest < end) {
                   7061:                        write_text_vram_char(dest - vram, REG8(AL));
                   7062:                        mem[dest++] = REG8(AL);
                   7063:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7064:                        mem[dest++] = REG8(BL);
1.1       root     7065:                }
1.1.1.35  root     7066: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7067:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7068: #endif
1.1       root     7069:        } else {
1.1.1.14  root     7070:                while(dest < end) {
1.1       root     7071:                        mem[dest++] = REG8(AL);
                   7072:                        mem[dest++] = REG8(BL);
                   7073:                }
                   7074:        }
                   7075: }
                   7076: 
                   7077: inline void pcbios_int_10h_0ah()
                   7078: {
                   7079:        COORD co;
                   7080:        
1.1.1.14  root     7081:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7082:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7083:        
1.1.1.16  root     7084:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7085:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7086:        
                   7087:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7088: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7089:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7090: #endif
1.1.1.16  root     7091:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7092:                while(dest < end) {
                   7093:                        write_text_vram_char(dest - vram, REG8(AL));
                   7094:                        mem[dest++] = REG8(AL);
                   7095:                        dest++;
1.1       root     7096:                }
1.1.1.35  root     7097: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7098:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7099: #endif
1.1       root     7100:        } else {
1.1.1.14  root     7101:                while(dest < end) {
1.1       root     7102:                        mem[dest++] = REG8(AL);
                   7103:                        dest++;
                   7104:                }
                   7105:        }
                   7106: }
                   7107: 
1.1.1.40  root     7108: HDC get_console_window_device_context()
                   7109: {
                   7110:        static HWND hwndFound = 0;
                   7111:        
                   7112:        if(hwndFound == 0) {
                   7113:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   7114:                char pszNewWindowTitle[1024];
                   7115:                char pszOldWindowTitle[1024];
                   7116:                
                   7117:                GetConsoleTitle(pszOldWindowTitle, 1024);
                   7118:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
                   7119:                SetConsoleTitle(pszNewWindowTitle);
                   7120:                Sleep(100);
                   7121:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
                   7122:                SetConsoleTitle(pszOldWindowTitle);
                   7123:        }
                   7124:        return GetDC(hwndFound);
                   7125: }
                   7126: 
                   7127: inline void pcbios_int_10h_0ch()
                   7128: {
                   7129:        HDC hdc = get_console_window_device_context();
                   7130:        
                   7131:        if(hdc != NULL) {
                   7132:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7133:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7134:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7135:                
                   7136:                if(REG8(AL) & 0x80) {
                   7137:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7138:                        if(color != CLR_INVALID) {
                   7139:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7140:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7141:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7142:                        }
                   7143:                }
                   7144:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7145:        }
                   7146: }
                   7147: 
                   7148: inline void pcbios_int_10h_0dh()
                   7149: {
                   7150:        HDC hdc = get_console_window_device_context();
                   7151:        BYTE r = 0;
                   7152:        BYTE g = 0;
                   7153:        BYTE b = 0;
                   7154:        
                   7155:        if(hdc != NULL) {
                   7156:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7157:                if(color != CLR_INVALID) {
                   7158:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7159:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7160:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7161:                }
                   7162:        }
                   7163:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7164: }
                   7165: 
1.1       root     7166: inline void pcbios_int_10h_0eh()
                   7167: {
1.1.1.14  root     7168:        DWORD num;
                   7169:        COORD co;
                   7170:        
1.1.1.54  root     7171:        co.X = mem[0x450 + mem[0x462] * 2];
                   7172:        co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14  root     7173:        
                   7174:        if(REG8(AL) == 7) {
                   7175:                //MessageBeep(-1);
                   7176:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7177:                if(REG8(AL) == 10) {
                   7178:                        vram_flush();
                   7179:                }
1.1.1.23  root     7180:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7181:                cursor_moved = true;
                   7182:        } else {
1.1.1.54  root     7183:                int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35  root     7184: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7185:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7186: #endif
1.1.1.54  root     7187:                int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
                   7188:                write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7189: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7190:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7191: #endif
1.1.1.54  root     7192:                
                   7193:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   7194:                if(++co.X == scr_width) {
                   7195:                        co.X = 0;
                   7196:                        if(++co.Y == scr_height) {
                   7197:                                vram_flush();
                   7198:                                WriteConsole(hStdout, "\n", 1, &num, NULL);
1.1.1.14  root     7199:                                cursor_moved = true;
                   7200:                        }
                   7201:                }
1.1.1.54  root     7202:                if(!cursor_moved) {
                   7203:                        co.Y += scr_top;
                   7204:                        SetConsoleCursorPosition(hStdout, co);
                   7205:                        cursor_moved = true;
                   7206:                }
1.1.1.14  root     7207:                mem[dest] = REG8(AL);
                   7208:        }
1.1       root     7209: }
                   7210: 
                   7211: inline void pcbios_int_10h_0fh()
                   7212: {
                   7213:        REG8(AL) = mem[0x449];
                   7214:        REG8(AH) = mem[0x44a];
                   7215:        REG8(BH) = mem[0x462];
                   7216: }
                   7217: 
1.1.1.14  root     7218: inline void pcbios_int_10h_11h()
                   7219: {
                   7220:        switch(REG8(AL)) {
1.1.1.16  root     7221:        case 0x01:
1.1.1.14  root     7222:        case 0x11:
1.1.1.54  root     7223: //             pcbios_set_console_size(80, 28, true);
1.1.1.14  root     7224:                break;
1.1.1.16  root     7225:        case 0x02:
1.1.1.14  root     7226:        case 0x12:
1.1.1.54  root     7227: //             pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7228:                break;
1.1.1.16  root     7229:        case 0x04:
1.1.1.14  root     7230:        case 0x14:
1.1.1.54  root     7231: //             pcbios_set_console_size(80, 25, true);
1.1.1.14  root     7232:                break;
1.1.1.54  root     7233: //     case 0x18:
                   7234: //             pcbios_set_console_size(80, 50, true);
                   7235: //             break;
1.1.1.14  root     7236:        case 0x30:
                   7237:                SREG(ES) = 0;
                   7238:                i386_load_segment_descriptor(ES);
                   7239:                REG16(BP) = 0;
                   7240:                REG16(CX) = mem[0x485];
                   7241:                REG8(DL) = mem[0x484];
                   7242:                break;
1.1.1.54  root     7243:        default:
                   7244:                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));
                   7245:                m_CF = 1;
                   7246:                break;
1.1.1.14  root     7247:        }
                   7248: }
                   7249: 
                   7250: inline void pcbios_int_10h_12h()
                   7251: {
1.1.1.16  root     7252:        switch(REG8(BL)) {
                   7253:        case 0x10:
1.1.1.14  root     7254:                REG16(BX) = 0x0003;
                   7255:                REG16(CX) = 0x0009;
1.1.1.16  root     7256:                break;
1.1.1.14  root     7257:        }
                   7258: }
                   7259: 
1.1       root     7260: inline void pcbios_int_10h_13h()
                   7261: {
1.1.1.3   root     7262:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7263:        COORD co;
                   7264:        DWORD num;
                   7265:        
                   7266:        co.X = REG8(DL);
1.1.1.14  root     7267:        co.Y = REG8(DH) + scr_top;
                   7268:        
                   7269:        vram_flush();
1.1       root     7270:        
                   7271:        switch(REG8(AL)) {
                   7272:        case 0x00:
                   7273:        case 0x01:
                   7274:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7275:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7276:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7277:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7278:                        SetConsoleCursorPosition(hStdout, co);
                   7279:                        
                   7280:                        if(csbi.wAttributes != REG8(BL)) {
                   7281:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7282:                        }
1.1.1.14  root     7283:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   7284:                        
1.1       root     7285:                        if(csbi.wAttributes != REG8(BL)) {
                   7286:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7287:                        }
                   7288:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7289:                                if(!restore_console_on_exit) {
                   7290:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7291:                                        scr_top = csbi.srWindow.Top;
                   7292:                                }
1.1.1.14  root     7293:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7294:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7295:                                SetConsoleCursorPosition(hStdout, co);
                   7296:                        } else {
                   7297:                                cursor_moved = true;
                   7298:                        }
                   7299:                } else {
1.1.1.3   root     7300:                        m_CF = 1;
1.1       root     7301:                }
                   7302:                break;
                   7303:        case 0x02:
                   7304:        case 0x03:
                   7305:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7306:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7307:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7308:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7309:                        SetConsoleCursorPosition(hStdout, co);
                   7310:                        
                   7311:                        WORD wAttributes = csbi.wAttributes;
                   7312:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7313:                                if(wAttributes != mem[ofs + 1]) {
                   7314:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7315:                                        wAttributes = mem[ofs + 1];
                   7316:                                }
1.1.1.14  root     7317:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7318:                        }
                   7319:                        if(csbi.wAttributes != wAttributes) {
                   7320:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7321:                        }
                   7322:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7323:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7324:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7325:                                SetConsoleCursorPosition(hStdout, co);
                   7326:                        } else {
                   7327:                                cursor_moved = true;
                   7328:                        }
                   7329:                } else {
1.1.1.3   root     7330:                        m_CF = 1;
1.1       root     7331:                }
                   7332:                break;
                   7333:        case 0x10:
                   7334:        case 0x11:
                   7335:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7336:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7337:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7338:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7339:                        for(int i = 0; i < num; i++) {
                   7340:                                mem[ofs++] = scr_char[i];
                   7341:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7342:                                if(REG8(AL) & 0x01) {
1.1       root     7343:                                        mem[ofs++] = 0;
                   7344:                                        mem[ofs++] = 0;
                   7345:                                }
                   7346:                        }
                   7347:                } else {
1.1.1.16  root     7348:                        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     7349:                                mem[ofs++] = mem[src++];
                   7350:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7351:                                if(REG8(AL) & 0x01) {
1.1       root     7352:                                        mem[ofs++] = 0;
                   7353:                                        mem[ofs++] = 0;
                   7354:                                }
1.1.1.14  root     7355:                                if(++co.X == scr_width) {
                   7356:                                        if(++co.Y == scr_height) {
1.1       root     7357:                                                break;
                   7358:                                        }
                   7359:                                        co.X = 0;
                   7360:                                }
                   7361:                        }
                   7362:                }
                   7363:                break;
1.1.1.45  root     7364:        case 0x12: // ???
                   7365:        case 0x13: // ???
1.1       root     7366:        case 0x20:
                   7367:        case 0x21:
                   7368:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7369:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7370:                        int len = min(REG16(CX), scr_width * scr_height);
                   7371:                        for(int i = 0; i < len; i++) {
1.1       root     7372:                                scr_char[i] = mem[ofs++];
                   7373:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7374:                                if(REG8(AL) & 0x01) {
1.1       root     7375:                                        ofs += 2;
                   7376:                                }
                   7377:                        }
1.1.1.14  root     7378:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7379:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7380:                } else {
1.1.1.16  root     7381:                        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     7382:                                mem[dest++] = mem[ofs++];
                   7383:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7384:                                if(REG8(AL) & 0x01) {
1.1       root     7385:                                        ofs += 2;
                   7386:                                }
1.1.1.14  root     7387:                                if(++co.X == scr_width) {
                   7388:                                        if(++co.Y == scr_height) {
1.1       root     7389:                                                break;
                   7390:                                        }
                   7391:                                        co.X = 0;
                   7392:                                }
                   7393:                        }
                   7394:                }
                   7395:                break;
                   7396:        default:
1.1.1.22  root     7397:                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     7398:                m_CF = 1;
1.1       root     7399:                break;
                   7400:        }
                   7401: }
                   7402: 
1.1.1.30  root     7403: inline void pcbios_int_10h_18h()
                   7404: {
                   7405:        switch(REG8(AL)) {
                   7406:        case 0x00:
                   7407:        case 0x01:
                   7408: //             REG8(AL) = 0x86;
                   7409:                REG8(AL) = 0x00;
                   7410:                break;
                   7411:        default:
                   7412:                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));
                   7413:                m_CF = 1;
                   7414:                break;
                   7415:        }
                   7416: }
                   7417: 
1.1.1.14  root     7418: inline void pcbios_int_10h_1ah()
                   7419: {
                   7420:        switch(REG8(AL)) {
                   7421:        case 0x00:
                   7422:                REG8(AL) = 0x1a;
                   7423:                REG8(BL) = 0x08;
                   7424:                REG8(BH) = 0x00;
                   7425:                break;
                   7426:        default:
1.1.1.22  root     7427:                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     7428:                m_CF = 1;
                   7429:                break;
                   7430:        }
                   7431: }
                   7432: 
1.1       root     7433: inline void pcbios_int_10h_1dh()
                   7434: {
                   7435:        switch(REG8(AL)) {
1.1.1.43  root     7436:        case 0x00:
                   7437:                // DOS/V Shift Status Line Control is not supported
                   7438:                m_CF = 1;
                   7439:                break;
1.1       root     7440:        case 0x01:
                   7441:                break;
                   7442:        case 0x02:
                   7443:                REG16(BX) = 0;
                   7444:                break;
                   7445:        default:
1.1.1.22  root     7446:                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));
                   7447:                m_CF = 1;
                   7448:                break;
                   7449:        }
                   7450: }
                   7451: 
                   7452: inline void pcbios_int_10h_4fh()
                   7453: {
                   7454:        switch(REG8(AL)) {
                   7455:        case 0x00:
                   7456:                REG8(AH) = 0x02; // not supported
                   7457:                break;
                   7458:        case 0x01:
                   7459:        case 0x02:
                   7460:        case 0x03:
                   7461:        case 0x04:
                   7462:        case 0x05:
                   7463:        case 0x06:
                   7464:        case 0x07:
                   7465:        case 0x08:
                   7466:        case 0x09:
                   7467:        case 0x0a:
                   7468:        case 0x0b:
                   7469:        case 0x0c:
                   7470:                REG8(AH) = 0x01; // failed
                   7471:                break;
                   7472:        default:
                   7473:                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     7474:                m_CF = 1;
1.1       root     7475:                break;
                   7476:        }
                   7477: }
                   7478: 
                   7479: inline void pcbios_int_10h_82h()
                   7480: {
                   7481:        static UINT8 mode = 0;
                   7482:        
                   7483:        switch(REG8(AL)) {
1.1.1.22  root     7484:        case 0x00:
1.1       root     7485:                if(REG8(BL) != 0xff) {
                   7486:                        mode = REG8(BL);
                   7487:                }
                   7488:                REG8(AL) = mode;
                   7489:                break;
                   7490:        default:
1.1.1.22  root     7491:                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     7492:                m_CF = 1;
1.1       root     7493:                break;
                   7494:        }
                   7495: }
                   7496: 
1.1.1.22  root     7497: inline void pcbios_int_10h_83h()
                   7498: {
                   7499:        static UINT8 mode = 0;
                   7500:        
                   7501:        switch(REG8(AL)) {
                   7502:        case 0x00:
                   7503:                REG16(AX) = 0; // offset???
                   7504:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7505:                i386_load_segment_descriptor(ES);
                   7506:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7507:                break;
                   7508:        default:
                   7509:                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));
                   7510:                m_CF = 1;
                   7511:                break;
                   7512:        }
                   7513: }
                   7514: 
                   7515: inline void pcbios_int_10h_90h()
                   7516: {
                   7517:        REG8(AL) = mem[0x449];
                   7518: }
                   7519: 
                   7520: inline void pcbios_int_10h_91h()
                   7521: {
                   7522:        REG8(AL) = 0x04; // VGA
                   7523: }
                   7524: 
                   7525: inline void pcbios_int_10h_efh()
                   7526: {
                   7527:        REG16(DX) = 0xffff;
                   7528: }
                   7529: 
1.1       root     7530: inline void pcbios_int_10h_feh()
                   7531: {
                   7532:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7533:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7534:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7535:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7536:        }
                   7537:        int_10h_feh_called = true;
                   7538: }
                   7539: 
                   7540: inline void pcbios_int_10h_ffh()
                   7541: {
                   7542:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7543:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7544:                COORD co;
                   7545:                DWORD num;
                   7546:                
1.1.1.14  root     7547:                vram_flush();
                   7548:                
                   7549:                co.X = (REG16(DI) >> 1) % scr_width;
                   7550:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7551:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7552:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7553:                int len;
                   7554:                for(len = 0; ofs < end; len++) {
                   7555:                        scr_char[len] = mem[ofs++];
                   7556:                        scr_attr[len] = mem[ofs++];
                   7557:                }
                   7558:                co.Y += scr_top;
                   7559:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7560:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7561:        }
                   7562:        int_10h_ffh_called = true;
                   7563: }
                   7564: 
1.1.1.42  root     7565: int pcbios_update_drive_param(int drive_num, int force_update)
                   7566: {
                   7567:        if(drive_num >= 0 && drive_num < 26) {
                   7568:                drive_param_t *drive_param = &drive_params[drive_num];
                   7569:                
                   7570:                if(force_update || !drive_param->initialized) {
                   7571:                        drive_param->valid = 0;
                   7572:                        char dev[64];
                   7573:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7574:                        
                   7575:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7576:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7577:                                DWORD dwSize;
                   7578:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7579:                                        drive_param->valid = 1;
                   7580:                                }
                   7581:                                CloseHandle(hFile);
                   7582:                        }
                   7583:                        drive_param->initialized = 1;
                   7584:                }
                   7585:                return(drive_param->valid);
                   7586:        }
                   7587:        return(0);
                   7588: }
                   7589: 
                   7590: inline void pcbios_int_13h_00h()
                   7591: {
                   7592:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7593:        
                   7594:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7595:                REG8(AH) = 0x00; // successful completion
                   7596:        } else {
                   7597:                if(REG8(DL) & 0x80) {
                   7598:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7599:                } else {
                   7600:                        REG8(AH) = 0x80; // timeout (not ready)
                   7601:                }
                   7602:                m_CF = 1;
                   7603:        }
                   7604: }
                   7605: 
                   7606: inline void pcbios_int_13h_02h()
                   7607: {
                   7608:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7609:        
                   7610:        if(REG8(AL) == 0) {
                   7611:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7612:                m_CF = 1;
                   7613:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7614:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7615:                m_CF = 1;
                   7616:        } else {
                   7617:                drive_param_t *drive_param = &drive_params[drive_num];
                   7618:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7619:                char dev[64];
                   7620:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7621:                
                   7622:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7623:                if(hFile == INVALID_HANDLE_VALUE) {
                   7624:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7625:                        m_CF = 1;
                   7626:                } else {
                   7627:                        UINT32 sector_num = REG8(AL);
                   7628:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7629:                        UINT32 head = REG8(DH);
                   7630:                        UINT32 sector = REG8(CL) & 0x3f;
                   7631:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7632:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7633:                        DWORD dwSize;
                   7634:                        
                   7635: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7636: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7637: //                             m_CF = 1;
                   7638: //                     } else 
                   7639:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7640:                                REG8(AH) = 0x04; // sector not found/read error
                   7641:                                m_CF = 1;
                   7642:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7643:                                REG8(AH) = 0x04; // sector not found/read error
                   7644:                                m_CF = 1;
                   7645:                        } else {
                   7646:                                REG8(AH) = 0x00; // successful completion
                   7647:                        }
                   7648:                        CloseHandle(hFile);
                   7649:                }
                   7650:        }
                   7651: }
                   7652: 
                   7653: inline void pcbios_int_13h_03h()
                   7654: {
                   7655:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7656:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7657:        
                   7658:        if(REG8(AL) == 0) {
                   7659:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7660:                m_CF = 1;
                   7661:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7662:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7663:                m_CF = 1;
                   7664:        } else if(!drive_params[drive_num].is_fdd()) {
                   7665:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7666:                m_CF = 1;
                   7667:        } else {
                   7668:                drive_param_t *drive_param = &drive_params[drive_num];
                   7669:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7670:                char dev[64];
                   7671:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7672:                
                   7673:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7674:                if(hFile == INVALID_HANDLE_VALUE) {
                   7675:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7676:                        m_CF = 1;
                   7677:                } else {
                   7678:                        UINT32 sector_num = REG8(AL);
                   7679:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7680:                        UINT32 head = REG8(DH);
                   7681:                        UINT32 sector = REG8(CL) & 0x3f;
                   7682:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7683:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7684:                        DWORD dwSize;
                   7685:                        
                   7686: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7687: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7688: //                             m_CF = 1;
                   7689: //                     } else 
                   7690:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7691:                                REG8(AH) = 0x04; // sector not found/read error
                   7692:                                m_CF = 1;
                   7693:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7694:                                REG8(AH) = 0x04; // sector not found/read error
                   7695:                                m_CF = 1;
                   7696:                        } else {
                   7697:                                REG8(AH) = 0x00; // successful completion
                   7698:                        }
                   7699:                        CloseHandle(hFile);
                   7700:                }
                   7701:        }
                   7702: }
                   7703: 
                   7704: inline void pcbios_int_13h_04h()
                   7705: {
                   7706:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7707:        
                   7708:        if(REG8(AL) == 0) {
                   7709:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7710:                m_CF = 1;
                   7711:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7712:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7713:                m_CF = 1;
                   7714:        } else {
                   7715:                drive_param_t *drive_param = &drive_params[drive_num];
                   7716:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7717:                char dev[64];
                   7718:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7719:                
                   7720:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7721:                if(hFile == INVALID_HANDLE_VALUE) {
                   7722:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7723:                        m_CF = 1;
                   7724:                } else {
                   7725:                        UINT32 sector_num = REG8(AL);
                   7726:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7727:                        UINT32 head = REG8(DH);
                   7728:                        UINT32 sector = REG8(CL) & 0x3f;
                   7729:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7730:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7731:                        DWORD dwSize;
                   7732:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   7733:                        
                   7734: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7735: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7736: //                             m_CF = 1;
                   7737: //                     } else 
                   7738:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7739:                                REG8(AH) = 0x04; // sector not found/read error
                   7740:                                m_CF = 1;
                   7741:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7742:                                REG8(AH) = 0x04; // sector not found/read error
                   7743:                                m_CF = 1;
                   7744:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   7745:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   7746:                                m_CF = 1;
                   7747:                        } else {
                   7748:                                REG8(AH) = 0x00; // successful completion
                   7749:                        }
                   7750:                        free(tmp_buffer);
                   7751:                        CloseHandle(hFile);
                   7752:                }
                   7753:        }
                   7754: }
                   7755: 
                   7756: inline void pcbios_int_13h_08h()
                   7757: {
                   7758:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7759:        
                   7760:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7761:                drive_param_t *drive_param = &drive_params[drive_num];
                   7762:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7763:                
                   7764:                REG16(AX) = 0x0000;
                   7765:                switch(geo->MediaType) {
                   7766:                case F5_360_512:
                   7767:                case F5_320_512:
                   7768:                case F5_320_1024:
                   7769:                case F5_180_512:
                   7770:                case F5_160_512:
                   7771:                        REG8(BL) = 0x01; // 320K/360K disk
                   7772:                        break;
                   7773:                case F5_1Pt2_512:
                   7774:                case F3_1Pt2_512:
                   7775:                case F3_1Pt23_1024:
                   7776:                case F5_1Pt23_1024:
                   7777:                        REG8(BL) = 0x02; // 1.2M disk
                   7778:                        break;
                   7779:                case F3_720_512:
                   7780:                case F3_640_512:
                   7781:                case F5_640_512:
                   7782:                case F5_720_512:
                   7783:                        REG8(BL) = 0x03; // 720K disk
                   7784:                        break;
                   7785:                case F3_1Pt44_512:
                   7786:                        REG8(BL) = 0x04; // 1.44M disk
                   7787:                        break;
                   7788:                case F3_2Pt88_512:
                   7789:                        REG8(BL) = 0x06; // 2.88M disk
                   7790:                        break;
                   7791:                case RemovableMedia:
                   7792:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   7793:                        break;
                   7794:                default:
                   7795:                        REG8(BL) = 0x00; // unknown
                   7796:                        break;
                   7797:                }
                   7798:                if(REG8(DL) & 0x80) {
                   7799:                        switch(GetLogicalDrives() & 0x0c) {
                   7800:                        case 0x00: REG8(DL) = 0x00; break;
                   7801:                        case 0x04:
                   7802:                        case 0x08: REG8(DL) = 0x01; break;
                   7803:                        case 0x0c: REG8(DL) = 0x02; break;
                   7804:                        }
                   7805:                } else {
                   7806:                        switch(GetLogicalDrives() & 0x03) {
                   7807:                        case 0x00: REG8(DL) = 0x00; break;
                   7808:                        case 0x01:
                   7809:                        case 0x02: REG8(DL) = 0x01; break;
                   7810:                        case 0x03: REG8(DL) = 0x02; break;
                   7811:                        }
                   7812:                }
                   7813:                REG8(DH) = drive_param->head_num();
                   7814:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   7815:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   7816:                REG8(CH) = cyl & 0xff;
                   7817:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   7818:        } else {
                   7819:                REG8(AH) = 0x07;
                   7820:                m_CF = 1;
                   7821:        }
                   7822: }
                   7823: 
                   7824: inline void pcbios_int_13h_10h()
                   7825: {
                   7826:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7827:        
                   7828:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7829:                REG8(AH) = 0x00; // successful completion
                   7830:        } else {
                   7831:                if(REG8(DL) & 0x80) {
                   7832:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   7833:                } else {
                   7834:                        REG8(AH) = 0x80; // timeout (not ready)
                   7835:                }
                   7836:                m_CF = 1;
                   7837:        }
                   7838: }
                   7839: 
                   7840: inline void pcbios_int_13h_15h()
                   7841: {
                   7842:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7843:        
                   7844:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7845:                if(REG8(DL) & 0x80) {
                   7846:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   7847:                } else {
                   7848:                        REG8(AH) = 0x03; // hard disk
                   7849:                }
                   7850:        } else {
                   7851:                REG8(AH) = 0x00; // no such drive
                   7852:        }
                   7853: }
                   7854: 
1.1.1.43  root     7855: inline void pcbios_int_13h_41h()
                   7856: {
                   7857:        if(REG16(BX) == 0x55aa) {
                   7858:                // IBM/MS INT 13 Extensions is not installed
                   7859:                REG8(AH) = 0x01;
                   7860:                m_CF = 1;
                   7861:        } else {
                   7862:                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));
                   7863:                REG8(AH) = 0x01;
                   7864:                m_CF = 1;
                   7865:        }
                   7866: }
                   7867: 
1.1.1.25  root     7868: inline void pcbios_int_14h_00h()
                   7869: {
1.1.1.29  root     7870:        if(REG16(DX) < 4) {
1.1.1.25  root     7871:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7872:                UINT8 selector = sio_read(REG16(DX), 3);
                   7873:                selector &= ~0x3f;
                   7874:                selector |= REG8(AL) & 0x1f;
                   7875:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7876:                sio_write(REG16(DX), 3, selector | 0x80);
                   7877:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7878:                sio_write(REG16(DX), 1, divisor >> 8);
                   7879:                sio_write(REG16(DX), 3, selector);
                   7880:                REG8(AH) = sio_read(REG16(DX), 5);
                   7881:                REG8(AL) = sio_read(REG16(DX), 6);
                   7882:        } else {
                   7883:                REG8(AH) = 0x80;
                   7884:        }
                   7885: }
                   7886: 
                   7887: inline void pcbios_int_14h_01h()
                   7888: {
1.1.1.29  root     7889:        if(REG16(DX) < 4) {
1.1.1.25  root     7890:                UINT8 selector = sio_read(REG16(DX), 3);
                   7891:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7892:                sio_write(REG16(DX), 0, REG8(AL));
                   7893:                sio_write(REG16(DX), 3, selector);
                   7894:                REG8(AH) = sio_read(REG16(DX), 5);
                   7895:        } else {
                   7896:                REG8(AH) = 0x80;
                   7897:        }
                   7898: }
                   7899: 
                   7900: inline void pcbios_int_14h_02h()
                   7901: {
1.1.1.29  root     7902:        if(REG16(DX) < 4) {
1.1.1.25  root     7903:                UINT8 selector = sio_read(REG16(DX), 3);
                   7904:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7905:                REG8(AL) = sio_read(REG16(DX), 0);
                   7906:                sio_write(REG16(DX), 3, selector);
                   7907:                REG8(AH) = sio_read(REG16(DX), 5);
                   7908:        } else {
                   7909:                REG8(AH) = 0x80;
                   7910:        }
                   7911: }
                   7912: 
                   7913: inline void pcbios_int_14h_03h()
                   7914: {
1.1.1.29  root     7915:        if(REG16(DX) < 4) {
1.1.1.25  root     7916:                REG8(AH) = sio_read(REG16(DX), 5);
                   7917:                REG8(AL) = sio_read(REG16(DX), 6);
                   7918:        } else {
                   7919:                REG8(AH) = 0x80;
                   7920:        }
                   7921: }
                   7922: 
                   7923: inline void pcbios_int_14h_04h()
                   7924: {
1.1.1.29  root     7925:        if(REG16(DX) < 4) {
1.1.1.25  root     7926:                UINT8 selector = sio_read(REG16(DX), 3);
                   7927:                if(REG8(CH) <= 0x03) {
                   7928:                        selector = (selector & ~0x03) | REG8(CH);
                   7929:                }
                   7930:                if(REG8(BL) == 0x00) {
                   7931:                        selector &= ~0x04;
                   7932:                } else if(REG8(BL) == 0x01) {
                   7933:                        selector |= 0x04;
                   7934:                }
                   7935:                if(REG8(BH) == 0x00) {
                   7936:                        selector = (selector & ~0x38) | 0x00;
                   7937:                } else if(REG8(BH) == 0x01) {
                   7938:                        selector = (selector & ~0x38) | 0x08;
                   7939:                } else if(REG8(BH) == 0x02) {
                   7940:                        selector = (selector & ~0x38) | 0x18;
                   7941:                } else if(REG8(BH) == 0x03) {
                   7942:                        selector = (selector & ~0x38) | 0x28;
                   7943:                } else if(REG8(BH) == 0x04) {
                   7944:                        selector = (selector & ~0x38) | 0x38;
                   7945:                }
                   7946:                if(REG8(AL) == 0x00) {
                   7947:                        selector |= 0x40;
                   7948:                } else if(REG8(AL) == 0x01) {
                   7949:                        selector &= ~0x40;
                   7950:                }
                   7951:                if(REG8(CL) <= 0x0b) {
                   7952:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   7953:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   7954:                        sio_write(REG16(DX), 3, selector | 0x80);
                   7955:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   7956:                        sio_write(REG16(DX), 1, divisor >> 8);
                   7957:                }
                   7958:                sio_write(REG16(DX), 3, selector);
                   7959:                REG8(AH) = sio_read(REG16(DX), 5);
                   7960:                REG8(AL) = sio_read(REG16(DX), 6);
                   7961:        } else {
                   7962:                REG8(AH) = 0x80;
                   7963:        }
                   7964: }
                   7965: 
                   7966: inline void pcbios_int_14h_05h()
                   7967: {
1.1.1.29  root     7968:        if(REG16(DX) < 4) {
1.1.1.25  root     7969:                if(REG8(AL) == 0x00) {
                   7970:                        REG8(BL) = sio_read(REG16(DX), 4);
                   7971:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7972:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7973:                } else if(REG8(AL) == 0x01) {
                   7974:                        sio_write(REG16(DX), 4, REG8(BL));
                   7975:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7976:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7977:                } else {
                   7978:                        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));
                   7979:                }
                   7980:        } else {
                   7981:                REG8(AH) = 0x80;
                   7982:        }
                   7983: }
                   7984: 
1.1.1.14  root     7985: inline void pcbios_int_15h_10h()
                   7986: {
1.1.1.22  root     7987:        switch(REG8(AL)) {
                   7988:        case 0x00:
1.1.1.14  root     7989:                Sleep(10);
1.1.1.35  root     7990:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     7991:                break;
                   7992:        default:
                   7993:                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     7994:                REG8(AH) = 0x86;
                   7995:                m_CF = 1;
                   7996:        }
                   7997: }
                   7998: 
1.1       root     7999: inline void pcbios_int_15h_23h()
                   8000: {
                   8001:        switch(REG8(AL)) {
1.1.1.22  root     8002:        case 0x00:
1.1.1.8   root     8003:                REG8(CL) = cmos_read(0x2d);
                   8004:                REG8(CH) = cmos_read(0x2e);
1.1       root     8005:                break;
1.1.1.22  root     8006:        case 0x01:
1.1.1.8   root     8007:                cmos_write(0x2d, REG8(CL));
                   8008:                cmos_write(0x2e, REG8(CH));
1.1       root     8009:                break;
                   8010:        default:
1.1.1.22  root     8011:                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     8012:                REG8(AH) = 0x86;
1.1.1.3   root     8013:                m_CF = 1;
1.1       root     8014:                break;
                   8015:        }
                   8016: }
                   8017: 
                   8018: inline void pcbios_int_15h_24h()
                   8019: {
                   8020:        switch(REG8(AL)) {
1.1.1.22  root     8021:        case 0x00:
1.1.1.3   root     8022:                i386_set_a20_line(0);
1.1       root     8023:                REG8(AH) = 0;
                   8024:                break;
1.1.1.22  root     8025:        case 0x01:
1.1.1.3   root     8026:                i386_set_a20_line(1);
1.1       root     8027:                REG8(AH) = 0;
                   8028:                break;
1.1.1.22  root     8029:        case 0x02:
1.1       root     8030:                REG8(AH) = 0;
1.1.1.3   root     8031:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     8032:                REG16(CX) = 0;
                   8033:                break;
1.1.1.22  root     8034:        case 0x03:
1.1       root     8035:                REG16(AX) = 0;
                   8036:                REG16(BX) = 0;
                   8037:                break;
1.1.1.22  root     8038:        default:
                   8039:                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));
                   8040:                REG8(AH) = 0x86;
                   8041:                m_CF = 1;
                   8042:                break;
1.1       root     8043:        }
                   8044: }
                   8045: 
                   8046: inline void pcbios_int_15h_49h()
                   8047: {
1.1.1.27  root     8048:        REG8(AH) = 0x00;
                   8049:        REG8(BL) = 0x00; // DOS/V
1.1       root     8050: }
                   8051: 
1.1.1.22  root     8052: inline void pcbios_int_15h_50h()
                   8053: {
                   8054:        switch(REG8(AL)) {
                   8055:        case 0x00:
                   8056:        case 0x01:
                   8057:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8058:                        REG8(AH) = 0x01; // invalid font type in bh
                   8059:                        m_CF = 1;
1.1.1.27  root     8060:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8061:                        REG8(AH) = 0x02; // bl not zero
                   8062:                        m_CF = 1;
                   8063:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8064:                        REG8(AH) = 0x04; // invalid code page
                   8065:                        m_CF = 1;
1.1.1.27  root     8066:                } else if(REG8(AL) == 0x01) {
                   8067:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8068:                        m_CF = 1;
1.1.1.27  root     8069:                } else {
1.1.1.49  root     8070:                        // dummy font read routine is at fffc:000d
                   8071:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8072:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8073:                        REG16(BX) = 0x000d;
1.1.1.27  root     8074:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8075:                }
                   8076:                break;
                   8077:        default:
                   8078:                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));
                   8079:                REG8(AH) = 0x86;
                   8080:                m_CF = 1;
                   8081:                break;
                   8082:        }
                   8083: }
                   8084: 
1.1.1.30  root     8085: inline void pcbios_int_15h_53h()
                   8086: {
                   8087:        switch(REG8(AL)) {
                   8088:        case 0x00:
                   8089:                // APM is not installed
                   8090:                REG8(AH) = 0x86;
                   8091:                m_CF = 1;
                   8092:                break;
                   8093:        default:
                   8094:                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));
                   8095:                REG8(AH) = 0x86;
                   8096:                m_CF = 1;
                   8097:                break;
                   8098:        }
                   8099: }
                   8100: 
1.1.1.43  root     8101: inline void pcbios_int_15h_84h()
                   8102: {
                   8103:        // joystick support (from DOSBox)
                   8104:        switch(REG16(DX)) {
                   8105:        case 0x00:
                   8106:                REG16(AX) = 0x00f0;
                   8107:                REG16(DX) = 0x0201;
                   8108:                m_CF = 1;
                   8109:                break;
                   8110:        case 0x01:
                   8111:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8112:                m_CF = 1;
                   8113:                break;
                   8114:        default:
                   8115:                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));
                   8116:                REG8(AH) = 0x86;
                   8117:                m_CF = 1;
                   8118:                break;
                   8119:        }
                   8120: }
1.1.1.35  root     8121: 
                   8122: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8123: {
                   8124:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8125:        UINT32 msec = usec / 1000;
                   8126:        
1.1.1.54  root     8127:        while(msec && !m_exit) {
1.1.1.14  root     8128:                UINT32 tmp = min(msec, 100);
                   8129:                if(msec - tmp < 10) {
                   8130:                        tmp = msec;
                   8131:                }
                   8132:                Sleep(tmp);
                   8133:                msec -= tmp;
                   8134:        }
1.1.1.35  root     8135:        
                   8136: #ifdef USE_SERVICE_THREAD
                   8137:        service_exit = true;
                   8138: #endif
                   8139:        return(0);
                   8140: }
                   8141: 
                   8142: inline void pcbios_int_15h_86h()
                   8143: {
                   8144:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8145: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8146:                if(!in_service && !in_service_29h) {
                   8147:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8148:                } else {
                   8149: #endif
                   8150:                        pcbios_int_15h_86h_thread(NULL);
                   8151:                        REQUEST_HARDWRE_UPDATE();
                   8152: #ifdef USE_SERVICE_THREAD
                   8153:                }
1.1.1.35  root     8154: #endif
                   8155:        }
1.1       root     8156: }
                   8157: 
                   8158: inline void pcbios_int_15h_87h()
                   8159: {
                   8160:        // copy extended memory (from DOSBox)
                   8161:        int len = REG16(CX) * 2;
1.1.1.3   root     8162:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8163:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8164:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8165:        memcpy(mem + dst, mem + src, len);
                   8166:        REG16(AX) = 0x00;
                   8167: }
                   8168: 
                   8169: inline void pcbios_int_15h_88h()
                   8170: {
1.1.1.17  root     8171:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8172: }
                   8173: 
                   8174: inline void pcbios_int_15h_89h()
                   8175: {
1.1.1.21  root     8176: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8177:        // switch to protected mode (from DOSBox)
                   8178:        write_io_byte(0x20, 0x10);
                   8179:        write_io_byte(0x21, REG8(BH));
                   8180:        write_io_byte(0x21, 0x00);
                   8181:        write_io_byte(0xa0, 0x10);
                   8182:        write_io_byte(0xa1, REG8(BL));
                   8183:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8184:        i386_set_a20_line(1);
                   8185:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8186:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8187:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8188:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8189:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8190: #if defined(HAS_I386)
                   8191:        m_cr[0] |= 1;
                   8192: #else
                   8193:        m_msw |= 1;
                   8194: #endif
                   8195:        SREG(DS) = 0x18;
                   8196:        SREG(ES) = 0x20;
                   8197:        SREG(SS) = 0x28;
                   8198:        i386_load_segment_descriptor(DS);
                   8199:        i386_load_segment_descriptor(ES);
                   8200:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8201:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8202:        REG16(SP) += 6;
1.1.1.3   root     8203: #if defined(HAS_I386)
1.1.1.21  root     8204:        UINT32 flags = get_flags();
                   8205:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8206:        set_flags(flags);
1.1.1.3   root     8207: #else
1.1.1.21  root     8208:        UINT32 flags = CompressFlags();
                   8209:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8210:        ExpandFlags(flags);
1.1.1.3   root     8211: #endif
1.1       root     8212:        REG16(AX) = 0x00;
1.1.1.21  root     8213:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8214: #else
1.1.1.21  root     8215:        // i86/i186/v30: protected mode is not supported
1.1       root     8216:        REG8(AH) = 0x86;
1.1.1.3   root     8217:        m_CF = 1;
1.1       root     8218: #endif
                   8219: }
                   8220: 
1.1.1.21  root     8221: inline void pcbios_int_15h_8ah()
                   8222: {
                   8223:        UINT32 size = MAX_MEM - 0x100000;
                   8224:        REG16(AX) = size & 0xffff;
                   8225:        REG16(DX) = size >> 16;
                   8226: }
                   8227: 
1.1.1.54  root     8228: #ifdef EXT_BIOS_TOP
                   8229: inline void pcbios_int_15h_c1h()
                   8230: {
                   8231:        SREG(ES) = EXT_BIOS_TOP >> 4;
                   8232:        i386_load_segment_descriptor(ES);
                   8233: }
                   8234: #endif
                   8235: 
                   8236: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
                   8237: {
                   8238:        // from DOSBox DoPS2Callback()
                   8239:        UINT16 mdat = 0x08;
                   8240:        INT16 xdiff = mouse.position.x - mouse.prev_position.x;
                   8241:        INT16 ydiff = mouse.prev_position.y - mouse.position.y;
                   8242:        
                   8243:        if(mouse.buttons[0].status) {
                   8244:                mdat |= 0x01;
                   8245:        }
                   8246:        if(mouse.buttons[1].status) {
                   8247:                mdat |= 0x02;
                   8248:        }
                   8249:        mouse.prev_position.x = mouse.position.x;
                   8250:        mouse.prev_position.y = mouse.position.y;
                   8251:        if((xdiff > 0xff) || (xdiff < -0xff)) {
                   8252:                mdat |= 0x40;   // x overflow
                   8253:        }
                   8254:        if((ydiff > 0xff) || (ydiff < -0xff)) {
                   8255:                mdat |= 0x80;   // y overflow
                   8256:        }
                   8257:        xdiff %= 256;
                   8258:        ydiff %= 256;
                   8259:        if(xdiff < 0) {
                   8260:                xdiff = (0x100 + xdiff);
                   8261:                mdat |= 0x10;
                   8262:        }
                   8263:        if(ydiff < 0) {
                   8264:                ydiff = (0x100 + ydiff);
                   8265:                mdat |= 0x20;
                   8266:        }
                   8267:        *data_1st = (UINT16)mdat;
                   8268:        *data_2nd = (UINT16)(xdiff % 256);
                   8269:        *data_3rd = (UINT16)(ydiff % 256);
                   8270: }
                   8271: 
                   8272: inline void pcbios_int_15h_c2h()
                   8273: {
                   8274:        static UINT8 enabled = 0;
                   8275:        static UINT8 sampling_rate = 5;
                   8276:        static UINT8 resolution = 2;
                   8277:        static UINT8 scaling = 1;
                   8278:        
                   8279:        switch(REG8(AL)) {
                   8280:        case 0x00:
                   8281:                if(REG8(BH) == 0x00 || REG8(BH) == 0x01) {
                   8282:                        enabled = REG8(BH);
                   8283:                        REG8(AH) = 0x00; // successful
                   8284:                } else {
                   8285:                        REG8(AH) = 0x01; // invalid function
                   8286:                        m_CF = 1;
                   8287:                }
                   8288:                break;
                   8289:        case 0x01:
                   8290:                REG8(BH) = 0x00; // device id
                   8291:                REG8(BL) = 0xaa; // mouse
                   8292:        case 0x05:
                   8293:                enabled = 0;
                   8294:                sampling_rate = 5;
                   8295:                resolution = 2;
                   8296:                scaling = 1;
                   8297:                REG8(AH) = 0x00; // successful
                   8298:                break;
                   8299:        case 0x02:
                   8300:                sampling_rate = REG8(BH);
                   8301:                REG8(AH) = 0x00; // successful
                   8302:                break;
                   8303:        case 0x03:
                   8304:                resolution = REG8(BH);
                   8305:                REG8(AH) = 0x00; // successful
                   8306:                break;
                   8307:        case 0x04:
                   8308:                REG8(BH) = 0x00; // device id
                   8309:                REG8(AH) = 0x00; // successful
                   8310:                break;
                   8311:        case 0x06:
                   8312:                switch(REG8(BH)) {
                   8313:                case 0x00:
                   8314:                        REG8(BL) = 0x00;
                   8315:                        if(mouse.buttons[1].status) {
                   8316:                                REG8(BL) |= 0x01;
                   8317:                        }
                   8318:                        if(mouse.buttons[0].status) {
                   8319:                                REG8(BL) |= 0x04;
                   8320:                        }
                   8321:                        if(scaling == 2) {
                   8322:                                REG8(BL) |= 0x10;
                   8323:                        }
                   8324:                        REG8(CL) = resolution;
                   8325:                        switch(sampling_rate) {
                   8326:                        case 0:  REG8(DL) =  10; break;
                   8327:                        case 1:  REG8(DL) =  20; break;
                   8328:                        case 2:  REG8(DL) =  40; break;
                   8329:                        case 3:  REG8(DL) =  60; break;
                   8330:                        case 4:  REG8(DL) =  80; break;
                   8331: //                     case 5:  REG8(DL) = 100; break;
                   8332:                        case 6:  REG8(DL) = 200; break;
                   8333:                        default: REG8(DL) = 100; break;
                   8334:                        }
                   8335:                        REG8(AH) = 0x00; // successful
                   8336:                        break;
                   8337:                case 0x01:
                   8338:                case 0x02:
                   8339:                        scaling = REG8(BH);
                   8340:                        REG8(AH) = 0x00; // successful
                   8341:                        break;
                   8342:                default:
                   8343:                        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));
                   8344:                        REG8(AH) = 0x01; // invalid function
                   8345:                        m_CF = 1;
                   8346:                        break;
                   8347:                }
                   8348:                break;
                   8349:        case 0x07: // set device handler addr
                   8350:                mouse.call_addr_ps2.w.l = REG16(BX);
                   8351:                mouse.call_addr_ps2.w.h = SREG(ES);
                   8352:                REG8(AH) = 0x00; // successful
                   8353:                break;
                   8354:        case 0x08:
                   8355:                REG8(AH) = 0x00; // successful
                   8356:                break;
                   8357:        case 0x09:
                   8358:                {
                   8359:                        UINT16 data_1st, data_2nd, data_3rd;
                   8360:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   8361:                        REG8(BL) = (UINT8)(data_1st & 0xff);
                   8362:                        REG8(CL) = (UINT8)(data_2nd & 0xff);
                   8363:                        REG8(DL) = (UINT8)(data_3rd & 0xff);
                   8364:                }
                   8365:                REG8(AH) = 0x00; // successful
                   8366:                break;
                   8367:        default:
                   8368:                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));
                   8369: //             REG8(AH) = 0x86;
                   8370:                REG8(AH) = 0x01; // invalid function
                   8371:                m_CF = 1;
                   8372:                break;
                   8373:        }
                   8374: }
                   8375: 
1.1.1.3   root     8376: #if defined(HAS_I386)
1.1       root     8377: inline void pcbios_int_15h_c9h()
                   8378: {
                   8379:        REG8(AH) = 0x00;
                   8380:        REG8(CH) = cpu_type;
                   8381:        REG8(CL) = cpu_step;
                   8382: }
1.1.1.3   root     8383: #endif
1.1       root     8384: 
                   8385: inline void pcbios_int_15h_cah()
                   8386: {
                   8387:        switch(REG8(AL)) {
1.1.1.22  root     8388:        case 0x00:
1.1       root     8389:                if(REG8(BL) > 0x3f) {
                   8390:                        REG8(AH) = 0x03;
1.1.1.3   root     8391:                        m_CF = 1;
1.1       root     8392:                } else if(REG8(BL) < 0x0e) {
                   8393:                        REG8(AH) = 0x04;
1.1.1.3   root     8394:                        m_CF = 1;
1.1       root     8395:                } else {
1.1.1.8   root     8396:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8397:                }
                   8398:                break;
1.1.1.22  root     8399:        case 0x01:
1.1       root     8400:                if(REG8(BL) > 0x3f) {
                   8401:                        REG8(AH) = 0x03;
1.1.1.3   root     8402:                        m_CF = 1;
1.1       root     8403:                } else if(REG8(BL) < 0x0e) {
                   8404:                        REG8(AH) = 0x04;
1.1.1.3   root     8405:                        m_CF = 1;
1.1       root     8406:                } else {
1.1.1.8   root     8407:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8408:                }
                   8409:                break;
                   8410:        default:
1.1.1.22  root     8411:                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     8412:                REG8(AH) = 0x86;
1.1.1.3   root     8413:                m_CF = 1;
1.1       root     8414:                break;
                   8415:        }
                   8416: }
                   8417: 
1.1.1.22  root     8418: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8419: {
1.1.1.22  root     8420:        switch(REG8(AL)) {
                   8421: #if defined(HAS_I386)
                   8422:        case 0x01:
                   8423:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8424:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8425:                break;
1.1.1.17  root     8426: #endif
1.1.1.22  root     8427:        default:
                   8428:                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));
                   8429:                REG8(AH) = 0x86;
                   8430:                m_CF = 1;
                   8431:                break;
                   8432:        }
                   8433: }
1.1.1.17  root     8434: 
1.1.1.55! root     8435: bool pcbios_is_key_buffer_empty()
        !          8436: {
        !          8437:        return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
        !          8438: }
        !          8439: 
1.1.1.51  root     8440: void pcbios_clear_key_buffer()
                   8441: {
                   8442:        key_buf_char->clear();
                   8443:        key_buf_scan->clear();
                   8444:        
                   8445:        // update key buffer
                   8446:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8447: }
                   8448: 
                   8449: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8450: {
                   8451:        // update key buffer
                   8452:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8453:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8454:        UINT16 next = tail + 2;
                   8455:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8456:                next = *(UINT16 *)(mem + 0x480);
                   8457:        }
                   8458:        if(next != head) {
                   8459:                *(UINT16 *)(mem + 0x41c) = next;
                   8460:                mem[0x400 + (tail++)] = key_char;
                   8461:                mem[0x400 + (tail++)] = key_scan;
1.1.1.55! root     8462:        } else {
        !          8463:                // store to extra key buffer
        !          8464:                if(key_buf_char != NULL && key_buf_scan != NULL) {
        !          8465:                        key_buf_char->write(key_char);
        !          8466:                        key_buf_scan->write(key_scan);
        !          8467:                }
1.1.1.51  root     8468:        }
                   8469: }
                   8470: 
                   8471: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8472: {
                   8473:        // update key buffer
                   8474:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8475:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8476:        UINT16 next = head + 2;
                   8477:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8478:                next = *(UINT16 *)(mem + 0x480);
                   8479:        }
                   8480:        if(head != tail) {
                   8481:                *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55! root     8482:                *key_char = mem[0x400 + (head++)];
        !          8483:                *key_scan = mem[0x400 + (head++)];
        !          8484:                
        !          8485:                // restore from extra key buffer
        !          8486:                if(key_buf_char != NULL && key_buf_scan != NULL) {
        !          8487:                        if(!key_buf_char->empty()) {
        !          8488:                                pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
        !          8489:                        }
        !          8490:                }
        !          8491:                return(true);
        !          8492:        } else {
        !          8493:                *key_char = 0x00;
        !          8494:                *key_scan = 0x00;
        !          8495:                return(false);
1.1.1.51  root     8496:        }
                   8497: }
                   8498: 
1.1.1.33  root     8499: void pcbios_update_key_code(bool wait)
1.1       root     8500: {
1.1.1.32  root     8501:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8502: #ifdef USE_SERVICE_THREAD
                   8503:                EnterCriticalSection(&key_buf_crit_sect);
                   8504: #endif
1.1.1.55! root     8505:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     8506: #ifdef USE_SERVICE_THREAD
                   8507:                LeaveCriticalSection(&key_buf_crit_sect);
                   8508: #endif
                   8509:                if(empty) {
1.1.1.32  root     8510:                        if(!update_key_buffer()) {
1.1.1.33  root     8511:                                if(wait) {
1.1.1.32  root     8512:                                        Sleep(10);
                   8513:                                } else {
                   8514:                                        maybe_idle();
                   8515:                                }
1.1.1.14  root     8516:                        }
                   8517:                }
1.1.1.34  root     8518:        }
                   8519:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8520: #ifdef USE_SERVICE_THREAD
                   8521:                EnterCriticalSection(&key_buf_crit_sect);
                   8522: #endif
1.1.1.51  root     8523:                int key_char, key_scan;
                   8524:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8525:                        key_code  = key_char << 0;
                   8526:                        key_code |= key_scan << 8;
1.1.1.35  root     8527:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8528:                }
                   8529:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8530:                        key_code |= key_char << 16;
                   8531:                        key_code |= key_scan << 24;
1.1.1.33  root     8532:                        key_recv |= 0xffff0000;
1.1.1.32  root     8533:                }
1.1.1.35  root     8534: #ifdef USE_SERVICE_THREAD
                   8535:                LeaveCriticalSection(&key_buf_crit_sect);
                   8536: #endif
1.1       root     8537:        }
                   8538: }
                   8539: 
1.1.1.35  root     8540: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8541: {
1.1.1.54  root     8542:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     8543:                pcbios_update_key_code(true);
1.1       root     8544:        }
1.1.1.33  root     8545:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8546:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8547:                        if(REG8(AH) == 0x10) {
                   8548:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8549:                        } else {
                   8550:                                key_code = ((key_code >> 16) & 0xff00);
                   8551:                        }
                   8552:                        key_recv >>= 16;
1.1       root     8553:                }
                   8554:        }
                   8555:        REG16(AX) = key_code & 0xffff;
                   8556:        key_code >>= 16;
1.1.1.33  root     8557:        key_recv >>= 16;
1.1.1.35  root     8558:        
                   8559: #ifdef USE_SERVICE_THREAD
                   8560:        service_exit = true;
                   8561: #endif
                   8562:        return(0);
                   8563: }
                   8564: 
                   8565: inline void pcbios_int_16h_00h()
                   8566: {
                   8567: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8568:        if(!in_service && !in_service_29h) {
                   8569:                start_service_loop(pcbios_int_16h_00h_thread);
                   8570:        } else {
                   8571: #endif
                   8572:                pcbios_int_16h_00h_thread(NULL);
                   8573:                REQUEST_HARDWRE_UPDATE();
                   8574: #ifdef USE_SERVICE_THREAD
                   8575:        }
1.1.1.35  root     8576: #endif
1.1       root     8577: }
                   8578: 
                   8579: inline void pcbios_int_16h_01h()
                   8580: {
1.1.1.33  root     8581:        if(key_recv == 0) {
                   8582:                pcbios_update_key_code(false);
1.1.1.5   root     8583:        }
1.1.1.33  root     8584:        if(key_recv != 0) {
                   8585:                UINT32 key_code_tmp = key_code;
                   8586:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8587:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8588:                                if(REG8(AH) == 0x11) {
                   8589:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8590:                                } else {
                   8591:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8592:                                }
                   8593:                        }
1.1       root     8594:                }
1.1.1.5   root     8595:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8596: #if defined(HAS_I386)
1.1.1.33  root     8597:                m_ZF = 0;
                   8598: #else
                   8599:                m_ZeroVal = 1;
                   8600: #endif
                   8601:        } else {
                   8602: #if defined(HAS_I386)
                   8603:                m_ZF = 1;
1.1.1.3   root     8604: #else
1.1.1.33  root     8605:                m_ZeroVal = 0;
1.1.1.3   root     8606: #endif
1.1.1.33  root     8607:        }
1.1       root     8608: }
                   8609: 
                   8610: inline void pcbios_int_16h_02h()
                   8611: {
                   8612:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8613:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8614:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8615:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8616:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8617:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8618:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8619:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8620: }
                   8621: 
                   8622: inline void pcbios_int_16h_03h()
                   8623: {
                   8624:        static UINT16 status = 0;
                   8625:        
                   8626:        switch(REG8(AL)) {
                   8627:        case 0x05:
                   8628:                status = REG16(BX);
                   8629:                break;
                   8630:        case 0x06:
                   8631:                REG16(BX) = status;
                   8632:                break;
                   8633:        default:
1.1.1.3   root     8634:                m_CF = 1;
1.1       root     8635:                break;
                   8636:        }
                   8637: }
                   8638: 
                   8639: inline void pcbios_int_16h_05h()
                   8640: {
1.1.1.32  root     8641:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8642: #ifdef USE_SERVICE_THREAD
                   8643:                EnterCriticalSection(&key_buf_crit_sect);
                   8644: #endif
1.1.1.51  root     8645:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     8646: #ifdef USE_SERVICE_THREAD
                   8647:                LeaveCriticalSection(&key_buf_crit_sect);
                   8648: #endif
1.1.1.32  root     8649:        }
1.1       root     8650:        REG8(AL) = 0x00;
                   8651: }
                   8652: 
                   8653: inline void pcbios_int_16h_12h()
                   8654: {
                   8655:        pcbios_int_16h_02h();
                   8656:        
                   8657:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8658:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8659:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8660:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8661:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8662:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8663:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8664:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8665: }
                   8666: 
                   8667: inline void pcbios_int_16h_13h()
                   8668: {
                   8669:        static UINT16 status = 0;
                   8670:        
                   8671:        switch(REG8(AL)) {
                   8672:        case 0x00:
                   8673:                status = REG16(DX);
                   8674:                break;
                   8675:        case 0x01:
                   8676:                REG16(DX) = status;
                   8677:                break;
                   8678:        default:
1.1.1.22  root     8679:                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     8680:                m_CF = 1;
1.1       root     8681:                break;
                   8682:        }
                   8683: }
                   8684: 
                   8685: inline void pcbios_int_16h_14h()
                   8686: {
                   8687:        static UINT8 status = 0;
                   8688:        
                   8689:        switch(REG8(AL)) {
                   8690:        case 0x00:
                   8691:        case 0x01:
                   8692:                status = REG8(AL);
                   8693:                break;
                   8694:        case 0x02:
                   8695:                REG8(AL) = status;
                   8696:                break;
                   8697:        default:
1.1.1.22  root     8698:                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     8699:                m_CF = 1;
1.1       root     8700:                break;
                   8701:        }
                   8702: }
                   8703: 
1.1.1.24  root     8704: inline void pcbios_int_16h_55h()
                   8705: {
                   8706:        switch(REG8(AL)) {
                   8707:        case 0x00:
                   8708:                // keyboard tsr is not present
                   8709:                break;
                   8710:        case 0xfe:
                   8711:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8712:                break;
                   8713:        case 0xff:
                   8714:                break;
                   8715:        default:
                   8716:                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));
                   8717:                m_CF = 1;
                   8718:                break;
                   8719:        }
                   8720: }
                   8721: 
1.1.1.30  root     8722: inline void pcbios_int_16h_6fh()
                   8723: {
                   8724:        switch(REG8(AL)) {
                   8725:        case 0x00:
                   8726:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8727:                break;
                   8728:        default:
                   8729:                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));
                   8730:                m_CF = 1;
                   8731:                break;
                   8732:        }
                   8733: }
                   8734: 
1.1.1.37  root     8735: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8736: {
                   8737:        UINT8 hi = jis >> 8;
                   8738:        UINT8 lo = jis & 0xff;
                   8739:        
                   8740:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8741:        hi = (hi - 0x21) / 2 + 0x81;
                   8742:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8743:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8744:        
                   8745:        return((hi << 8) + lo);
                   8746: }
                   8747: 
                   8748: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8749: {
                   8750:        UINT8 hi = sjis >> 8;
                   8751:        UINT8 lo = sjis & 0xff;
                   8752:        
                   8753:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8754:                return(0x2121);
                   8755:        }
                   8756:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8757:                return(0x2121);
                   8758:        }
                   8759:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8760:                // gaiji
                   8761:                if(lo >= 0x40 && lo <= 0x7e) {
                   8762:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   8763:                }
                   8764:                if(lo >= 0x80 && lo <= 0x9e) {
                   8765:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   8766:                }
                   8767:                if(lo >= 0x9f && lo <= 0xfc) {
                   8768:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   8769:                }
                   8770:        }
                   8771:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   8772:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   8773:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   8774:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   8775:        
                   8776:        return((hi << 8) + lo);
                   8777: }
                   8778: 
1.1.1.38  root     8779: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   8780: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   8781: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     8782: 
                   8783: void pcbios_printer_out(int c, UINT8 data)
                   8784: {
                   8785:        if(pio[c].conv_mode) {
                   8786:                if(pio[c].sjis_hi != 0) {
                   8787:                        if(!pio[c].jis_mode) {
                   8788:                                printer_out(c, 0x1c);
                   8789:                                printer_out(c, 0x26);
                   8790:                                pio[c].jis_mode = true;
                   8791:                        }
                   8792:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   8793:                        printer_out(c, jis >> 8);
                   8794:                        printer_out(c, jis & 0xff);
                   8795:                        pio[c].sjis_hi = 0;
                   8796:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   8797:                        printer_out(c, data);
                   8798:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8799:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8800:                        }
                   8801:                        pio[c].esc_len++;
                   8802:                        
                   8803:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8804:                        case 0x33: // 1Bh 33h XX
                   8805:                        case 0x4a: // 1Bh 4Ah XX
                   8806:                        case 0x4e: // 1Bh 4Eh XX
                   8807:                        case 0x51: // 1Bh 51h XX
                   8808:                        case 0x55: // 1Bh 55h XX
                   8809:                        case 0x6c: // 1Bh 6Ch XX
                   8810:                        case 0x71: // 1Bh 71h XX
                   8811:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     8812:                                if(pio[c].esc_len == 3) {
                   8813:                                        pio[c].esc_buf[0] = 0x00;
                   8814:                                }
                   8815:                                break;
1.1.1.38  root     8816:                        case 0x24: // 1Bh 24h XX XX
                   8817:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     8818:                                if(pio[c].esc_len == 4) {
                   8819:                                        pio[c].esc_buf[0] = 0x00;
                   8820:                                }
                   8821:                                break;
1.1.1.38  root     8822:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     8823:                                if(pio[c].esc_len >= 3) {
                   8824:                                        switch(pio[c].esc_buf[2]) {
                   8825:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   8826:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   8827:                                                        pio[c].esc_buf[0] = 0x00;
                   8828:                                                }
                   8829:                                                break;
                   8830:                                        case 32: case 33: case 38: case 39: case 40:
                   8831:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   8832:                                                        pio[c].esc_buf[0] = 0x00;
                   8833:                                                }
                   8834:                                                break;
1.1.1.38  root     8835:                                        case 71: case 72: case 73:
                   8836:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   8837:                                                        pio[c].esc_buf[0] = 0x00;
                   8838:                                                }
                   8839:                                                break;
1.1.1.37  root     8840:                                        default:
                   8841:                                                pio[c].esc_buf[0] = 0x00;
                   8842:                                                break;
                   8843:                                        }
                   8844:                                }
                   8845:                                break;
1.1.1.38  root     8846:                        case 0x40: // 1Bh 40h
1.1.1.37  root     8847:                                if(pio[c].jis_mode) {
                   8848:                                        printer_out(c, 0x1c);
                   8849:                                        printer_out(c, 0x2e);
                   8850:                                        pio[c].jis_mode = false;
                   8851:                                }
                   8852:                                pio[c].esc_buf[0] = 0x00;
                   8853:                                break;
1.1.1.38  root     8854:                        case 0x42: // 1Bh 42h data 00h
                   8855:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8856:                                if(pio[c].esc_len >= 3 && data == 0) {
                   8857:                                        pio[c].esc_buf[0] = 0x00;
                   8858:                                }
                   8859:                                break;
1.1.1.38  root     8860:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     8861:                                if(pio[c].esc_len >= 3 && data != 0) {
                   8862:                                        pio[c].esc_buf[0] = 0x00;
                   8863:                                }
                   8864:                                break;
1.1.1.38  root     8865:                        default: // 1Bh XX
1.1.1.37  root     8866:                                pio[c].esc_buf[0] = 0x00;
                   8867:                                break;
                   8868:                        }
                   8869:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   8870:                        printer_out(c, data);
                   8871:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8872:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8873:                        }
                   8874:                        pio[c].esc_len++;
                   8875:                        
                   8876:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8877:                        case 0x21: // 1Ch 21h XX
                   8878:                        case 0x2d: // 1Ch 2Dh XX
                   8879:                        case 0x57: // 1Ch 57h XX
                   8880:                        case 0x6b: // 1Ch 6Bh XX
                   8881:                        case 0x72: // 1Ch 72h XX
                   8882:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     8883:                                if(pio[c].esc_len == 3) {
                   8884:                                        pio[c].esc_buf[0] = 0x00;
                   8885:                                }
                   8886:                                break;
1.1.1.38  root     8887:                        case 0x26: // 1Ch 26h
1.1.1.37  root     8888:                                pio[c].jis_mode = true;
                   8889:                                pio[c].esc_buf[0] = 0x00;
                   8890:                                break;
1.1.1.38  root     8891:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     8892:                                pio[c].jis_mode = false;
                   8893:                                pio[c].esc_buf[0] = 0x00;
                   8894:                                break;
1.1.1.38  root     8895:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     8896:                                if(pio[c].esc_len == 76) {
                   8897:                                        pio[c].esc_buf[0] = 0x00;
                   8898:                                }
                   8899:                                break;
1.1.1.38  root     8900:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8901:                                if(pio[c].esc_len == 6) {
                   8902:                                        pio[c].esc_buf[0] = 0x00;
                   8903:                                }
                   8904:                                break;
1.1.1.38  root     8905:                        case 0x53: // 1Ch 53h XX XX
                   8906:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     8907:                                if(pio[c].esc_len == 4) {
                   8908:                                        pio[c].esc_buf[0] = 0x00;
                   8909:                                }
                   8910:                                break;
1.1.1.38  root     8911:                        default: // 1Ch XX
1.1.1.37  root     8912:                                pio[c].esc_buf[0] = 0x00;
                   8913:                                break;
                   8914:                        }
                   8915:                } else if(data == 0x1b || data == 0x1c) {
                   8916:                        printer_out(c, data);
                   8917:                        pio[c].esc_buf[0] = data;
                   8918:                        pio[c].esc_len = 1;
                   8919:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   8920:                        pio[c].sjis_hi = data;
                   8921:                } else {
                   8922:                        if(pio[c].jis_mode) {
                   8923:                                printer_out(c, 0x1c);
                   8924:                                printer_out(c, 0x2e);
                   8925:                                pio[c].jis_mode = false;
                   8926:                        }
                   8927:                        printer_out(c, data);
                   8928:                }
                   8929:        } else {
                   8930:                if(pio[c].jis_mode) {
                   8931:                        printer_out(c, 0x1c);
                   8932:                        printer_out(c, 0x2e);
                   8933:                        pio[c].jis_mode = false;
                   8934:                }
                   8935:                printer_out(c, data);
                   8936:        }
                   8937: }
                   8938: 
                   8939: inline void pcbios_int_17h_00h()
                   8940: {
                   8941:        if(REG16(DX) < 3) {
                   8942:                pcbios_printer_out(REG16(DX), REG8(AL));
                   8943:                REG8(AH) = 0xd0;
                   8944:        }
                   8945: }
                   8946: 
                   8947: inline void pcbios_int_17h_01h()
                   8948: {
                   8949:        if(REG16(DX) < 3) {
                   8950:                REG8(AH) = 0xd0;
                   8951:        }
                   8952: }
                   8953: 
                   8954: inline void pcbios_int_17h_02h()
                   8955: {
                   8956:        if(REG16(DX) < 3) {
                   8957:                REG8(AH) = 0xd0;
                   8958:        }
                   8959: }
                   8960: 
                   8961: inline void pcbios_int_17h_03h()
                   8962: {
                   8963:        switch(REG8(AL)) {
                   8964:        case 0x00:
                   8965:                if(REG16(DX) < 3) {
                   8966:                        if(pio[REG16(DX)].jis_mode) {
                   8967:                                printer_out(REG16(DX), 0x1c);
                   8968:                                printer_out(REG16(DX), 0x2e);
                   8969:                                pio[REG16(DX)].jis_mode = false;
                   8970:                        }
                   8971:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   8972:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   8973:                        }
                   8974:                        REG16(CX) = 0x0000;
                   8975:                        REG8(AH) = 0xd0;
                   8976:                }
                   8977:                break;
                   8978:        default:
                   8979:                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));
                   8980:                break;
                   8981:        }
                   8982: }
                   8983: 
                   8984: inline void pcbios_int_17h_50h()
                   8985: {
                   8986:        switch(REG8(AL)) {
                   8987:        case 0x00:
                   8988:                if(REG16(DX) < 3) {
                   8989:                        if(REG16(BX) = 0x0001) {
                   8990:                                pio[REG16(DX)].conv_mode = false;
                   8991:                                REG8(AL) = 0x00;
                   8992:                        } else if(REG16(BX) = 0x0051) {
                   8993:                                pio[REG16(DX)].conv_mode = true;
                   8994:                                REG8(AL) = 0x00;
                   8995:                        } else {
                   8996:                                REG8(AL) = 0x01;
                   8997:                        }
                   8998:                } else {
                   8999:                        REG8(AL) = 0x02;
                   9000:                }
                   9001:                break;
                   9002:        case 0x01:
                   9003:                if(REG16(DX) < 3) {
                   9004:                        if(pio[REG16(DX)].conv_mode) {
                   9005:                                REG16(BX) = 0x0051;
                   9006:                        } else {
                   9007:                                REG16(BX) = 0x0001;
                   9008:                        }
                   9009:                        REG8(AL) = 0x00;
                   9010:                } else {
                   9011:                        REG8(AL) = 0x02;
                   9012:                }
                   9013:                break;
                   9014:        default:
                   9015:                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));
                   9016:                break;
                   9017:        }
                   9018: }
                   9019: 
                   9020: inline void pcbios_int_17h_51h()
                   9021: {
                   9022:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9023:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9024:        } else {
                   9025:                REG16(DX) = 0x0000;
                   9026:        }
                   9027: }
                   9028: 
                   9029: inline void pcbios_int_17h_52h()
                   9030: {
                   9031:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9032:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9033:        } else {
                   9034:                REG16(DX) = 0x0000;
                   9035:        }
                   9036: }
                   9037: 
                   9038: inline void pcbios_int_17h_84h()
                   9039: {
                   9040:        if(REG16(DX) < 3) {
                   9041:                if(pio[REG16(DX)].jis_mode) {
                   9042:                        printer_out(REG16(DX), 0x1c);
                   9043:                        printer_out(REG16(DX), 0x2e);
                   9044:                        pio[REG16(DX)].jis_mode = false;
                   9045:                }
                   9046:                printer_out(REG16(DX), REG8(AL));
                   9047:                REG8(AH) = 0xd0;
                   9048:        }
                   9049: }
                   9050: 
                   9051: inline void pcbios_int_17h_85h()
                   9052: {
                   9053:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9054: }
                   9055: 
1.1       root     9056: inline void pcbios_int_1ah_00h()
                   9057: {
1.1.1.19  root     9058:        pcbios_update_daily_timer_counter(timeGetTime());
                   9059:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9060:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9061:        REG8(AL) = mem[0x470];
                   9062:        mem[0x470] = 0;
1.1       root     9063: }
                   9064: 
                   9065: inline int to_bcd(int t)
                   9066: {
                   9067:        int u = (t % 100) / 10;
                   9068:        return (u << 4) | (t % 10);
                   9069: }
                   9070: 
                   9071: inline void pcbios_int_1ah_02h()
                   9072: {
                   9073:        SYSTEMTIME time;
                   9074:        
                   9075:        GetLocalTime(&time);
                   9076:        REG8(CH) = to_bcd(time.wHour);
                   9077:        REG8(CL) = to_bcd(time.wMinute);
                   9078:        REG8(DH) = to_bcd(time.wSecond);
                   9079:        REG8(DL) = 0x00;
                   9080: }
                   9081: 
                   9082: inline void pcbios_int_1ah_04h()
                   9083: {
                   9084:        SYSTEMTIME time;
                   9085:        
                   9086:        GetLocalTime(&time);
                   9087:        REG8(CH) = to_bcd(time.wYear / 100);
                   9088:        REG8(CL) = to_bcd(time.wYear);
                   9089:        REG8(DH) = to_bcd(time.wMonth);
                   9090:        REG8(DL) = to_bcd(time.wDay);
                   9091: }
                   9092: 
                   9093: inline void pcbios_int_1ah_0ah()
                   9094: {
                   9095:        SYSTEMTIME time;
                   9096:        FILETIME file_time;
                   9097:        WORD dos_date, dos_time;
                   9098:        
                   9099:        GetLocalTime(&time);
                   9100:        SystemTimeToFileTime(&time, &file_time);
                   9101:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9102:        REG16(CX) = dos_date;
                   9103: }
                   9104: 
                   9105: // msdos system call
                   9106: 
1.1.1.43  root     9107: inline void msdos_int_21h_56h(int lfn);
                   9108: 
1.1       root     9109: inline void msdos_int_21h_00h()
                   9110: {
1.1.1.3   root     9111:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9112: }
                   9113: 
1.1.1.35  root     9114: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9115: {
                   9116:        REG8(AL) = msdos_getche();
1.1.1.33  root     9117:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9118:        
1.1.1.35  root     9119: #ifdef USE_SERVICE_THREAD
                   9120:        service_exit = true;
                   9121: #endif
                   9122:        return(0);
                   9123: }
                   9124: 
                   9125: inline void msdos_int_21h_01h()
                   9126: {
                   9127: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9128:        if(!in_service && !in_service_29h &&
                   9129:           *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     9130:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9131:                // msdos_putch() will be used in this service
                   9132:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9133:                start_service_loop(msdos_int_21h_01h_thread);
                   9134:        } else {
                   9135: #endif
                   9136:                msdos_int_21h_01h_thread(NULL);
                   9137:                REQUEST_HARDWRE_UPDATE();
                   9138: #ifdef USE_SERVICE_THREAD
                   9139:        }
1.1.1.35  root     9140: #endif
1.1       root     9141: }
                   9142: 
                   9143: inline void msdos_int_21h_02h()
                   9144: {
1.1.1.33  root     9145:        UINT8 data = REG8(DL);
                   9146:        msdos_putch(data);
                   9147:        REG8(AL) = data;
                   9148:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9149: }
                   9150: 
                   9151: inline void msdos_int_21h_03h()
                   9152: {
                   9153:        REG8(AL) = msdos_aux_in();
                   9154: }
                   9155: 
                   9156: inline void msdos_int_21h_04h()
                   9157: {
                   9158:        msdos_aux_out(REG8(DL));
                   9159: }
                   9160: 
                   9161: inline void msdos_int_21h_05h()
                   9162: {
                   9163:        msdos_prn_out(REG8(DL));
                   9164: }
                   9165: 
                   9166: inline void msdos_int_21h_06h()
                   9167: {
                   9168:        if(REG8(DL) == 0xff) {
                   9169:                if(msdos_kbhit()) {
                   9170:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9171: #if defined(HAS_I386)
                   9172:                        m_ZF = 0;
                   9173: #else
                   9174:                        m_ZeroVal = 1;
                   9175: #endif
1.1       root     9176:                } else {
                   9177:                        REG8(AL) = 0;
1.1.1.3   root     9178: #if defined(HAS_I386)
                   9179:                        m_ZF = 1;
                   9180: #else
                   9181:                        m_ZeroVal = 0;
                   9182: #endif
1.1.1.14  root     9183:                        maybe_idle();
1.1       root     9184:                }
                   9185:        } else {
1.1.1.33  root     9186:                UINT8 data = REG8(DL);
                   9187:                msdos_putch(data);
                   9188:                REG8(AL) = data;
1.1       root     9189:        }
                   9190: }
                   9191: 
1.1.1.35  root     9192: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9193: {
                   9194:        REG8(AL) = msdos_getch();
1.1.1.26  root     9195:        
1.1.1.35  root     9196: #ifdef USE_SERVICE_THREAD
                   9197:        service_exit = true;
                   9198: #endif
                   9199:        return(0);
1.1       root     9200: }
                   9201: 
1.1.1.35  root     9202: inline void msdos_int_21h_07h()
                   9203: {
                   9204: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9205:        if(!in_service && !in_service_29h) {
                   9206:                start_service_loop(msdos_int_21h_07h_thread);
                   9207:        } else {
                   9208: #endif
                   9209:                msdos_int_21h_07h_thread(NULL);
                   9210:                REQUEST_HARDWRE_UPDATE();
                   9211: #ifdef USE_SERVICE_THREAD
                   9212:        }
1.1.1.35  root     9213: #endif
                   9214: }
                   9215: 
                   9216: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9217: {
                   9218:        REG8(AL) = msdos_getch();
1.1.1.33  root     9219:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9220:        
1.1.1.35  root     9221: #ifdef USE_SERVICE_THREAD
                   9222:        service_exit = true;
                   9223: #endif
                   9224:        return(0);
                   9225: }
                   9226: 
                   9227: inline void msdos_int_21h_08h()
                   9228: {
                   9229: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9230:        if(!in_service && !in_service_29h) {
                   9231:                start_service_loop(msdos_int_21h_08h_thread);
                   9232:        } else {
                   9233: #endif
                   9234:                msdos_int_21h_08h_thread(NULL);
                   9235:                REQUEST_HARDWRE_UPDATE();
                   9236: #ifdef USE_SERVICE_THREAD
                   9237:        }
1.1.1.35  root     9238: #endif
1.1       root     9239: }
                   9240: 
                   9241: inline void msdos_int_21h_09h()
                   9242: {
1.1.1.21  root     9243:        msdos_stdio_reopen();
                   9244:        
1.1.1.20  root     9245:        process_t *process = msdos_process_info_get(current_psp);
                   9246:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9247:        
1.1.1.14  root     9248:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9249:        int len = 0;
1.1       root     9250:        
1.1.1.14  root     9251:        while(str[len] != '$' && len < 0x10000) {
                   9252:                len++;
                   9253:        }
1.1.1.20  root     9254:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9255:                // stdout is redirected to file
1.1.1.20  root     9256:                msdos_write(fd, str, len);
1.1       root     9257:        } else {
                   9258:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9259:                        msdos_putch(str[i]);
1.1       root     9260:                }
                   9261:        }
1.1.1.33  root     9262:        REG8(AL) = '$';
                   9263:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9264: }
                   9265: 
1.1.1.35  root     9266: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9267: {
1.1.1.3   root     9268:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9269:        int max = mem[ofs] - 1;
                   9270:        UINT8 *buf = mem + ofs + 2;
                   9271:        int chr, p = 0;
                   9272:        
                   9273:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9274:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9275:                        p = 0;
1.1.1.33  root     9276:                        msdos_putch(0x03);
                   9277:                        msdos_putch(0x0d);
                   9278:                        msdos_putch(0x0a);
1.1.1.26  root     9279:                        break;
1.1.1.33  root     9280:                } else if(ctrl_break_pressed) {
                   9281:                        // skip this byte
1.1.1.26  root     9282:                } else if(chr == 0x00) {
1.1       root     9283:                        // skip 2nd byte
                   9284:                        msdos_getch();
                   9285:                } else if(chr == 0x08) {
                   9286:                        // back space
                   9287:                        if(p > 0) {
                   9288:                                p--;
1.1.1.20  root     9289:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9290:                                        msdos_putch(0x08);
                   9291:                                        msdos_putch(0x08);
                   9292:                                        msdos_putch(0x20);
                   9293:                                        msdos_putch(0x20);
                   9294:                                        msdos_putch(0x08);
                   9295:                                        msdos_putch(0x08);
1.1.1.36  root     9296:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9297:                                        p--;
                   9298:                                        msdos_putch(0x08);
                   9299:                                        msdos_putch(0x08);
                   9300:                                        msdos_putch(0x20);
                   9301:                                        msdos_putch(0x20);
                   9302:                                        msdos_putch(0x08);
                   9303:                                        msdos_putch(0x08);
1.1.1.34  root     9304:                                } else {
                   9305:                                        msdos_putch(0x08);
                   9306:                                        msdos_putch(0x20);
                   9307:                                        msdos_putch(0x08);
                   9308:                                }
                   9309:                        }
                   9310:                } else if(chr == 0x1b) {
                   9311:                        // escape
                   9312:                        while(p > 0) {
                   9313:                                p--;
                   9314:                                if(msdos_ctrl_code_check(buf[p])) {
                   9315:                                        msdos_putch(0x08);
                   9316:                                        msdos_putch(0x08);
                   9317:                                        msdos_putch(0x20);
                   9318:                                        msdos_putch(0x20);
                   9319:                                        msdos_putch(0x08);
                   9320:                                        msdos_putch(0x08);
1.1.1.20  root     9321:                                } else {
1.1.1.34  root     9322:                                        msdos_putch(0x08);
                   9323:                                        msdos_putch(0x20);
                   9324:                                        msdos_putch(0x08);
1.1.1.20  root     9325:                                }
1.1       root     9326:                        }
                   9327:                } else if(p < max) {
                   9328:                        buf[p++] = chr;
                   9329:                        msdos_putch(chr);
                   9330:                }
                   9331:        }
                   9332:        buf[p] = 0x0d;
                   9333:        mem[ofs + 1] = p;
1.1.1.33  root     9334:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9335:        
1.1.1.35  root     9336: #ifdef USE_SERVICE_THREAD
                   9337:        service_exit = true;
                   9338: #endif
                   9339:        return(0);
                   9340: }
                   9341: 
                   9342: inline void msdos_int_21h_0ah()
                   9343: {
                   9344:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9345: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9346:                if(!in_service && !in_service_29h &&
                   9347:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     9348:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9349:                        // msdos_putch() will be used in this service
                   9350:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9351:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9352:                } else {
                   9353: #endif
                   9354:                        msdos_int_21h_0ah_thread(NULL);
                   9355:                        REQUEST_HARDWRE_UPDATE();
                   9356: #ifdef USE_SERVICE_THREAD
                   9357:                }
1.1.1.35  root     9358: #endif
                   9359:        }
1.1       root     9360: }
                   9361: 
                   9362: inline void msdos_int_21h_0bh()
                   9363: {
                   9364:        if(msdos_kbhit()) {
                   9365:                REG8(AL) = 0xff;
                   9366:        } else {
                   9367:                REG8(AL) = 0x00;
1.1.1.14  root     9368:                maybe_idle();
1.1       root     9369:        }
1.1.1.33  root     9370:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9371: }
                   9372: 
                   9373: inline void msdos_int_21h_0ch()
                   9374: {
                   9375:        // clear key buffer
1.1.1.21  root     9376:        msdos_stdio_reopen();
                   9377:        
1.1.1.20  root     9378:        process_t *process = msdos_process_info_get(current_psp);
                   9379:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9380:        
                   9381:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9382:                // stdin is redirected to file
                   9383:        } else {
                   9384:                while(msdos_kbhit()) {
                   9385:                        msdos_getch();
                   9386:                }
                   9387:        }
                   9388:        
                   9389:        switch(REG8(AL)) {
                   9390:        case 0x01:
                   9391:                msdos_int_21h_01h();
                   9392:                break;
                   9393:        case 0x06:
                   9394:                msdos_int_21h_06h();
                   9395:                break;
                   9396:        case 0x07:
                   9397:                msdos_int_21h_07h();
                   9398:                break;
                   9399:        case 0x08:
                   9400:                msdos_int_21h_08h();
                   9401:                break;
                   9402:        case 0x0a:
                   9403:                msdos_int_21h_0ah();
                   9404:                break;
                   9405:        default:
1.1.1.48  root     9406:                // the buffer is flushed but no input is attempted
1.1       root     9407:                break;
                   9408:        }
                   9409: }
                   9410: 
                   9411: inline void msdos_int_21h_0dh()
                   9412: {
                   9413: }
                   9414: 
                   9415: inline void msdos_int_21h_0eh()
                   9416: {
                   9417:        if(REG8(DL) < 26) {
                   9418:                _chdrive(REG8(DL) + 1);
                   9419:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9420:                msdos_sda_update(current_psp);
1.1       root     9421:        }
                   9422:        REG8(AL) = 26; // zdrive
                   9423: }
                   9424: 
1.1.1.14  root     9425: inline void msdos_int_21h_0fh()
                   9426: {
                   9427:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9428:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9429:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9430:        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     9431:        
1.1.1.14  root     9432:        if(hFile == INVALID_HANDLE_VALUE) {
                   9433:                REG8(AL) = 0xff;
                   9434:        } else {
                   9435:                REG8(AL) = 0;
                   9436:                fcb->current_block = 0;
                   9437:                fcb->record_size = 128;
                   9438:                fcb->file_size = GetFileSize(hFile, NULL);
                   9439:                fcb->handle = hFile;
                   9440:                fcb->cur_record = 0;
                   9441:        }
                   9442: }
                   9443: 
                   9444: inline void msdos_int_21h_10h()
                   9445: {
                   9446:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9447:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9448:        
                   9449:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9450: }
                   9451: 
1.1       root     9452: inline void msdos_int_21h_11h()
                   9453: {
1.1.1.3   root     9454:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9455:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9456:        
                   9457:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9458:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9459:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9460:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9461:        const char *path = msdos_fcb_path(fcb);
1.1       root     9462:        WIN32_FIND_DATA fd;
                   9463:        
1.1.1.13  root     9464:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9465:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9466:                FindClose(dtainfo->find_handle);
                   9467:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9468:        }
                   9469:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9470:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9471:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9472:        
1.1.1.14  root     9473:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9474:                dtainfo->allowable_mask &= ~8;
1.1       root     9475:        }
1.1.1.14  root     9476:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9477:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9478:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9479:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9480:                                FindClose(dtainfo->find_handle);
                   9481:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9482:                                break;
                   9483:                        }
                   9484:                }
                   9485:        }
1.1.1.13  root     9486:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9487:                if(ext_fcb->flag == 0xff) {
                   9488:                        ext_find->flag = 0xff;
                   9489:                        memset(ext_find->reserved, 0, 5);
                   9490:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9491:                }
                   9492:                find->drive = _getdrive();
1.1.1.13  root     9493:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9494:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9495:                find->nt_res = 0;
                   9496:                msdos_find_file_conv_local_time(&fd);
                   9497:                find->create_time_ms = 0;
                   9498:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9499:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9500:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9501:                find->cluster_hi = find->cluster_lo = 0;
                   9502:                find->file_size = fd.nFileSizeLow;
                   9503:                REG8(AL) = 0x00;
1.1.1.14  root     9504:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9505:                if(ext_fcb->flag == 0xff) {
                   9506:                        ext_find->flag = 0xff;
                   9507:                        memset(ext_find->reserved, 0, 5);
                   9508:                        ext_find->attribute = 8;
                   9509:                }
                   9510:                find->drive = _getdrive();
                   9511:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9512:                find->attribute = 8;
                   9513:                find->nt_res = 0;
                   9514:                msdos_find_file_conv_local_time(&fd);
                   9515:                find->create_time_ms = 0;
                   9516:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9517:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9518:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9519:                find->cluster_hi = find->cluster_lo = 0;
                   9520:                find->file_size = 0;
1.1.1.14  root     9521:                dtainfo->allowable_mask &= ~8;
1.1       root     9522:                REG8(AL) = 0x00;
                   9523:        } else {
                   9524:                REG8(AL) = 0xff;
                   9525:        }
                   9526: }
                   9527: 
                   9528: inline void msdos_int_21h_12h()
                   9529: {
1.1.1.3   root     9530:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9531: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9532:        
                   9533:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9534:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9535:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9536:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9537:        WIN32_FIND_DATA fd;
                   9538:        
1.1.1.13  root     9539:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9540:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9541:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9542:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9543:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9544:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9545:                                        FindClose(dtainfo->find_handle);
                   9546:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9547:                                        break;
                   9548:                                }
                   9549:                        }
                   9550:                } else {
1.1.1.13  root     9551:                        FindClose(dtainfo->find_handle);
                   9552:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9553:                }
                   9554:        }
1.1.1.13  root     9555:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9556:                if(ext_fcb->flag == 0xff) {
                   9557:                        ext_find->flag = 0xff;
                   9558:                        memset(ext_find->reserved, 0, 5);
                   9559:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9560:                }
                   9561:                find->drive = _getdrive();
1.1.1.13  root     9562:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9563:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9564:                find->nt_res = 0;
                   9565:                msdos_find_file_conv_local_time(&fd);
                   9566:                find->create_time_ms = 0;
                   9567:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9568:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9569:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9570:                find->cluster_hi = find->cluster_lo = 0;
                   9571:                find->file_size = fd.nFileSizeLow;
                   9572:                REG8(AL) = 0x00;
1.1.1.14  root     9573:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9574:                if(ext_fcb->flag == 0xff) {
                   9575:                        ext_find->flag = 0xff;
                   9576:                        memset(ext_find->reserved, 0, 5);
                   9577:                        ext_find->attribute = 8;
                   9578:                }
                   9579:                find->drive = _getdrive();
                   9580:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9581:                find->attribute = 8;
                   9582:                find->nt_res = 0;
                   9583:                msdos_find_file_conv_local_time(&fd);
                   9584:                find->create_time_ms = 0;
                   9585:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9586:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9587:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9588:                find->cluster_hi = find->cluster_lo = 0;
                   9589:                find->file_size = 0;
1.1.1.14  root     9590:                dtainfo->allowable_mask &= ~8;
1.1       root     9591:                REG8(AL) = 0x00;
                   9592:        } else {
                   9593:                REG8(AL) = 0xff;
                   9594:        }
                   9595: }
                   9596: 
                   9597: inline void msdos_int_21h_13h()
                   9598: {
1.1.1.3   root     9599:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9600:                REG8(AL) = 0xff;
                   9601:        } else {
                   9602:                REG8(AL) = 0x00;
                   9603:        }
                   9604: }
                   9605: 
1.1.1.16  root     9606: inline void msdos_int_21h_14h()
                   9607: {
                   9608:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9609:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9610:        process_t *process = msdos_process_info_get(current_psp);
                   9611:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9612:        DWORD num = 0;
                   9613:        
                   9614:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9615:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9616:                REG8(AL) = 1;
                   9617:        } else {
                   9618:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9619:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9620:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9621:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9622:        }
                   9623: }
                   9624: 
                   9625: inline void msdos_int_21h_15h()
1.1.1.14  root     9626: {
                   9627:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9628:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9629:        process_t *process = msdos_process_info_get(current_psp);
                   9630:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9631:        DWORD num = 0;
1.1.1.14  root     9632:        
1.1.1.16  root     9633:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9634:                REG8(AL) = 1;
                   9635:        } else {
                   9636:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9637:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9638:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9639:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9640:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9641:        }
                   9642: }
                   9643: 
                   9644: inline void msdos_int_21h_16h()
                   9645: {
                   9646:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9647:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9648:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9649:        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     9650:        
1.1.1.14  root     9651:        if(hFile == INVALID_HANDLE_VALUE) {
                   9652:                REG8(AL) = 0xff;
                   9653:        } else {
                   9654:                REG8(AL) = 0;
                   9655:                fcb->current_block = 0;
                   9656:                fcb->record_size = 128;
                   9657:                fcb->file_size = 0;
                   9658:                fcb->handle = hFile;
                   9659:                fcb->cur_record = 0;
                   9660:        }
                   9661: }
                   9662: 
1.1.1.16  root     9663: inline void msdos_int_21h_17h()
                   9664: {
                   9665:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9666:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     9667: //     const char *path_src = msdos_fcb_path(fcb_src);
                   9668:        char path_src[MAX_PATH];
                   9669:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   9670:        
1.1.1.16  root     9671:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9672:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     9673: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   9674:        char path_dst[MAX_PATH];
                   9675:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9676:        
                   9677:        if(rename(path_src, path_dst)) {
                   9678:                REG8(AL) = 0xff;
                   9679:        } else {
                   9680:                REG8(AL) = 0;
                   9681:        }
                   9682: }
                   9683: 
1.1       root     9684: inline void msdos_int_21h_18h()
                   9685: {
                   9686:        REG8(AL) = 0x00;
                   9687: }
                   9688: 
                   9689: inline void msdos_int_21h_19h()
                   9690: {
                   9691:        REG8(AL) = _getdrive() - 1;
                   9692: }
                   9693: 
                   9694: inline void msdos_int_21h_1ah()
                   9695: {
                   9696:        process_t *process = msdos_process_info_get(current_psp);
                   9697:        
                   9698:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9699:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9700:        msdos_sda_update(current_psp);
1.1       root     9701: }
                   9702: 
                   9703: inline void msdos_int_21h_1bh()
                   9704: {
                   9705:        int drive_num = _getdrive() - 1;
                   9706:        UINT16 seg, ofs;
                   9707:        
                   9708:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9709:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9710:                REG8(AL) = dpb->highest_sector_num + 1;
                   9711:                REG16(CX) = dpb->bytes_per_sector;
                   9712:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9713:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9714:        } else {
                   9715:                REG8(AL) = 0xff;
1.1.1.3   root     9716:                m_CF = 1;
1.1       root     9717:        }
                   9718: 
                   9719: }
                   9720: 
                   9721: inline void msdos_int_21h_1ch()
                   9722: {
1.1.1.41  root     9723:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9724:        UINT16 seg, ofs;
                   9725:        
                   9726:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9727:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9728:                REG8(AL) = dpb->highest_sector_num + 1;
                   9729:                REG16(CX) = dpb->bytes_per_sector;
                   9730:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9731:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9732:        } else {
                   9733:                REG8(AL) = 0xff;
1.1.1.3   root     9734:                m_CF = 1;
1.1       root     9735:        }
                   9736: 
                   9737: }
                   9738: 
                   9739: inline void msdos_int_21h_1dh()
                   9740: {
                   9741:        REG8(AL) = 0;
                   9742: }
                   9743: 
                   9744: inline void msdos_int_21h_1eh()
                   9745: {
                   9746:        REG8(AL) = 0;
                   9747: }
                   9748: 
                   9749: inline void msdos_int_21h_1fh()
                   9750: {
                   9751:        int drive_num = _getdrive() - 1;
                   9752:        UINT16 seg, ofs;
                   9753:        
                   9754:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9755:                REG8(AL) = 0;
1.1.1.3   root     9756:                SREG(DS) = seg;
                   9757:                i386_load_segment_descriptor(DS);
1.1       root     9758:                REG16(BX) = ofs;
                   9759:        } else {
                   9760:                REG8(AL) = 0xff;
1.1.1.3   root     9761:                m_CF = 1;
1.1       root     9762:        }
                   9763: }
                   9764: 
                   9765: inline void msdos_int_21h_20h()
                   9766: {
                   9767:        REG8(AL) = 0;
                   9768: }
                   9769: 
1.1.1.14  root     9770: inline void msdos_int_21h_21h()
                   9771: {
                   9772:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9773:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9774:        
                   9775:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9776:                REG8(AL) = 1;
                   9777:        } else {
                   9778:                process_t *process = msdos_process_info_get(current_psp);
                   9779:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9780:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9781:                DWORD num = 0;
1.1.1.14  root     9782:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9783:                        REG8(AL) = 1;
                   9784:                } else {
                   9785:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9786:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9787:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9788:                }
                   9789:        }
                   9790: }
                   9791: 
                   9792: inline void msdos_int_21h_22h()
                   9793: {
                   9794:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9795:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9796:        
                   9797:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9798:                REG8(AL) = 0xff;
                   9799:        } else {
                   9800:                process_t *process = msdos_process_info_get(current_psp);
                   9801:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9802:                DWORD num = 0;
1.1.1.14  root     9803:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9804:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9805:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9806:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9807:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9808:        }
                   9809: }
                   9810: 
1.1.1.16  root     9811: inline void msdos_int_21h_23h()
                   9812: {
                   9813:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9814:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9815:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     9816:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9817:        
                   9818:        if(hFile == INVALID_HANDLE_VALUE) {
                   9819:                REG8(AL) = 0xff;
                   9820:        } else {
                   9821:                UINT32 size = GetFileSize(hFile, NULL);
                   9822:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9823:                REG8(AL) = 0;
                   9824:        }
                   9825: }
                   9826: 
                   9827: inline void msdos_int_21h_24h()
                   9828: {
                   9829:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9830:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9831:        
                   9832:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9833: }
                   9834: 
1.1       root     9835: inline void msdos_int_21h_25h()
                   9836: {
                   9837:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9838:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9839: }
                   9840: 
                   9841: inline void msdos_int_21h_26h()
                   9842: {
                   9843:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9844:        
                   9845:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     9846:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     9847:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9848:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9849:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9850:        psp->parent_psp = 0;
                   9851: }
                   9852: 
1.1.1.16  root     9853: inline void msdos_int_21h_27h()
                   9854: {
                   9855:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9856:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9857:        
                   9858:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9859:                REG8(AL) = 1;
                   9860:        } else {
                   9861:                process_t *process = msdos_process_info_get(current_psp);
                   9862:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9863:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   9864:                DWORD num = 0;
                   9865:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   9866:                        REG8(AL) = 1;
                   9867:                } else {
                   9868:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9869:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9870:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9871:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9872:                }
                   9873:        }
                   9874: }
                   9875: 
                   9876: inline void msdos_int_21h_28h()
                   9877: {
                   9878:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9879:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9880:        
                   9881:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9882:                REG8(AL) = 0xff;
                   9883:        } else {
                   9884:                process_t *process = msdos_process_info_get(current_psp);
                   9885:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9886:                DWORD num = 0;
                   9887:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   9888:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9889:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9890:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9891:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9892:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9893:        }
                   9894: }
                   9895: 
1.1       root     9896: inline void msdos_int_21h_29h()
                   9897: {
1.1.1.20  root     9898:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   9899:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     9900:        UINT8 drv = 0;
                   9901:        char sep_chars[] = ":.;,=+";
                   9902:        char end_chars[] = "\\<>|/\"[]";
                   9903:        char spc_chars[] = " \t";
                   9904:        
1.1.1.20  root     9905:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   9906:        buffer[1023] = 0;
                   9907:        memset(name, 0x20, sizeof(name));
                   9908:        memset(ext, 0x20, sizeof(ext));
                   9909:        
1.1       root     9910:        if(REG8(AL) & 1) {
1.1.1.20  root     9911:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   9912:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     9913:                        ofs++;
                   9914:                }
                   9915:        }
1.1.1.20  root     9916:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     9917:        
1.1.1.24  root     9918:        if(buffer[ofs + 1] == ':') {
                   9919:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   9920:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     9921:                        ofs += 2;
1.1.1.24  root     9922:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9923:                                ofs++;
                   9924:                        }
                   9925:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   9926:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     9927:                        ofs += 2;
1.1.1.24  root     9928:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9929:                                ofs++;
                   9930:                        }
1.1       root     9931:                }
                   9932:        }
1.1.1.20  root     9933:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9934:                UINT8 c = buffer[ofs];
                   9935:                if(is_kanji) {
                   9936:                        is_kanji = 0;
                   9937:                } else if(msdos_lead_byte_check(c)) {
                   9938:                        is_kanji = 1;
                   9939:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9940:                        break;
                   9941:                } else if(c >= 'a' && c <= 'z') {
                   9942:                        c -= 0x20;
                   9943:                }
                   9944:                ofs++;
                   9945:                name[i] = c;
                   9946:        }
1.1.1.20  root     9947:        if(buffer[ofs] == '.') {
1.1       root     9948:                ofs++;
1.1.1.20  root     9949:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9950:                        UINT8 c = buffer[ofs];
                   9951:                        if(is_kanji) {
                   9952:                                is_kanji = 0;
                   9953:                        } else if(msdos_lead_byte_check(c)) {
                   9954:                                is_kanji = 1;
                   9955:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9956:                                break;
                   9957:                        } else if(c >= 'a' && c <= 'z') {
                   9958:                                c -= 0x20;
                   9959:                        }
                   9960:                        ofs++;
                   9961:                        ext[i] = c;
                   9962:                }
                   9963:        }
1.1.1.20  root     9964:        int si = REG16(SI) + ofs;
1.1.1.3   root     9965:        int ds = SREG(DS);
1.1       root     9966:        while(si > 0xffff) {
                   9967:                si -= 0x10;
                   9968:                ds++;
                   9969:        }
                   9970:        REG16(SI) = si;
1.1.1.3   root     9971:        SREG(DS) = ds;
                   9972:        i386_load_segment_descriptor(DS);
1.1       root     9973:        
1.1.1.3   root     9974:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     9975:        if(!(REG8(AL) & 2) || drv != 0) {
                   9976:                fcb[0] = drv;
                   9977:        }
                   9978:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   9979:                memcpy(fcb + 1, name, 8);
                   9980:        }
                   9981:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   9982:                memcpy(fcb + 9, ext, 3);
                   9983:        }
                   9984:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     9985:                if(fcb[i] == '*') {
                   9986:                        found_star = 1;
                   9987:                }
                   9988:                if(found_star) {
                   9989:                        fcb[i] = '?';
                   9990:                }
                   9991:        }
1.1.1.20  root     9992:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9993:                if(fcb[i] == '*') {
                   9994:                        found_star = 1;
                   9995:                }
                   9996:                if(found_star) {
                   9997:                        fcb[i] = '?';
                   9998:                }
                   9999:        }
                   10000:        
1.1.1.44  root     10001:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     10002:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   10003:                        REG8(AL) = 0x01;
1.1.1.20  root     10004:                } else {
                   10005:                        REG8(AL) = 0x00;
1.1       root     10006:                }
                   10007:        } else {
                   10008:                REG8(AL) = 0xff;
                   10009:        }
                   10010: }
                   10011: 
                   10012: inline void msdos_int_21h_2ah()
                   10013: {
                   10014:        SYSTEMTIME sTime;
                   10015:        
                   10016:        GetLocalTime(&sTime);
                   10017:        REG16(CX) = sTime.wYear;
                   10018:        REG8(DH) = (UINT8)sTime.wMonth;
                   10019:        REG8(DL) = (UINT8)sTime.wDay;
                   10020:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10021: }
                   10022: 
                   10023: inline void msdos_int_21h_2bh()
                   10024: {
1.1.1.14  root     10025:        REG8(AL) = 0xff;
1.1       root     10026: }
                   10027: 
                   10028: inline void msdos_int_21h_2ch()
                   10029: {
                   10030:        SYSTEMTIME sTime;
                   10031:        
                   10032:        GetLocalTime(&sTime);
                   10033:        REG8(CH) = (UINT8)sTime.wHour;
                   10034:        REG8(CL) = (UINT8)sTime.wMinute;
                   10035:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10036:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10037: }
                   10038: 
                   10039: inline void msdos_int_21h_2dh()
                   10040: {
                   10041:        REG8(AL) = 0x00;
                   10042: }
                   10043: 
                   10044: inline void msdos_int_21h_2eh()
                   10045: {
                   10046:        process_t *process = msdos_process_info_get(current_psp);
                   10047:        
                   10048:        process->verify = REG8(AL);
                   10049: }
                   10050: 
                   10051: inline void msdos_int_21h_2fh()
                   10052: {
                   10053:        process_t *process = msdos_process_info_get(current_psp);
                   10054:        
                   10055:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10056:        SREG(ES) = process->dta.w.h;
                   10057:        i386_load_segment_descriptor(ES);
1.1       root     10058: }
                   10059: 
                   10060: inline void msdos_int_21h_30h()
                   10061: {
                   10062:        // Version Flag / OEM
1.1.1.27  root     10063:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10064: #ifdef SUPPORT_HMA
                   10065:                REG16(BX) = 0x0000;
                   10066: #else
                   10067:                REG16(BX) = 0x1000; // DOS is in HMA
                   10068: #endif
1.1       root     10069:        } else {
1.1.1.27  root     10070:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10071:                // but this is not correct on Windows 98 SE
                   10072: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10073:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10074:        }
1.1.1.27  root     10075:        REG16(CX) = 0x0000;
1.1.1.30  root     10076:        REG8(AL) = dos_major_version;   // 7
                   10077:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10078: }
                   10079: 
                   10080: inline void msdos_int_21h_31h()
                   10081: {
1.1.1.29  root     10082:        try {
                   10083:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10084:        } catch(...) {
                   10085:                // recover the broken mcb
                   10086:                int mcb_seg = current_psp - 1;
                   10087:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10088:                
1.1.1.29  root     10089:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10090:                        mcb->mz = 'M';
                   10091:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10092:                        
1.1.1.29  root     10093:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10094:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10095:                        } else {
1.1.1.39  root     10096:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10097:                        }
                   10098:                } else {
                   10099:                        mcb->mz = 'Z';
1.1.1.30  root     10100:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10101:                }
                   10102:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10103:        }
1.1       root     10104:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10105: }
                   10106: 
                   10107: inline void msdos_int_21h_32h()
                   10108: {
                   10109:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10110:        UINT16 seg, ofs;
                   10111:        
                   10112:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10113:                REG8(AL) = 0;
1.1.1.3   root     10114:                SREG(DS) = seg;
                   10115:                i386_load_segment_descriptor(DS);
1.1       root     10116:                REG16(BX) = ofs;
                   10117:        } else {
                   10118:                REG8(AL) = 0xff;
1.1.1.3   root     10119:                m_CF = 1;
1.1       root     10120:        }
                   10121: }
                   10122: 
                   10123: inline void msdos_int_21h_33h()
                   10124: {
                   10125:        char path[MAX_PATH];
1.1.1.48  root     10126:        char drive = 3; // C:
1.1       root     10127:        
                   10128:        switch(REG8(AL)) {
                   10129:        case 0x00:
1.1.1.33  root     10130:                REG8(DL) = ctrl_break_checking;
1.1       root     10131:                break;
                   10132:        case 0x01:
1.1.1.33  root     10133:                ctrl_break_checking = REG8(DL);
                   10134:                break;
                   10135:        case 0x02:
                   10136:                {
                   10137:                        UINT8 old = ctrl_break_checking;
                   10138:                        ctrl_break_checking = REG8(DL);
                   10139:                        REG8(DL) = old;
                   10140:                }
                   10141:                break;
                   10142:        case 0x03:
                   10143:        case 0x04:
                   10144:                // DOS 4.0+ - Unused
1.1       root     10145:                break;
                   10146:        case 0x05:
1.1.1.48  root     10147:                if(GetSystemDirectory(path, MAX_PATH) != 0) {
                   10148:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10149:                                drive = path[0] - 'a' + 1;
                   10150:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10151:                                drive = path[0] - 'A' + 1;
                   10152:                        }
1.1       root     10153:                }
1.1.1.48  root     10154:                REG8(DL) = (UINT8)drive;
1.1       root     10155:                break;
                   10156:        case 0x06:
1.1.1.2   root     10157:                // MS-DOS version (7.10)
1.1       root     10158:                REG8(BL) = 7;
1.1.1.2   root     10159:                REG8(BH) = 10;
1.1       root     10160:                REG8(DL) = 0;
1.1.1.29  root     10161: #ifdef SUPPORT_HMA
                   10162:                REG8(DH) = 0x00;
                   10163: #else
                   10164:                REG8(DH) = 0x10; // DOS is in HMA
                   10165: #endif
1.1       root     10166:                break;
1.1.1.6   root     10167:        case 0x07:
                   10168:                if(REG8(DL) == 0) {
                   10169:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10170:                } else if(REG8(DL) == 1) {
                   10171:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10172:                }
                   10173:                break;
1.1       root     10174:        default:
1.1.1.22  root     10175:                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     10176: //             REG16(AX) = 0x01;
                   10177: //             m_CF = 1;
                   10178:                REG8(AL) = 0xff;
1.1       root     10179:                break;
                   10180:        }
                   10181: }
                   10182: 
1.1.1.23  root     10183: inline void msdos_int_21h_34h()
                   10184: {
                   10185:        SREG(ES) = SDA_TOP >> 4;
                   10186:        i386_load_segment_descriptor(ES);
                   10187:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   10188: }
                   10189: 
1.1       root     10190: inline void msdos_int_21h_35h()
                   10191: {
                   10192:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10193:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10194:        i386_load_segment_descriptor(ES);
1.1       root     10195: }
                   10196: 
                   10197: inline void msdos_int_21h_36h()
                   10198: {
                   10199:        struct _diskfree_t df = {0};
                   10200:        
                   10201:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10202:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10203:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10204:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10205:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10206:        } else {
                   10207:                REG16(AX) = 0xffff;
                   10208:        }
                   10209: }
                   10210: 
                   10211: inline void msdos_int_21h_37h()
                   10212: {
1.1.1.22  root     10213:        static UINT8 dev_flag = 0xff;
1.1       root     10214:        
                   10215:        switch(REG8(AL)) {
                   10216:        case 0x00:
1.1.1.22  root     10217:                {
                   10218:                        process_t *process = msdos_process_info_get(current_psp);
                   10219:                        REG8(AL) = 0x00;
                   10220:                        REG8(DL) = process->switchar;
                   10221:                }
1.1       root     10222:                break;
                   10223:        case 0x01:
1.1.1.22  root     10224:                {
                   10225:                        process_t *process = msdos_process_info_get(current_psp);
                   10226:                        REG8(AL) = 0x00;
                   10227:                        process->switchar = REG8(DL);
1.1.1.23  root     10228:                        msdos_sda_update(current_psp);
1.1.1.22  root     10229:                }
                   10230:                break;
                   10231:        case 0x02:
                   10232:                REG8(DL) = dev_flag;
                   10233:                break;
                   10234:        case 0x03:
                   10235:                dev_flag = REG8(DL);
                   10236:                break;
                   10237:        case 0xd0:
                   10238:        case 0xd1:
                   10239:        case 0xd2:
                   10240:        case 0xd3:
                   10241:        case 0xd4:
                   10242:        case 0xd5:
                   10243:        case 0xd6:
                   10244:        case 0xd7:
                   10245:        case 0xdc:
                   10246:        case 0xdd:
                   10247:        case 0xde:
                   10248:        case 0xdf:
1.1.1.48  root     10249:                // DIET v1.43e
                   10250: //             REG16(AX) = 1;
                   10251:                REG8(AL) = 0xff;
1.1       root     10252:                break;
                   10253:        default:
1.1.1.22  root     10254:                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     10255: //             REG16(AX) = 1;
                   10256:                REG8(AL) = 0xff;
1.1       root     10257:                break;
                   10258:        }
                   10259: }
                   10260: 
1.1.1.52  root     10261: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10262: {
                   10263:        char LCdata[80];
                   10264:        
1.1.1.19  root     10265:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     10266:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10267:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     10268:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10269:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     10270:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10271:        ci->date_format = *LCdata - '0';
1.1.1.42  root     10272:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10273:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     10274:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10275:        *ci->date_sep = *LCdata;
1.1.1.42  root     10276:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10277:        *ci->dec_sep = *LCdata;
1.1.1.42  root     10278:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10279:        *ci->list_sep = *LCdata;
1.1.1.42  root     10280:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10281:        *ci->thou_sep = *LCdata;
1.1.1.42  root     10282:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10283:        *ci->time_sep = *LCdata;
1.1.1.42  root     10284:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10285:        if(strchr(LCdata, 'H') != NULL) {
                   10286:                ci->time_format = 1;
                   10287:        }
1.1.1.49  root     10288:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10289:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.42  root     10290:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10291:        return atoi(LCdata);
                   10292: }
                   10293: 
1.1.1.42  root     10294: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10295: {
                   10296:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10297: }
                   10298: 
1.1.1.43  root     10299: void set_country_info(country_info_t *ci, int size)
                   10300: {
                   10301:        char LCdata[80];
                   10302:        
                   10303:        if(size >= 0x00 + 2) {
                   10304:                memset(LCdata, 0, sizeof(LCdata));
                   10305:                *LCdata = '0' + ci->date_format;
                   10306:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   10307:        }
                   10308:        if(size >= 0x02 + 5) {
                   10309:                memset(LCdata, 0, sizeof(LCdata));
                   10310:                memcpy(LCdata, &ci->currency_symbol, 4);
                   10311:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   10312:        }
                   10313:        if(size >= 0x07 + 2) {
                   10314:                memset(LCdata, 0, sizeof(LCdata));
                   10315:                *LCdata = *ci->thou_sep;
                   10316:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   10317:        }
                   10318:        if(size >= 0x09 + 2) {
                   10319:                memset(LCdata, 0, sizeof(LCdata));
                   10320:                *LCdata = *ci->dec_sep;
                   10321:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   10322:        }
                   10323:        if(size >= 0x0b + 2) {
                   10324:                memset(LCdata, 0, sizeof(LCdata));
                   10325:                *LCdata = *ci->date_sep;
                   10326:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   10327:        }
                   10328:        if(size >= 0x0d + 2) {
                   10329:                memset(LCdata, 0, sizeof(LCdata));
                   10330:                *LCdata = *ci->time_sep;
                   10331:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   10332:        }
                   10333:        if(size >= 0x0f + 1) {
                   10334:                memset(LCdata, 0, sizeof(LCdata));
                   10335:                *LCdata = '0' + ci->currency_format;
                   10336:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   10337:        }
                   10338:        if(size >= 0x10 + 1) {
                   10339:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   10340:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   10341:        }
                   10342:        if(size >= 0x11 + 1) {
                   10343:                // FIXME: is time format always H/h:mm:ss ???
                   10344:                if(ci->time_format & 1) {
                   10345:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10346:                } else {
                   10347:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10348:                }
                   10349:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   10350:        }
                   10351:        if(size >= 0x12 + 4) {
                   10352:                // 12h  DWORD   address of case map routine
                   10353:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10354:        }
                   10355:        if(size >= 0x16 + 2) {
                   10356:                memset(LCdata, 0, sizeof(LCdata));
                   10357:                *LCdata = *ci->list_sep;
                   10358:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10359:        }
                   10360: }
                   10361: 
1.1.1.42  root     10362: #ifndef SUBLANG_SWAHILI
                   10363:        #define SUBLANG_SWAHILI 0x01
                   10364: #endif
                   10365: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10366:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10367: #endif
                   10368: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10369:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10370: #endif
                   10371: #ifndef LANG_BANGLA
                   10372:        #define LANG_BANGLA 0x45
                   10373: #endif
                   10374: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10375:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10376: #endif
                   10377: 
                   10378: static const struct {
                   10379:        int code;
                   10380:        USHORT usPrimaryLanguage;
                   10381:        USHORT usSubLanguage;
                   10382: } country_table[] = {
                   10383:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10384:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10385:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10386:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10387:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10388:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10389: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10390: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10391: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10392: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10393: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10394:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10395:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10396: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10397:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10398: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10399:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10400:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10401:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10402:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10403:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10404:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10405:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10406: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10407: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10408:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10409:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10410:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10411:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10412:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10413: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10414: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10415: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10416:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10417: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10418: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10419: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10420: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10421:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10422:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10423:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10424: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10425:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10426:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10427:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10428:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10429:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10430:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10431:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10432:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10433:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10434:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10435:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10436:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10437:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10438: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10439:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10440:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10441:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10442:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10443:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10444:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10445:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10446:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10447:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10448:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10449: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10450:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10451: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10452:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10453:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10454:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10455:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10456:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10457:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10458:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10459:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10460: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10461:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10462: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10463: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10464:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10465: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10466:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10467:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10468:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10469:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10470:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10471:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10472:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10473: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10474: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10475:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10476: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10477:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10478:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10479:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10480:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10481: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10482: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10483: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10484:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10485:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10486:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10487:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10488:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10489:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10490:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10491:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10492:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10493:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10494:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10495:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10496:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10497:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10498:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10499:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10500:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10501:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10502:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10503:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10504:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10505: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10506:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10507: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10508:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10509:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10510:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10511:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10512:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10513:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10514:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10515:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10516:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10517:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10518:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10519:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10520:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10521:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10522:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10523:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10524:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10525:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10526:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10527:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10528:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10529:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10530:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10531:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10532:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10533: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10534:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10535:        {-1, 0, 0},
                   10536: };
                   10537: 
1.1.1.14  root     10538: inline void msdos_int_21h_38h()
                   10539: {
                   10540:        switch(REG8(AL)) {
                   10541:        case 0x00:
1.1.1.19  root     10542:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10543:                break;
                   10544:        default:
1.1.1.42  root     10545:                for(int i = 0;; i++) {
                   10546:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10547:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10548:                                break;
                   10549:                        } else if(country_table[i].code == -1) {
                   10550: //                             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));
                   10551: //                             REG16(AX) = 2;
                   10552: //                             m_CF = 1;
1.1.1.48  root     10553:                                // get current coutry info
1.1.1.42  root     10554:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10555:                                break;
                   10556:                        }
                   10557:                }
1.1.1.14  root     10558:                break;
                   10559:        }
                   10560: }
                   10561: 
1.1       root     10562: inline void msdos_int_21h_39h(int lfn)
                   10563: {
1.1.1.3   root     10564:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10565:                REG16(AX) = errno;
1.1.1.3   root     10566:                m_CF = 1;
1.1       root     10567:        }
                   10568: }
                   10569: 
                   10570: inline void msdos_int_21h_3ah(int lfn)
                   10571: {
1.1.1.3   root     10572:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10573:                REG16(AX) = errno;
1.1.1.3   root     10574:                m_CF = 1;
1.1       root     10575:        }
                   10576: }
                   10577: 
                   10578: inline void msdos_int_21h_3bh(int lfn)
                   10579: {
1.1.1.45  root     10580:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10581:        
                   10582:        if(_chdir(path)) {
1.1.1.17  root     10583:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10584:                m_CF = 1;
1.1.1.44  root     10585:        } else {
                   10586:                int drv = _getdrive() - 1;
                   10587:                if(path[1] == ':') {
                   10588:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10589:                                drv = path[0] - 'A';
                   10590:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10591:                                drv = path[0] - 'a';
                   10592:                        }
                   10593:                }
                   10594:                msdos_cds_update(drv, path);
1.1       root     10595:        }
                   10596: }
                   10597: 
                   10598: inline void msdos_int_21h_3ch()
                   10599: {
1.1.1.45  root     10600:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10601:        int attr = GetFileAttributes(path);
1.1.1.37  root     10602:        int fd = -1;
                   10603:        int sio_port = 0;
                   10604:        int lpt_port = 0;
1.1       root     10605:        
1.1.1.45  root     10606:        if(msdos_is_device_path(path)) {
                   10607:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10608:        } else {
                   10609:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10610:        }
                   10611:        if(fd != -1) {
                   10612:                if(attr == -1) {
                   10613:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10614:                }
                   10615:                SetFileAttributes(path, attr);
                   10616:                REG16(AX) = fd;
1.1.1.45  root     10617:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10618:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10619:        } else {
                   10620:                REG16(AX) = errno;
1.1.1.3   root     10621:                m_CF = 1;
1.1       root     10622:        }
                   10623: }
                   10624: 
                   10625: inline void msdos_int_21h_3dh()
                   10626: {
1.1.1.45  root     10627:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10628:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10629:        int fd = -1;
                   10630:        int sio_port = 0;
                   10631:        int lpt_port = 0;
1.1       root     10632:        
                   10633:        if(mode < 0x03) {
1.1.1.45  root     10634:                if(msdos_is_device_path(path)) {
                   10635:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10636:                } else {
1.1.1.13  root     10637:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10638:                }
1.1       root     10639:                if(fd != -1) {
                   10640:                        REG16(AX) = fd;
1.1.1.45  root     10641:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10642:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10643:                } else {
                   10644:                        REG16(AX) = errno;
1.1.1.3   root     10645:                        m_CF = 1;
1.1       root     10646:                }
                   10647:        } else {
                   10648:                REG16(AX) = 0x0c;
1.1.1.3   root     10649:                m_CF = 1;
1.1       root     10650:        }
                   10651: }
                   10652: 
                   10653: inline void msdos_int_21h_3eh()
                   10654: {
                   10655:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10656:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10657:        
1.1.1.20  root     10658:        if(fd < process->max_files && file_handler[fd].valid) {
                   10659:                _close(fd);
                   10660:                msdos_file_handler_close(fd);
                   10661:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10662:        } else {
                   10663:                REG16(AX) = 0x06;
1.1.1.3   root     10664:                m_CF = 1;
1.1       root     10665:        }
                   10666: }
                   10667: 
1.1.1.35  root     10668: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10669: {
                   10670:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10671:        int max = REG16(CX);
                   10672:        int p = 0;
                   10673:        
                   10674:        while(max > p) {
                   10675:                int chr = msdos_getch();
                   10676:                
                   10677:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10678:                        p = 0;
                   10679:                        buf[p++] = 0x0d;
                   10680:                        if(max > p) {
                   10681:                                buf[p++] = 0x0a;
                   10682:                        }
                   10683:                        msdos_putch(0x03);
                   10684:                        msdos_putch(0x0d);
                   10685:                        msdos_putch(0x0a);
                   10686:                        break;
                   10687:                } else if(ctrl_break_pressed) {
                   10688:                        // skip this byte
                   10689:                } else if(chr == 0x00) {
                   10690:                        // skip 2nd byte
                   10691:                        msdos_getch();
                   10692:                } else if(chr == 0x0d) {
                   10693:                        // carriage return
                   10694:                        buf[p++] = 0x0d;
                   10695:                        if(max > p) {
                   10696:                                buf[p++] = 0x0a;
                   10697:                        }
                   10698:                        msdos_putch('\n');
                   10699:                        break;
                   10700:                } else if(chr == 0x08) {
                   10701:                        // back space
                   10702:                        if(p > 0) {
                   10703:                                p--;
                   10704:                                if(msdos_ctrl_code_check(buf[p])) {
                   10705:                                        msdos_putch(0x08);
                   10706:                                        msdos_putch(0x08);
                   10707:                                        msdos_putch(0x20);
                   10708:                                        msdos_putch(0x20);
                   10709:                                        msdos_putch(0x08);
                   10710:                                        msdos_putch(0x08);
1.1.1.36  root     10711:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10712:                                        p--;
                   10713:                                        msdos_putch(0x08);
                   10714:                                        msdos_putch(0x08);
                   10715:                                        msdos_putch(0x20);
                   10716:                                        msdos_putch(0x20);
                   10717:                                        msdos_putch(0x08);
                   10718:                                        msdos_putch(0x08);
1.1.1.35  root     10719:                                } else {
                   10720:                                        msdos_putch(0x08);
                   10721:                                        msdos_putch(0x20);
                   10722:                                        msdos_putch(0x08);
                   10723:                                }
                   10724:                        }
                   10725:                } else if(chr == 0x1b) {
                   10726:                        // escape
                   10727:                        while(p > 0) {
                   10728:                                p--;
                   10729:                                if(msdos_ctrl_code_check(buf[p])) {
                   10730:                                        msdos_putch(0x08);
                   10731:                                        msdos_putch(0x08);
                   10732:                                        msdos_putch(0x20);
                   10733:                                        msdos_putch(0x20);
                   10734:                                        msdos_putch(0x08);
                   10735:                                        msdos_putch(0x08);
                   10736:                                } else {
                   10737:                                        msdos_putch(0x08);
                   10738:                                        msdos_putch(0x20);
                   10739:                                        msdos_putch(0x08);
                   10740:                                }
                   10741:                        }
                   10742:                } else {
                   10743:                        buf[p++] = chr;
                   10744:                        msdos_putch(chr);
                   10745:                }
                   10746:        }
                   10747:        REG16(AX) = p;
                   10748:        
                   10749: #ifdef USE_SERVICE_THREAD
                   10750:        service_exit = true;
                   10751: #endif
                   10752:        return(0);
                   10753: }
                   10754: 
1.1       root     10755: inline void msdos_int_21h_3fh()
                   10756: {
                   10757:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10758:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10759:        
1.1.1.20  root     10760:        if(fd < process->max_files && file_handler[fd].valid) {
                   10761:                if(file_mode[file_handler[fd].mode].in) {
                   10762:                        if(file_handler[fd].atty) {
1.1       root     10763:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10764:                                if(REG16(CX) != 0) {
                   10765: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     10766:                                        if(!in_service && !in_service_29h &&
                   10767:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     10768:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   10769:                                                // msdos_putch() will be used in this service
                   10770:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   10771:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   10772:                                        } else {
                   10773: #endif
                   10774:                                                msdos_int_21h_3fh_thread(NULL);
                   10775:                                                REQUEST_HARDWRE_UPDATE();
                   10776: #ifdef USE_SERVICE_THREAD
                   10777:                                        }
1.1.1.35  root     10778: #endif
                   10779:                                } else {
                   10780:                                        REG16(AX) = 0;
1.1       root     10781:                                }
                   10782:                        } else {
1.1.1.37  root     10783:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10784:                        }
                   10785:                } else {
                   10786:                        REG16(AX) = 0x05;
1.1.1.3   root     10787:                        m_CF = 1;
1.1       root     10788:                }
                   10789:        } else {
                   10790:                REG16(AX) = 0x06;
1.1.1.3   root     10791:                m_CF = 1;
1.1       root     10792:        }
                   10793: }
                   10794: 
                   10795: inline void msdos_int_21h_40h()
                   10796: {
                   10797:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10798:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10799:        
1.1.1.20  root     10800:        if(fd < process->max_files && file_handler[fd].valid) {
                   10801:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10802:                        if(REG16(CX)) {
1.1.1.20  root     10803:                                if(file_handler[fd].atty) {
1.1       root     10804:                                        // BX is stdout/stderr or is redirected to stdout
                   10805:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10806:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10807:                                        }
                   10808:                                        REG16(AX) = REG16(CX);
                   10809:                                } else {
1.1.1.20  root     10810:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10811:                                }
                   10812:                        } else {
1.1.1.20  root     10813:                                UINT32 pos = _tell(fd);
                   10814:                                _lseek(fd, 0, SEEK_END);
                   10815:                                UINT32 size = _tell(fd);
1.1.1.12  root     10816:                                if(pos < size) {
1.1.1.20  root     10817:                                        _lseek(fd, pos, SEEK_SET);
                   10818:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10819:                                } else {
                   10820:                                        for(UINT32 i = size; i < pos; i++) {
                   10821:                                                UINT8 tmp = 0;
1.1.1.23  root     10822:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10823:                                        }
1.1.1.20  root     10824:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10825:                                }
1.1.1.23  root     10826:                                REG16(AX) = 0;
1.1       root     10827:                        }
                   10828:                } else {
                   10829:                        REG16(AX) = 0x05;
1.1.1.3   root     10830:                        m_CF = 1;
1.1       root     10831:                }
                   10832:        } else {
                   10833:                REG16(AX) = 0x06;
1.1.1.3   root     10834:                m_CF = 1;
1.1       root     10835:        }
                   10836: }
                   10837: 
                   10838: inline void msdos_int_21h_41h(int lfn)
                   10839: {
1.1.1.3   root     10840:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10841:                REG16(AX) = errno;
1.1.1.3   root     10842:                m_CF = 1;
1.1       root     10843:        }
                   10844: }
                   10845: 
                   10846: inline void msdos_int_21h_42h()
                   10847: {
                   10848:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10849:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10850:        
1.1.1.20  root     10851:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10852:                if(REG8(AL) < 0x03) {
1.1.1.35  root     10853:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     10854:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   10855:                        UINT32 pos = _tell(fd);
1.1       root     10856:                        REG16(AX) = pos & 0xffff;
                   10857:                        REG16(DX) = (pos >> 16);
                   10858:                } else {
                   10859:                        REG16(AX) = 0x01;
1.1.1.3   root     10860:                        m_CF = 1;
1.1       root     10861:                }
                   10862:        } else {
                   10863:                REG16(AX) = 0x06;
1.1.1.3   root     10864:                m_CF = 1;
1.1       root     10865:        }
                   10866: }
                   10867: 
                   10868: inline void msdos_int_21h_43h(int lfn)
                   10869: {
1.1.1.45  root     10870:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     10871:        int attr;
                   10872:        
1.1.1.14  root     10873:        if(!lfn && REG8(AL) > 2) {
                   10874:                REG16(AX) = 0x01;
                   10875:                m_CF = 1;
                   10876:                return;
                   10877:        }
                   10878:        switch(REG8(lfn ? BL : AL)) {
1.1       root     10879:        case 0x00:
                   10880:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     10881:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   10882:                } else {
                   10883:                        REG16(AX) = (UINT16)GetLastError();
                   10884:                        m_CF = 1;
                   10885:                }
                   10886:                break;
                   10887:        case 0x01:
                   10888:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   10889:                        REG16(AX) = (UINT16)GetLastError();
                   10890:                        m_CF = 1;
                   10891:                }
                   10892:                break;
                   10893:        case 0x02:
                   10894:                {
1.1.1.45  root     10895:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
                   10896:                        if(compressed_size != INVALID_FILE_SIZE) {
                   10897:                                if(compressed_size != 0) {
                   10898:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10899:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   10900:                                                file_size = GetFileSize(hFile, NULL);
                   10901:                                                CloseHandle(hFile);
                   10902:                                        }
                   10903:                                        if(compressed_size == file_size) {
                   10904:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   10905:                                                // this isn't correct if the file is in the NTFS MFT
                   10906:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   10907:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   10908:                                                }
1.1.1.14  root     10909:                                        }
                   10910:                                }
1.1.1.45  root     10911:                                REG16(AX) = LOWORD(compressed_size);
                   10912:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     10913:                        } else {
                   10914:                                REG16(AX) = (UINT16)GetLastError();
                   10915:                                m_CF = 1;
1.1       root     10916:                        }
1.1.1.14  root     10917:                }
                   10918:                break;
                   10919:        case 0x03:
                   10920:        case 0x05:
                   10921:        case 0x07:
1.1.1.48  root     10922:                if(lfn) {
1.1.1.14  root     10923:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10924:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10925:                                FILETIME local, time;
                   10926:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   10927:                                if(REG8(BL) == 7) {
                   10928:                                        ULARGE_INTEGER hund;
                   10929:                                        hund.LowPart = local.dwLowDateTime;
                   10930:                                        hund.HighPart = local.dwHighDateTime;
                   10931:                                        hund.QuadPart += REG16(SI) * 100000;
                   10932:                                        local.dwLowDateTime = hund.LowPart;
                   10933:                                        local.dwHighDateTime = hund.HighPart;
                   10934:                                }
                   10935:                                LocalFileTimeToFileTime(&local, &time);
                   10936:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   10937:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   10938:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   10939:                                        REG16(AX) = (UINT16)GetLastError();
                   10940:                                        m_CF = 1;
                   10941:                                }
                   10942:                                CloseHandle(hFile);
                   10943:                        } else {
                   10944:                                REG16(AX) = (UINT16)GetLastError();
                   10945:                                m_CF = 1;
1.1       root     10946:                        }
1.1.1.48  root     10947:                } else {
                   10948:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   10949:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   10950:                        // 214307 DR DOS 6.0 - Set File Owner
                   10951: //                     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));
                   10952:                        REG16(AX) = 0x01;
                   10953:                        m_CF = 1;
1.1.1.14  root     10954:                }
                   10955:                break;
                   10956:        case 0x04:
                   10957:        case 0x06:
                   10958:        case 0x08:
1.1.1.48  root     10959:                if(lfn) {
1.1.1.14  root     10960:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   10961:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   10962:                                FILETIME *time, local;
                   10963:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   10964:                                                   0x06 ? &fad.ftLastAccessTime :
                   10965:                                                          &fad.ftCreationTime;
                   10966:                                FileTimeToLocalFileTime(time, &local);
                   10967:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   10968:                                if(REG8(BL) == 0x08) {
                   10969:                                        ULARGE_INTEGER hund;
                   10970:                                        hund.LowPart = local.dwLowDateTime;
                   10971:                                        hund.HighPart = local.dwHighDateTime;
                   10972:                                        hund.QuadPart /= 100000;
                   10973:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   10974:                                }
                   10975:                        } else {
                   10976:                                REG16(AX) = (UINT16)GetLastError();
                   10977:                                m_CF = 1;
1.1       root     10978:                        }
1.1.1.48  root     10979:                } else {
                   10980:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   10981:                        // 214306 DR DOS 6.0 - Get File Owner
                   10982: //                     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));
                   10983:                        REG16(AX) = 0x01;
                   10984:                        m_CF = 1;
1.1.1.14  root     10985:                }
                   10986:                break;
1.1.1.43  root     10987:        case 0xff:
1.1.1.48  root     10988:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     10989:                        if(REG8(CL) == 0x39) {
                   10990:                                msdos_int_21h_39h(1);
                   10991:                                break;
                   10992:                        } else if(REG8(CL) == 0x56) {
                   10993:                                msdos_int_21h_56h(1);
                   10994:                                break;
                   10995:                        }
                   10996:                }
1.1.1.14  root     10997:        default:
1.1.1.22  root     10998:                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     10999:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     11000:                m_CF = 1;
                   11001:                break;
                   11002:        }
                   11003: }
                   11004: 
                   11005: inline void msdos_int_21h_44h()
                   11006: {
1.1.1.22  root     11007:        static UINT16 iteration_count = 0;
                   11008:        
1.1.1.44  root     11009:        process_t *process;
                   11010:        int fd, drv;
1.1.1.14  root     11011:        
                   11012:        switch(REG8(AL)) {
                   11013:        case 0x00:
                   11014:        case 0x01:
                   11015:        case 0x02:
                   11016:        case 0x03:
                   11017:        case 0x04:
                   11018:        case 0x05:
                   11019:        case 0x06:
                   11020:        case 0x07:
1.1.1.44  root     11021:                process = msdos_process_info_get(current_psp);
                   11022:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11023:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11024:                        REG16(AX) = 0x06;
                   11025:                        m_CF = 1;
                   11026:                        return;
1.1.1.14  root     11027:                }
                   11028:                break;
                   11029:        case 0x08:
                   11030:        case 0x09:
1.1.1.44  root     11031:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11032:                if(!msdos_is_valid_drive(drv)) {
                   11033:                        // invalid drive
1.1.1.14  root     11034:                        REG16(AX) = 0x0f;
                   11035:                        m_CF = 1;
                   11036:                        return;
1.1       root     11037:                }
                   11038:                break;
                   11039:        }
                   11040:        switch(REG8(AL)) {
1.1.1.48  root     11041:        case 0x00: // Get Device Information
1.1.1.20  root     11042:                REG16(DX) = file_handler[fd].info;
1.1       root     11043:                break;
1.1.1.48  root     11044:        case 0x01: // Set Device Information
1.1.1.45  root     11045:                if(REG8(DH) != 0) {
                   11046: //                     REG16(AX) = 0x0d; // data invalid
                   11047: //                     m_CF = 1;
                   11048:                        file_handler[fd].info = REG16(DX);
                   11049:                } else {
                   11050:                        file_handler[fd].info &= 0xff00;
                   11051:                        file_handler[fd].info |= REG8(DL);
                   11052:                }
1.1       root     11053:                break;
1.1.1.48  root     11054:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11055:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11056:                        // from DOSBox
                   11057:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11058:                        case 0x00:
                   11059:                                if(REG16(CX) >= 6) {
                   11060:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11061:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11062:                                        REG16(AX) = 6; // number of bytes actually read
                   11063:                                } else {
                   11064:                                        REG16(AX) = 0x0d; // data invalid
                   11065:                                        m_CF = 1;
                   11066:                                }
                   11067:                                break;
                   11068:                        case 0x01:
                   11069:                                if(REG16(CX) >= 6) {
                   11070:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11071:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11072:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11073:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11074:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11075:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11076:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11077:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11078:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11079:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11080:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11081:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11082:                                                } else {
                   11083:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11084:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11085:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11086:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11087:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11088:                                                }
                   11089:                                        }
                   11090:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11091:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11092:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11093:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11094:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11095:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11096:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11097:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11098:                                        
                   11099:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   11100:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   11101:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11102:                                        REG16(AX) = 6; // number of bytes actually read
                   11103:                                } else {
                   11104:                                        REG16(AX) = 0x0d; // data invalid
                   11105:                                        m_CF = 1;
                   11106:                                }
                   11107:                                break;
                   11108:                        case 0x02:
                   11109:                                if(REG16(CX) >= 2) {
                   11110:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11111:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11112:                                        REG16(AX) = 2; // number of bytes actually read
                   11113:                                } else {
                   11114:                                        REG16(AX) = 0x0d; // data invalid
                   11115:                                        m_CF = 1;
                   11116:                                }
                   11117:                                break;
                   11118:                        case 0x03:
                   11119:                                if(REG16(CX) >= 4) {
                   11120:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11121:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11122:                                        REG16(AX) = 4; // number of bytes actually read
                   11123:                                } else {
                   11124:                                        REG16(AX) = 0x0d; // data invalid
                   11125:                                        m_CF = 1;
                   11126:                                }
                   11127:                                break;
                   11128:                        default:
                   11129:                                REG16(AX) = 0x01; // function number invalid
                   11130:                                m_CF = 1;
                   11131:                        }
                   11132:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11133:                        if(REG16(CX) >= 5) {
                   11134:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11135:                                REG16(AX) = 5; // number of bytes actually read
                   11136:                        } else {
                   11137:                                REG16(AX) = 0x0d; // data invalid
                   11138:                                m_CF = 1;
                   11139:                        }
                   11140:                } else {
                   11141: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11142: //                     REG16(AX) = REG16(CX);
                   11143:                        REG16(AX) = 0x05; // access denied
                   11144:                        m_CF = 1;
                   11145:                }
                   11146:                break;
1.1.1.48  root     11147:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11148: //             REG16(AX) = 0x05;
                   11149: //             m_CF = 1;
                   11150:                REG16(AX) = 0x00; // success
                   11151:                break;
1.1.1.48  root     11152:        case 0x04: // Read From Block Device Control Channel
                   11153:        case 0x05: // Write To Block Device Control Channel
1.1       root     11154:                REG16(AX) = 0x05;
1.1.1.3   root     11155:                m_CF = 1;
1.1       root     11156:                break;
1.1.1.48  root     11157:        case 0x06: // Get Input Status
1.1.1.20  root     11158:                if(file_mode[file_handler[fd].mode].in) {
                   11159:                        if(file_handler[fd].atty) {
1.1.1.14  root     11160:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11161:                        } else {
1.1.1.20  root     11162:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11163:                        }
1.1.1.14  root     11164:                } else {
                   11165:                        REG8(AL) = 0x00;
1.1       root     11166:                }
                   11167:                break;
1.1.1.48  root     11168:        case 0x07: // Get Output Status
1.1.1.20  root     11169:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11170:                        REG8(AL) = 0xff;
                   11171:                } else {
                   11172:                        REG8(AL) = 0x00;
1.1       root     11173:                }
                   11174:                break;
1.1.1.48  root     11175:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11176:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11177:                        // removable drive
                   11178:                        REG16(AX) = 0x00;
1.1       root     11179:                } else {
1.1.1.14  root     11180:                        // fixed drive
                   11181:                        REG16(AX) = 0x01;
1.1       root     11182:                }
                   11183:                break;
1.1.1.48  root     11184:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11185:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11186:                        // remote drive
                   11187:                        REG16(DX) = 0x1000;
1.1.1.44  root     11188:                } else if(msdos_is_subst_drive(drv)) {
                   11189:                        // subst drive
                   11190:                        REG16(DX) = 0x8000;
1.1       root     11191:                } else {
1.1.1.14  root     11192:                        // local drive
1.1.1.44  root     11193:                        REG16(DX) = 0x0000;
1.1       root     11194:                }
                   11195:                break;
1.1.1.48  root     11196:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11197:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11198:                        REG16(DX) = 0x8000;
                   11199:                } else {
                   11200:                        REG16(DX) = 0x0000;
                   11201:                }
1.1.1.21  root     11202:                break;
1.1.1.48  root     11203:        case 0x0b: // Set Sharing Retry Count
1.1       root     11204:                break;
1.1.1.48  root     11205:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11206:                if(REG8(CL) == 0x45) {
                   11207:                        // set iteration (retry) count
                   11208:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11209:                } else if(REG8(CL) == 0x4a) {
                   11210:                        // select code page
                   11211:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11212:                        msdos_nls_tables_update();
1.1.1.44  root     11213:                } else if(REG8(CL) == 0x4c) {
                   11214:                        // start code-page preparation
                   11215:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11216:                        int count = 1, offset = 0;
                   11217:                        if(active_code_page != 437) {
                   11218:                                ids[count++] = active_code_page;
                   11219:                        }
                   11220:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11221:                                ids[count++] = system_code_page;
                   11222:                        }
                   11223:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11224:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11225:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11226:                        for(int i = 0; i < count; i++) {
                   11227:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11228:                        }
                   11229:                } else if(REG8(CL) == 0x4d) {
                   11230:                        // end code-page preparation
                   11231:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11232:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11233:                } else if(REG8(CL) == 0x5f) {
                   11234:                        // set display information
                   11235:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11236:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11237:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11238:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11239:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11240:                                
                   11241:                                if(cur_width != new_width || cur_height != new_height) {
                   11242:                                        pcbios_set_console_size(new_width, new_height, true);
                   11243:                                }
                   11244:                        }
1.1.1.22  root     11245:                } else if(REG8(CL) == 0x65) {
                   11246:                        // get iteration (retry) count
                   11247:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11248:                } else if(REG8(CL) == 0x6a) {
                   11249:                        // query selected code page
                   11250:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11251:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11252:                        
                   11253:                        CPINFO info;
                   11254:                        GetCPInfo(active_code_page, &info);
                   11255:                        
                   11256:                        if(info.MaxCharSize != 1) {
                   11257:                                for(int i = 0;; i++) {
                   11258:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11259:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11260:                                        
                   11261:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11262:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11263:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11264:                                        
                   11265:                                        if(lo == 0 && hi == 0) {
                   11266:                                                break;
                   11267:                                        }
                   11268:                                }
                   11269:                        }
1.1.1.44  root     11270:                } else if(REG8(CL) == 0x6b) {
                   11271:                        // query prepare list
                   11272:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11273:                        int count = 1, offset = 0;
                   11274:                        if(active_code_page != 437) {
                   11275:                                ids[count++] = active_code_page;
                   11276:                        }
                   11277:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11278:                                ids[count++] = system_code_page;
                   11279:                        }
                   11280:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11281:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11282:                        for(int i = 0; i < count; i++) {
                   11283:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11284:                        }
                   11285:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11286:                        for(int i = 0; i < count; i++) {
                   11287:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11288:                        }
1.1.1.22  root     11289:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11290:                        // get display information
1.1.1.50  root     11291:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11292:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11293:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11294:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11295:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11296:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11297:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11298:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11299:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11300:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11301:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11302:                } else {
                   11303:                        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));
                   11304:                        REG16(AX) = 0x01; // invalid function
                   11305:                        m_CF = 1;
                   11306:                }
                   11307:                break;
1.1.1.48  root     11308:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11309:                if(REG8(CL) == 0x40) {
                   11310:                        // set device parameters
1.1.1.48  root     11311: //             } else if(REG8(CL) == 0x41) {
                   11312: //                     // write logical device track
                   11313: //             } else if(REG8(CL) == 0x42) {
                   11314: //                     // format and verify logical device track
1.1.1.22  root     11315:                } else if(REG8(CL) == 0x46) {
                   11316:                        // set volume serial number
1.1.1.48  root     11317:                } else if(REG8(CL) == 0x47) {
                   11318:                        // set access flag
                   11319: //             } else if(REG8(CL) == 0x48) {
                   11320: //                     // set media lock state
                   11321: //             } else if(REG8(CL) == 0x49) {
                   11322: //                     // eject media in drive
1.1.1.22  root     11323:                } else if(REG8(CL) == 0x4a) {
                   11324:                        // lock logical volume
                   11325:                } else if(REG8(CL) == 0x4b) {
                   11326:                        // lock physical volume
                   11327:                } else if(REG8(CL) == 0x60) {
                   11328:                        // get device parameters
1.1.1.42  root     11329:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11330:                        
1.1.1.42  root     11331:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11332:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11333:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11334:                                
                   11335:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11336:                                switch(geo->MediaType) {
                   11337:                                case F5_360_512:
                   11338:                                case F5_320_512:
                   11339:                                case F5_320_1024:
                   11340:                                case F5_180_512:
                   11341:                                case F5_160_512:
                   11342:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11343:                                        break;
                   11344:                                case F5_1Pt2_512:
                   11345:                                case F3_1Pt2_512:
                   11346:                                case F3_1Pt23_1024:
                   11347:                                case F5_1Pt23_1024:
                   11348:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11349:                                        break;
                   11350:                                case F3_720_512:
                   11351:                                case F3_640_512:
                   11352:                                case F5_640_512:
                   11353:                                case F5_720_512:
                   11354:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11355:                                        break;
                   11356:                                case F8_256_128:
                   11357:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11358:                                        break;
                   11359:                                case FixedMedia:
                   11360:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11361:                                        break;
                   11362:                                case F3_1Pt44_512:
                   11363:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11364:                                        break;
                   11365:                                case F3_2Pt88_512:
                   11366:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11367:                                        break;
                   11368:                                default:
                   11369:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11370: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11371:                                        break;
1.1.1.22  root     11372:                                }
1.1.1.42  root     11373:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11374:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11375:                                switch(geo->MediaType) {
                   11376:                                case F5_360_512:
                   11377:                                case F5_320_512:
                   11378:                                case F5_320_1024:
                   11379:                                case F5_180_512:
                   11380:                                case F5_160_512:
                   11381:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11382:                                        break;
                   11383:                                default:
                   11384:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11385:                                        break;
                   11386:                                }
                   11387:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11388:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11389:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11390:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11391:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11392:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11393:                                switch(geo->MediaType) {
                   11394:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11395:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11396:                                        break;
                   11397:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11398:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11399:                                        break;
                   11400:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11401:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11402:                                        break;
                   11403:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11404:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11405:                                        break;
                   11406:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11407:                                case F3_1Pt2_512:
                   11408:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11409:                                case F5_720_512:
                   11410:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11411:                                        break;
                   11412:                                case FixedMedia:        // hard disk
                   11413:                                case RemovableMedia:
                   11414:                                case Unknown:
                   11415:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11416:                                        break;
                   11417:                                default:
                   11418:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11419:                                        break;
                   11420:                                }
                   11421:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11422:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11423:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11424:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11425:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11426:                                // 21h  BYTE    device type
                   11427:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11428:                        } else {
                   11429:                                REG16(AX) = 0x0f; // invalid drive
                   11430:                                m_CF = 1;
                   11431:                        }
1.1.1.48  root     11432: //             } else if(REG8(CL) == 0x61) {
                   11433: //                     // read logical device track
                   11434: //             } else if(REG8(CL) == 0x62) {
                   11435: //                     // verify logical device track
1.1.1.22  root     11436:                } else if(REG8(CL) == 0x66) {
                   11437:                        // get volume serial number
                   11438:                        char path[] = "A:\\";
                   11439:                        char volume_label[MAX_PATH];
                   11440:                        DWORD serial_number = 0;
                   11441:                        char file_system[MAX_PATH];
                   11442:                        
                   11443:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11444:                        
                   11445:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11446:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11447:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11448:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11449:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11450:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11451:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11452:                        } else {
                   11453:                                REG16(AX) = 0x0f; // invalid drive
                   11454:                                m_CF = 1;
                   11455:                        }
                   11456:                } else if(REG8(CL) == 0x67) {
                   11457:                        // get access flag
                   11458:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11459:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11460:                } else if(REG8(CL) == 0x68) {
                   11461:                        // sense media type
1.1.1.42  root     11462:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11463:                        
1.1.1.42  root     11464:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11465:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11466:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11467:                                
                   11468:                                switch(geo->MediaType) {
                   11469:                                case F3_720_512:
                   11470:                                case F5_720_512:
                   11471:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11472:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11473:                                        break;
                   11474:                                case F3_1Pt44_512:
                   11475:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11476:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11477:                                        break;
                   11478:                                case F3_2Pt88_512:
                   11479:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11480:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11481:                                        break;
                   11482:                                default:
                   11483:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11484:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11485:                                        break;
1.1.1.22  root     11486:                                }
                   11487:                        } else {
                   11488:                                REG16(AX) = 0x0f; // invalid drive
                   11489:                                m_CF = 1;
                   11490:                        }
                   11491:                } else if(REG8(CL) == 0x6a) {
                   11492:                        // unlock logical volume
                   11493:                } else if(REG8(CL) == 0x6b) {
                   11494:                        // unlock physical volume
1.1.1.48  root     11495: //             } else if(REG8(CL) == 0x6c) {
                   11496: //                     // get lock flag
                   11497: //             } else if(REG8(CL) == 0x6d) {
                   11498: //                     // enumerate open files
                   11499: //             } else if(REG8(CL) == 0x6e) {
                   11500: //                     // find swap file
                   11501: //             } else if(REG8(CL) == 0x6f) {
                   11502: //                     // get drive map information
                   11503: //             } else if(REG8(CL) == 0x70) {
                   11504: //                     // get current lock state
                   11505: //             } else if(REG8(CL) == 0x71) {
                   11506: //                     // get first cluster
1.1.1.22  root     11507:                } else {
                   11508:                        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));
                   11509:                        REG16(AX) = 0x01; // invalid function
                   11510:                        m_CF = 1;
                   11511:                }
                   11512:                break;
1.1.1.48  root     11513:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11514:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11515:                        REG16(AX) = 0x0f; // invalid drive
                   11516:                        m_CF = 1;
                   11517:                } else {
                   11518:                        REG8(AL) = 0;
1.1.1.22  root     11519:                }
                   11520:                break;
1.1.1.48  root     11521:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11522:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11523:                        REG16(AX) = 0x0f; // invalid drive
                   11524:                        m_CF = 1;
1.1.1.22  root     11525:                }
                   11526:                break;
1.1.1.48  root     11527:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11528:                switch(REG8(CL)) {
                   11529:                case 0x45:
                   11530:                case 0x4a:
1.1.1.48  root     11531:                case 0x4c:
                   11532:                case 0x4d:
1.1.1.22  root     11533:                case 0x65:
                   11534:                case 0x6a:
1.1.1.48  root     11535:                case 0x6b:
1.1.1.22  root     11536:                case 0x7f:
                   11537:                        REG16(AX) = 0x0000; // supported
                   11538:                        break;
                   11539:                default:
                   11540:                        REG8(AL) = 0x01; // ioctl capability not available
                   11541:                        m_CF = 1;
                   11542:                        break;
                   11543:                }
                   11544:                break;
1.1.1.48  root     11545:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     11546:                switch(REG8(CL)) {
                   11547:                case 0x40:
                   11548:                case 0x46:
                   11549:                case 0x4a:
                   11550:                case 0x4b:
                   11551:                case 0x60:
                   11552:                case 0x66:
                   11553:                case 0x67:
                   11554:                case 0x68:
                   11555:                case 0x6a:
                   11556:                case 0x6b:
1.1.1.48  root     11557:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   11558:                                // CH = 00h     Unknown
                   11559:                                // CH = 01h     COMn:
                   11560:                                // CH = 03h     CON
                   11561:                                // CH = 05h     LPTn:
                   11562:                                REG16(AX) = 0x0000; // supported
                   11563:                                break;
                   11564:                        }
1.1.1.22  root     11565:                default:
                   11566:                        REG8(AL) = 0x01; // ioctl capability not available
                   11567:                        m_CF = 1;
                   11568:                        break;
                   11569:                }
                   11570:                break;
1.1.1.48  root     11571:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   11572:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   11573:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   11574:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   11575:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   11576:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   11577:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   11578:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   11579:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   11580:        case 0x59: // DR Multiuser DOS 5.0 - API
                   11581:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     11582:                m_CF = 1;
                   11583:                break;
1.1       root     11584:        default:
1.1.1.22  root     11585:                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     11586:                REG16(AX) = 0x01;
1.1.1.3   root     11587:                m_CF = 1;
1.1       root     11588:                break;
                   11589:        }
                   11590: }
                   11591: 
                   11592: inline void msdos_int_21h_45h()
                   11593: {
                   11594:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11595:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11596:        
1.1.1.20  root     11597:        if(fd < process->max_files && file_handler[fd].valid) {
                   11598:                int dup_fd = _dup(fd);
                   11599:                if(dup_fd != -1) {
                   11600:                        REG16(AX) = dup_fd;
                   11601:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11602: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11603:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11604:                } else {
                   11605:                        REG16(AX) = errno;
1.1.1.3   root     11606:                        m_CF = 1;
1.1       root     11607:                }
                   11608:        } else {
                   11609:                REG16(AX) = 0x06;
1.1.1.3   root     11610:                m_CF = 1;
1.1       root     11611:        }
                   11612: }
                   11613: 
                   11614: inline void msdos_int_21h_46h()
                   11615: {
                   11616:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11617:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11618:        int dup_fd = REG16(CX);
                   11619:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11620:        
1.1.1.20  root     11621:        if(REG16(BX) == REG16(CX)) {
                   11622:                REG16(AX) = 0x06;
                   11623:                m_CF = 1;
                   11624:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11625:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11626:                        _close(tmp_fd);
                   11627:                        msdos_file_handler_close(tmp_fd);
                   11628:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11629:                }
                   11630:                if(_dup2(fd, dup_fd) != -1) {
                   11631:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11632: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11633:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11634:                } else {
                   11635:                        REG16(AX) = errno;
1.1.1.3   root     11636:                        m_CF = 1;
1.1       root     11637:                }
                   11638:        } else {
                   11639:                REG16(AX) = 0x06;
1.1.1.3   root     11640:                m_CF = 1;
1.1       root     11641:        }
                   11642: }
                   11643: 
                   11644: inline void msdos_int_21h_47h(int lfn)
                   11645: {
                   11646:        char path[MAX_PATH];
                   11647:        
                   11648:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     11649:                if(!lfn) {
                   11650:                        strcpy(path, msdos_short_path(path));
                   11651:                }
1.1       root     11652:                if(path[1] == ':') {
                   11653:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     11654:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11655:                } else {
1.1.1.45  root     11656:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11657:                }
                   11658:        } else {
                   11659:                REG16(AX) = errno;
1.1.1.3   root     11660:                m_CF = 1;
1.1       root     11661:        }
                   11662: }
                   11663: 
                   11664: inline void msdos_int_21h_48h()
                   11665: {
1.1.1.19  root     11666:        int seg, umb_linked;
1.1       root     11667:        
1.1.1.8   root     11668:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11669:                // unlink umb not to allocate memory in umb
                   11670:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11671:                        msdos_mem_unlink_umb();
                   11672:                }
1.1.1.8   root     11673:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11674:                        REG16(AX) = seg;
                   11675:                } else {
                   11676:                        REG16(AX) = 0x08;
                   11677:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11678:                        m_CF = 1;
                   11679:                }
1.1.1.19  root     11680:                if(umb_linked != 0) {
                   11681:                        msdos_mem_link_umb();
                   11682:                }
1.1.1.8   root     11683:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11684:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11685:                        REG16(AX) = seg;
                   11686:                } else {
                   11687:                        REG16(AX) = 0x08;
                   11688:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11689:                        m_CF = 1;
                   11690:                }
                   11691:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11692:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11693:                        REG16(AX) = seg;
                   11694:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11695:                        REG16(AX) = seg;
                   11696:                } else {
                   11697:                        REG16(AX) = 0x08;
                   11698:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11699:                        m_CF = 1;
                   11700:                }
1.1       root     11701:        }
                   11702: }
                   11703: 
                   11704: inline void msdos_int_21h_49h()
                   11705: {
1.1.1.14  root     11706:        int mcb_seg = SREG(ES) - 1;
                   11707:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11708:        
                   11709:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11710:                msdos_mem_free(SREG(ES));
                   11711:        } else {
1.1.1.33  root     11712:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11713:                m_CF = 1;
                   11714:        }
1.1       root     11715: }
                   11716: 
                   11717: inline void msdos_int_21h_4ah()
                   11718: {
1.1.1.14  root     11719:        int mcb_seg = SREG(ES) - 1;
                   11720:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11721:        int max_paragraphs;
                   11722:        
1.1.1.14  root     11723:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11724:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11725:                        REG16(AX) = 0x08;
                   11726:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11727:                        m_CF = 1;
                   11728:                }
                   11729:        } else {
1.1.1.33  root     11730:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11731:                m_CF = 1;
1.1       root     11732:        }
                   11733: }
                   11734: 
                   11735: inline void msdos_int_21h_4bh()
                   11736: {
1.1.1.3   root     11737:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11738:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11739:        
                   11740:        switch(REG8(AL)) {
                   11741:        case 0x00:
                   11742:        case 0x01:
                   11743:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11744:                        REG16(AX) = 0x02;
1.1.1.3   root     11745:                        m_CF = 1;
1.1       root     11746:                }
                   11747:                break;
1.1.1.14  root     11748:        case 0x03:
                   11749:                {
                   11750:                        int fd;
                   11751:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11752:                                REG16(AX) = 0x02;
                   11753:                                m_CF = 1;
                   11754:                                break;
                   11755:                        }
                   11756:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11757:                        _close(fd);
                   11758:                        
                   11759:                        UINT16 *overlay = (UINT16 *)param;
                   11760:                        
                   11761:                        // check exe header
                   11762:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11763:                        int header_size = 0;
                   11764:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11765:                                header_size = header->header_size * 16;
                   11766:                                // relocation
                   11767:                                int start_seg = overlay[1];
                   11768:                                for(int i = 0; i < header->relocations; i++) {
                   11769:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11770:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11771:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11772:                                }
                   11773:                        }
                   11774:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11775:                }
                   11776:                break;
1.1.1.48  root     11777:        case 0x04:
                   11778:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   11779: //     case 0x05:
                   11780: //             // DOS 5+ - Set Execution State
                   11781:        case 0x80:
                   11782:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   11783:        case 0xf0:
                   11784:        case 0xf1:
                   11785:                // DIET v1.10+
1.1.1.43  root     11786:        case 0xfd:
                   11787:        case 0xfe:
                   11788:                // unknown function called in FreeCOM
                   11789:                REG16(AX) = 0x01;
                   11790:                m_CF = 1;
                   11791:                break;
1.1       root     11792:        default:
1.1.1.22  root     11793:                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     11794:                REG16(AX) = 0x01;
1.1.1.3   root     11795:                m_CF = 1;
1.1       root     11796:                break;
                   11797:        }
                   11798: }
                   11799: 
                   11800: inline void msdos_int_21h_4ch()
                   11801: {
                   11802:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11803: }
                   11804: 
                   11805: inline void msdos_int_21h_4dh()
                   11806: {
                   11807:        REG16(AX) = retval;
                   11808: }
                   11809: 
                   11810: inline void msdos_int_21h_4eh()
                   11811: {
                   11812:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11813:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11814:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     11815:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11816:        WIN32_FIND_DATA fd;
                   11817:        
1.1.1.14  root     11818:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11819:        find->find_magic = FIND_MAGIC;
                   11820:        find->dta_index = dtainfo - dtalist;
1.1       root     11821:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11822:        dtainfo->allowable_mask = REG8(CL);
                   11823:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11824:        
1.1.1.14  root     11825:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11826:                dtainfo->allowable_mask &= ~8;
1.1       root     11827:        }
1.1.1.14  root     11828:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11829:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11830:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11831:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11832:                                FindClose(dtainfo->find_handle);
                   11833:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11834:                                break;
                   11835:                        }
                   11836:                }
                   11837:        }
1.1.1.13  root     11838:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11839:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11840:                msdos_find_file_conv_local_time(&fd);
                   11841:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11842:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11843:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11844:                REG16(AX) = 0;
1.1.1.14  root     11845:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11846:                find->attrib = 8;
                   11847:                find->size = 0;
                   11848:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11849:                dtainfo->allowable_mask &= ~8;
1.1       root     11850:                REG16(AX) = 0;
                   11851:        } else {
                   11852:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11853:                m_CF = 1;
1.1       root     11854:        }
                   11855: }
                   11856: 
                   11857: inline void msdos_int_21h_4fh()
                   11858: {
                   11859:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11860:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11861:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     11862:        WIN32_FIND_DATA fd;
                   11863:        
1.1.1.14  root     11864:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   11865:                REG16(AX) = 0x12;
                   11866:                m_CF = 1;
                   11867:                return;
                   11868:        }
                   11869:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     11870:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11871:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11872:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11873:                              !msdos_find_file_has_8dot3name(&fd)) {
                   11874:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11875:                                        FindClose(dtainfo->find_handle);
                   11876:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11877:                                        break;
                   11878:                                }
                   11879:                        }
                   11880:                } else {
1.1.1.13  root     11881:                        FindClose(dtainfo->find_handle);
                   11882:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11883:                }
                   11884:        }
1.1.1.13  root     11885:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11886:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11887:                msdos_find_file_conv_local_time(&fd);
                   11888:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11889:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11890:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11891:                REG16(AX) = 0;
1.1.1.14  root     11892:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11893:                find->attrib = 8;
                   11894:                find->size = 0;
                   11895:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11896:                dtainfo->allowable_mask &= ~8;
1.1       root     11897:                REG16(AX) = 0;
                   11898:        } else {
                   11899:                REG16(AX) = 0x12;
1.1.1.3   root     11900:                m_CF = 1;
1.1       root     11901:        }
                   11902: }
                   11903: 
                   11904: inline void msdos_int_21h_50h()
                   11905: {
1.1.1.8   root     11906:        if(current_psp != REG16(BX)) {
                   11907:                process_t *process = msdos_process_info_get(current_psp);
                   11908:                if(process != NULL) {
                   11909:                        process->psp = REG16(BX);
                   11910:                }
                   11911:                current_psp = REG16(BX);
1.1.1.23  root     11912:                msdos_sda_update(current_psp);
1.1.1.8   root     11913:        }
1.1       root     11914: }
                   11915: 
                   11916: inline void msdos_int_21h_51h()
                   11917: {
                   11918:        REG16(BX) = current_psp;
                   11919: }
                   11920: 
                   11921: inline void msdos_int_21h_52h()
                   11922: {
1.1.1.25  root     11923:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     11924:        i386_load_segment_descriptor(ES);
1.1.1.25  root     11925:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     11926: }
                   11927: 
1.1.1.43  root     11928: inline void msdos_int_21h_53h()
                   11929: {
                   11930:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   11931:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   11932:        
                   11933:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   11934:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   11935:        dpb->shift_count = 0;
                   11936:        dpb->reserved_sectors = 0;
                   11937:        dpb->fat_num = bpb->fat_num;
                   11938:        dpb->root_entries = bpb->root_entries;
                   11939:        dpb->first_data_sector = 0;
                   11940:        if(bpb->sectors_per_cluster != 0) {
                   11941:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   11942:        } else {
                   11943:                dpb->highest_cluster_num = 0;
                   11944:        }
                   11945:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   11946:        dpb->first_dir_sector = 0;
                   11947:        dpb->device_driver_header = 0;
                   11948:        dpb->media_type = bpb->media_type;
                   11949:        dpb->drive_accessed = 0;
                   11950:        dpb->next_dpb_ofs = 0xffff;
                   11951:        dpb->next_dpb_seg = 0xffff;
                   11952:        dpb->first_free_cluster = 0;
                   11953:        dpb->free_clusters = 0xffff;
                   11954: }
                   11955: 
1.1       root     11956: inline void msdos_int_21h_54h()
                   11957: {
                   11958:        process_t *process = msdos_process_info_get(current_psp);
                   11959:        
                   11960:        REG8(AL) = process->verify;
                   11961: }
                   11962: 
                   11963: inline void msdos_int_21h_55h()
                   11964: {
                   11965:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   11966:        
                   11967:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   11968:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   11969:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   11970:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   11971:        psp->parent_psp = current_psp;
                   11972: }
                   11973: 
                   11974: inline void msdos_int_21h_56h(int lfn)
                   11975: {
                   11976:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     11977:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   11978:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     11979:        
                   11980:        if(rename(src, dst)) {
                   11981:                REG16(AX) = errno;
1.1.1.3   root     11982:                m_CF = 1;
1.1       root     11983:        }
                   11984: }
                   11985: 
                   11986: inline void msdos_int_21h_57h()
                   11987: {
                   11988:        FILETIME time, local;
1.1.1.14  root     11989:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     11990:        HANDLE hHandle;
1.1       root     11991:        
1.1.1.21  root     11992:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     11993:                REG16(AX) = (UINT16)GetLastError();
                   11994:                m_CF = 1;
                   11995:                return;
                   11996:        }
                   11997:        ctime = atime = mtime = NULL;
                   11998:        
1.1       root     11999:        switch(REG8(AL)) {
                   12000:        case 0x00:
1.1.1.6   root     12001:        case 0x01:
1.1.1.14  root     12002:                mtime = &time;
1.1.1.6   root     12003:                break;
                   12004:        case 0x04:
                   12005:        case 0x05:
1.1.1.14  root     12006:                atime = &time;
1.1       root     12007:                break;
1.1.1.6   root     12008:        case 0x06:
                   12009:        case 0x07:
1.1.1.14  root     12010:                ctime = &time;
                   12011:                break;
                   12012:        default:
1.1.1.22  root     12013:                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     12014:                REG16(AX) = 0x01;
                   12015:                m_CF = 1;
                   12016:                return;
                   12017:        }
                   12018:        if(REG8(AL) & 1) {
1.1       root     12019:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12020:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12021:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12022:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12023:                        m_CF = 1;
1.1       root     12024:                }
1.1.1.14  root     12025:        } else {
1.1.1.21  root     12026:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12027:                        // assume a device and use the current time
                   12028:                        GetSystemTimeAsFileTime(&time);
                   12029:                }
                   12030:                FileTimeToLocalFileTime(&time, &local);
                   12031:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12032:        }
                   12033: }
                   12034: 
                   12035: inline void msdos_int_21h_58h()
                   12036: {
                   12037:        switch(REG8(AL)) {
                   12038:        case 0x00:
1.1.1.7   root     12039:                REG16(AX) = malloc_strategy;
                   12040:                break;
                   12041:        case 0x01:
1.1.1.24  root     12042: //             switch(REG16(BX)) {
                   12043:                switch(REG8(BL)) {
1.1.1.7   root     12044:                case 0x0000:
                   12045:                case 0x0001:
                   12046:                case 0x0002:
                   12047:                case 0x0040:
                   12048:                case 0x0041:
                   12049:                case 0x0042:
                   12050:                case 0x0080:
                   12051:                case 0x0081:
                   12052:                case 0x0082:
                   12053:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12054:                        msdos_sda_update(current_psp);
1.1.1.7   root     12055:                        break;
                   12056:                default:
1.1.1.22  root     12057:                        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     12058:                        REG16(AX) = 0x01;
                   12059:                        m_CF = 1;
                   12060:                        break;
                   12061:                }
                   12062:                break;
                   12063:        case 0x02:
1.1.1.19  root     12064:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12065:                break;
                   12066:        case 0x03:
1.1.1.24  root     12067: //             switch(REG16(BX)) {
                   12068:                switch(REG8(BL)) {
1.1.1.7   root     12069:                case 0x0000:
1.1.1.19  root     12070:                        msdos_mem_unlink_umb();
                   12071:                        break;
1.1.1.7   root     12072:                case 0x0001:
1.1.1.19  root     12073:                        msdos_mem_link_umb();
1.1.1.7   root     12074:                        break;
                   12075:                default:
1.1.1.22  root     12076:                        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     12077:                        REG16(AX) = 0x01;
                   12078:                        m_CF = 1;
                   12079:                        break;
                   12080:                }
1.1       root     12081:                break;
                   12082:        default:
1.1.1.22  root     12083:                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     12084:                REG16(AX) = 0x01;
1.1.1.3   root     12085:                m_CF = 1;
1.1       root     12086:                break;
                   12087:        }
                   12088: }
                   12089: 
                   12090: inline void msdos_int_21h_59h()
                   12091: {
1.1.1.47  root     12092:        if(REG16(BX) == 0x0000) {
                   12093:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12094:                
                   12095:                REG16(AX) = sda->extended_error_code;
                   12096:                REG8(BH)  = sda->error_class;
                   12097:                REG8(BL)  = sda->suggested_action;
                   12098:                REG8(CH)  = sda->locus_of_last_error;
                   12099:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12100:                if(sda->int21h_5d0ah_called != 0) {
                   12101:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12102:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12103: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12104:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12105: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12106: //                     i386_load_segment_descriptor(DS);
                   12107:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12108:                        i386_load_segment_descriptor(ES);
                   12109:                }
                   12110:                sda->int21h_5d0ah_called = 0;
                   12111: //     } else if(REG16(BX) == 0x0001) {
                   12112: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12113:        } else {
                   12114:                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));
                   12115:                REG16(AX) = 0x01;
                   12116:                m_CF = 1;
                   12117:        }
1.1       root     12118: }
                   12119: 
                   12120: inline void msdos_int_21h_5ah()
                   12121: {
1.1.1.3   root     12122:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12123:        int len = strlen(path);
                   12124:        char tmp[MAX_PATH];
                   12125:        
                   12126:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   12127:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12128:                
                   12129:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12130:                REG16(AX) = fd;
                   12131:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12132:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12133:                
                   12134:                strcpy(path, tmp);
                   12135:                int dx = REG16(DX) + len;
1.1.1.3   root     12136:                int ds = SREG(DS);
1.1       root     12137:                while(dx > 0xffff) {
                   12138:                        dx -= 0x10;
                   12139:                        ds++;
                   12140:                }
                   12141:                REG16(DX) = dx;
1.1.1.3   root     12142:                SREG(DS) = ds;
                   12143:                i386_load_segment_descriptor(DS);
1.1       root     12144:        } else {
                   12145:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12146:                m_CF = 1;
1.1       root     12147:        }
                   12148: }
                   12149: 
                   12150: inline void msdos_int_21h_5bh()
                   12151: {
1.1.1.45  root     12152:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12153:        
1.1.1.45  root     12154: //     if(msdos_is_existing_file(path)) {
                   12155:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12156:                // already exists
                   12157:                REG16(AX) = 0x50;
1.1.1.3   root     12158:                m_CF = 1;
1.1       root     12159:        } else {
                   12160:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12161:                
                   12162:                if(fd != -1) {
                   12163:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12164:                        REG16(AX) = fd;
                   12165:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12166:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12167:                } else {
                   12168:                        REG16(AX) = errno;
1.1.1.3   root     12169:                        m_CF = 1;
1.1       root     12170:                }
                   12171:        }
                   12172: }
                   12173: 
                   12174: inline void msdos_int_21h_5ch()
                   12175: {
                   12176:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12177:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12178:        
1.1.1.20  root     12179:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12180:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12181:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12182:                        UINT32 pos = _tell(fd);
                   12183:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12184:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12185:                                REG16(AX) = errno;
1.1.1.3   root     12186:                                m_CF = 1;
1.1       root     12187:                        }
1.1.1.20  root     12188:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12189:                        
1.1       root     12190:                        // some seconds may be passed in _locking()
1.1.1.35  root     12191:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12192:                } else {
                   12193:                        REG16(AX) = 0x01;
1.1.1.3   root     12194:                        m_CF = 1;
1.1       root     12195:                }
                   12196:        } else {
                   12197:                REG16(AX) = 0x06;
1.1.1.3   root     12198:                m_CF = 1;
1.1       root     12199:        }
                   12200: }
                   12201: 
1.1.1.22  root     12202: inline void msdos_int_21h_5dh()
                   12203: {
                   12204:        switch(REG8(AL)) {
1.1.1.45  root     12205:        case 0x00:
                   12206:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12207:                        // current system
                   12208:                        static bool reenter = false;
                   12209:                        if(!reenter) {
                   12210:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12211:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12212:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12213:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12214:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12215:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12216:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12217:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12218:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12219:                                i386_load_segment_descriptor(DS);
                   12220:                                i386_load_segment_descriptor(ES);
                   12221:                                reenter = true;
                   12222:                                try {
                   12223:                                        msdos_syscall(0x21);
                   12224:                                } catch(...) {
                   12225:                                }
                   12226:                                reenter = false;
                   12227:                        }
                   12228:                } else {
                   12229:                        REG16(AX) = 0x49; //  network software not installed
                   12230:                        m_CF = 1;
                   12231:                }
                   12232:                break;
1.1.1.22  root     12233:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     12234:                SREG(DS) = (SDA_TOP >> 4);
                   12235:                i386_load_segment_descriptor(DS);
                   12236:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12237:                REG16(CX) = 0x80;
                   12238:                REG16(DX) = 0x1a;
                   12239:                break;
1.1.1.45  root     12240:        case 0x07: // get redirected printer mode
                   12241:        case 0x08: // set redirected printer mode
                   12242:        case 0x09: // flush redirected printer output
                   12243:                REG16(AX) = 0x49; //  network software not installed
                   12244:                m_CF = 1;
                   12245:                break;
1.1.1.43  root     12246:        case 0x0a: // set extended error information
                   12247:                {
                   12248:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12249:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12250:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12251:                        // XXX: which one is correct ???
                   12252: #if 1
                   12253:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12254:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12255:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12256:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12257:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12258: #else
                   12259:                        // PC DOS 7 Technical Update
                   12260:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12261:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12262:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12263:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12264: #endif
                   12265:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12266: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12267:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12268: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12269:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12270:                }
                   12271:                break;
1.1.1.23  root     12272:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12273:                REG16(AX) = 0x01;
                   12274:                m_CF = 1;
                   12275:                break;
                   12276:        default:
                   12277:                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));
                   12278:                REG16(AX) = 0x01;
                   12279:                m_CF = 1;
                   12280:                break;
                   12281:        }
                   12282: }
                   12283: 
1.1.1.42  root     12284: inline void msdos_int_21h_5eh()
                   12285: {
                   12286:        switch(REG8(AL)) {
                   12287:        case 0x00:
                   12288:                {
                   12289:                        char name[256] = {0};
                   12290:                        DWORD dwSize = 256;
                   12291:                        
                   12292:                        if(GetComputerName(name, &dwSize)) {
                   12293:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12294:                                for(int i = 0; i < 15; i++) {
                   12295:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12296:                                }
                   12297:                                dest[15] = '\0';
                   12298:                                REG8(CH) = 0x01; // nonzero valid
                   12299:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12300:                        } else {
                   12301:                                REG16(AX) = 0x01;
                   12302:                                m_CF = 1;
                   12303:                        }
                   12304:                }
                   12305:                break;
                   12306:        default:
1.1.1.45  root     12307: //             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));
                   12308: //             REG16(AX) = 0x01;
                   12309:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12310:                m_CF = 1;
                   12311:                break;
                   12312:        }
                   12313: }
                   12314: 
1.1.1.30  root     12315: inline void msdos_int_21h_5fh()
                   12316: {
                   12317:        switch(REG8(AL)) {
1.1.1.42  root     12318:        case 0x05:
1.1.1.44  root     12319:                REG16(BP) = 0;
                   12320:                for(int i = 0; i < 26; i++) {
                   12321:                        if(msdos_is_remote_drive(i)) {
                   12322:                                REG16(BP)++;
1.1.1.42  root     12323:                        }
                   12324:                }
1.1.1.30  root     12325:        case 0x02:
1.1.1.44  root     12326:                for(int i = 0, index = 0; i < 26; i++) {
                   12327:                        if(msdos_is_remote_drive(i)) {
                   12328:                                if(index == REG16(BX)) {
                   12329:                                        char volume[] = "A:";
1.1.1.30  root     12330:                                        volume[0] = 'A' + i;
1.1.1.44  root     12331:                                        DWORD dwSize = 128;
                   12332:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   12333:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   12334:                                        REG8(BH) = 0x00; // valid
                   12335:                                        REG8(BL) = 0x04; // disk drive
                   12336:                                        REG16(CX) = 0x00; // LANtastic
                   12337:                                        return;
1.1.1.30  root     12338:                                }
1.1.1.44  root     12339:                                index++;
1.1.1.30  root     12340:                        }
                   12341:                }
                   12342:                REG16(AX) = 0x12; // no more files
                   12343:                m_CF = 1;
                   12344:                break;
1.1.1.44  root     12345:        case 0x07:
                   12346:                if(msdos_is_valid_drive(REG8(DL))) {
                   12347:                        msdos_cds_update(REG8(DL));
                   12348:                } else {
                   12349:                        REG16(AX) = 0x0f; // invalid drive
                   12350:                        m_CF = 1;
                   12351:                }
                   12352:                break;
                   12353:        case 0x08:
                   12354:                if(msdos_is_valid_drive(REG8(DL))) {
                   12355:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12356:                        cds->drive_attrib = 0x0000;
                   12357:                } else {
                   12358:                        REG16(AX) = 0x0f; // invalid drive
                   12359:                        m_CF = 1;
                   12360:                }
                   12361:                break;
1.1.1.30  root     12362:        default:
1.1.1.45  root     12363: //             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));
                   12364: //             REG16(AX) = 0x01;
                   12365:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12366:                m_CF = 1;
                   12367:                break;
                   12368:        }
                   12369: }
                   12370: 
1.1       root     12371: inline void msdos_int_21h_60h(int lfn)
                   12372: {
1.1.1.45  root     12373:        char full[MAX_PATH];
                   12374:        const char *path = NULL;
1.1.1.14  root     12375:        
1.1       root     12376:        if(lfn) {
1.1.1.14  root     12377:                char *name;
                   12378:                *full = '\0';
1.1.1.3   root     12379:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12380:                switch(REG8(CL)) {
                   12381:                case 1:
                   12382:                        GetShortPathName(full, full, MAX_PATH);
                   12383:                        my_strupr(full);
                   12384:                        break;
                   12385:                case 2:
                   12386:                        GetLongPathName(full, full, MAX_PATH);
                   12387:                        break;
                   12388:                }
                   12389:                path = full;
                   12390:        } else {
                   12391:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12392:        }
                   12393:        if(*path != '\0') {
                   12394:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12395:        } else {
1.1.1.14  root     12396:                REG16(AX) = (UINT16)GetLastError();
                   12397:                m_CF = 1;
1.1       root     12398:        }
                   12399: }
                   12400: 
                   12401: inline void msdos_int_21h_61h()
                   12402: {
                   12403:        REG8(AL) = 0;
                   12404: }
                   12405: 
                   12406: inline void msdos_int_21h_62h()
                   12407: {
                   12408:        REG16(BX) = current_psp;
                   12409: }
                   12410: 
                   12411: inline void msdos_int_21h_63h()
                   12412: {
                   12413:        switch(REG8(AL)) {
                   12414:        case 0x00:
1.1.1.3   root     12415:                SREG(DS) = (DBCS_TABLE >> 4);
                   12416:                i386_load_segment_descriptor(DS);
1.1       root     12417:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12418:                REG8(AL) = 0x00;
                   12419:                break;
1.1.1.22  root     12420:        case 0x01: // set korean input mode
                   12421:        case 0x02: // get korean input mode
                   12422:                REG8(AL) = 0xff; // not supported
                   12423:                break;
1.1       root     12424:        default:
1.1.1.22  root     12425:                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     12426:                REG16(AX) = 0x01;
1.1.1.3   root     12427:                m_CF = 1;
1.1       root     12428:                break;
                   12429:        }
                   12430: }
                   12431: 
1.1.1.25  root     12432: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12433: {
1.1.1.25  root     12434:        switch(func) {
1.1.1.17  root     12435:        case 0x01:
                   12436:                if(REG16(CX) >= 5) {
1.1.1.19  root     12437:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12438:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12439:                                REG16(CX) = sizeof(data);
                   12440:                        ZeroMemory(data, sizeof(data));
                   12441:                        data[0] = 0x01;
                   12442:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12443:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12444:                        *(UINT16 *)(data + 5) = active_code_page;
                   12445:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12446: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12447:                } else {
1.1.1.25  root     12448:                        return(0x08); // insufficient memory
1.1.1.17  root     12449:                }
                   12450:                break;
                   12451:        case 0x02:
                   12452:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12453:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12454:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12455: //             REG16(AX) = active_code_page;
1.1.1.17  root     12456:                REG16(CX) = 0x05;
                   12457:                break;
1.1.1.23  root     12458:        case 0x03:
                   12459:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12460:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12461:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12462: //             REG16(AX) = active_code_page;
1.1.1.23  root     12463:                REG16(CX) = 0x05;
                   12464:                break;
1.1.1.17  root     12465:        case 0x04:
                   12466:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12467:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12468:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12469: //             REG16(AX) = active_code_page;
1.1.1.17  root     12470:                REG16(CX) = 0x05;
                   12471:                break;
                   12472:        case 0x05:
                   12473:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12474:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12475:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12476: //             REG16(AX) = active_code_page;
1.1.1.17  root     12477:                REG16(CX) = 0x05;
                   12478:                break;
                   12479:        case 0x06:
                   12480:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12481:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12482:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12483: //             REG16(AX) = active_code_page;
1.1.1.17  root     12484:                REG16(CX) = 0x05;
                   12485:                break;
1.1       root     12486:        case 0x07:
1.1.1.3   root     12487:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12488:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12489:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12490: //             REG16(AX) = active_code_page;
1.1       root     12491:                REG16(CX) = 0x05;
                   12492:                break;
1.1.1.25  root     12493:        default:
                   12494:                return(0x01); // function number invalid
                   12495:        }
                   12496:        return(0x00);
                   12497: }
                   12498: 
                   12499: inline void msdos_int_21h_65h()
                   12500: {
                   12501:        char tmp[0x10000];
                   12502:        
                   12503:        switch(REG8(AL)) {
1.1.1.43  root     12504:        case 0x00:
                   12505:                if(REG16(CX) >= 7) {
                   12506:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12507:                        REG16(AX) = system_code_page;
                   12508:                } else {
                   12509:                        REG16(AX) = 0x0c;
                   12510:                        m_CF = 1;
                   12511:                }
                   12512:                break;
1.1.1.25  root     12513:        case 0x01:
                   12514:        case 0x02:
                   12515:        case 0x03:
                   12516:        case 0x04:
                   12517:        case 0x05:
                   12518:        case 0x06:
                   12519:        case 0x07:
                   12520:                {
                   12521:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12522:                        if(result) {
                   12523:                                REG16(AX) = result;
                   12524:                                m_CF = 1;
                   12525:                        } else {
                   12526:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12527:                        }
                   12528:                }
                   12529:                break;
1.1       root     12530:        case 0x20:
1.1.1.25  root     12531:        case 0xa0:
1.1.1.19  root     12532:                memset(tmp, 0, sizeof(tmp));
                   12533:                tmp[0] = REG8(DL);
1.1       root     12534:                my_strupr(tmp);
                   12535:                REG8(DL) = tmp[0];
                   12536:                break;
                   12537:        case 0x21:
1.1.1.25  root     12538:        case 0xa1:
1.1       root     12539:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12540:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12541:                my_strupr(tmp);
1.1.1.3   root     12542:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12543:                break;
                   12544:        case 0x22:
1.1.1.25  root     12545:        case 0xa2:
1.1.1.3   root     12546:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12547:                break;
1.1.1.25  root     12548:        case 0x23:
                   12549:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12550:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12551:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12552:                        REG16(AX) = 0x00;
                   12553:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12554:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12555:                        REG16(AX) = 0x01;
                   12556:                } else {
                   12557:                        REG16(AX) = 0x02;
                   12558:                }
                   12559:                break;
1.1       root     12560:        default:
1.1.1.22  root     12561:                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     12562:                REG16(AX) = 0x01;
1.1.1.3   root     12563:                m_CF = 1;
1.1       root     12564:                break;
                   12565:        }
                   12566: }
                   12567: 
                   12568: inline void msdos_int_21h_66h()
                   12569: {
                   12570:        switch(REG8(AL)) {
                   12571:        case 0x01:
                   12572:                REG16(BX) = active_code_page;
                   12573:                REG16(DX) = system_code_page;
                   12574:                break;
                   12575:        case 0x02:
                   12576:                if(active_code_page == REG16(BX)) {
                   12577:                        REG16(AX) = 0xeb41;
                   12578:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12579:                        active_code_page = REG16(BX);
1.1.1.17  root     12580:                        msdos_nls_tables_update();
1.1       root     12581:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12582:                        SetConsoleCP(active_code_page);
                   12583:                        SetConsoleOutputCP(active_code_page);
1.1       root     12584:                } else {
                   12585:                        REG16(AX) = 0x25;
1.1.1.3   root     12586:                        m_CF = 1;
1.1       root     12587:                }
                   12588:                break;
                   12589:        default:
1.1.1.22  root     12590:                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     12591:                REG16(AX) = 0x01;
1.1.1.3   root     12592:                m_CF = 1;
1.1       root     12593:                break;
                   12594:        }
                   12595: }
                   12596: 
                   12597: inline void msdos_int_21h_67h()
                   12598: {
                   12599:        process_t *process = msdos_process_info_get(current_psp);
                   12600:        
                   12601:        if(REG16(BX) <= MAX_FILES) {
                   12602:                process->max_files = max(REG16(BX), 20);
                   12603:        } else {
                   12604:                REG16(AX) = 0x08;
1.1.1.3   root     12605:                m_CF = 1;
1.1       root     12606:        }
                   12607: }
                   12608: 
                   12609: inline void msdos_int_21h_68h()
                   12610: {
                   12611:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12612:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12613:        
1.1.1.20  root     12614:        if(fd < process->max_files && file_handler[fd].valid) {
                   12615:                // fflush(_fdopen(fd, ""));
1.1       root     12616:        } else {
                   12617:                REG16(AX) = 0x06;
1.1.1.3   root     12618:                m_CF = 1;
1.1       root     12619:        }
                   12620: }
                   12621: 
                   12622: inline void msdos_int_21h_69h()
                   12623: {
1.1.1.3   root     12624:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12625:        char path[] = "A:\\";
                   12626:        char volume_label[MAX_PATH];
                   12627:        DWORD serial_number = 0;
                   12628:        char file_system[MAX_PATH];
                   12629:        
                   12630:        if(REG8(BL) == 0) {
                   12631:                path[0] = 'A' + _getdrive() - 1;
                   12632:        } else {
                   12633:                path[0] = 'A' + REG8(BL) - 1;
                   12634:        }
                   12635:        
                   12636:        switch(REG8(AL)) {
                   12637:        case 0x00:
                   12638:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12639:                        info->info_level = 0;
                   12640:                        info->serial_number = serial_number;
                   12641:                        memset(info->volume_label, 0x20, 11);
                   12642:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12643:                        memset(info->file_system, 0x20, 8);
                   12644:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12645:                } else {
                   12646:                        REG16(AX) = errno;
1.1.1.3   root     12647:                        m_CF = 1;
1.1       root     12648:                }
                   12649:                break;
                   12650:        case 0x01:
                   12651:                REG16(AX) = 0x03;
1.1.1.3   root     12652:                m_CF = 1;
1.1.1.45  root     12653:                break;
                   12654:        default:
                   12655:                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));
                   12656:                REG16(AX) = 0x01;
                   12657:                m_CF = 1;
                   12658:                break;
1.1       root     12659:        }
                   12660: }
                   12661: 
                   12662: inline void msdos_int_21h_6ah()
                   12663: {
                   12664:        REG8(AH) = 0x68;
                   12665:        msdos_int_21h_68h();
                   12666: }
                   12667: 
                   12668: inline void msdos_int_21h_6bh()
                   12669: {
1.1.1.45  root     12670:        REG8(AL) = 0x00;
1.1       root     12671: }
                   12672: 
                   12673: inline void msdos_int_21h_6ch(int lfn)
                   12674: {
1.1.1.45  root     12675:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12676:        int mode = REG8(BL) & 0x03;
                   12677:        
                   12678:        if(mode < 0x03) {
1.1.1.29  root     12679:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12680:                        // file exists
                   12681:                        if(REG8(DL) & 1) {
1.1.1.37  root     12682:                                int fd = -1;
                   12683:                                int sio_port = 0;
                   12684:                                int lpt_port = 0;
1.1       root     12685:                                
1.1.1.45  root     12686:                                if(msdos_is_device_path(path)) {
                   12687:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12688:                                } else {
1.1.1.13  root     12689:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12690:                                }
1.1       root     12691:                                if(fd != -1) {
                   12692:                                        REG16(AX) = fd;
                   12693:                                        REG16(CX) = 1;
1.1.1.45  root     12694:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12695:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12696:                                } else {
                   12697:                                        REG16(AX) = errno;
1.1.1.3   root     12698:                                        m_CF = 1;
1.1       root     12699:                                }
                   12700:                        } else if(REG8(DL) & 2) {
                   12701:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12702:                                int fd = -1;
                   12703:                                int sio_port = 0;
                   12704:                                int lpt_port = 0;
1.1       root     12705:                                
1.1.1.45  root     12706:                                if(msdos_is_device_path(path)) {
                   12707:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12708:                                } else {
                   12709:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12710:                                }
                   12711:                                if(fd != -1) {
                   12712:                                        if(attr == -1) {
                   12713:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12714:                                        }
                   12715:                                        SetFileAttributes(path, attr);
                   12716:                                        REG16(AX) = fd;
                   12717:                                        REG16(CX) = 3;
1.1.1.45  root     12718:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12719:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12720:                                } else {
                   12721:                                        REG16(AX) = errno;
1.1.1.3   root     12722:                                        m_CF = 1;
1.1       root     12723:                                }
                   12724:                        } else {
                   12725:                                REG16(AX) = 0x50;
1.1.1.3   root     12726:                                m_CF = 1;
1.1       root     12727:                        }
                   12728:                } else {
                   12729:                        // file not exists
                   12730:                        if(REG8(DL) & 0x10) {
                   12731:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12732:                                
                   12733:                                if(fd != -1) {
                   12734:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12735:                                        REG16(AX) = fd;
                   12736:                                        REG16(CX) = 2;
                   12737:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12738:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12739:                                } else {
                   12740:                                        REG16(AX) = errno;
1.1.1.3   root     12741:                                        m_CF = 1;
1.1       root     12742:                                }
                   12743:                        } else {
                   12744:                                REG16(AX) = 0x02;
1.1.1.3   root     12745:                                m_CF = 1;
1.1       root     12746:                        }
                   12747:                }
                   12748:        } else {
                   12749:                REG16(AX) = 0x0c;
1.1.1.3   root     12750:                m_CF = 1;
1.1       root     12751:        }
                   12752: }
                   12753: 
1.1.1.43  root     12754: inline void msdos_int_21h_70h()
                   12755: {
                   12756:        switch(REG8(AL)) {
1.1.1.48  root     12757:        case 0x00: // get ??? info
                   12758:        case 0x01: // set above info
                   12759: //             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));
                   12760:                REG16(AX) = 0x7000;
                   12761:                m_CF = 1;
                   12762:                break;
                   12763:        case 0x02: // set general internationalization info
1.1.1.43  root     12764:                if(REG16(CX) >= 7) {
                   12765:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12766:                        msdos_nls_tables_update();
                   12767:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12768:                        REG16(AX) = system_code_page;
                   12769:                } else {
                   12770:                        REG16(AX) = 0x0c;
                   12771:                        m_CF = 1;
                   12772:                }
                   12773:                break;
                   12774:        default:
                   12775:                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     12776:                REG16(AX) = 0x7000;
1.1.1.43  root     12777:                m_CF = 1;
                   12778:                break;
                   12779:        }
                   12780: }
                   12781: 
1.1       root     12782: inline void msdos_int_21h_710dh()
                   12783: {
                   12784:        // reset drive
                   12785: }
                   12786: 
1.1.1.48  root     12787: inline void msdos_int_21h_7141h()
1.1.1.17  root     12788: {
                   12789:        if(REG16(SI) == 0) {
1.1.1.48  root     12790:                msdos_int_21h_41h(1);
1.1.1.17  root     12791:                return;
                   12792:        }
                   12793:        if(REG16(SI) != 1) {
                   12794:                REG16(AX) = 5;
                   12795:                m_CF = 1;
                   12796:        }
                   12797:        /* wild card and matching attributes... */
                   12798:        char tmp[MAX_PATH * 2];
                   12799:        // copy search pathname (and quick check overrun)
                   12800:        ZeroMemory(tmp, sizeof(tmp));
                   12801:        tmp[MAX_PATH - 1] = '\0';
                   12802:        tmp[MAX_PATH] = 1;
                   12803:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12804:        
                   12805:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12806:                REG16(AX) = 1;
                   12807:                m_CF = 1;
                   12808:                return;
                   12809:        }
                   12810:        for(char *s = tmp; *s; ++s) {
                   12811:                if(*s == '/') {
                   12812:                        *s = '\\';
                   12813:                }
                   12814:        }
                   12815:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12816:        if(tmp_name) {
                   12817:                ++tmp_name;
                   12818:        } else {
                   12819:                tmp_name = strchr(tmp, ':');
                   12820:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12821:        }
                   12822:        
                   12823:        WIN32_FIND_DATAA fd;
                   12824:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12825:        if(fh == INVALID_HANDLE_VALUE) {
                   12826:                REG16(AX) = 2;
                   12827:                m_CF = 1;
                   12828:                return;
                   12829:        }
                   12830:        do {
                   12831:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12832:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     12833:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     12834:                                REG16(AX) = 5;
                   12835:                                m_CF = 1;
                   12836:                                break;
                   12837:                        }
                   12838:                }
                   12839:        } while(FindNextFileA(fh, &fd));
                   12840:        if(!m_CF) {
                   12841:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   12842:                        m_CF = 1;
                   12843:                        REG16(AX) = 2;
                   12844:                }
                   12845:        }
                   12846:        FindClose(fh);
                   12847: }
                   12848: 
1.1       root     12849: inline void msdos_int_21h_714eh()
                   12850: {
                   12851:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12852:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   12853:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12854:        WIN32_FIND_DATA fd;
                   12855:        
1.1.1.13  root     12856:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12857:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12858:                FindClose(dtainfo->find_handle);
                   12859:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12860:        }
                   12861:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12862:        dtainfo->allowable_mask = REG8(CL);
                   12863:        dtainfo->required_mask = REG8(CH);
                   12864:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12865:        
1.1.1.14  root     12866:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12867:                dtainfo->allowable_mask &= ~8;
1.1       root     12868:        }
1.1.1.14  root     12869:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12870:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12871:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12872:                                FindClose(dtainfo->find_handle);
                   12873:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12874:                                break;
                   12875:                        }
                   12876:                }
                   12877:        }
1.1.1.13  root     12878:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12879:                find->attrib = fd.dwFileAttributes;
                   12880:                msdos_find_file_conv_local_time(&fd);
                   12881:                if(REG16(SI) == 0) {
                   12882:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12883:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12884:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12885:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12886:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12887:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12888:                } else {
                   12889:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12890:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12891:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12892:                }
                   12893:                find->size_hi = fd.nFileSizeHigh;
                   12894:                find->size_lo = fd.nFileSizeLow;
                   12895:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12896:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12897:                REG16(AX) = dtainfo - dtalist + 1;
                   12898:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12899:                // volume label
                   12900:                find->attrib = 8;
                   12901:                find->size_hi = find->size_lo = 0;
                   12902:                strcpy(find->full_name, process->volume_label);
                   12903:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12904:                dtainfo->allowable_mask &= ~8;
                   12905:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     12906:        } else {
                   12907:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12908:                m_CF = 1;
1.1       root     12909:        }
                   12910: }
                   12911: 
                   12912: inline void msdos_int_21h_714fh()
                   12913: {
                   12914:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12915:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12916:        WIN32_FIND_DATA fd;
                   12917:        
1.1.1.14  root     12918:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12919:                REG16(AX) = 6;
1.1.1.13  root     12920:                m_CF = 1;
                   12921:                return;
                   12922:        }
1.1.1.14  root     12923:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12924:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12925:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12926:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12927:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12928:                                        FindClose(dtainfo->find_handle);
                   12929:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12930:                                        break;
                   12931:                                }
                   12932:                        }
                   12933:                } else {
1.1.1.13  root     12934:                        FindClose(dtainfo->find_handle);
                   12935:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12936:                }
                   12937:        }
1.1.1.13  root     12938:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12939:                find->attrib = fd.dwFileAttributes;
                   12940:                msdos_find_file_conv_local_time(&fd);
                   12941:                if(REG16(SI) == 0) {
                   12942:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12943:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12944:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12945:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12946:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12947:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12948:                } else {
                   12949:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12950:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12951:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12952:                }
                   12953:                find->size_hi = fd.nFileSizeHigh;
                   12954:                find->size_lo = fd.nFileSizeLow;
                   12955:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12956:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12957:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12958:                // volume label
                   12959:                find->attrib = 8;
                   12960:                find->size_hi = find->size_lo = 0;
                   12961:                strcpy(find->full_name, process->volume_label);
                   12962:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12963:                dtainfo->allowable_mask &= ~8;
1.1       root     12964:        } else {
                   12965:                REG16(AX) = 0x12;
1.1.1.3   root     12966:                m_CF = 1;
1.1       root     12967:        }
                   12968: }
                   12969: 
                   12970: inline void msdos_int_21h_71a0h()
                   12971: {
                   12972:        DWORD max_component_len, file_sys_flag;
                   12973:        
1.1.1.14  root     12974:        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))) {
                   12975:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   12976:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     12977:                REG16(CX) = (UINT16)max_component_len;          // 255
                   12978:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   12979:        } else {
                   12980:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12981:                m_CF = 1;
1.1       root     12982:        }
                   12983: }
                   12984: 
                   12985: inline void msdos_int_21h_71a1h()
                   12986: {
1.1.1.14  root     12987:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12988:                REG16(AX) = 6;
1.1.1.13  root     12989:                m_CF = 1;
                   12990:                return;
                   12991:        }
1.1.1.14  root     12992:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12993:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12994:                FindClose(dtainfo->find_handle);
                   12995:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12996:        }
                   12997: }
                   12998: 
                   12999: inline void msdos_int_21h_71a6h()
                   13000: {
                   13001:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13002:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13003:        
1.1.1.3   root     13004:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13005:        struct _stat64 status;
                   13006:        DWORD serial_number = 0;
                   13007:        
1.1.1.20  root     13008:        if(fd < process->max_files && file_handler[fd].valid) {
                   13009:                if(_fstat64(fd, &status) == 0) {
                   13010:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13011:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13012:                                char volume[] = "A:\\";
1.1.1.20  root     13013:                                volume[0] = file_handler[fd].path[1];
1.1       root     13014:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   13015:                        }
1.1.1.20  root     13016:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     13017:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13018:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13019:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13020:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13021:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13022:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13023:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13024:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13025:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13026:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13027:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13028:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13029:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13030:                } else {
                   13031:                        REG16(AX) = errno;
1.1.1.3   root     13032:                        m_CF = 1;
1.1       root     13033:                }
                   13034:        } else {
                   13035:                REG16(AX) = 0x06;
1.1.1.3   root     13036:                m_CF = 1;
1.1       root     13037:        }
                   13038: }
                   13039: 
                   13040: inline void msdos_int_21h_71a7h()
                   13041: {
                   13042:        switch(REG8(BL)) {
                   13043:        case 0x00:
1.1.1.3   root     13044:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13045:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13046:                        m_CF = 1;
1.1       root     13047:                }
                   13048:                break;
                   13049:        case 0x01:
                   13050:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13051:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13052:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13053:                        m_CF = 1;
1.1       root     13054:                }
                   13055:                break;
                   13056:        default:
1.1.1.22  root     13057:                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     13058:                REG16(AX) = 0x7100;
1.1.1.3   root     13059:                m_CF = 1;
1.1       root     13060:                break;
                   13061:        }
                   13062: }
                   13063: 
                   13064: inline void msdos_int_21h_71a8h()
                   13065: {
                   13066:        if(REG8(DH) == 0) {
                   13067:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13068:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13069:                memset(fcb, 0x20, sizeof(fcb));
                   13070:                int len = strlen(tmp);
1.1.1.21  root     13071:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13072:                        if(tmp[i] == '.') {
                   13073:                                pos = 8;
                   13074:                        } else {
                   13075:                                if(msdos_lead_byte_check(tmp[i])) {
                   13076:                                        fcb[pos++] = tmp[i++];
                   13077:                                }
                   13078:                                fcb[pos++] = tmp[i];
                   13079:                        }
                   13080:                }
1.1.1.3   root     13081:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13082:        } else {
1.1.1.3   root     13083:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13084:        }
                   13085: }
                   13086: 
1.1.1.22  root     13087: inline void msdos_int_21h_71aah()
                   13088: {
                   13089:        char drv[] = "A:", path[MAX_PATH];
                   13090:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13091:        
                   13092:        if(REG8(BL) == 0) {
                   13093:                drv[0] = 'A' + _getdrive() - 1;
                   13094:        } else {
                   13095:                drv[0] = 'A' + REG8(BL) - 1;
                   13096:        }
                   13097:        switch(REG8(BH)) {
                   13098:        case 0x00:
1.1.1.44  root     13099:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13100:                        REG16(AX) = 0x0f; // invalid drive
                   13101:                        m_CF = 1;
                   13102:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   13103:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13104:                        m_CF = 1;
                   13105:                }
                   13106:                break;
                   13107:        case 0x01:
1.1.1.44  root     13108:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13109:                        REG16(AX) = 0x0f; // invalid drive
                   13110:                        m_CF = 1;
                   13111:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13112:                        REG16(AX) = 0x0f; // invalid drive
                   13113:                        m_CF = 1;
                   13114:                }
                   13115:                break;
                   13116:        case 0x02:
1.1.1.44  root     13117:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13118:                        REG16(AX) = 0x0f; // invalid drive
                   13119:                        m_CF = 1;
                   13120:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13121:                        REG16(AX) = 0x0f; // invalid drive
                   13122:                        m_CF = 1;
                   13123:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13124:                        REG16(AX) = 0x0f; // invalid drive
                   13125:                        m_CF = 1;
                   13126:                } else {
                   13127:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13128:                }
                   13129:                break;
                   13130:        default:
                   13131:                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     13132:                REG16(AX) = 0x7100;
1.1.1.22  root     13133:                m_CF = 1;
                   13134:                break;
                   13135:        }
                   13136: }
                   13137: 
1.1.1.14  root     13138: inline void msdos_int_21h_7300h()
                   13139: {
1.1.1.44  root     13140:        REG8(AL) = REG8(CL);
                   13141:        REG8(AH) = 0;
1.1.1.14  root     13142: }
                   13143: 
                   13144: inline void msdos_int_21h_7302h()
                   13145: {
                   13146:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13147:        UINT16 seg, ofs;
                   13148:        
                   13149:        if(REG16(CX) < 0x3f) {
                   13150:                REG8(AL) = 0x18;
                   13151:                m_CF = 1;
                   13152:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13153:                REG8(AL) = 0xff;
                   13154:                m_CF = 1;
                   13155:        } else {
                   13156:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13157:        }
                   13158: }
                   13159: 
1.1       root     13160: inline void msdos_int_21h_7303h()
                   13161: {
1.1.1.3   root     13162:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13163:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13164:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13165:        
                   13166:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   13167:                info->size_of_structure = sizeof(ext_space_info_t);
                   13168:                info->structure_version = 0;
                   13169:                info->sectors_per_cluster = sectors_per_cluster;
                   13170:                info->bytes_per_sector = bytes_per_sector;
                   13171:                info->available_clusters_on_drive = free_clusters;
                   13172:                info->total_clusters_on_drive = total_clusters;
                   13173:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13174:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13175:                info->available_allocation_units = free_clusters;       // ???
                   13176:                info->total_allocation_units = total_clusters;          // ???
                   13177:        } else {
                   13178:                REG16(AX) = errno;
1.1.1.3   root     13179:                m_CF = 1;
1.1       root     13180:        }
                   13181: }
                   13182: 
1.1.1.30  root     13183: inline void msdos_int_21h_dbh()
                   13184: {
                   13185:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13186:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13187:        REG8(AL) = dos_info->last_drive;
                   13188: }
                   13189: 
                   13190: inline void msdos_int_21h_dch()
                   13191: {
                   13192:        // Novell NetWare - Connection Services - Get Connection Number
                   13193:        REG8(AL) = 0x00;
                   13194: }
                   13195: 
1.1.1.32  root     13196: inline void msdos_int_24h()
                   13197: {
                   13198:        const char *message = NULL;
                   13199:        int key = 0;
                   13200:        
                   13201:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13202:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13203:                        if(active_code_page == 932) {
                   13204:                                message = critical_error_table[i].message_japanese;
                   13205:                        }
                   13206:                        if(message == NULL) {
                   13207:                                message = critical_error_table[i].message_english;
                   13208:                        }
                   13209:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13210:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13211:                        
                   13212:                        SREG(ES) = WORK_TOP >> 4;
                   13213:                        i386_load_segment_descriptor(ES);
                   13214:                        REG16(DI) = 0x0000;
                   13215:                        break;
                   13216:                }
                   13217:        }
                   13218:        fprintf(stderr, "\n%s", message);
                   13219:        if(!(REG8(AH) & 0x80)) {
                   13220:                if(REG8(AH) & 0x01) {
                   13221:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13222:                } else {
                   13223:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13224:                }
                   13225:        }
                   13226:        fprintf(stderr, "\n");
                   13227:        
1.1.1.33  root     13228:        {
1.1.1.32  root     13229:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13230:        }
1.1.1.32  root     13231:        if(REG8(AH) & 0x10) {
                   13232:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13233:        }
                   13234:        if(REG8(AH) & 0x20) {
                   13235:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13236:        }
                   13237:        if(REG8(AH) & 0x08) {
                   13238:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13239:        }
                   13240:        fprintf(stderr, "? ");
                   13241:        
                   13242:        while(1) {
                   13243:                while(!_kbhit()) {
                   13244:                        Sleep(10);
                   13245:                }
                   13246:                key = _getch();
                   13247:                
                   13248:                if(key == 'I' || key == 'i') {
                   13249:                        if(REG8(AH) & 0x20) {
                   13250:                                REG8(AL) = 0;
                   13251:                                break;
                   13252:                        }
                   13253:                } else if(key == 'R' || key == 'r') {
                   13254:                        if(REG8(AH) & 0x10) {
                   13255:                                REG8(AL) = 1;
                   13256:                                break;
                   13257:                        }
                   13258:                } else if(key == 'A' || key == 'a') {
                   13259:                        REG8(AL) = 2;
                   13260:                        break;
                   13261:                } else if(key == 'F' || key == 'f') {
                   13262:                        if(REG8(AH) & 0x08) {
                   13263:                                REG8(AL) = 3;
                   13264:                                break;
                   13265:                        }
                   13266:                }
                   13267:        }
                   13268:        fprintf(stderr, "%c\n", key);
                   13269: }
                   13270: 
1.1       root     13271: inline void msdos_int_25h()
                   13272: {
                   13273:        UINT16 seg, ofs;
                   13274:        DWORD dwSize;
                   13275:        
1.1.1.3   root     13276: #if defined(HAS_I386)
                   13277:        I386OP(pushf)();
                   13278: #else
                   13279:        PREFIX86(_pushf());
                   13280: #endif
1.1       root     13281:        
                   13282:        if(!(REG8(AL) < 26)) {
                   13283:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13284:                m_CF = 1;
1.1       root     13285:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13286:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13287:                m_CF = 1;
1.1       root     13288:        } else {
                   13289:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13290:                char dev[64];
                   13291:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13292:                
                   13293:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13294:                if(hFile == INVALID_HANDLE_VALUE) {
                   13295:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13296:                        m_CF = 1;
1.1       root     13297:                } else {
1.1.1.19  root     13298:                        UINT32 top_sector  = REG16(DX);
                   13299:                        UINT16 sector_num  = REG16(CX);
                   13300:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13301:                        
                   13302:                        if(sector_num == 0xffff) {
                   13303:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13304:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13305:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13306:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13307:                                buffer_addr = (seg << 4) + ofs;
                   13308:                        }
                   13309: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13310: //                             REG8(AL) = 0x02; // drive not ready
                   13311: //                             m_CF = 1;
                   13312: //                     } else 
                   13313:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13314:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13315:                                m_CF = 1;
1.1.1.19  root     13316:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13317:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13318:                                m_CF = 1;
1.1       root     13319:                        }
                   13320:                        CloseHandle(hFile);
                   13321:                }
                   13322:        }
                   13323: }
                   13324: 
                   13325: inline void msdos_int_26h()
                   13326: {
1.1.1.42  root     13327:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13328:        UINT16 seg, ofs;
                   13329:        DWORD dwSize;
                   13330:        
1.1.1.3   root     13331: #if defined(HAS_I386)
                   13332:        I386OP(pushf)();
                   13333: #else
                   13334:        PREFIX86(_pushf());
                   13335: #endif
1.1       root     13336:        
                   13337:        if(!(REG8(AL) < 26)) {
                   13338:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13339:                m_CF = 1;
1.1       root     13340:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13341:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13342:                m_CF = 1;
1.1       root     13343:        } else {
                   13344:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13345:                char dev[64];
                   13346:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13347:                
                   13348:                if(dpb->media_type == 0xf8) {
                   13349:                        // this drive is not a floppy
1.1.1.6   root     13350: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13351: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13352: //                     }
1.1       root     13353:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13354:                        m_CF = 1;
1.1       root     13355:                } else {
                   13356:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13357:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13358:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13359:                                m_CF = 1;
1.1       root     13360:                        } else {
1.1.1.19  root     13361:                                UINT32 top_sector  = REG16(DX);
                   13362:                                UINT16 sector_num  = REG16(CX);
                   13363:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13364:                                
                   13365:                                if(sector_num == 0xffff) {
                   13366:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13367:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13368:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13369:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13370:                                        buffer_addr = (seg << 4) + ofs;
                   13371:                                }
1.1       root     13372:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13373:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13374:                                        m_CF = 1;
1.1.1.19  root     13375:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13376:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13377:                                        m_CF = 1;
1.1.1.19  root     13378:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13379:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13380:                                        m_CF = 1;
1.1       root     13381:                                }
                   13382:                                CloseHandle(hFile);
                   13383:                        }
                   13384:                }
                   13385:        }
                   13386: }
                   13387: 
                   13388: inline void msdos_int_27h()
                   13389: {
1.1.1.29  root     13390:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13391:        try {
                   13392:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13393:        } catch(...) {
                   13394:                // recover the broken mcb
                   13395:                int mcb_seg = SREG(CS) - 1;
                   13396:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13397:                
1.1.1.29  root     13398:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13399:                        mcb->mz = 'M';
                   13400:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13401:                        
1.1.1.29  root     13402:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13403:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13404:                        } else {
1.1.1.39  root     13405:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13406:                        }
                   13407:                } else {
                   13408:                        mcb->mz = 'Z';
1.1.1.30  root     13409:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13410:                }
                   13411:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13412:        }
1.1.1.3   root     13413:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13414: }
                   13415: 
                   13416: inline void msdos_int_29h()
                   13417: {
1.1.1.50  root     13418:        msdos_putch_fast(REG8(AL));
1.1       root     13419: }
                   13420: 
                   13421: inline void msdos_int_2eh()
                   13422: {
                   13423:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13424:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13425:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13426:        char *token = my_strtok(tmp, " ");
                   13427:        strcpy(command, token);
                   13428:        strcpy(opt, token + strlen(token) + 1);
                   13429:        
                   13430:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13431:        param->env_seg = 0;
                   13432:        param->cmd_line.w.l = 44;
                   13433:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13434:        param->fcb1.w.l = 24;
                   13435:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13436:        param->fcb2.w.l = 24;
                   13437:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13438:        
                   13439:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13440:        
                   13441:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13442:        cmd_line->len = strlen(opt);
                   13443:        strcpy(cmd_line->cmd, opt);
                   13444:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13445:        
1.1.1.28  root     13446:        try {
                   13447:                if(msdos_process_exec(command, param, 0)) {
                   13448:                        REG16(AX) = 0xffff; // error before processing command
                   13449:                } else {
                   13450:                        // set flag to set retval to ax when the started process is terminated
                   13451:                        process_t *process = msdos_process_info_get(current_psp);
                   13452:                        process->called_by_int2eh = true;
                   13453:                }
                   13454:        } catch(...) {
                   13455:                REG16(AX) = 0xffff; // error before processing command
                   13456:        }
1.1       root     13457: }
                   13458: 
1.1.1.29  root     13459: inline void msdos_int_2fh_05h()
                   13460: {
                   13461:        switch(REG8(AL)) {
                   13462:        case 0x00:
1.1.1.49  root     13463:                // critical error handler is installed
1.1.1.32  root     13464:                REG8(AL) = 0xff;
                   13465:                break;
                   13466:        case 0x01:
                   13467:        case 0x02:
                   13468:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13469:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13470:                                const char *message = NULL;
                   13471:                                if(active_code_page == 932) {
                   13472:                                        message = standard_error_table[i].message_japanese;
                   13473:                                }
                   13474:                                if(message == NULL) {
                   13475:                                        message = standard_error_table[i].message_english;
                   13476:                                }
                   13477:                                strcpy((char *)(mem + WORK_TOP), message);
                   13478:                                
                   13479:                                SREG(ES) = WORK_TOP >> 4;
                   13480:                                i386_load_segment_descriptor(ES);
                   13481:                                REG16(DI) = 0x0000;
                   13482:                                REG8(AL) = 0x01;
                   13483:                                break;
                   13484:                        }
                   13485:                }
1.1.1.29  root     13486:                break;
                   13487:        default:
                   13488:                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     13489:                REG16(AX) = 0x01;
1.1.1.29  root     13490:                m_CF = 1;
                   13491:        }
                   13492: }
                   13493: 
1.1.1.44  root     13494: inline void msdos_int_2fh_06h()
                   13495: {
                   13496:        switch(REG8(AL)) {
                   13497:        case 0x00:
                   13498:                // ASSIGN is not installed
1.1.1.49  root     13499: //             REG8(AL) = 0x00;
1.1.1.44  root     13500:                break;
                   13501:        case 0x01:
                   13502:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13503:                REG16(AX) = 0x01;
                   13504:                m_CF = 1;
                   13505:                break;
                   13506:        default:
                   13507:                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));
                   13508:                REG16(AX) = 0x01;
                   13509:                m_CF = 1;
                   13510:                break;
                   13511:        }
                   13512: }
                   13513: 
1.1.1.22  root     13514: inline void msdos_int_2fh_11h()
                   13515: {
                   13516:        switch(REG8(AL)) {
                   13517:        case 0x00:
1.1.1.29  root     13518:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     13519: #ifdef SUPPORT_MSCDEX
                   13520:                        // MSCDEX is installed
                   13521:                        REG8(AL) = 0xff;
                   13522:                        i386_write_stack(0xadad);
                   13523: #else
1.1.1.29  root     13524:                        // MSCDEX is not installed
                   13525: //                     REG8(AL) = 0x00;
1.1.1.53  root     13526: #endif
1.1.1.29  root     13527:                } else {
                   13528:                        // Network Redirector is not installed
                   13529: //                     REG8(AL) = 0x00;
                   13530:                }
1.1.1.22  root     13531:                break;
                   13532:        default:
1.1.1.43  root     13533: //             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     13534:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13535:                m_CF = 1;
                   13536:                break;
                   13537:        }
                   13538: }
                   13539: 
1.1.1.21  root     13540: inline void msdos_int_2fh_12h()
                   13541: {
                   13542:        switch(REG8(AL)) {
1.1.1.22  root     13543:        case 0x00:
1.1.1.29  root     13544:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13545:                REG8(AL) = 0xff;
                   13546:                break;
1.1.1.29  root     13547: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13548:        case 0x02:
                   13549:                {
                   13550:                        UINT16 stack = i386_read_stack();
                   13551:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13552:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13553:                        i386_load_segment_descriptor(ES);
                   13554:                }
                   13555:                break;
1.1.1.30  root     13556:        case 0x03:
                   13557:                SREG(DS) = (DEVICE_TOP >> 4);
                   13558:                i386_load_segment_descriptor(DS);
                   13559:                break;
1.1.1.29  root     13560:        case 0x04:
                   13561:                {
                   13562:                        UINT16 stack = i386_read_stack();
                   13563:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13564: #if defined(HAS_I386)
                   13565:                        m_ZF = (REG8(AL) == '\\');
                   13566: #else
                   13567:                        m_ZeroVal = (REG8(AL) != '\\');
                   13568: #endif
                   13569:                }
                   13570:                break;
                   13571:        case 0x05:
1.1.1.49  root     13572:                {
                   13573:                        UINT16 c = i386_read_stack();
                   13574:                        if((c >> 0) & 0xff) {
                   13575:                                msdos_putch((c >> 0) & 0xff);
                   13576:                        }
                   13577:                        if((c >> 8) & 0xff) {
                   13578:                                msdos_putch((c >> 8) & 0xff);
                   13579:                        }
                   13580:                }
1.1.1.29  root     13581:                break;
1.1.1.49  root     13582: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     13583: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13584: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     13585: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     13586: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13587: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13588: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13589:        case 0x0d:
                   13590:                {
                   13591:                        SYSTEMTIME time;
                   13592:                        FILETIME file_time;
                   13593:                        WORD dos_date, dos_time;
                   13594:                        GetLocalTime(&time);
                   13595:                        SystemTimeToFileTime(&time, &file_time);
                   13596:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13597:                        REG16(AX) = dos_date;
                   13598:                        REG16(DX) = dos_time;
                   13599:                }
                   13600:                break;
                   13601: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13602: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13603: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13604:        case 0x11:
                   13605:                {
                   13606:                        char path[MAX_PATH], *p;
                   13607:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13608:                        my_strupr(path);
                   13609:                        while((p = my_strchr(path, '/')) != NULL) {
                   13610:                                *p = '\\';
                   13611:                        }
                   13612:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13613:                }
                   13614:                break;
                   13615:        case 0x12:
                   13616:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13617:                break;
                   13618:        case 0x13:
                   13619:                {
                   13620:                        char tmp[2] = {0};
                   13621:                        tmp[0] = i386_read_stack();
                   13622:                        my_strupr(tmp);
                   13623:                        REG8(AL) = tmp[0];
                   13624:                }
                   13625:                break;
                   13626:        case 0x14:
                   13627: #if defined(HAS_I386)
                   13628:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13629: #else
                   13630:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13631: #endif
                   13632:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13633:                break;
                   13634: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13635:        case 0x16:
                   13636:                if(REG16(BX) < 20) {
                   13637:                        SREG(ES) = SFT_TOP >> 4;
                   13638:                        i386_load_segment_descriptor(ES);
                   13639:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13640:                        
                   13641:                        // update system file table
                   13642:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13643:                        if(file_handler[REG16(BX)].valid) {
                   13644:                                int count = 0;
                   13645:                                for(int i = 0; i < 20; i++) {
                   13646:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13647:                                                count++;
                   13648:                                        }
                   13649:                                }
                   13650:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13651:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13652:                                _lseek(REG16(BX), 0, SEEK_END);
                   13653:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13654:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13655:                        } else {
                   13656:                                memset(sft, 0, 0x3b);
                   13657:                        }
                   13658:                } else {
                   13659:                        REG16(AX) = 0x06;
                   13660:                        m_CF = 1;
                   13661:                }
                   13662:                break;
1.1.1.49  root     13663:        case 0x17:
                   13664:                {
                   13665:                        UINT16 drive = i386_read_stack();
                   13666:                        if(msdos_is_valid_drive(drive)) {
                   13667:                                msdos_cds_update(drive);
                   13668:                        }
                   13669:                        REG16(SI) = 88 * drive;
                   13670:                        SREG(DS) = (CDS_TOP >> 4);
                   13671:                        i386_load_segment_descriptor(DS);
                   13672:                }
                   13673:                break;
1.1.1.29  root     13674: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13675: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13676:        case 0x1a:
                   13677:                {
                   13678:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13679:                        if(path[1] == ':') {
                   13680:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13681:                                        REG8(AL) = path[0] - 'a' + 1;
                   13682:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13683:                                        REG8(AL) = path[0] - 'A' + 1;
                   13684:                                } else {
                   13685:                                        REG8(AL) = 0xff; // invalid
                   13686:                                }
                   13687:                                strcpy(full, path);
                   13688:                                strcpy(path, full + 2);
                   13689:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13690:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13691:                                        REG8(AL) = full[0] - 'a' + 1;
                   13692:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13693:                                        REG8(AL) = full[0] - 'A' + 1;
                   13694:                                } else {
                   13695:                                        REG8(AL) = 0xff; // invalid
                   13696:                                }
                   13697:                        } else {
                   13698:                                REG8(AL) = 0x00; // default
                   13699:                        }
                   13700:                }
                   13701:                break;
                   13702:        case 0x1b:
                   13703:                {
                   13704:                        int year = REG16(CX) + 1980;
                   13705:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13706:                }
                   13707:                break;
                   13708: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13709: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13710:        case 0x1e:
                   13711:                {
                   13712:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13713:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13714:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13715: #if defined(HAS_I386)
                   13716:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13717: #else
                   13718:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13719: #endif
                   13720:                        } else {
                   13721: #if defined(HAS_I386)
                   13722:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13723: #else
                   13724:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13725: #endif
                   13726:                        }
                   13727:                }
                   13728:                break;
1.1.1.49  root     13729:        case 0x1f:
                   13730:                {
                   13731:                        UINT16 drive = i386_read_stack();
                   13732:                        if(msdos_is_valid_drive(drive)) {
                   13733:                                msdos_cds_update(drive);
                   13734:                        }
                   13735:                        REG16(SI) = 88 * drive;
                   13736:                        SREG(ES) = (CDS_TOP >> 4);
                   13737:                        i386_load_segment_descriptor(ES);
                   13738:                }
                   13739:                break;
1.1.1.21  root     13740:        case 0x20:
                   13741:                {
                   13742:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13743:                        
                   13744:                        if(fd < 20) {
                   13745:                                SREG(ES) = current_psp;
                   13746:                                i386_load_segment_descriptor(ES);
                   13747:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13748:                        } else {
                   13749:                                REG16(AX) = 0x06;
                   13750:                                m_CF = 1;
                   13751:                        }
                   13752:                }
                   13753:                break;
1.1.1.29  root     13754:        case 0x21:
                   13755:                msdos_int_21h_60h(0);
                   13756:                break;
1.1.1.49  root     13757:        case 0x22:
                   13758:                {
                   13759:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13760:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   13761:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   13762:                        }
                   13763:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   13764:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   13765:                        }
                   13766:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   13767:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   13768:                        }
                   13769:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   13770:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   13771:                        }
                   13772:                }
                   13773:                break;
1.1.1.29  root     13774: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13775: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13776:        case 0x25:
                   13777:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13778:                break;
                   13779:        case 0x26:
                   13780:                REG8(AL) = REG8(CL);
                   13781:                msdos_int_21h_3dh();
                   13782:                break;
                   13783:        case 0x27:
                   13784:                msdos_int_21h_3eh();
                   13785:                break;
                   13786:        case 0x28:
                   13787:                REG16(AX) = REG16(BP);
                   13788:                msdos_int_21h_42h();
                   13789:                break;
                   13790:        case 0x29:
                   13791:                msdos_int_21h_3fh();
                   13792:                break;
                   13793: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13794:        case 0x2b:
                   13795:                REG16(AX) = REG16(BP);
                   13796:                msdos_int_21h_44h();
                   13797:                break;
                   13798:        case 0x2c:
                   13799:                REG16(BX) = DEVICE_TOP >> 4;
                   13800:                REG16(AX) = 22;
                   13801:                break;
                   13802:        case 0x2d:
                   13803:                {
                   13804:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13805:                        REG16(AX) = sda->extended_error_code;
                   13806:                }
                   13807:                break;
                   13808:        case 0x2e:
                   13809:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13810:                        SREG(ES) = 0x0001;
                   13811:                        i386_load_segment_descriptor(ES);
                   13812:                        REG16(DI) = 0x00;
                   13813:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     13814:                        // dummy parameter error message read routine is at fffc:0010
                   13815:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     13816:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13817:                        REG16(DI) = 0x0010;
1.1.1.22  root     13818:                }
                   13819:                break;
1.1.1.29  root     13820:        case 0x2f:
                   13821:                if(REG16(DX) != 0) {
1.1.1.30  root     13822:                        dos_major_version = REG8(DL);
                   13823:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13824:                } else {
                   13825:                        REG8(DL) = 7;
                   13826:                        REG8(DH) = 10;
                   13827:                }
                   13828:                break;
                   13829: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13830: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13831:        default:
                   13832:                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));
                   13833:                REG16(AX) = 0x01;
                   13834:                m_CF = 1;
                   13835:                break;
                   13836:        }
                   13837: }
                   13838: 
1.1.1.30  root     13839: inline void msdos_int_2fh_13h()
                   13840: {
                   13841:        static UINT16 prevDS = 0, prevDX = 0;
                   13842:        static UINT16 prevES = 0, prevBX = 0;
                   13843:        UINT16 tmp;
                   13844:        
                   13845:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   13846:        i386_load_segment_descriptor(DS);
                   13847:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   13848:        
                   13849:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   13850:        i386_load_segment_descriptor(ES);
                   13851:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   13852: }
                   13853: 
1.1.1.22  root     13854: inline void msdos_int_2fh_14h()
                   13855: {
                   13856:        switch(REG8(AL)) {
                   13857:        case 0x00:
1.1.1.29  root     13858:                // NLSFUNC.COM is installed
                   13859:                REG8(AL) = 0xff;
1.1.1.25  root     13860:                break;
                   13861:        case 0x01:
                   13862:        case 0x03:
                   13863:                REG8(AL) = 0x00;
                   13864:                active_code_page = REG16(BX);
                   13865:                msdos_nls_tables_update();
                   13866:                break;
                   13867:        case 0x02:
                   13868:                REG8(AL) = get_extended_country_info(REG16(BP));
                   13869:                break;
                   13870:        case 0x04:
1.1.1.42  root     13871:                for(int i = 0;; i++) {
                   13872:                        if(country_table[i].code == REG16(DX)) {
                   13873:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   13874:                                break;
                   13875:                        } else if(country_table[i].code == -1) {
                   13876:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13877:                                break;
                   13878:                        }
                   13879:                }
1.1.1.25  root     13880:                REG8(AL) = 0x00;
1.1.1.22  root     13881:                break;
                   13882:        default:
                   13883:                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));
                   13884:                REG16(AX) = 0x01;
                   13885:                m_CF = 1;
                   13886:                break;
                   13887:        }
                   13888: }
                   13889: 
                   13890: inline void msdos_int_2fh_15h()
                   13891: {
                   13892:        switch(REG8(AL)) {
1.1.1.29  root     13893:        case 0x00: // CD-ROM - Installation Check
                   13894:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     13895: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     13896:                        // MSCDEX is installed
                   13897:                        REG16(BX) = 0;
                   13898:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     13899:                                if(msdos_is_cdrom_drive(i)) {
                   13900:                                        if(REG16(BX) == 0) {
                   13901:                                                REG16(CX) = i;
1.1.1.43  root     13902:                                        }
1.1.1.44  root     13903:                                        REG16(BX)++;
1.1.1.43  root     13904:                                }
                   13905:                        }
                   13906: #else
1.1.1.29  root     13907:                        // MSCDEX is not installed
                   13908: //                     REG8(AL) = 0x00;
1.1.1.43  root     13909: #endif
1.1.1.29  root     13910:                } else {
                   13911:                        // GRAPHICS.COM is not installed
                   13912: //                     REG8(AL) = 0x00;
                   13913:                }
1.1.1.22  root     13914:                break;
1.1.1.43  root     13915:        case 0x0b:
1.1.1.44  root     13916:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   13917:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   13918:                REG16(BX) = 0xadad;
1.1.1.43  root     13919:                break;
                   13920:        case 0x0d:
1.1.1.44  root     13921:                for(int i = 0, n = 0; i < 26; i++) {
                   13922:                        if(msdos_is_cdrom_drive(i)) {
                   13923:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     13924:                        }
                   13925:                }
                   13926:                break;
1.1.1.22  root     13927:        case 0xff:
1.1.1.29  root     13928:                if(REG16(BX) == 0x0000) {
                   13929:                        // CORELCDX is not installed
                   13930:                } else {
                   13931:                        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));
                   13932:                        REG16(AX) = 0x01;
                   13933:                        m_CF = 1;
                   13934:                }
1.1.1.22  root     13935:                break;
1.1.1.21  root     13936:        default:
1.1.1.22  root     13937:                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     13938:                REG16(AX) = 0x01;
                   13939:                m_CF = 1;
                   13940:                break;
                   13941:        }
                   13942: }
                   13943: 
1.1       root     13944: inline void msdos_int_2fh_16h()
                   13945: {
                   13946:        switch(REG8(AL)) {
                   13947:        case 0x00:
1.1.1.14  root     13948:                if(no_windows) {
1.1.1.29  root     13949:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   13950: //                     REG8(AL) = 0x00;
1.1.1.14  root     13951:                } else {
1.1.1.30  root     13952:                        REG8(AL) = win_major_version;
                   13953:                        REG8(AH) = win_minor_version;
1.1       root     13954:                }
                   13955:                break;
1.1.1.43  root     13956:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     13957:                // from DOSBox
                   13958:                i386_set_a20_line(1);
                   13959:                break;
1.1.1.49  root     13960:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     13961:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   13962:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   13963:                break;
                   13964:        case 0x07:
                   13965:                // Virtual Device Call API
                   13966:                break;
1.1.1.22  root     13967:        case 0x0a:
                   13968:                if(!no_windows) {
                   13969:                        REG16(AX) = 0x0000;
1.1.1.30  root     13970:                        REG8(BH) = win_major_version;
                   13971:                        REG8(BL) = win_minor_version;
1.1.1.49  root     13972: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     13973:                        REG16(CX) = 0x0003; // enhanced
                   13974:                }
                   13975:                break;
1.1.1.30  root     13976:        case 0x0b:
                   13977:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     13978:        case 0x0e:
                   13979:        case 0x0f:
1.1.1.30  root     13980:        case 0x10:
1.1.1.22  root     13981:        case 0x11:
                   13982:        case 0x12:
                   13983:        case 0x13:
                   13984:        case 0x14:
1.1.1.30  root     13985:        case 0x15:
1.1.1.43  root     13986:        case 0x81:
                   13987:        case 0x82:
1.1.1.44  root     13988:        case 0x84:
1.1.1.49  root     13989:        case 0x85:
1.1.1.33  root     13990:        case 0x86:
1.1.1.22  root     13991:        case 0x87:
1.1.1.30  root     13992:        case 0x89:
1.1.1.33  root     13993:        case 0x8a:
1.1.1.22  root     13994:                // function not supported, do not clear AX
                   13995:                break;
1.1.1.14  root     13996:        case 0x80:
                   13997:                Sleep(10);
1.1.1.35  root     13998:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     13999:                REG8(AL) = 0x00;
1.1.1.14  root     14000:                break;
1.1.1.33  root     14001:        case 0x83:
                   14002:                REG16(BX) = 0x01; // system vm id
                   14003:                break;
1.1.1.22  root     14004:        case 0x8e:
                   14005:                REG16(AX) = 0x00; // failed
                   14006:                break;
1.1.1.20  root     14007:        case 0x8f:
                   14008:                switch(REG8(DH)) {
                   14009:                case 0x01:
1.1.1.49  root     14010: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14011: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14012:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14013:                        break;
                   14014:                default:
                   14015:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14016:                        break;
                   14017:                }
                   14018:                break;
1.1       root     14019:        default:
1.1.1.22  root     14020:                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));
                   14021:                REG16(AX) = 0x01;
                   14022:                m_CF = 1;
                   14023:                break;
                   14024:        }
                   14025: }
                   14026: 
                   14027: inline void msdos_int_2fh_19h()
                   14028: {
                   14029:        switch(REG8(AL)) {
                   14030:        case 0x00:
1.1.1.29  root     14031:                // SHELLB.COM is not installed
                   14032: //             REG8(AL) = 0x00;
1.1.1.22  root     14033:                break;
                   14034:        case 0x01:
                   14035:        case 0x02:
                   14036:        case 0x03:
                   14037:        case 0x04:
                   14038:                REG16(AX) = 0x01;
                   14039:                m_CF = 1;
                   14040:                break;
1.1.1.29  root     14041:        case 0x80:
                   14042:                // IBM ROM-DOS v4.0 is not installed
                   14043: //             REG8(AL) = 0x00;
                   14044:                break;
1.1.1.22  root     14045:        default:
                   14046:                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     14047:                REG16(AX) = 0x01;
1.1.1.3   root     14048:                m_CF = 1;
1.1       root     14049:                break;
                   14050:        }
                   14051: }
                   14052: 
                   14053: inline void msdos_int_2fh_1ah()
                   14054: {
                   14055:        switch(REG8(AL)) {
                   14056:        case 0x00:
1.1.1.29  root     14057:                // ANSI.SYS is installed
1.1       root     14058:                REG8(AL) = 0xff;
                   14059:                break;
1.1.1.49  root     14060:        case 0x01:
1.1.1.50  root     14061:                if(REG8(CL) == 0x5f) {
                   14062:                        // set display information
                   14063:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14064:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14065:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14066:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14067:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14068:                                
                   14069:                                if(cur_width != new_width || cur_height != new_height) {
                   14070:                                        pcbios_set_console_size(new_width, new_height, true);
                   14071:                                }
                   14072:                        }
                   14073:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14074:                        // get display information
1.1.1.50  root     14075:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14076:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14077:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14078:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14079:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14080:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14081:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14082:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14083:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14084:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14085:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14086:                } else {
                   14087:                        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));
                   14088:                        REG16(AX) = 0x01;
                   14089:                        m_CF = 1;
                   14090:                }
                   14091:                break;
1.1       root     14092:        default:
1.1.1.22  root     14093:                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));
                   14094:                REG16(AX) = 0x01;
                   14095:                m_CF = 1;
                   14096:                break;
                   14097:        }
                   14098: }
                   14099: 
1.1.1.30  root     14100: inline void msdos_int_2fh_40h()
1.1.1.22  root     14101: {
                   14102:        switch(REG8(AL)) {
                   14103:        case 0x00:
1.1.1.30  root     14104:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14105:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14106:                break;
1.1.1.43  root     14107:        case 0x10:
                   14108:                // OS/2 v2.0+ - Installation Check
                   14109:                REG16(AX) = 0x01;
                   14110:                m_CF = 1;
                   14111:                break;
1.1.1.22  root     14112:        default:
                   14113:                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     14114:                REG16(AX) = 0x01;
1.1.1.3   root     14115:                m_CF = 1;
1.1       root     14116:                break;
                   14117:        }
                   14118: }
                   14119: 
                   14120: inline void msdos_int_2fh_43h()
                   14121: {
                   14122:        switch(REG8(AL)) {
                   14123:        case 0x00:
1.1.1.29  root     14124:                // XMS is installed ?
1.1.1.19  root     14125: #ifdef SUPPORT_XMS
                   14126:                if(support_xms) {
                   14127:                        REG8(AL) = 0x80;
1.1.1.44  root     14128:                }
                   14129: #endif
                   14130:                break;
                   14131:        case 0x08:
                   14132: #ifdef SUPPORT_XMS
                   14133:                if(support_xms) {
                   14134:                        REG8(AL) = 0x43;
                   14135:                        REG8(BL) = 0x01; // IBM PC/AT
                   14136:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14137:                }
1.1.1.19  root     14138: #endif
                   14139:                break;
                   14140:        case 0x10:
                   14141:                SREG(ES) = XMS_TOP >> 4;
                   14142:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14143:                REG16(BX) = 0x15;
1.1       root     14144:                break;
1.1.1.44  root     14145:        case 0xe0:
                   14146:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14147:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14148:                        break;
                   14149:                }
1.1       root     14150:        default:
1.1.1.22  root     14151:                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));
                   14152:                REG16(AX) = 0x01;
                   14153:                m_CF = 1;
                   14154:                break;
                   14155:        }
                   14156: }
                   14157: 
                   14158: inline void msdos_int_2fh_46h()
                   14159: {
                   14160:        switch(REG8(AL)) {
                   14161:        case 0x80:
1.1.1.29  root     14162:                // Windows v3.0 is not installed
                   14163: //             REG8(AL) = 0x00;
1.1.1.22  root     14164:                break;
                   14165:        default:
                   14166:                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));
                   14167:                REG16(AX) = 0x01;
                   14168:                m_CF = 1;
                   14169:                break;
                   14170:        }
                   14171: }
                   14172: 
                   14173: inline void msdos_int_2fh_48h()
                   14174: {
                   14175:        switch(REG8(AL)) {
                   14176:        case 0x00:
1.1.1.29  root     14177:                // DOSKEY is not installed
                   14178: //             REG8(AL) = 0x00;
1.1.1.22  root     14179:                break;
                   14180:        case 0x10:
                   14181:                msdos_int_21h_0ah();
                   14182:                REG16(AX) = 0x00;
                   14183:                break;
                   14184:        default:
                   14185:                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     14186:                REG16(AX) = 0x01;
1.1.1.3   root     14187:                m_CF = 1;
1.1       root     14188:                break;
                   14189:        }
                   14190: }
                   14191: 
                   14192: inline void msdos_int_2fh_4ah()
                   14193: {
                   14194:        switch(REG8(AL)) {
1.1.1.29  root     14195: #ifdef SUPPORT_HMA
                   14196:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14197:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14198:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14199:                                // restore first free mcb in high memory area
                   14200:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14201:                        }
                   14202:                        int offset = 0xffff;
                   14203:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14204:                                REG16(DI) = offset + 0x10;
                   14205:                        } else {
                   14206:                                REG16(DI) = 0xffff;
                   14207:                        }
                   14208:                } else {
                   14209:                        // HMA is already used
                   14210:                        REG16(BX) = 0;
                   14211:                        REG16(DI) = 0xffff;
                   14212:                }
                   14213:                SREG(ES) = 0xffff;
                   14214:                i386_load_segment_descriptor(ES);
                   14215:                break;
                   14216:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14217:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14218:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14219:                                // restore first free mcb in high memory area
                   14220:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14221:                        }
                   14222:                        int size = REG16(BX), offset;
                   14223:                        if((size % 16) != 0) {
                   14224:                                size &= ~15;
                   14225:                                size += 16;
                   14226:                        }
                   14227:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14228:                                REG16(BX) = size;
                   14229:                                REG16(DI) = offset + 0x10;
                   14230:                                is_hma_used_by_int_2fh = true;
                   14231:                        } else {
                   14232:                                REG16(BX) = 0;
                   14233:                                REG16(DI) = 0xffff;
                   14234:                        }
                   14235:                } else {
                   14236:                        // HMA is already used
                   14237:                        REG16(BX) = 0;
                   14238:                        REG16(DI) = 0xffff;
                   14239:                }
                   14240:                SREG(ES) = 0xffff;
                   14241:                i386_load_segment_descriptor(ES);
                   14242:                break;
                   14243:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14244:                if(REG8(DL) == 0x00) {
                   14245:                        if(!is_hma_used_by_xms) {
                   14246:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14247:                                        // restore first free mcb in high memory area
                   14248:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14249:                                        is_hma_used_by_int_2fh = false;
                   14250:                                }
                   14251:                                int size = REG16(BX), offset;
                   14252:                                if((size % 16) != 0) {
                   14253:                                        size &= ~15;
                   14254:                                        size += 16;
                   14255:                                }
                   14256:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14257: //                                     REG16(BX) = size;
                   14258:                                        SREG(ES) = 0xffff;
                   14259:                                        i386_load_segment_descriptor(ES);
                   14260:                                        REG16(DI) = offset + 0x10;
                   14261:                                        is_hma_used_by_int_2fh = true;
                   14262:                                } else {
                   14263:                                        REG16(DI) = 0xffff;
                   14264:                                }
                   14265:                        } else {
                   14266:                                REG16(DI) = 0xffff;
                   14267:                        }
                   14268:                } else if(REG8(DL) == 0x01) {
                   14269:                        if(!is_hma_used_by_xms) {
                   14270:                                int size = REG16(BX);
                   14271:                                if((size % 16) != 0) {
                   14272:                                        size &= ~15;
                   14273:                                        size += 16;
                   14274:                                }
                   14275:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14276:                                        // memory block address is not changed
                   14277:                                } else {
                   14278:                                        REG16(DI) = 0xffff;
                   14279:                                }
                   14280:                        } else {
                   14281:                                REG16(DI) = 0xffff;
                   14282:                        }
                   14283:                } else if(REG8(DL) == 0x02) {
                   14284:                        if(!is_hma_used_by_xms) {
                   14285:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14286:                                        // restore first free mcb in high memory area
                   14287:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14288:                                        is_hma_used_by_int_2fh = false;
                   14289:                                } else {
                   14290:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14291:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14292:                                                is_hma_used_by_int_2fh = false;
                   14293:                                        }
                   14294:                                }
                   14295:                        }
                   14296:                } else {
                   14297:                        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));
                   14298:                        REG16(AX) = 0x01;
                   14299:                        m_CF = 1;
                   14300:                }
                   14301:                break;
                   14302:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14303:                if(!is_hma_used_by_xms) {
                   14304:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14305:                                // restore first free mcb in high memory area
                   14306:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14307:                                is_hma_used_by_int_2fh = false;
                   14308:                        }
                   14309:                        REG16(AX) = 0x0000;
                   14310:                        SREG(ES) = 0xffff;
                   14311:                        i386_load_segment_descriptor(ES);
                   14312:                        REG16(DI) = 0x10;
                   14313:                }
                   14314:                break;
                   14315: #else
1.1       root     14316:        case 0x01:
                   14317:        case 0x02:
1.1.1.29  root     14318:                // HMA is already used
1.1.1.27  root     14319:                REG16(BX) = 0x0000;
1.1.1.3   root     14320:                SREG(ES) = 0xffff;
                   14321:                i386_load_segment_descriptor(ES);
1.1       root     14322:                REG16(DI) = 0xffff;
                   14323:                break;
1.1.1.19  root     14324:        case 0x03:
                   14325:                // unable to allocate
                   14326:                REG16(DI) = 0xffff;
                   14327:                break;
                   14328:        case 0x04:
                   14329:                // function not supported, do not clear AX
                   14330:                break;
1.1.1.29  root     14331: #endif
                   14332:        case 0x10:
1.1.1.42  root     14333:                switch(REG16(BX)) {
                   14334:                case 0x0000:
                   14335:                case 0x0001:
                   14336:                case 0x0002:
                   14337:                case 0x0003:
                   14338:                case 0x0004:
                   14339:                case 0x0005:
                   14340:                case 0x0006:
                   14341:                case 0x0007:
                   14342:                case 0x0008:
                   14343:                case 0x000a:
                   14344:                case 0x1234:
                   14345:                        // SMARTDRV v4.00+ is not installed
                   14346:                        break;
                   14347:                default:
1.1.1.29  root     14348:                        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));
                   14349:                        REG16(AX) = 0x01;
                   14350:                        m_CF = 1;
1.1.1.42  root     14351:                        break;
1.1.1.29  root     14352:                }
                   14353:                break;
                   14354:        case 0x11:
1.1.1.42  root     14355:                switch(REG16(BX)) {
                   14356:                case 0x0000:
                   14357:                case 0x0001:
                   14358:                case 0x0002:
                   14359:                case 0x0003:
                   14360:                case 0x0004:
                   14361:                case 0x0005:
                   14362:                case 0x0006:
                   14363:                case 0x0007:
                   14364:                case 0x0008:
                   14365:                case 0x0009:
                   14366:                case 0x000a:
                   14367:                case 0x000b:
                   14368:                case 0xfffe:
                   14369:                case 0xffff:
1.1.1.29  root     14370:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14371:                        break;
                   14372:                default:
                   14373:                        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));
                   14374:                        REG16(AX) = 0x01;
                   14375:                        m_CF = 1;
                   14376:                        break;
                   14377:                }
                   14378:                break;
                   14379:        case 0x12:
                   14380:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14381:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14382:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14383:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14384:                } else {
                   14385:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14386:                        REG16(AX) = 0x01;
                   14387:                        m_CF = 1;
                   14388:                }
1.1.1.22  root     14389:                break;
1.1.1.42  root     14390:        case 0x13:
                   14391:                // DBLSPACE.BIN is not installed
                   14392:                break;
1.1.1.22  root     14393:        default:
                   14394:                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));
                   14395:                REG16(AX) = 0x01;
                   14396:                m_CF = 1;
                   14397:                break;
                   14398:        }
                   14399: }
                   14400: 
                   14401: inline void msdos_int_2fh_4bh()
                   14402: {
                   14403:        switch(REG8(AL)) {
1.1.1.24  root     14404:        case 0x01:
1.1.1.22  root     14405:        case 0x02:
1.1.1.29  root     14406:                // Task Switcher is not installed
1.1.1.24  root     14407:                break;
                   14408:        case 0x03:
                   14409:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14410:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14411:                break;
1.1.1.30  root     14412:        case 0x04:
                   14413:                REG16(BX) = 0x0000; // free switcher id successfully
                   14414:                break;
1.1.1.43  root     14415:        case 0x05:
                   14416:                REG16(BX) = 0x0000; // no instance data chain
                   14417:                SREG(ES) = 0x0000;
                   14418:                i386_load_segment_descriptor(ES);
                   14419:                break;
1.1       root     14420:        default:
1.1.1.22  root     14421:                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     14422:                REG16(AX) = 0x01;
1.1.1.3   root     14423:                m_CF = 1;
1.1       root     14424:                break;
                   14425:        }
                   14426: }
                   14427: 
1.1.1.44  root     14428: inline void msdos_int_2fh_4dh()
                   14429: {
                   14430:        switch(REG8(AL)) {
                   14431:        case 0x00:
                   14432:                // KKCFUNC is not installed ???
                   14433:                break;
                   14434:        default:
                   14435: //             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));
                   14436:                REG16(AX) = 0x01; // invalid function
                   14437:                m_CF = 1;
                   14438:                break;
                   14439:        }
                   14440: }
                   14441: 
1.1       root     14442: inline void msdos_int_2fh_4fh()
                   14443: {
                   14444:        switch(REG8(AL)) {
                   14445:        case 0x00:
1.1.1.29  root     14446:                // BILING is installed
1.1.1.27  root     14447:                REG16(AX) = 0x0000;
                   14448:                REG8(DL) = 0x01;        // major version
                   14449:                REG8(DH) = 0x00;        // minor version
1.1       root     14450:                break;
                   14451:        case 0x01:
1.1.1.27  root     14452:                REG16(AX) = 0x0000;
1.1       root     14453:                REG16(BX) = active_code_page;
                   14454:                break;
                   14455:        default:
1.1.1.22  root     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:                break;
                   14460:        }
                   14461: }
                   14462: 
                   14463: inline void msdos_int_2fh_55h()
                   14464: {
                   14465:        switch(REG8(AL)) {
                   14466:        case 0x00:
                   14467:        case 0x01:
                   14468: //             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));
                   14469:                break;
                   14470:        default:
                   14471:                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     14472:                REG16(AX) = 0x01;
1.1.1.3   root     14473:                m_CF = 1;
1.1       root     14474:                break;
                   14475:        }
                   14476: }
                   14477: 
1.1.1.44  root     14478: inline void msdos_int_2fh_56h()
                   14479: {
                   14480:        switch(REG8(AL)) {
                   14481:        case 0x00:
                   14482:                // INTERLNK is not installed
                   14483:                break;
                   14484:        case 0x01:
                   14485:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14486: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14487: //                     REG8(AL) = 0x00;
                   14488: //             }
                   14489:                break;
                   14490:        default:
                   14491:                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));
                   14492:                REG16(AX) = 0x01;
                   14493:                m_CF = 1;
                   14494:                break;
                   14495:        }
                   14496: }
                   14497: 
1.1.1.24  root     14498: inline void msdos_int_2fh_adh()
                   14499: {
                   14500:        switch(REG8(AL)) {
                   14501:        case 0x00:
1.1.1.29  root     14502:                // DISPLAY.SYS is installed
1.1.1.24  root     14503:                REG8(AL) = 0xff;
                   14504:                REG16(BX) = 0x100; // ???
                   14505:                break;
                   14506:        case 0x01:
                   14507:                active_code_page = REG16(BX);
                   14508:                msdos_nls_tables_update();
                   14509:                REG16(AX) = 0x01;
                   14510:                break;
                   14511:        case 0x02:
                   14512:                REG16(BX) = active_code_page;
                   14513:                break;
                   14514:        case 0x03:
                   14515:                // FIXME
                   14516:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14517:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14518:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14519:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14520:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14521:                break;
                   14522:        case 0x80:
1.1.1.49  root     14523:                // KEYB.COM is not installed
                   14524:                break;
1.1.1.24  root     14525:        default:
                   14526:                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));
                   14527:                REG16(AX) = 0x01;
                   14528:                m_CF = 1;
                   14529:                break;
                   14530:        }
                   14531: }
                   14532: 
1.1       root     14533: inline void msdos_int_2fh_aeh()
                   14534: {
                   14535:        switch(REG8(AL)) {
                   14536:        case 0x00:
1.1.1.28  root     14537:                // FIXME: we need to check the given command line
                   14538:                REG8(AL) = 0x00; // the command should be executed as usual
                   14539: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14540:                break;
                   14541:        case 0x01:
                   14542:                {
                   14543:                        char command[MAX_PATH];
                   14544:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14545:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14546:                        
                   14547:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14548:                        param->env_seg = 0;
                   14549:                        param->cmd_line.w.l = 44;
                   14550:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14551:                        param->fcb1.w.l = 24;
                   14552:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14553:                        param->fcb2.w.l = 24;
                   14554:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14555:                        
                   14556:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14557:                        
                   14558:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14559:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14560:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14561:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14562:                        
1.1.1.28  root     14563:                        try {
                   14564:                                msdos_process_exec(command, param, 0);
                   14565:                        } catch(...) {
                   14566:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14567:                        }
                   14568:                }
                   14569:                break;
                   14570:        default:
1.1.1.22  root     14571:                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     14572:                REG16(AX) = 0x01;
1.1.1.3   root     14573:                m_CF = 1;
1.1       root     14574:                break;
                   14575:        }
                   14576: }
                   14577: 
1.1.1.34  root     14578: inline void msdos_int_2fh_b7h()
                   14579: {
                   14580:        switch(REG8(AL)) {
                   14581:        case 0x00:
                   14582:                // APPEND is not installed
                   14583: //             REG8(AL) = 0x00;
                   14584:                break;
1.1.1.44  root     14585:        case 0x06:
                   14586:                REG16(BX) = 0x0000;
                   14587:                break;
1.1.1.34  root     14588:        case 0x07:
1.1.1.43  root     14589:        case 0x11:
1.1.1.34  root     14590:                // COMMAND.COM calls this service without checking APPEND is installed
                   14591:                break;
                   14592:        default:
                   14593:                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));
                   14594:                REG16(AX) = 0x01;
                   14595:                m_CF = 1;
                   14596:                break;
                   14597:        }
                   14598: }
                   14599: 
1.1.1.24  root     14600: inline void msdos_int_33h_0000h()
                   14601: {
                   14602:        REG16(AX) = 0xffff; // hardware/driver installed
                   14603:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14604: }
                   14605: 
                   14606: inline void msdos_int_33h_0001h()
                   14607: {
1.1.1.34  root     14608:        if(mouse.hidden > 0) {
                   14609:                mouse.hidden--;
                   14610:        }
                   14611:        if(mouse.hidden == 0) {
1.1.1.24  root     14612:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14613:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14614:                }
                   14615:                pic[1].imr &= ~0x10;    // enable irq12
                   14616:        }
                   14617: }
                   14618: 
                   14619: inline void msdos_int_33h_0002h()
                   14620: {
1.1.1.34  root     14621:        mouse.hidden++;
                   14622: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   14623:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     14624: }
                   14625: 
                   14626: inline void msdos_int_33h_0003h()
                   14627: {
1.1.1.34  root     14628: //     if(mouse.hidden > 0) {
                   14629:                update_console_input();
                   14630: //     }
1.1.1.24  root     14631:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14632:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14633:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14634: }
                   14635: 
                   14636: inline void msdos_int_33h_0004h()
                   14637: {
                   14638:        mouse.position.x = REG16(CX);
                   14639:        mouse.position.x = REG16(DX);
1.1.1.24  root     14640: }
                   14641: 
                   14642: inline void msdos_int_33h_0005h()
                   14643: {
1.1.1.34  root     14644: //     if(mouse.hidden > 0) {
                   14645:                update_console_input();
                   14646: //     }
1.1.1.24  root     14647:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14648:                int idx = REG16(BX);
1.1.1.34  root     14649:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14650:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14651:                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     14652:                mouse.buttons[idx].pressed_times = 0;
                   14653:        } else {
                   14654:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14655:        }
                   14656:        REG16(AX) = mouse.get_buttons();
                   14657: }
                   14658: 
                   14659: inline void msdos_int_33h_0006h()
                   14660: {
1.1.1.34  root     14661: //     if(mouse.hidden > 0) {
                   14662:                update_console_input();
                   14663: //     }
1.1.1.24  root     14664:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14665:                int idx = REG16(BX);
1.1.1.34  root     14666:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14667:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14668:                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     14669:                mouse.buttons[idx].released_times = 0;
                   14670:        } else {
                   14671:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14672:        }
                   14673:        REG16(AX) = mouse.get_buttons();
                   14674: }
                   14675: 
                   14676: inline void msdos_int_33h_0007h()
                   14677: {
                   14678:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14679:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14680: }
                   14681: 
                   14682: inline void msdos_int_33h_0008h()
                   14683: {
                   14684:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14685:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14686: }
                   14687: 
                   14688: inline void msdos_int_33h_0009h()
                   14689: {
                   14690:        mouse.hot_spot[0] = REG16(BX);
                   14691:        mouse.hot_spot[1] = REG16(CX);
                   14692: }
                   14693: 
1.1.1.49  root     14694: inline void msdos_int_33h_000ah()
                   14695: {
                   14696:        mouse.screen_mask = REG16(CX);
                   14697:        mouse.cursor_mask = REG16(DX);
                   14698: }
                   14699: 
1.1.1.24  root     14700: inline void msdos_int_33h_000bh()
                   14701: {
1.1.1.34  root     14702: //     if(mouse.hidden > 0) {
                   14703:                update_console_input();
                   14704: //     }
1.1.1.24  root     14705:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14706:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14707:        mouse.prev_position.x = mouse.position.x;
                   14708:        mouse.prev_position.y = mouse.position.y;
                   14709:        REG16(CX) = dx;
                   14710:        REG16(DX) = dy;
                   14711: }
                   14712: 
                   14713: inline void msdos_int_33h_000ch()
                   14714: {
                   14715:        mouse.call_mask = REG16(CX);
                   14716:        mouse.call_addr.w.l = REG16(DX);
                   14717:        mouse.call_addr.w.h = SREG(ES);
                   14718: }
                   14719: 
                   14720: inline void msdos_int_33h_000fh()
                   14721: {
                   14722:        mouse.mickey.x = REG16(CX);
                   14723:        mouse.mickey.y = REG16(DX);
                   14724: }
                   14725: 
                   14726: inline void msdos_int_33h_0011h()
                   14727: {
                   14728:        REG16(AX) = 0xffff;
                   14729:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14730: }
                   14731: 
                   14732: inline void msdos_int_33h_0014h()
                   14733: {
                   14734:        UINT16 old_mask = mouse.call_mask;
                   14735:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14736:        UINT16 old_seg = mouse.call_addr.w.h;
                   14737:        
                   14738:        mouse.call_mask = REG16(CX);
                   14739:        mouse.call_addr.w.l = REG16(DX);
                   14740:        mouse.call_addr.w.h = SREG(ES);
                   14741:        
                   14742:        REG16(CX) = old_mask;
                   14743:        REG16(DX) = old_ofs;
                   14744:        SREG(ES) = old_seg;
                   14745:        i386_load_segment_descriptor(ES);
                   14746: }
                   14747: 
                   14748: inline void msdos_int_33h_0015h()
                   14749: {
                   14750:        REG16(BX) = sizeof(mouse);
                   14751: }
                   14752: 
                   14753: inline void msdos_int_33h_0016h()
                   14754: {
                   14755:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14756: }
                   14757: 
                   14758: inline void msdos_int_33h_0017h()
                   14759: {
                   14760:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14761: }
                   14762: 
1.1.1.43  root     14763: inline void msdos_int_33h_0018h()
                   14764: {
                   14765:        for(int i = 0; i < 8; i++) {
                   14766:                if(REG16(CX) & (1 << i)) {
                   14767:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14768:                                // event handler already exists
                   14769:                                REG16(AX) = 0xffff;
                   14770:                                break;
                   14771:                        }
                   14772:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14773:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14774:                }
                   14775:        }
                   14776: }
                   14777: 
                   14778: inline void msdos_int_33h_0019h()
                   14779: {
                   14780:        UINT16 call_mask = REG16(CX);
                   14781:        
                   14782:        REG16(CX) = 0;
                   14783:        
                   14784:        for(int i = 0; i < 8; i++) {
                   14785:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14786:                        for(int j = 0; j < 8; j++) {
                   14787:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14788:                                        REG16(CX) |= (1 << j);
                   14789:                                }
                   14790:                        }
                   14791:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14792:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14793:                        break;
                   14794:                }
                   14795:        }
                   14796: }
                   14797: 
1.1.1.24  root     14798: inline void msdos_int_33h_001ah()
                   14799: {
                   14800:        mouse.sensitivity[0] = REG16(BX);
                   14801:        mouse.sensitivity[1] = REG16(CX);
                   14802:        mouse.sensitivity[2] = REG16(DX);
                   14803: }
                   14804: 
                   14805: inline void msdos_int_33h_001bh()
                   14806: {
                   14807:        REG16(BX) = mouse.sensitivity[0];
                   14808:        REG16(CX) = mouse.sensitivity[1];
                   14809:        REG16(DX) = mouse.sensitivity[2];
                   14810: }
                   14811: 
                   14812: inline void msdos_int_33h_001dh()
                   14813: {
                   14814:        mouse.display_page = REG16(BX);
                   14815: }
                   14816: 
                   14817: inline void msdos_int_33h_001eh()
                   14818: {
                   14819:        REG16(BX) = mouse.display_page;
                   14820: }
                   14821: 
1.1.1.34  root     14822: inline void msdos_int_33h_001fh()
                   14823: {
                   14824:        // from DOSBox
                   14825:        REG16(BX) = 0x0000;
                   14826:        SREG(ES) = 0x0000;
                   14827:        i386_load_segment_descriptor(ES);
                   14828:        mouse.enabled = false;
                   14829:        mouse.old_hidden = mouse.hidden;
                   14830:        mouse.hidden = 1;
                   14831: }
                   14832: 
                   14833: inline void msdos_int_33h_0020h()
                   14834: {
                   14835:        // from DOSBox
                   14836:        mouse.enabled = true;
                   14837:        mouse.hidden = mouse.old_hidden;
                   14838: }
                   14839: 
1.1.1.24  root     14840: inline void msdos_int_33h_0021h()
                   14841: {
                   14842:        REG16(AX) = 0xffff;
                   14843:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14844: }
                   14845: 
                   14846: inline void msdos_int_33h_0022h()
                   14847: {
                   14848:        mouse.language = REG16(BX);
                   14849: }
                   14850: 
                   14851: inline void msdos_int_33h_0023h()
                   14852: {
                   14853:        REG16(BX) = mouse.language;
                   14854: }
                   14855: 
                   14856: inline void msdos_int_33h_0024h()
                   14857: {
                   14858:        REG16(BX) = 0x0805; // V8.05
                   14859:        REG16(CX) = 0x0400; // PS/2
                   14860: }
                   14861: 
1.1.1.49  root     14862: inline void msdos_int_33h_0025h()
                   14863: {
                   14864:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   14865: }
                   14866: 
1.1.1.24  root     14867: inline void msdos_int_33h_0026h()
                   14868: {
                   14869:        REG16(BX) = 0x0000;
                   14870:        REG16(CX) = mouse.max_position.x;
                   14871:        REG16(DX) = mouse.max_position.y;
                   14872: }
                   14873: 
1.1.1.49  root     14874: inline void msdos_int_33h_0027h()
                   14875: {
                   14876: //     if(mouse.hidden > 0) {
                   14877:                update_console_input();
                   14878: //     }
                   14879:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14880:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14881:        mouse.prev_position.x = mouse.position.x;
                   14882:        mouse.prev_position.y = mouse.position.y;
                   14883:        REG16(AX) = mouse.screen_mask;
                   14884:        REG16(BX) = mouse.cursor_mask;
                   14885:        REG16(CX) = dx;
                   14886:        REG16(DX) = dy;
                   14887: }
                   14888: 
                   14889: inline void msdos_int_33h_0028h()
                   14890: {
                   14891:        if(REG16(CX) != 0) {
                   14892:                UINT8 tmp = REG8(AL);
                   14893:                REG8(AL) = REG8(CL);
                   14894:                pcbios_int_10h_00h();
                   14895:                REG8(AL) = tmp;
                   14896:        }
                   14897:        REG8(CL) = 0x00; // successful
                   14898: }
                   14899: 
                   14900: inline void msdos_int_33h_0029h()
                   14901: {
                   14902:        switch(REG16(CX)) {
                   14903:        case 0x0000:
                   14904:                REG16(CX) = 0x0003;
                   14905:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   14906:                break;
                   14907:        case 0x0003:
                   14908:                REG16(CX) = 0x0070;
                   14909:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   14910:                break;
                   14911:        case 0x0070:
                   14912:                REG16(CX) = 0x0071;
                   14913:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   14914:                break;
                   14915:        case 0x0071:
                   14916:                REG16(CX) = 0x0073;
                   14917:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   14918:                break;
                   14919:        default:
                   14920:                REG16(CX) = 0x0000;
                   14921:                break;
                   14922:        }
                   14923:        if(REG16(CX) != 0) {
                   14924:                SREG(DS) = (WORK_TOP >> 4);
                   14925:        } else {
                   14926:                SREG(DS) = 0x0000;
                   14927:        }
                   14928:        i386_load_segment_descriptor(DS);
                   14929:        REG16(DX) = 0x0000;
                   14930: }
                   14931: 
1.1.1.24  root     14932: inline void msdos_int_33h_002ah()
                   14933: {
1.1.1.34  root     14934:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     14935:        REG16(BX) = mouse.hot_spot[0];
                   14936:        REG16(CX) = mouse.hot_spot[1];
                   14937:        REG16(DX) = 4; // PS/2
                   14938: }
                   14939: 
                   14940: inline void msdos_int_33h_0031h()
                   14941: {
                   14942:        REG16(AX) = mouse.min_position.x;
                   14943:        REG16(BX) = mouse.min_position.y;
                   14944:        REG16(CX) = mouse.max_position.x;
                   14945:        REG16(DX) = mouse.max_position.y;
                   14946: }
                   14947: 
                   14948: inline void msdos_int_33h_0032h()
                   14949: {
                   14950:        REG16(AX) = 0;
1.1.1.49  root     14951:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     14952:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     14953:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     14954: //     REG16(AX) |= 0x1000; // 0028h
                   14955: //     REG16(AX) |= 0x0800; // 0029h
                   14956:        REG16(AX) |= 0x0400; // 002ah
                   14957: //     REG16(AX) |= 0x0200; // 002bh
                   14958: //     REG16(AX) |= 0x0100; // 002ch
                   14959: //     REG16(AX) |= 0x0080; // 002dh
                   14960: //     REG16(AX) |= 0x0040; // 002eh
                   14961:        REG16(AX) |= 0x0020; // 002fh
                   14962: //     REG16(AX) |= 0x0010; // 0030h
                   14963:        REG16(AX) |= 0x0008; // 0031h
                   14964:        REG16(AX) |= 0x0004; // 0032h
                   14965: //     REG16(AX) |= 0x0002; // 0033h
                   14966: //     REG16(AX) |= 0x0001; // 0034h
                   14967: }
                   14968: 
1.1.1.49  root     14969: inline void msdos_int_33h_004dh()
                   14970: {
                   14971:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   14972: }
                   14973: 
                   14974: inline void msdos_int_33h_006dh()
                   14975: {
                   14976:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   14977:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   14978: }
                   14979: 
1.1.1.19  root     14980: inline void msdos_int_67h_40h()
                   14981: {
                   14982:        if(!support_ems) {
                   14983:                REG8(AH) = 0x84;
                   14984:        } else {
                   14985:                REG8(AH) = 0x00;
                   14986:        }
                   14987: }
                   14988: 
                   14989: inline void msdos_int_67h_41h()
                   14990: {
                   14991:        if(!support_ems) {
                   14992:                REG8(AH) = 0x84;
                   14993:        } else {
                   14994:                REG8(AH) = 0x00;
                   14995:                REG16(BX) = EMS_TOP >> 4;
                   14996:        }
                   14997: }
                   14998: 
                   14999: inline void msdos_int_67h_42h()
                   15000: {
                   15001:        if(!support_ems) {
                   15002:                REG8(AH) = 0x84;
                   15003:        } else {
                   15004:                REG8(AH) = 0x00;
                   15005:                REG16(BX) = free_ems_pages;
                   15006:                REG16(DX) = MAX_EMS_PAGES;
                   15007:        }
                   15008: }
                   15009: 
                   15010: inline void msdos_int_67h_43h()
                   15011: {
                   15012:        if(!support_ems) {
                   15013:                REG8(AH) = 0x84;
                   15014:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15015:                REG8(AH) = 0x87;
                   15016:        } else if(REG16(BX) > free_ems_pages) {
                   15017:                REG8(AH) = 0x88;
                   15018:        } else if(REG16(BX) == 0) {
                   15019:                REG8(AH) = 0x89;
                   15020:        } else {
1.1.1.31  root     15021:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15022:                        if(!ems_handles[i].allocated) {
                   15023:                                ems_allocate_pages(i, REG16(BX));
                   15024:                                REG8(AH) = 0x00;
                   15025:                                REG16(DX) = i;
                   15026:                                return;
                   15027:                        }
                   15028:                }
                   15029:                REG8(AH) = 0x85;
                   15030:        }
                   15031: }
                   15032: 
                   15033: inline void msdos_int_67h_44h()
                   15034: {
                   15035:        if(!support_ems) {
                   15036:                REG8(AH) = 0x84;
1.1.1.31  root     15037:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15038:                REG8(AH) = 0x83;
                   15039:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15040:                REG8(AH) = 0x8a;
                   15041: //     } else if(!(REG8(AL) < 4)) {
                   15042: //             REG8(AH) = 0x8b;
                   15043:        } else if(REG16(BX) == 0xffff) {
                   15044:                ems_unmap_page(REG8(AL) & 3);
                   15045:                REG8(AH) = 0x00;
                   15046:        } else {
                   15047:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15048:                REG8(AH) = 0x00;
                   15049:        }
                   15050: }
                   15051: 
                   15052: inline void msdos_int_67h_45h()
                   15053: {
                   15054:        if(!support_ems) {
                   15055:                REG8(AH) = 0x84;
1.1.1.31  root     15056:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15057:                REG8(AH) = 0x83;
                   15058:        } else {
                   15059:                ems_release_pages(REG16(DX));
                   15060:                REG8(AH) = 0x00;
                   15061:        }
                   15062: }
                   15063: 
                   15064: inline void msdos_int_67h_46h()
                   15065: {
                   15066:        if(!support_ems) {
                   15067:                REG8(AH) = 0x84;
                   15068:        } else {
1.1.1.29  root     15069: //             REG16(AX) = 0x0032; // EMS 3.2
                   15070:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15071:        }
                   15072: }
                   15073: 
                   15074: inline void msdos_int_67h_47h()
                   15075: {
                   15076:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15077:        process_t *process = msdos_process_info_get(current_psp);
                   15078:        
                   15079:        if(!support_ems) {
                   15080:                REG8(AH) = 0x84;
1.1.1.31  root     15081: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15082: //             REG8(AH) = 0x83;
                   15083:        } else if(process->ems_pages_stored) {
                   15084:                REG8(AH) = 0x8d;
                   15085:        } else {
                   15086:                for(int i = 0; i < 4; i++) {
                   15087:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15088:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15089:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15090:                }
                   15091:                process->ems_pages_stored = true;
                   15092:                REG8(AH) = 0x00;
                   15093:        }
                   15094: }
                   15095: 
                   15096: inline void msdos_int_67h_48h()
                   15097: {
                   15098:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15099:        process_t *process = msdos_process_info_get(current_psp);
                   15100:        
                   15101:        if(!support_ems) {
                   15102:                REG8(AH) = 0x84;
1.1.1.31  root     15103: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15104: //             REG8(AH) = 0x83;
                   15105:        } else if(!process->ems_pages_stored) {
                   15106:                REG8(AH) = 0x8e;
                   15107:        } else {
                   15108:                for(int i = 0; i < 4; i++) {
                   15109:                        if(process->ems_pages[i].mapped) {
                   15110:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15111:                        } else {
                   15112:                                ems_unmap_page(i);
                   15113:                        }
                   15114:                }
                   15115:                process->ems_pages_stored = false;
                   15116:                REG8(AH) = 0x00;
                   15117:        }
                   15118: }
                   15119: 
                   15120: inline void msdos_int_67h_4bh()
                   15121: {
                   15122:        if(!support_ems) {
                   15123:                REG8(AH) = 0x84;
                   15124:        } else {
                   15125:                REG8(AH) = 0x00;
                   15126:                REG16(BX) = 0;
1.1.1.31  root     15127:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15128:                        if(ems_handles[i].allocated) {
                   15129:                                REG16(BX)++;
                   15130:                        }
                   15131:                }
                   15132:        }
                   15133: }
                   15134: 
                   15135: inline void msdos_int_67h_4ch()
                   15136: {
                   15137:        if(!support_ems) {
                   15138:                REG8(AH) = 0x84;
1.1.1.31  root     15139:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15140:                REG8(AH) = 0x83;
                   15141:        } else {
                   15142:                REG8(AH) = 0x00;
                   15143:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15144:        }
                   15145: }
                   15146: 
                   15147: inline void msdos_int_67h_4dh()
                   15148: {
                   15149:        if(!support_ems) {
                   15150:                REG8(AH) = 0x84;
                   15151:        } else {
                   15152:                REG8(AH) = 0x00;
                   15153:                REG16(BX) = 0;
1.1.1.31  root     15154:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15155:                        if(ems_handles[i].allocated) {
                   15156:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15157:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15158:                                REG16(BX)++;
                   15159:                        }
                   15160:                }
                   15161:        }
                   15162: }
                   15163: 
1.1.1.20  root     15164: inline void msdos_int_67h_4eh()
                   15165: {
                   15166:        if(!support_ems) {
                   15167:                REG8(AH) = 0x84;
                   15168:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15169:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15170:                        // save page map
                   15171:                        for(int i = 0; i < 4; i++) {
                   15172:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15173:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15174:                        }
                   15175:                }
                   15176:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15177:                        // restore page map
                   15178:                        for(int i = 0; i < 4; i++) {
                   15179:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15180:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15181:                                
1.1.1.31  root     15182:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15183:                                        ems_map_page(i, handle, page);
                   15184:                                } else {
                   15185:                                        ems_unmap_page(i);
                   15186:                                }
                   15187:                        }
                   15188:                }
                   15189:                REG8(AH) = 0x00;
                   15190:        } else if(REG8(AL) == 0x03) {
                   15191:                REG8(AH) = 0x00;
1.1.1.21  root     15192:                REG8(AL) = 4 * 4;
                   15193:        } else {
1.1.1.22  root     15194:                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     15195:                REG8(AH) = 0x8f;
                   15196:        }
                   15197: }
                   15198: 
                   15199: inline void msdos_int_67h_4fh()
                   15200: {
                   15201:        if(!support_ems) {
                   15202:                REG8(AH) = 0x84;
                   15203:        } else if(REG8(AL) == 0x00) {
                   15204:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15205:                
                   15206:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15207:                for(int i = 0; i < count; i++) {
                   15208:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15209:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15210:                        
                   15211: //                     if(!(physical < 4)) {
                   15212: //                             REG8(AH) = 0x8b;
                   15213: //                             return;
                   15214: //                     }
                   15215:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15216:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15217:                        *(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     15218:                }
                   15219:                REG8(AH) = 0x00;
                   15220:        } else if(REG8(AL) == 0x01) {
                   15221:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15222:                
                   15223:                for(int i = 0; i < count; i++) {
                   15224:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15225:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15226:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15227:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15228:                        
                   15229: //                     if(!(physical < 4)) {
                   15230: //                             REG8(AH) = 0x8b;
                   15231: //                             return;
                   15232: //                     } else
1.1.1.41  root     15233:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15234:                                ems_map_page(physical & 3, handle, logical);
                   15235:                        } else {
1.1.1.41  root     15236:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15237:                        }
                   15238:                }
                   15239:                REG8(AH) = 0x00;
                   15240:        } else if(REG8(AL) == 0x02) {
                   15241:                REG8(AH) = 0x00;
                   15242:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15243:        } else {
1.1.1.22  root     15244:                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     15245:                REG8(AH) = 0x8f;
                   15246:        }
                   15247: }
                   15248: 
                   15249: inline void msdos_int_67h_50h()
                   15250: {
                   15251:        if(!support_ems) {
                   15252:                REG8(AH) = 0x84;
1.1.1.31  root     15253:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15254:                REG8(AH) = 0x83;
                   15255:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15256:                for(int i = 0; i < REG16(CX); i++) {
                   15257:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15258:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15259:                        
                   15260:                        if(REG8(AL) == 0x01) {
                   15261:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15262:                        }
                   15263: //                     if(!(physical < 4)) {
                   15264: //                             REG8(AH) = 0x8b;
                   15265: //                             return;
                   15266: //                     } else
                   15267:                        if(logical == 0xffff) {
                   15268:                                ems_unmap_page(physical & 3);
                   15269:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15270:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15271:                        } else {
                   15272:                                REG8(AH) = 0x8a;
                   15273:                                return;
                   15274:                        }
                   15275:                }
                   15276:                REG8(AH) = 0x00;
                   15277:        } else {
1.1.1.22  root     15278:                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     15279:                REG8(AH) = 0x8f;
                   15280:        }
                   15281: }
                   15282: 
1.1.1.19  root     15283: inline void msdos_int_67h_51h()
                   15284: {
                   15285:        if(!support_ems) {
                   15286:                REG8(AH) = 0x84;
1.1.1.31  root     15287:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15288:                REG8(AH) = 0x83;
                   15289:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15290:                REG8(AH) = 0x87;
                   15291:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15292:                REG8(AH) = 0x88;
                   15293:        } else {
                   15294:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15295:                REG8(AH) = 0x00;
                   15296:        }
                   15297: }
                   15298: 
1.1.1.20  root     15299: inline void msdos_int_67h_52h()
                   15300: {
                   15301:        if(!support_ems) {
                   15302:                REG8(AH) = 0x84;
1.1.1.31  root     15303: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15304: //             REG8(AH) = 0x83;
1.1.1.20  root     15305:        } else if(REG8(AL) == 0x00) {
                   15306:                REG8(AL) = 0x00; // handle is volatile
                   15307:                REG8(AH) = 0x00;
                   15308:        } else if(REG8(AL) == 0x01) {
                   15309:                if(REG8(BL) == 0x00) {
                   15310:                        REG8(AH) = 0x00;
                   15311:                } else {
                   15312:                        REG8(AH) = 0x90; // undefined attribute type
                   15313:                }
                   15314:        } else if(REG8(AL) == 0x02) {
                   15315:                REG8(AL) = 0x00; // only volatile handles supported
                   15316:                REG8(AH) = 0x00;
                   15317:        } else {
1.1.1.22  root     15318:                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     15319:                REG8(AH) = 0x8f;
                   15320:        }
                   15321: }
                   15322: 
1.1.1.19  root     15323: inline void msdos_int_67h_53h()
                   15324: {
                   15325:        if(!support_ems) {
                   15326:                REG8(AH) = 0x84;
1.1.1.31  root     15327:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15328:                REG8(AH) = 0x83;
                   15329:        } else if(REG8(AL) == 0x00) {
                   15330:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15331:                REG8(AH) = 0x00;
                   15332:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15333:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15334:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15335:                                REG8(AH) = 0xa1;
                   15336:                                return;
                   15337:                        }
                   15338:                }
                   15339:                REG8(AH) = 0x00;
                   15340:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15341:        } else {
1.1.1.22  root     15342:                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     15343:                REG8(AH) = 0x8f;
1.1.1.19  root     15344:        }
                   15345: }
                   15346: 
                   15347: inline void msdos_int_67h_54h()
                   15348: {
                   15349:        if(!support_ems) {
                   15350:                REG8(AH) = 0x84;
                   15351:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15352:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15353:                        if(ems_handles[i].allocated) {
                   15354:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15355:                        } else {
                   15356:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15357:                        }
                   15358:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15359:                }
                   15360:                REG8(AH) = 0x00;
                   15361:                REG8(AL) = MAX_EMS_HANDLES;
                   15362:        } else if(REG8(AL) == 0x01) {
                   15363:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15364:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15365:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15366:                                REG8(AH) = 0x00;
                   15367:                                REG16(DX) = i;
                   15368:                                break;
                   15369:                        }
                   15370:                }
                   15371:        } else if(REG8(AL) == 0x02) {
                   15372:                REG8(AH) = 0x00;
                   15373:                REG16(BX) = MAX_EMS_HANDLES;
                   15374:        } else {
1.1.1.22  root     15375:                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     15376:                REG8(AH) = 0x8f;
                   15377:        }
                   15378: }
                   15379: 
1.1.1.49  root     15380: inline void msdos_int_67h_55h()
                   15381: {
                   15382:        if(!support_ems) {
                   15383:                REG8(AH) = 0x84;
                   15384:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15385:                REG8(AH) = 0x83;
                   15386:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15387:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15388:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15389:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15390:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15391:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15392:                
                   15393:                for(int i = 0; i < (int)entries; i++) {
                   15394:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15395:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15396:                        
                   15397:                        if(REG8(AL) == 0x01) {
                   15398:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15399:                        }
                   15400: //                     if(!(physical < 4)) {
                   15401: //                             REG8(AH) = 0x8b;
                   15402: //                             return;
                   15403: //                     } else
                   15404:                        if(logical == 0xffff) {
                   15405:                                ems_unmap_page(physical & 3);
                   15406:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15407:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15408:                        } else {
                   15409:                                REG8(AH) = 0x8a;
                   15410:                                return;
                   15411:                        }
                   15412:                }
                   15413:                i386_jmp_far(jump_seg, jump_ofs);
                   15414:                REG8(AH) = 0x00;
                   15415:        } else {
                   15416:                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));
                   15417:                REG8(AH) = 0x8f;
                   15418:        }
                   15419: }
                   15420: 
                   15421: inline void msdos_int_67h_56h()
                   15422: {
                   15423:        if(!support_ems) {
                   15424:                REG8(AH) = 0x84;
                   15425:        } else if(REG8(AL) == 0x02) {
                   15426:                REG16(BX) = (2 + 2) * 4;
                   15427:                REG8(AH) = 0x00;
                   15428:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15429:                REG8(AH) = 0x83;
                   15430:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15431:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15432:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15433:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15434:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15435:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15436: #if 0
                   15437:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15438:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15439:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15440: #endif
                   15441:                UINT16 handles[4], pages[4];
                   15442:                
                   15443:                // alter page map and call routine is at fffc:001f
                   15444:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15445:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15446:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15447:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15448:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15449:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15450:                } else {
                   15451:                        // invalid call addr :-(
                   15452:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15453:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15454:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15455:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15456:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15457:                }
                   15458:                // do call far (push cs/ip) in old mapping
                   15459:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15460:                
                   15461:                // get old mapping data
                   15462: #if 0
                   15463:                for(int i = 0; i < (int)old_entries; i++) {
                   15464:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15465:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15466:                        
                   15467:                        if(REG8(AL) == 0x01) {
                   15468:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15469:                        }
                   15470: //                     if(!(physical < 4)) {
                   15471: //                             REG8(AH) = 0x8b;
                   15472: //                             return;
                   15473: //                     } else
                   15474:                        if(logical == 0xffff) {
                   15475:                                ems_unmap_page(physical & 3);
                   15476:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15477:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15478:                        } else {
                   15479:                                REG8(AH) = 0x8a;
                   15480:                                return;
                   15481:                        }
                   15482:                }
                   15483: #endif
                   15484:                for(int i = 0; i < 4; i++) {
                   15485:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15486:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15487:                }
                   15488:                
                   15489:                // set new mapping
                   15490:                for(int i = 0; i < (int)new_entries; i++) {
                   15491:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   15492:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   15493:                        
                   15494:                        if(REG8(AL) == 0x01) {
                   15495:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15496:                        }
                   15497: //                     if(!(physical < 4)) {
                   15498: //                             REG8(AH) = 0x8b;
                   15499: //                             return;
                   15500: //                     } else
                   15501:                        if(logical == 0xffff) {
                   15502:                                ems_unmap_page(physical & 3);
                   15503:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15504:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15505:                        } else {
                   15506:                                REG8(AH) = 0x8a;
                   15507:                                return;
                   15508:                        }
                   15509:                }
                   15510:                
                   15511:                // push old mapping data in new mapping
                   15512:                for(int i = 0; i < 4; i++) {
                   15513:                        i386_push16(handles[i]);
                   15514:                        i386_push16(pages  [i]);
                   15515:                }
                   15516:                REG8(AH) = 0x00;
                   15517:        } else {
                   15518:                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));
                   15519:                REG8(AH) = 0x8f;
                   15520:        }
                   15521: }
                   15522: 
1.1.1.20  root     15523: inline void msdos_int_67h_57h_tmp()
                   15524: {
                   15525:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15526:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15527:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   15528:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   15529:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   15530:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   15531:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15532:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   15533:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   15534:        
1.1.1.32  root     15535:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     15536:        UINT32 src_addr, dest_addr;
                   15537:        UINT32 src_addr_max, dest_addr_max;
                   15538:        
                   15539:        if(src_type == 0) {
                   15540:                src_buffer = mem;
                   15541:                src_addr = (src_seg << 4) + src_ofs;
                   15542:                src_addr_max = MAX_MEM;
                   15543:        } else {
1.1.1.31  root     15544:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     15545:                        REG8(AH) = 0x83;
                   15546:                        return;
                   15547:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   15548:                        REG8(AH) = 0x8a;
                   15549:                        return;
                   15550:                }
1.1.1.32  root     15551:                if(ems_handles[src_handle].buffer != NULL) {
                   15552:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   15553:                }
1.1.1.20  root     15554:                src_addr = src_ofs;
1.1.1.32  root     15555:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     15556:        }
                   15557:        if(dest_type == 0) {
                   15558:                dest_buffer = mem;
                   15559:                dest_addr = (dest_seg << 4) + dest_ofs;
                   15560:                dest_addr_max = MAX_MEM;
                   15561:        } else {
1.1.1.31  root     15562:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     15563:                        REG8(AH) = 0x83;
                   15564:                        return;
                   15565:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   15566:                        REG8(AH) = 0x8a;
                   15567:                        return;
                   15568:                }
1.1.1.32  root     15569:                if(ems_handles[dest_handle].buffer != NULL) {
                   15570:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   15571:                }
1.1.1.20  root     15572:                dest_addr = dest_ofs;
1.1.1.32  root     15573:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     15574:        }
1.1.1.32  root     15575:        if(src_buffer != NULL && dest_buffer != NULL) {
                   15576:                for(int i = 0; i < copy_length; i++) {
                   15577:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15578:                                if(REG8(AL) == 0x00) {
                   15579:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15580:                                } else if(REG8(AL) == 0x01) {
                   15581:                                        UINT8 tmp = dest_buffer[dest_addr];
                   15582:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   15583:                                        src_buffer[src_addr++] = tmp;
                   15584:                                }
                   15585:                        } else {
                   15586:                                REG8(AH) = 0x93;
                   15587:                                return;
1.1.1.20  root     15588:                        }
                   15589:                }
1.1.1.32  root     15590:                REG8(AH) = 0x00;
                   15591:        } else {
                   15592:                REG8(AH) = 0x80;
1.1.1.20  root     15593:        }
                   15594: }
                   15595: 
                   15596: inline void msdos_int_67h_57h()
                   15597: {
                   15598:        if(!support_ems) {
                   15599:                REG8(AH) = 0x84;
                   15600:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15601:                struct {
                   15602:                        UINT16 handle;
                   15603:                        UINT16 page;
                   15604:                        bool mapped;
                   15605:                } tmp_pages[4];
                   15606:                
                   15607:                // unmap pages to copy memory data to ems buffer
                   15608:                for(int i = 0; i < 4; i++) {
                   15609:                        tmp_pages[i].handle = ems_pages[i].handle;
                   15610:                        tmp_pages[i].page   = ems_pages[i].page;
                   15611:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   15612:                        ems_unmap_page(i);
                   15613:                }
                   15614:                
                   15615:                // run move/exchange operation
                   15616:                msdos_int_67h_57h_tmp();
                   15617:                
                   15618:                // restore unmapped pages
                   15619:                for(int i = 0; i < 4; i++) {
                   15620:                        if(tmp_pages[i].mapped) {
                   15621:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   15622:                        }
                   15623:                }
                   15624:        } else {
1.1.1.22  root     15625:                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     15626:                REG8(AH) = 0x8f;
                   15627:        }
                   15628: }
                   15629: 
                   15630: inline void msdos_int_67h_58h()
                   15631: {
                   15632:        if(!support_ems) {
                   15633:                REG8(AH) = 0x84;
                   15634:        } else if(REG8(AL) == 0x00) {
                   15635:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     15636:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   15637:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     15638:                }
                   15639:                REG8(AH) = 0x00;
                   15640:                REG16(CX) = 4;
                   15641:        } else if(REG8(AL) == 0x01) {
                   15642:                REG8(AH) = 0x00;
                   15643:                REG16(CX) = 4;
                   15644:        } else {
1.1.1.22  root     15645:                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     15646:                REG8(AH) = 0x8f;
                   15647:        }
                   15648: }
                   15649: 
1.1.1.42  root     15650: inline void msdos_int_67h_59h()
                   15651: {
                   15652:        if(!support_ems) {
                   15653:                REG8(AH) = 0x84;
                   15654:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     15655:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   15656:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   15657:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   15658:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   15659:                REG8(AH) = 0x00;
                   15660: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     15661:        } else if(REG8(AL) == 0x01) {
                   15662:                REG8(AH) = 0x00;
                   15663:                REG16(BX) = free_ems_pages;
                   15664:                REG16(DX) = MAX_EMS_PAGES;
                   15665:        } else {
                   15666:                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));
                   15667:                REG8(AH) = 0x8f;
                   15668:        }
                   15669: }
                   15670: 
1.1.1.20  root     15671: inline void msdos_int_67h_5ah()
                   15672: {
                   15673:        if(!support_ems) {
1.1.1.19  root     15674:                REG8(AH) = 0x84;
1.1.1.20  root     15675:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15676:                REG8(AH) = 0x87;
                   15677:        } else if(REG16(BX) > free_ems_pages) {
                   15678:                REG8(AH) = 0x88;
                   15679: //     } else if(REG16(BX) == 0) {
                   15680: //             REG8(AH) = 0x89;
                   15681:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     15682:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     15683:                        if(!ems_handles[i].allocated) {
                   15684:                                ems_allocate_pages(i, REG16(BX));
                   15685:                                REG8(AH) = 0x00;
                   15686:                                REG16(DX) = i;
                   15687:                                return;
                   15688:                        }
                   15689:                }
                   15690:                REG8(AH) = 0x85;
                   15691:        } else {
1.1.1.22  root     15692:                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     15693:                REG8(AH) = 0x8f;
1.1.1.19  root     15694:        }
                   15695: }
                   15696: 
1.1.1.49  root     15697: inline void msdos_int_67h_5bh()
                   15698: {
                   15699:        static UINT8  stored_bl = 0x00;
                   15700:        static UINT16 stored_es = 0x0000;
                   15701:        static UINT16 stored_di = 0x0000;
                   15702:        
                   15703:        if(!support_ems) {
                   15704:                REG8(AH) = 0x84;
                   15705:        } else if(REG8(AL) == 0x00) {
                   15706:                if(stored_bl == 0x00) {
                   15707:                        if(!(stored_es == 0 && stored_di == 0)) {
                   15708:                                for(int i = 0; i < 4; i++) {
                   15709:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15710:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15711:                                }
                   15712:                        }
                   15713:                        SREG(ES) = stored_es;
                   15714:                        i386_load_segment_descriptor(ES);
                   15715:                        REG16(DI) = stored_di;
                   15716:                } else {
                   15717:                        REG8(BL) = stored_bl;
                   15718:                }
                   15719:                REG8(AH) = 0x00;
                   15720:        } else if(REG8(AL) == 0x01) {
                   15721:                if(REG8(BL) == 0x00) {
                   15722:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   15723:                                for(int i = 0; i < 4; i++) {
                   15724:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   15725:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   15726:                                        
                   15727:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   15728:                                                ems_map_page(i, handle, page);
                   15729:                                        } else {
                   15730:                                                ems_unmap_page(i);
                   15731:                                        }
                   15732:                                }
                   15733:                        }
                   15734:                }
                   15735:                stored_bl = REG8(BL);
                   15736:                stored_es = SREG(ES);
                   15737:                stored_di = REG16(DI);
                   15738:                REG8(AH) = 0x00;
                   15739:        } else if(REG8(AL) == 0x02) {
                   15740:                REG16(DX) = 4 * 4;
                   15741:                REG8(AH) = 0x00;
                   15742:        } else if(REG8(AL) == 0x03) {
                   15743:                REG8(BL) = 0x00; // not supported
                   15744:                REG8(AH) = 0x00;
                   15745:        } else if(REG8(AL) == 0x04) {
                   15746:                REG8(AH) = 0x00;
                   15747:        } else if(REG8(AL) == 0x05) {
                   15748:                REG8(BL) = 0x00; // not supported
                   15749:                REG8(AH) = 0x00;
                   15750:        } else {
                   15751:                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));
                   15752:                REG8(AH) = 0x8f;
                   15753:        }
                   15754: }
                   15755: 
1.1.1.43  root     15756: inline void msdos_int_67h_5dh()
                   15757: {
                   15758:        if(!support_ems) {
                   15759:                REG8(AH) = 0x84;
                   15760:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15761:                REG8(AH) = 0xa4; // operating system denied access
                   15762:        } else {
                   15763:                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));
                   15764:                REG8(AH) = 0x8f;
                   15765:        }
                   15766: }
                   15767: 
1.1.1.49  root     15768: inline void msdos_int_67h_70h()
                   15769: {
                   15770:        if(!support_ems) {
                   15771:                REG8(AH) = 0x84;
                   15772:        } else if(REG8(AL) == 0x00) {
                   15773:                REG8(AL) = 0x00;
                   15774:                REG8(AH) = 0x00;
                   15775:        } else if(REG8(AL) == 0x01) {
                   15776:                REG8(AL) = 0x00;
                   15777: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   15778:                REG8(AH) = 0x00;
                   15779:        } else {
                   15780:                unimplemented_67h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x67, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   15781:                REG8(AH) = 0x8f;
                   15782:        }
                   15783: }
                   15784: 
1.1.1.30  root     15785: inline void msdos_int_67h_deh()
                   15786: {
                   15787:        REG8(AH) = 0x84;
                   15788: }
                   15789: 
1.1.1.19  root     15790: #ifdef SUPPORT_XMS
                   15791: 
1.1.1.32  root     15792: void msdos_xms_init()
1.1.1.26  root     15793: {
1.1.1.30  root     15794:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15795:        emb_handle_top->address = EMB_TOP;
                   15796:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     15797:        xms_a20_local_enb_count = 0;
                   15798: }
                   15799: 
1.1.1.32  root     15800: void msdos_xms_finish()
                   15801: {
                   15802:        msdos_xms_release();
                   15803: }
                   15804: 
                   15805: void msdos_xms_release()
1.1.1.30  root     15806: {
                   15807:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   15808:                emb_handle_t *next_handle = emb_handle->next;
                   15809:                free(emb_handle);
                   15810:                emb_handle = next_handle;
                   15811:        }
                   15812: }
                   15813: 
                   15814: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   15815: {
                   15816:        if(handle != 0) {
                   15817:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15818:                        if(emb_handle->handle == handle) {
                   15819:                                return(emb_handle);
                   15820:                        }
                   15821:                }
                   15822:        }
                   15823:        return(NULL);
                   15824: }
                   15825: 
                   15826: int msdos_xms_get_unused_emb_handle_id()
                   15827: {
                   15828:        for(int handle = 1;; handle++) {
                   15829:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   15830:                        return(handle);
                   15831:                }
                   15832:        }
                   15833:        return(0);
                   15834: }
                   15835: 
                   15836: int msdos_xms_get_unused_emb_handle_count()
                   15837: {
                   15838:        int count = 64; //255;
                   15839:        
                   15840:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15841:                if(emb_handle->handle != 0) {
                   15842:                        if(--count == 1) {
                   15843:                                break;
                   15844:                        }
                   15845:                }
                   15846:        }
                   15847:        return(count);
                   15848: }
                   15849: 
                   15850: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   15851: {
                   15852:        if(emb_handle->size_kb > size_kb) {
                   15853:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15854:                
                   15855:                new_handle->address = emb_handle->address + size_kb * 1024;
                   15856:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   15857:                emb_handle->size_kb = size_kb;
                   15858:                
                   15859:                new_handle->prev = emb_handle;
                   15860:                new_handle->next = emb_handle->next;
                   15861:                if(emb_handle->next != NULL) {
                   15862:                        emb_handle->next->prev = new_handle;
                   15863:                }
                   15864:                emb_handle->next = new_handle;
                   15865:        }
                   15866: }
                   15867: 
                   15868: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   15869: {
                   15870:        emb_handle_t *next_handle = emb_handle->next;
                   15871:        
                   15872:        if(next_handle != NULL) {
                   15873:                emb_handle->size_kb += next_handle->size_kb;
                   15874:                
                   15875:                if(next_handle->next != NULL) {
                   15876:                        next_handle->next->prev = emb_handle;
                   15877:                }
                   15878:                emb_handle->next = next_handle->next;
                   15879:                free(next_handle);
                   15880:        }
                   15881: }
                   15882: 
                   15883: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   15884: {
                   15885:        emb_handle_t *target_handle = NULL;
                   15886:        
                   15887:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15888:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   15889:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   15890:                                target_handle = emb_handle;
                   15891:                        }
                   15892:                }
                   15893:        }
                   15894:        if(target_handle != NULL) {
                   15895:                if(target_handle->size_kb > size_kb) {
                   15896:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   15897:                }
                   15898: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15899:                return(target_handle);
                   15900:        }
                   15901:        return(NULL);
                   15902: }
                   15903: 
                   15904: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   15905: {
                   15906:        emb_handle_t *prev_handle = emb_handle->prev;
                   15907:        emb_handle_t *next_handle = emb_handle->next;
                   15908:        
                   15909:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   15910:                msdos_xms_combine_emb_handles(prev_handle);
                   15911:                emb_handle = prev_handle;
                   15912:        }
                   15913:        if(next_handle != NULL && next_handle->handle == 0) {
                   15914:                msdos_xms_combine_emb_handles(emb_handle);
                   15915:        }
                   15916:        emb_handle->handle = 0;
                   15917: }
                   15918: 
1.1.1.19  root     15919: inline void msdos_call_xms_00h()
                   15920: {
1.1.1.29  root     15921: #if defined(HAS_I386)
                   15922:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     15923: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     15924:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   15925: #else
                   15926:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   15927:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   15928: #endif
                   15929: //     REG16(DX) = 0x0000; // HMA does not exist
                   15930:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     15931: }
                   15932: 
                   15933: inline void msdos_call_xms_01h()
                   15934: {
1.1.1.29  root     15935:        if(REG8(AL) == 0x40) {
                   15936:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   15937:                // DX=KB free extended memory returned by last call of function 08h
                   15938:                REG16(AX) = 0x0000;
                   15939:                REG8(BL) = 0x91;
                   15940:                REG16(DX) = xms_dx_after_call_08h;
                   15941:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15942:                REG16(AX) = 0x0000;
                   15943:                REG8(BL) = 0x81; // Vdisk was detected
                   15944: #ifdef SUPPORT_HMA
                   15945:        } else if(is_hma_used_by_int_2fh) {
                   15946:                REG16(AX) = 0x0000;
                   15947:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15948:        } else if(is_hma_used_by_xms) {
                   15949:                REG16(AX) = 0x0000;
                   15950:                REG8(BL) = 0x91; // HMA is already in use
                   15951:        } else {
                   15952:                REG16(AX) = 0x0001;
                   15953:                is_hma_used_by_xms = true;
                   15954: #else
                   15955:        } else {
                   15956:                REG16(AX) = 0x0000;
                   15957:                REG8(BL) = 0x91; // HMA is already in use
                   15958: #endif
                   15959:        }
1.1.1.19  root     15960: }
                   15961: 
                   15962: inline void msdos_call_xms_02h()
                   15963: {
1.1.1.29  root     15964:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15965:                REG16(AX) = 0x0000;
                   15966:                REG8(BL) = 0x81; // Vdisk was detected
                   15967: #ifdef SUPPORT_HMA
                   15968:        } else if(is_hma_used_by_int_2fh) {
                   15969:                REG16(AX) = 0x0000;
                   15970:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15971:        } else if(!is_hma_used_by_xms) {
                   15972:                REG16(AX) = 0x0000;
                   15973:                REG8(BL) = 0x93; // HMA is not allocated
                   15974:        } else {
                   15975:                REG16(AX) = 0x0001;
                   15976:                is_hma_used_by_xms = false;
                   15977:                // restore first free mcb in high memory area
                   15978:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15979: #else
                   15980:        } else {
                   15981:                REG16(AX) = 0x0000;
                   15982:                REG8(BL) = 0x91; // HMA is already in use
                   15983: #endif
                   15984:        }
1.1.1.19  root     15985: }
                   15986: 
                   15987: inline void msdos_call_xms_03h()
                   15988: {
                   15989:        i386_set_a20_line(1);
                   15990:        REG16(AX) = 0x0001;
                   15991:        REG8(BL) = 0x00;
                   15992: }
                   15993: 
                   15994: inline void msdos_call_xms_04h()
                   15995: {
1.1.1.21  root     15996:        i386_set_a20_line(0);
                   15997:        REG16(AX) = 0x0001;
                   15998:        REG8(BL) = 0x00;
1.1.1.19  root     15999: }
                   16000: 
                   16001: inline void msdos_call_xms_05h()
                   16002: {
                   16003:        i386_set_a20_line(1);
                   16004:        REG16(AX) = 0x0001;
                   16005:        REG8(BL) = 0x00;
1.1.1.21  root     16006:        xms_a20_local_enb_count++;
1.1.1.19  root     16007: }
                   16008: 
                   16009: void msdos_call_xms_06h()
                   16010: {
1.1.1.21  root     16011:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16012:                if(--xms_a20_local_enb_count == 0) {
                   16013:                        i386_set_a20_line(0);
                   16014:                        REG16(AX) = 0x0001;
                   16015:                        REG8(BL) = 0x00;
                   16016:                } else {
                   16017:                        REG16(AX) = 0x0000;
                   16018:                        REG8(BL) = 0x94;
                   16019:                }
1.1.1.21  root     16020:        } else {
1.1.1.45  root     16021:                i386_set_a20_line(0);
1.1.1.21  root     16022:                REG16(AX) = 0x0001;
                   16023:                REG8(BL) = 0x00;
1.1.1.19  root     16024:        }
                   16025: }
                   16026: 
                   16027: inline void msdos_call_xms_07h()
                   16028: {
                   16029:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16030:        REG8(BL) = 0x00;
                   16031: }
                   16032: 
                   16033: inline void msdos_call_xms_08h()
                   16034: {
1.1.1.45  root     16035:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16036:        
1.1.1.30  root     16037:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16038:                if(emb_handle->handle == 0) {
1.1.1.45  root     16039:                        if(eax < emb_handle->size_kb) {
                   16040:                                eax = emb_handle->size_kb;
1.1.1.19  root     16041:                        }
1.1.1.45  root     16042:                        edx += emb_handle->size_kb;
1.1.1.19  root     16043:                }
                   16044:        }
1.1.1.45  root     16045:        if(eax > 65535) {
                   16046:                eax = 65535;
                   16047:        }
                   16048:        if(edx > 65535) {
                   16049:                edx = 65535;
                   16050:        }
                   16051:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16052:                REG8(BL) = 0xa0;
                   16053:        } else {
                   16054:                REG8(BL) = 0x00;
                   16055:        }
1.1.1.45  root     16056: #if defined(HAS_I386)
                   16057:        REG32(EAX) = eax;
                   16058:        REG32(EDX) = edx;
                   16059: #else
                   16060:        REG16(AX) = (UINT16)eax;
                   16061:        REG16(DX) = (UINT16)edx;
                   16062: #endif
1.1.1.29  root     16063:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16064: }
                   16065: 
1.1.1.30  root     16066: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16067: {
1.1.1.30  root     16068:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16069:        
                   16070:        if(emb_handle != NULL) {
                   16071:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16072:                
                   16073:                REG16(AX) = 0x0001;
                   16074:                REG16(DX) = emb_handle->handle;
                   16075:                REG8(BL) = 0x00;
                   16076:        } else {
                   16077:                REG16(AX) = REG16(DX) = 0x0000;
                   16078:                REG8(BL) = 0xa0;
1.1.1.19  root     16079:        }
1.1.1.30  root     16080: }
                   16081: 
                   16082: inline void msdos_call_xms_09h()
                   16083: {
                   16084:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16085: }
                   16086: 
                   16087: inline void msdos_call_xms_0ah()
                   16088: {
1.1.1.30  root     16089:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16090:        
                   16091:        if(emb_handle == NULL) {
1.1.1.19  root     16092:                REG16(AX) = 0x0000;
                   16093:                REG8(BL) = 0xa2;
1.1.1.45  root     16094: //     } else if(emb_handle->lock > 0) {
                   16095: //             REG16(AX) = 0x0000;
                   16096: //             REG8(BL) = 0xab;
1.1.1.19  root     16097:        } else {
1.1.1.30  root     16098:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16099:                
                   16100:                REG16(AX) = 0x0001;
                   16101:                REG8(BL) = 0x00;
                   16102:        }
                   16103: }
                   16104: 
                   16105: inline void msdos_call_xms_0bh()
                   16106: {
                   16107:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16108:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16109:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16110:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16111:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16112:        
                   16113:        UINT8 *src_buffer, *dest_buffer;
                   16114:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16115:        emb_handle_t *emb_handle;
1.1.1.19  root     16116:        
                   16117:        if(src_handle == 0) {
                   16118:                src_buffer = mem;
                   16119:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16120:                src_addr_max = MAX_MEM;
                   16121:        } else {
1.1.1.30  root     16122:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16123:                        REG16(AX) = 0x0000;
                   16124:                        REG8(BL) = 0xa3;
                   16125:                        return;
                   16126:                }
1.1.1.30  root     16127:                src_buffer = mem + emb_handle->address;
                   16128:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16129:        }
                   16130:        if(dest_handle == 0) {
                   16131:                dest_buffer = mem;
                   16132:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16133:                dest_addr_max = MAX_MEM;
                   16134:        } else {
1.1.1.30  root     16135:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16136:                        REG16(AX) = 0x0000;
                   16137:                        REG8(BL) = 0xa5;
                   16138:                        return;
                   16139:                }
1.1.1.30  root     16140:                dest_buffer = mem + emb_handle->address;
                   16141:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16142:        }
                   16143:        for(int i = 0; i < copy_length; i++) {
                   16144:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16145:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16146:                } else {
                   16147:                        break;
                   16148:                }
                   16149:        }
                   16150:        REG16(AX) = 0x0001;
                   16151:        REG8(BL) = 0x00;
                   16152: }
                   16153: 
                   16154: inline void msdos_call_xms_0ch()
                   16155: {
1.1.1.30  root     16156:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16157:        
                   16158:        if(emb_handle == NULL) {
1.1.1.19  root     16159:                REG16(AX) = 0x0000;
                   16160:                REG8(BL) = 0xa2;
                   16161:        } else {
1.1.1.45  root     16162:                if(emb_handle->lock < 255) {
                   16163:                        emb_handle->lock++;
                   16164:                }
1.1.1.19  root     16165:                REG16(AX) = 0x0001;
1.1.1.30  root     16166:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16167:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16168:        }
                   16169: }
                   16170: 
                   16171: inline void msdos_call_xms_0dh()
                   16172: {
1.1.1.30  root     16173:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16174:        
                   16175:        if(emb_handle == NULL) {
1.1.1.19  root     16176:                REG16(AX) = 0x0000;
                   16177:                REG8(BL) = 0xa2;
1.1.1.30  root     16178:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16179:                REG16(AX) = 0x0000;
                   16180:                REG8(BL) = 0xaa;
                   16181:        } else {
1.1.1.30  root     16182:                emb_handle->lock--;
1.1.1.19  root     16183:                REG16(AX) = 0x0001;
                   16184:                REG8(BL) = 0x00;
                   16185:        }
                   16186: }
                   16187: 
                   16188: inline void msdos_call_xms_0eh()
                   16189: {
1.1.1.30  root     16190:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16191:        
                   16192:        if(emb_handle == NULL) {
1.1.1.19  root     16193:                REG16(AX) = 0x0000;
                   16194:                REG8(BL) = 0xa2;
                   16195:        } else {
                   16196:                REG16(AX) = 0x0001;
1.1.1.30  root     16197:                REG8(BH) = emb_handle->lock;
                   16198:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16199:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16200:        }
                   16201: }
                   16202: 
1.1.1.30  root     16203: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16204: {
1.1.1.30  root     16205:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16206:        
                   16207:        if(emb_handle == NULL) {
1.1.1.19  root     16208:                REG16(AX) = 0x0000;
                   16209:                REG8(BL) = 0xa2;
1.1.1.30  root     16210:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16211:                REG16(AX) = 0x0000;
                   16212:                REG8(BL) = 0xab;
                   16213:        } else {
1.1.1.30  root     16214:                if(emb_handle->size_kb < size_kb) {
                   16215:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16216:                                msdos_xms_combine_emb_handles(emb_handle);
                   16217:                                if(emb_handle->size_kb > size_kb) {
                   16218:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16219:                                }
                   16220:                        } else {
                   16221:                                int old_handle = emb_handle->handle;
                   16222:                                int old_size_kb = emb_handle->size_kb;
                   16223:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16224:                                
                   16225:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16226:                                msdos_xms_free_emb_handle(emb_handle);
                   16227:                                
                   16228:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16229:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16230:                                }
                   16231:                                emb_handle->handle = old_handle;
                   16232:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16233:                                free(buffer);
                   16234:                        }
                   16235:                } else if(emb_handle->size_kb > size_kb) {
                   16236:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16237:                }
                   16238:                if(emb_handle->size_kb != size_kb) {
                   16239:                        REG16(AX) = 0x0000;
                   16240:                        REG8(BL) = 0xa0;
                   16241:                } else {
                   16242:                        REG16(AX) = 0x0001;
                   16243:                        REG8(BL) = 0x00;
                   16244:                }
1.1.1.19  root     16245:        }
                   16246: }
                   16247: 
1.1.1.30  root     16248: inline void msdos_call_xms_0fh()
                   16249: {
                   16250:        msdos_call_xms_0fh(REG16(BX));
                   16251: }
                   16252: 
1.1.1.19  root     16253: inline void msdos_call_xms_10h()
                   16254: {
                   16255:        int seg;
                   16256:        
                   16257:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16258:                REG16(AX) = 0x0001;
                   16259:                REG16(BX) = seg;
                   16260:        } else {
                   16261:                REG16(AX) = 0x0000;
                   16262:                REG8(BL) = 0xb0;
                   16263:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16264:        }
                   16265: }
                   16266: 
                   16267: inline void msdos_call_xms_11h()
                   16268: {
                   16269:        int mcb_seg = REG16(DX) - 1;
                   16270:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16271:        
                   16272:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16273:                msdos_mem_free(REG16(DX));
                   16274:                REG16(AX) = 0x0001;
                   16275:                REG8(BL) = 0x00;
                   16276:        } else {
                   16277:                REG16(AX) = 0x0000;
                   16278:                REG8(BL) = 0xb2;
                   16279:        }
                   16280: }
                   16281: 
                   16282: inline void msdos_call_xms_12h()
                   16283: {
                   16284:        int mcb_seg = REG16(DX) - 1;
                   16285:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16286:        int max_paragraphs;
                   16287:        
                   16288:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16289:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16290:                        REG16(AX) = 0x0001;
                   16291:                        REG8(BL) = 0x00;
                   16292:                } else {
                   16293:                        REG16(AX) = 0x0000;
                   16294:                        REG8(BL) = 0xb0;
                   16295:                        REG16(DX) = max_paragraphs;
                   16296:                }
                   16297:        } else {
                   16298:                REG16(AX) = 0x0000;
                   16299:                REG8(BL) = 0xb2;
                   16300:        }
                   16301: }
                   16302: 
1.1.1.29  root     16303: #if defined(HAS_I386)
                   16304: 
                   16305: inline void msdos_call_xms_88h()
                   16306: {
                   16307:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16308:        
1.1.1.30  root     16309:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16310:                if(emb_handle->handle == 0) {
                   16311:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16312:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16313:                        }
1.1.1.30  root     16314:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16315:                }
                   16316:        }
                   16317:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16318:                REG8(BL) = 0xa0;
                   16319:        } else {
                   16320:                REG8(BL) = 0x00;
                   16321:        }
                   16322:        REG32(ECX) = EMB_END - 1;
                   16323: }
                   16324: 
                   16325: inline void msdos_call_xms_89h()
                   16326: {
1.1.1.30  root     16327:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16328: }
                   16329: 
                   16330: inline void msdos_call_xms_8eh()
                   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.29  root     16335:                REG16(AX) = 0x0000;
                   16336:                REG8(BL) = 0xa2;
                   16337:        } else {
                   16338:                REG16(AX) = 0x0001;
1.1.1.30  root     16339:                REG8(BH) = emb_handle->lock;
                   16340:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16341:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16342:        }
                   16343: }
                   16344: 
                   16345: inline void msdos_call_xms_8fh()
                   16346: {
1.1.1.30  root     16347:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16348: }
                   16349: 
                   16350: #endif
1.1.1.19  root     16351: #endif
                   16352: 
1.1.1.26  root     16353: UINT16 msdos_get_equipment()
                   16354: {
                   16355:        static UINT16 equip = 0;
                   16356:        
                   16357:        if(equip == 0) {
                   16358: #ifdef SUPPORT_FPU
                   16359:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   16360: #endif
                   16361:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   16362:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   16363: //             equip |= (1 << 8);      // 0 if DMA installed
                   16364:                equip |= (2 << 9);      // number of serial ports
                   16365:                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     16366:                
                   16367:                // check only A: and B: if it is floppy drive
                   16368:                int n = 0;
                   16369:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     16370:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   16371:                                n++;
1.1.1.28  root     16372:                        }
                   16373:                }
                   16374:                if(n != 0) {
                   16375:                        equip |= (1 << 0);      // floppy disk(s) installed
                   16376:                        n--;
                   16377:                        equip |= (n << 6);      // number of floppies installed less 1
                   16378:                }
                   16379: //             if(joyGetNumDevs() != 0) {
                   16380: //                     equip |= (1 << 12);     // game port installed
                   16381: //             }
1.1.1.26  root     16382:        }
                   16383:        return(equip);
                   16384: }
                   16385: 
1.1       root     16386: void msdos_syscall(unsigned num)
                   16387: {
1.1.1.22  root     16388: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     16389:        if(num == 0x08 || num == 0x1c) {
                   16390:                // don't log the timer interrupts
1.1.1.45  root     16391: //             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     16392:        } else if(num == 0x30) {
                   16393:                // dummy interrupt for call 0005h (call near)
                   16394:                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     16395:        } else if(num == 0x68) {
1.1.1.22  root     16396:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     16397:                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     16398:        } else if(num == 0x69) {
                   16399:                // dummy interrupt for XMS (call far)
1.1.1.33  root     16400:                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     16401:        } else if(num >= 0x6a && num <= 0x6f) {
1.1.1.45  root     16402:                // dummy interrupt
1.1.1.22  root     16403:        } else {
1.1.1.33  root     16404:                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     16405:        }
                   16406: #endif
1.1.1.36  root     16407:        // update cursor position
                   16408:        if(cursor_moved) {
                   16409:                pcbios_update_cursor_position();
                   16410:                cursor_moved = false;
                   16411:        }
1.1.1.50  root     16412: #ifdef USE_SERVICE_THREAD
                   16413:        // this is called from dummy loop to wait until a serive that waits input is done
                   16414:        if(!in_service)
                   16415: #endif
1.1.1.33  root     16416:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     16417:        
1.1       root     16418:        switch(num) {
                   16419:        case 0x00:
1.1.1.28  root     16420:                try {
                   16421:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16422:                        error("division by zero\n");
                   16423:                } catch(...) {
                   16424:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   16425:                }
1.1       root     16426:                break;
                   16427:        case 0x04:
1.1.1.28  root     16428:                try {
                   16429:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16430:                        error("overflow\n");
                   16431:                } catch(...) {
                   16432:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   16433:                }
1.1       root     16434:                break;
                   16435:        case 0x06:
                   16436:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     16437:                if(!ignore_illegal_insn) {
1.1.1.28  root     16438:                        try {
                   16439:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16440:                                error("illegal instruction\n");
                   16441:                        } catch(...) {
                   16442:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   16443:                        }
1.1.1.14  root     16444:                } else {
                   16445: #if defined(HAS_I386)
1.1.1.39  root     16446:                        m_eip = m_int6h_skip_eip;
                   16447: #elif defined(HAS_I286)
                   16448:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     16449: #else
1.1.1.39  root     16450:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     16451: #endif
                   16452:                }
1.1       root     16453:                break;
1.1.1.33  root     16454:        case 0x09:
                   16455:                // ctrl-break is pressed
                   16456:                if(raise_int_1bh) {
                   16457: #if defined(HAS_I386)
                   16458:                        m_ext = 0; // not an external interrupt
                   16459:                        i386_trap(0x1b, 1, 0);
                   16460:                        m_ext = 1;
                   16461: #else
                   16462:                        PREFIX86(_interrupt)(0x1b);
                   16463: #endif
                   16464:                        raise_int_1bh = false;
                   16465:                }
1.1.1.8   root     16466:        case 0x08:
1.1.1.14  root     16467: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     16468:        case 0x0b:
                   16469:        case 0x0c:
                   16470:        case 0x0d:
                   16471:        case 0x0e:
                   16472:        case 0x0f:
                   16473:                // EOI
                   16474:                pic[0].isr &= ~(1 << (num - 0x08));
                   16475:                pic_update();
                   16476:                break;
1.1       root     16477:        case 0x10:
                   16478:                // PC BIOS - Video
1.1.1.14  root     16479:                if(!restore_console_on_exit) {
1.1.1.15  root     16480:                        change_console_size(scr_width, scr_height);
1.1       root     16481:                }
1.1.1.3   root     16482:                m_CF = 0;
1.1       root     16483:                switch(REG8(AH)) {
1.1.1.16  root     16484:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     16485:                case 0x01: pcbios_int_10h_01h(); break;
                   16486:                case 0x02: pcbios_int_10h_02h(); break;
                   16487:                case 0x03: pcbios_int_10h_03h(); break;
                   16488:                case 0x05: pcbios_int_10h_05h(); break;
                   16489:                case 0x06: pcbios_int_10h_06h(); break;
                   16490:                case 0x07: pcbios_int_10h_07h(); break;
                   16491:                case 0x08: pcbios_int_10h_08h(); break;
                   16492:                case 0x09: pcbios_int_10h_09h(); break;
                   16493:                case 0x0a: pcbios_int_10h_0ah(); break;
                   16494:                case 0x0b: break;
1.1.1.40  root     16495:                case 0x0c: pcbios_int_10h_0ch(); break;
                   16496:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     16497:                case 0x0e: pcbios_int_10h_0eh(); break;
                   16498:                case 0x0f: pcbios_int_10h_0fh(); break;
                   16499:                case 0x10: break;
1.1.1.14  root     16500:                case 0x11: pcbios_int_10h_11h(); break;
                   16501:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     16502:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     16503:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     16504:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     16505:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   16506:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     16507:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     16508:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   16509:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     16510:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     16511:                case 0x6f: break;
1.1.1.22  root     16512:                case 0x80: m_CF = 1; break; // unknown
                   16513:                case 0x81: m_CF = 1; break; // unknown
1.1       root     16514:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     16515:                case 0x83: pcbios_int_10h_83h(); break;
                   16516:                case 0x8b: break;
                   16517:                case 0x8c: m_CF = 1; break; // unknown
                   16518:                case 0x8d: m_CF = 1; break; // unknown
                   16519:                case 0x8e: m_CF = 1; break; // unknown
                   16520:                case 0x90: pcbios_int_10h_90h(); break;
                   16521:                case 0x91: pcbios_int_10h_91h(); break;
                   16522:                case 0x92: break;
                   16523:                case 0x93: break;
                   16524:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     16525:                case 0xfa: break; // ega register interface library is not installed
1.1       root     16526:                case 0xfe: pcbios_int_10h_feh(); break;
                   16527:                case 0xff: pcbios_int_10h_ffh(); break;
                   16528:                default:
1.1.1.22  root     16529:                        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));
                   16530:                        m_CF = 1;
1.1       root     16531:                        break;
                   16532:                }
                   16533:                break;
                   16534:        case 0x11:
                   16535:                // PC BIOS - Get Equipment List
1.1.1.26  root     16536:                REG16(AX) = msdos_get_equipment();
1.1       root     16537:                break;
                   16538:        case 0x12:
                   16539:                // PC BIOS - Get Memory Size
1.1.1.33  root     16540:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     16541:                break;
                   16542:        case 0x13:
1.1.1.42  root     16543:                // PC BIOS - Disk I/O
                   16544:                {
                   16545:                        static UINT8 last = 0x00;
                   16546:                        switch(REG8(AH)) {
                   16547:                        case 0x00: pcbios_int_13h_00h(); break;
                   16548:                        case 0x01: // get last status
                   16549:                                REG8(AH) = last;
                   16550:                                break;
                   16551:                        case 0x02: pcbios_int_13h_02h(); break;
                   16552:                        case 0x03: pcbios_int_13h_03h(); break;
                   16553:                        case 0x04: pcbios_int_13h_04h(); break;
                   16554:                        case 0x08: pcbios_int_13h_08h(); break;
                   16555:                        case 0x0a: pcbios_int_13h_02h(); break;
                   16556:                        case 0x0b: pcbios_int_13h_03h(); break;
                   16557:                        case 0x0d: pcbios_int_13h_00h(); break;
                   16558:                        case 0x10: pcbios_int_13h_10h(); break;
                   16559:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     16560:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     16561:                        case 0x05: // format
                   16562:                        case 0x06:
                   16563:                        case 0x07:
                   16564:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   16565:                                m_CF = 1;
                   16566:                                break;
                   16567:                        case 0x09:
                   16568:                        case 0x0c: // seek
                   16569:                        case 0x11: // recalib
                   16570:                        case 0x14:
                   16571:                        case 0x17:
                   16572:                                REG8(AH) = 0x00; // successful completion
                   16573:                                break;
1.1.1.43  root     16574:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   16575:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   16576:                                REG8(AH) = 0x01; // invalid function
                   16577:                                m_CF = 1;
                   16578:                                break;
1.1.1.42  root     16579:                        default:
                   16580:                                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));
                   16581:                                REG8(AH) = 0x01; // invalid function
                   16582:                                m_CF = 1;
                   16583:                                break;
                   16584:                        }
                   16585:                        last = REG8(AH);
                   16586:                }
1.1       root     16587:                break;
                   16588:        case 0x14:
                   16589:                // PC BIOS - Serial I/O
1.1.1.25  root     16590:                switch(REG8(AH)) {
                   16591:                case 0x00: pcbios_int_14h_00h(); break;
                   16592:                case 0x01: pcbios_int_14h_01h(); break;
                   16593:                case 0x02: pcbios_int_14h_02h(); break;
                   16594:                case 0x03: pcbios_int_14h_03h(); break;
                   16595:                case 0x04: pcbios_int_14h_04h(); break;
                   16596:                case 0x05: pcbios_int_14h_05h(); break;
                   16597:                default:
                   16598:                        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));
                   16599:                        break;
                   16600:                }
1.1       root     16601:                break;
                   16602:        case 0x15:
                   16603:                // PC BIOS
1.1.1.3   root     16604:                m_CF = 0;
1.1       root     16605:                switch(REG8(AH)) {
1.1.1.14  root     16606:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     16607:                case 0x23: pcbios_int_15h_23h(); break;
                   16608:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     16609:                case 0x41: break;
1.1       root     16610:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     16611:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     16612:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     16613:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     16614:                case 0x86: pcbios_int_15h_86h(); break;
                   16615:                case 0x87: pcbios_int_15h_87h(); break;
                   16616:                case 0x88: pcbios_int_15h_88h(); break;
                   16617:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     16618:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     16619:                case 0xc0: // PS/2 ???
1.1.1.54  root     16620: #ifndef EXT_BIOS_TOP
1.1.1.22  root     16621:                case 0xc1:
1.1.1.54  root     16622: #endif
1.1.1.30  root     16623:                case 0xc3: // PS50+ ???
                   16624:                case 0xc4:
1.1.1.22  root     16625:                        REG8(AH) = 0x86;
                   16626:                        m_CF = 1;
                   16627:                        break;
1.1.1.54  root     16628: #ifdef EXT_BIOS_TOP
                   16629:                case 0xc1: pcbios_int_15h_c1h(); break;
                   16630: #endif
                   16631:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     16632: #if defined(HAS_I386)
1.1       root     16633:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     16634: #endif
1.1       root     16635:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     16636:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     16637:                default:
1.1.1.22  root     16638:                        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));
                   16639:                        REG8(AH) = 0x86;
1.1.1.3   root     16640:                        m_CF = 1;
1.1       root     16641:                        break;
                   16642:                }
                   16643:                break;
                   16644:        case 0x16:
                   16645:                // PC BIOS - Keyboard
1.1.1.3   root     16646:                m_CF = 0;
1.1       root     16647:                switch(REG8(AH)) {
                   16648:                case 0x00: pcbios_int_16h_00h(); break;
                   16649:                case 0x01: pcbios_int_16h_01h(); break;
                   16650:                case 0x02: pcbios_int_16h_02h(); break;
                   16651:                case 0x03: pcbios_int_16h_03h(); break;
                   16652:                case 0x05: pcbios_int_16h_05h(); break;
                   16653:                case 0x10: pcbios_int_16h_00h(); break;
                   16654:                case 0x11: pcbios_int_16h_01h(); break;
                   16655:                case 0x12: pcbios_int_16h_12h(); break;
                   16656:                case 0x13: pcbios_int_16h_13h(); break;
                   16657:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     16658:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     16659:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     16660:                case 0xda: break; // unknown
1.1.1.43  root     16661:                case 0xdb: break; // unknown
1.1.1.22  root     16662:                case 0xff: break; // unknown
1.1       root     16663:                default:
1.1.1.22  root     16664:                        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     16665:                        break;
                   16666:                }
                   16667:                break;
                   16668:        case 0x17:
                   16669:                // PC BIOS - Printer
1.1.1.37  root     16670:                m_CF = 0;
                   16671:                switch(REG8(AH)) {
                   16672:                case 0x00: pcbios_int_17h_00h(); break;
                   16673:                case 0x01: pcbios_int_17h_01h(); break;
                   16674:                case 0x02: pcbios_int_17h_02h(); break;
                   16675:                case 0x03: pcbios_int_17h_03h(); break;
                   16676:                case 0x50: pcbios_int_17h_50h(); break;
                   16677:                case 0x51: pcbios_int_17h_51h(); break;
                   16678:                case 0x52: pcbios_int_17h_52h(); break;
                   16679:                case 0x84: pcbios_int_17h_84h(); break;
                   16680:                case 0x85: pcbios_int_17h_85h(); break;
                   16681:                default:
                   16682:                        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));
                   16683:                        break;
                   16684:                }
1.1       root     16685:                break;
                   16686:        case 0x1a:
                   16687:                // PC BIOS - Timer
1.1.1.3   root     16688:                m_CF = 0;
1.1       root     16689:                switch(REG8(AH)) {
                   16690:                case 0x00: pcbios_int_1ah_00h(); break;
                   16691:                case 0x01: break;
                   16692:                case 0x02: pcbios_int_1ah_02h(); break;
                   16693:                case 0x03: break;
                   16694:                case 0x04: pcbios_int_1ah_04h(); break;
                   16695:                case 0x05: break;
                   16696:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   16697:                case 0x0b: break;
1.1.1.14  root     16698:                case 0x35: break; // Word Perfect Third Party Interface?
                   16699:                case 0x36: break; // Word Perfect Third Party Interface
                   16700:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     16701:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     16702:                case 0xb1: break; // PCI BIOS v2.0c+
                   16703:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     16704:                default:
1.1.1.22  root     16705:                        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     16706:                        break;
                   16707:                }
                   16708:                break;
1.1.1.33  root     16709:        case 0x1b:
                   16710:                mem[0x471] = 0x00;
                   16711:                break;
1.1       root     16712:        case 0x20:
1.1.1.28  root     16713:                try {
                   16714:                        msdos_process_terminate(SREG(CS), retval, 1);
                   16715:                } catch(...) {
                   16716:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   16717:                }
1.1       root     16718:                break;
1.1.1.49  root     16719:        case 0x30:
1.1.1.46  root     16720:                // dummy interrupt for case map routine pointed in the country info
                   16721: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   16722: //                     REG8(AL) = 0x00;
                   16723: //                     break;
                   16724: //             }
1.1       root     16725:        case 0x21:
                   16726:                // MS-DOS System Call
1.1.1.3   root     16727:                m_CF = 0;
1.1.1.28  root     16728:                try {
1.1.1.46  root     16729:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     16730:                        case 0x00: msdos_int_21h_00h(); break;
                   16731:                        case 0x01: msdos_int_21h_01h(); break;
                   16732:                        case 0x02: msdos_int_21h_02h(); break;
                   16733:                        case 0x03: msdos_int_21h_03h(); break;
                   16734:                        case 0x04: msdos_int_21h_04h(); break;
                   16735:                        case 0x05: msdos_int_21h_05h(); break;
                   16736:                        case 0x06: msdos_int_21h_06h(); break;
                   16737:                        case 0x07: msdos_int_21h_07h(); break;
                   16738:                        case 0x08: msdos_int_21h_08h(); break;
                   16739:                        case 0x09: msdos_int_21h_09h(); break;
                   16740:                        case 0x0a: msdos_int_21h_0ah(); break;
                   16741:                        case 0x0b: msdos_int_21h_0bh(); break;
                   16742:                        case 0x0c: msdos_int_21h_0ch(); break;
                   16743:                        case 0x0d: msdos_int_21h_0dh(); break;
                   16744:                        case 0x0e: msdos_int_21h_0eh(); break;
                   16745:                        case 0x0f: msdos_int_21h_0fh(); break;
                   16746:                        case 0x10: msdos_int_21h_10h(); break;
                   16747:                        case 0x11: msdos_int_21h_11h(); break;
                   16748:                        case 0x12: msdos_int_21h_12h(); break;
                   16749:                        case 0x13: msdos_int_21h_13h(); break;
                   16750:                        case 0x14: msdos_int_21h_14h(); break;
                   16751:                        case 0x15: msdos_int_21h_15h(); break;
                   16752:                        case 0x16: msdos_int_21h_16h(); break;
                   16753:                        case 0x17: msdos_int_21h_17h(); break;
                   16754:                        case 0x18: msdos_int_21h_18h(); break;
                   16755:                        case 0x19: msdos_int_21h_19h(); break;
                   16756:                        case 0x1a: msdos_int_21h_1ah(); break;
                   16757:                        case 0x1b: msdos_int_21h_1bh(); break;
                   16758:                        case 0x1c: msdos_int_21h_1ch(); break;
                   16759:                        case 0x1d: msdos_int_21h_1dh(); break;
                   16760:                        case 0x1e: msdos_int_21h_1eh(); break;
                   16761:                        case 0x1f: msdos_int_21h_1fh(); break;
                   16762:                        case 0x20: msdos_int_21h_20h(); break;
                   16763:                        case 0x21: msdos_int_21h_21h(); break;
                   16764:                        case 0x22: msdos_int_21h_22h(); break;
                   16765:                        case 0x23: msdos_int_21h_23h(); break;
                   16766:                        case 0x24: msdos_int_21h_24h(); break;
                   16767:                        case 0x25: msdos_int_21h_25h(); break;
                   16768:                        case 0x26: msdos_int_21h_26h(); break;
                   16769:                        case 0x27: msdos_int_21h_27h(); break;
                   16770:                        case 0x28: msdos_int_21h_28h(); break;
                   16771:                        case 0x29: msdos_int_21h_29h(); break;
                   16772:                        case 0x2a: msdos_int_21h_2ah(); break;
                   16773:                        case 0x2b: msdos_int_21h_2bh(); break;
                   16774:                        case 0x2c: msdos_int_21h_2ch(); break;
                   16775:                        case 0x2d: msdos_int_21h_2dh(); break;
                   16776:                        case 0x2e: msdos_int_21h_2eh(); break;
                   16777:                        case 0x2f: msdos_int_21h_2fh(); break;
                   16778:                        case 0x30: msdos_int_21h_30h(); break;
                   16779:                        case 0x31: msdos_int_21h_31h(); break;
                   16780:                        case 0x32: msdos_int_21h_32h(); break;
                   16781:                        case 0x33: msdos_int_21h_33h(); break;
                   16782:                        case 0x34: msdos_int_21h_34h(); break;
                   16783:                        case 0x35: msdos_int_21h_35h(); break;
                   16784:                        case 0x36: msdos_int_21h_36h(); break;
                   16785:                        case 0x37: msdos_int_21h_37h(); break;
                   16786:                        case 0x38: msdos_int_21h_38h(); break;
                   16787:                        case 0x39: msdos_int_21h_39h(0); break;
                   16788:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   16789:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   16790:                        case 0x3c: msdos_int_21h_3ch(); break;
                   16791:                        case 0x3d: msdos_int_21h_3dh(); break;
                   16792:                        case 0x3e: msdos_int_21h_3eh(); break;
                   16793:                        case 0x3f: msdos_int_21h_3fh(); break;
                   16794:                        case 0x40: msdos_int_21h_40h(); break;
                   16795:                        case 0x41: msdos_int_21h_41h(0); break;
                   16796:                        case 0x42: msdos_int_21h_42h(); break;
                   16797:                        case 0x43: msdos_int_21h_43h(0); break;
                   16798:                        case 0x44: msdos_int_21h_44h(); break;
                   16799:                        case 0x45: msdos_int_21h_45h(); break;
                   16800:                        case 0x46: msdos_int_21h_46h(); break;
                   16801:                        case 0x47: msdos_int_21h_47h(0); break;
                   16802:                        case 0x48: msdos_int_21h_48h(); break;
                   16803:                        case 0x49: msdos_int_21h_49h(); break;
                   16804:                        case 0x4a: msdos_int_21h_4ah(); break;
                   16805:                        case 0x4b: msdos_int_21h_4bh(); break;
                   16806:                        case 0x4c: msdos_int_21h_4ch(); break;
                   16807:                        case 0x4d: msdos_int_21h_4dh(); break;
                   16808:                        case 0x4e: msdos_int_21h_4eh(); break;
                   16809:                        case 0x4f: msdos_int_21h_4fh(); break;
                   16810:                        case 0x50: msdos_int_21h_50h(); break;
                   16811:                        case 0x51: msdos_int_21h_51h(); break;
                   16812:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     16813:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     16814:                        case 0x54: msdos_int_21h_54h(); break;
                   16815:                        case 0x55: msdos_int_21h_55h(); break;
                   16816:                        case 0x56: msdos_int_21h_56h(0); break;
                   16817:                        case 0x57: msdos_int_21h_57h(); break;
                   16818:                        case 0x58: msdos_int_21h_58h(); break;
                   16819:                        case 0x59: msdos_int_21h_59h(); break;
                   16820:                        case 0x5a: msdos_int_21h_5ah(); break;
                   16821:                        case 0x5b: msdos_int_21h_5bh(); break;
                   16822:                        case 0x5c: msdos_int_21h_5ch(); break;
                   16823:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     16824:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     16825:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     16826:                        case 0x60: msdos_int_21h_60h(0); break;
                   16827:                        case 0x61: msdos_int_21h_61h(); break;
                   16828:                        case 0x62: msdos_int_21h_62h(); break;
                   16829:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     16830:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     16831:                        case 0x65: msdos_int_21h_65h(); break;
                   16832:                        case 0x66: msdos_int_21h_66h(); break;
                   16833:                        case 0x67: msdos_int_21h_67h(); break;
                   16834:                        case 0x68: msdos_int_21h_68h(); break;
                   16835:                        case 0x69: msdos_int_21h_69h(); break;
                   16836:                        case 0x6a: msdos_int_21h_6ah(); break;
                   16837:                        case 0x6b: msdos_int_21h_6bh(); break;
                   16838:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     16839:                        case 0x6d: // Find First ROM Program
                   16840:                        case 0x6e: // Find Next ROM Program
                   16841:                        case 0x6f: // Get/Set ROM Scan Start Address
                   16842:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   16843:                                break;
1.1.1.43  root     16844:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     16845:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     16846:                                switch(REG8(AL)) {
                   16847:                                case 0x0d: msdos_int_21h_710dh(); break;
                   16848:                                case 0x39: msdos_int_21h_39h(1); break;
                   16849:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   16850:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     16851:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     16852:                                case 0x43: msdos_int_21h_43h(1); break;
                   16853:                                case 0x47: msdos_int_21h_47h(1); break;
                   16854:                                case 0x4e: msdos_int_21h_714eh(); break;
                   16855:                                case 0x4f: msdos_int_21h_714fh(); break;
                   16856:                                case 0x56: msdos_int_21h_56h(1); break;
                   16857:                                case 0x60: msdos_int_21h_60h(1); break;
                   16858:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   16859:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   16860:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   16861:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   16862:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   16863:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     16864:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     16865:                                case 0xaa: msdos_int_21h_71aah(); break;
                   16866:                                default:
                   16867:                                        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));
                   16868:                                        REG16(AX) = 0x7100;
                   16869:                                        m_CF = 1;
                   16870:                                        break;
                   16871:                                }
                   16872:                                break;
1.1.1.48  root     16873:                        case 0x72: // Windows95 beta - LFN FindClose
                   16874: //                             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));
                   16875:                                REG16(AX) = 0x7200;
                   16876:                                m_CF = 1;
                   16877:                                break;
                   16878:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     16879:                                switch(REG8(AL)) {
                   16880:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     16881:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     16882:                                case 0x02: msdos_int_21h_7302h(); break;
                   16883:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     16884:                                // 0x04: Set DPB to Use for Formatting
                   16885:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     16886:                                default:
                   16887:                                        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));
                   16888:                                        REG16(AX) = 0x7300;
                   16889:                                        m_CF = 1;
                   16890:                                        break;
                   16891:                                }
1.1       root     16892:                                break;
1.1.1.30  root     16893:                        case 0xdb: msdos_int_21h_dbh(); break;
                   16894:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     16895:                        default:
1.1.1.22  root     16896:                                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     16897:                                REG16(AX) = 0x01;
1.1.1.3   root     16898:                                m_CF = 1;
1.1       root     16899:                                break;
                   16900:                        }
1.1.1.28  root     16901:                } catch(int error) {
                   16902:                        REG16(AX) = error;
                   16903:                        m_CF = 1;
                   16904:                } catch(...) {
                   16905:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     16906:                        m_CF = 1;
1.1       root     16907:                }
1.1.1.3   root     16908:                if(m_CF) {
1.1.1.23  root     16909:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     16910:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     16911:                        sda->extended_error_code = REG16(AX);
                   16912:                        switch(sda->extended_error_code) {
                   16913:                        case  4: // Too many open files
                   16914:                        case  8: // Insufficient memory
                   16915:                                sda->error_class = 1; // Out of resource
                   16916:                                break;
                   16917:                        case  5: // Access denied
                   16918:                                sda->error_class = 3; // Authorization
                   16919:                                break;
                   16920:                        case  7: // Memory control block destroyed
                   16921:                                sda->error_class = 4; // Internal
                   16922:                                break;
                   16923:                        case  2: // File not found
                   16924:                        case  3: // Path not found
                   16925:                        case 15: // Invaid drive specified
                   16926:                        case 18: // No more files
                   16927:                                sda->error_class = 8; // Not found
                   16928:                                break;
                   16929:                        case 32: // Sharing violation
                   16930:                        case 33: // Lock violation
                   16931:                                sda->error_class = 10; // Locked
                   16932:                                break;
                   16933: //                     case 16: // Removal of current directory attempted
                   16934:                        case 19: // Attempted write on protected disk
                   16935:                        case 21: // Drive not ready
                   16936: //                     case 29: // Write failure
                   16937: //                     case 30: // Read failure
                   16938: //                     case 82: // Cannot create subdirectory
                   16939:                                sda->error_class = 11; // Media
                   16940:                                break;
                   16941:                        case 80: // File already exists
                   16942:                                sda->error_class = 12; // Already exist
                   16943:                                break;
                   16944:                        default:
                   16945:                                sda->error_class = 13; // Unknown
                   16946:                                break;
                   16947:                        }
                   16948:                        sda->suggested_action = 1; // Retry
                   16949:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     16950:                }
1.1.1.33  root     16951:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     16952:                        // raise int 23h
                   16953: #if defined(HAS_I386)
                   16954:                        m_ext = 0; // not an external interrupt
                   16955:                        i386_trap(0x23, 1, 0);
                   16956:                        m_ext = 1;
                   16957: #else
                   16958:                        PREFIX86(_interrupt)(0x23);
                   16959: #endif
                   16960:                }
1.1       root     16961:                break;
                   16962:        case 0x22:
                   16963:                fatalerror("int 22h (terminate address)\n");
                   16964:        case 0x23:
1.1.1.28  root     16965:                try {
                   16966:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   16967:                } catch(...) {
                   16968:                        fatalerror("failed to terminate the current process by int 23h\n");
                   16969:                }
1.1       root     16970:                break;
                   16971:        case 0x24:
1.1.1.32  root     16972: /*
1.1.1.28  root     16973:                try {
                   16974:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16975:                } catch(...) {
                   16976:                        fatalerror("failed to terminate the current process by int 24h\n");
                   16977:                }
1.1.1.32  root     16978: */
                   16979:                msdos_int_24h();
1.1       root     16980:                break;
                   16981:        case 0x25:
                   16982:                msdos_int_25h();
                   16983:                break;
                   16984:        case 0x26:
                   16985:                msdos_int_26h();
                   16986:                break;
                   16987:        case 0x27:
1.1.1.28  root     16988:                try {
                   16989:                        msdos_int_27h();
                   16990:                } catch(...) {
                   16991:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   16992:                }
1.1       root     16993:                break;
                   16994:        case 0x28:
                   16995:                Sleep(10);
1.1.1.35  root     16996:                REQUEST_HARDWRE_UPDATE();
1.1       root     16997:                break;
                   16998:        case 0x29:
                   16999:                msdos_int_29h();
                   17000:                break;
                   17001:        case 0x2e:
                   17002:                msdos_int_2eh();
                   17003:                break;
                   17004:        case 0x2f:
                   17005:                // multiplex interrupt
                   17006:                switch(REG8(AH)) {
1.1.1.22  root     17007:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     17008:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     17009:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     17010:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17011:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17012:                case 0x14: msdos_int_2fh_14h(); break;
                   17013:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17014:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17015:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17016:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17017:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17018:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17019:                case 0x46: msdos_int_2fh_46h(); break;
                   17020:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17021:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17022:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17023:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17024:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17025:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17026:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17027:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17028:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17029:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17030:                default:
1.1.1.30  root     17031:                        switch(REG8(AL)) {
                   17032:                        case 0x00:
                   17033:                                // This is not installed
                   17034: //                             REG8(AL) = 0x00;
                   17035:                                break;
1.1.1.33  root     17036:                        case 0x01:
1.1.1.42  root     17037:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17038:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17039:                                        break;
                   17040:                                }
1.1.1.33  root     17041:                                // Banyan VINES v4+ is not installed
                   17042:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17043:                                        break;
                   17044:                                }
1.1.1.42  root     17045:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17046:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17047:                                        break;
                   17048:                                }
1.1.1.30  root     17049:                        default:
1.1.1.42  root     17050:                                // NORTON UTILITIES 5.0+
                   17051:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17052:                                        break;
                   17053:                                }
1.1.1.30  root     17054:                                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     17055:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17056:                                m_CF = 1;
                   17057:                                break;
                   17058:                        }
                   17059:                        break;
1.1       root     17060:                }
                   17061:                break;
1.1.1.24  root     17062:        case 0x33:
                   17063:                switch(REG8(AH)) {
                   17064:                case 0x00:
                   17065:                        // Mouse
1.1.1.49  root     17066:                        switch(REG16(AX)) {
                   17067:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17068:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17069:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17070:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17071:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17072:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17073:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17074:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17075:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17076:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17077:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17078:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17079:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17080:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17081:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17082:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17083:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17084:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17085:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17086:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17087:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17088:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17089:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17090:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17091:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17092:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17093:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17094:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17095:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17096:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17097:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17098:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17099:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17100:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17101:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17102:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17103:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17104:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17105:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17106:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17107:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17108:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17109:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17110:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17111:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17112:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17113:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17114:                        case 0x002f: break; // Mouse Hardware Reset
                   17115:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17116:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17117:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17118:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17119:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17120:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17121:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17122:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17123:                        default:
                   17124:                                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));
                   17125:                                break;
                   17126:                        }
                   17127:                        break;
                   17128:                default:
                   17129:                        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));
                   17130:                        break;
                   17131:                }
                   17132:                break;
1.1.1.50  root     17133: /*
                   17134:        case 0x67:
                   17135:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 68h
                   17136:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   17137:                break;
                   17138: */
1.1.1.19  root     17139:        case 0x68:
                   17140:                // dummy interrupt for EMS (int 67h)
                   17141:                switch(REG8(AH)) {
                   17142:                case 0x40: msdos_int_67h_40h(); break;
                   17143:                case 0x41: msdos_int_67h_41h(); break;
                   17144:                case 0x42: msdos_int_67h_42h(); break;
                   17145:                case 0x43: msdos_int_67h_43h(); break;
                   17146:                case 0x44: msdos_int_67h_44h(); break;
                   17147:                case 0x45: msdos_int_67h_45h(); break;
                   17148:                case 0x46: msdos_int_67h_46h(); break;
                   17149:                case 0x47: msdos_int_67h_47h(); break;
                   17150:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17151:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17152:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17153:                case 0x4b: msdos_int_67h_4bh(); break;
                   17154:                case 0x4c: msdos_int_67h_4ch(); break;
                   17155:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17156:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17157:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17158:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17159:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17160:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17161:                case 0x53: msdos_int_67h_53h(); break;
                   17162:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17163:                case 0x55: msdos_int_67h_55h(); break;
                   17164:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17165:                case 0x57: msdos_int_67h_57h(); break;
                   17166:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17167:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17168:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17169:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17170:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17171:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17172:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17173:                // 0xde: VCPI
1.1.1.30  root     17174:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17175:                default:
1.1.1.22  root     17176:                        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     17177:                        REG8(AH) = 0x84;
                   17178:                        break;
                   17179:                }
                   17180:                break;
                   17181: #ifdef SUPPORT_XMS
                   17182:        case 0x69:
                   17183:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17184:                try {
                   17185:                        switch(REG8(AH)) {
                   17186:                        case 0x00: msdos_call_xms_00h(); break;
                   17187:                        case 0x01: msdos_call_xms_01h(); break;
                   17188:                        case 0x02: msdos_call_xms_02h(); break;
                   17189:                        case 0x03: msdos_call_xms_03h(); break;
                   17190:                        case 0x04: msdos_call_xms_04h(); break;
                   17191:                        case 0x05: msdos_call_xms_05h(); break;
                   17192:                        case 0x06: msdos_call_xms_06h(); break;
                   17193:                        case 0x07: msdos_call_xms_07h(); break;
                   17194:                        case 0x08: msdos_call_xms_08h(); break;
                   17195:                        case 0x09: msdos_call_xms_09h(); break;
                   17196:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17197:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17198:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17199:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17200:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17201:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17202:                        case 0x10: msdos_call_xms_10h(); break;
                   17203:                        case 0x11: msdos_call_xms_11h(); break;
                   17204:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17205: #if defined(HAS_I386)
                   17206:                        case 0x88: msdos_call_xms_88h(); break;
                   17207:                        case 0x89: msdos_call_xms_89h(); break;
                   17208:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17209:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17210: #endif
1.1.1.28  root     17211:                        default:
                   17212:                                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));
                   17213:                                REG16(AX) = 0x0000;
                   17214:                                REG8(BL) = 0x80; // function not implemented
                   17215:                                break;
                   17216:                        }
                   17217:                } catch(...) {
1.1.1.19  root     17218:                        REG16(AX) = 0x0000;
1.1.1.28  root     17219:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17220:                }
                   17221:                break;
                   17222: #endif
                   17223:        case 0x6a:
1.1.1.24  root     17224:                // irq12 (mouse)
                   17225:                mouse_push_ax = REG16(AX);
                   17226:                mouse_push_bx = REG16(BX);
                   17227:                mouse_push_cx = REG16(CX);
                   17228:                mouse_push_dx = REG16(DX);
                   17229:                mouse_push_si = REG16(SI);
                   17230:                mouse_push_di = REG16(DI);
                   17231:                
1.1.1.43  root     17232:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17233:                        REG16(AX) = mouse.status_irq;
                   17234:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17235:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17236:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17237:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17238:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17239:                        
1.1.1.49  root     17240:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17241:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17242:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17243:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17244:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     17245:                        break;
1.1.1.24  root     17246:                }
1.1.1.43  root     17247:                for(int i = 0; i < 8; i++) {
                   17248:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17249:                                REG16(AX) = mouse.status_irq_alt;
                   17250:                                REG16(BX) = mouse.get_buttons();
                   17251:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17252:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17253:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17254:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17255:                                
1.1.1.49  root     17256:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17257:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17258:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17259:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17260:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.43  root     17261:                                break;
                   17262:                        }
                   17263:                }
1.1.1.54  root     17264:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw) {
                   17265:                        UINT16 data_1st, data_2nd, data_3rd;
                   17266:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   17267:                        i386_push16(data_1st);
                   17268:                        i386_push16(data_2nd);
                   17269:                        i386_push16(data_3rd);
                   17270:                        i386_push16(0x0000);
                   17271:                        
                   17272:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17273:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
                   17274:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
                   17275:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
                   17276:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
                   17277:                        break;
                   17278:                }
1.1.1.43  root     17279:                // invalid call addr :-(
1.1.1.49  root     17280:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17281:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17282:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17283:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17284:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.24  root     17285:                break;
                   17286:        case 0x6b:
                   17287:                // end of irq12 (mouse)
                   17288:                REG16(AX) = mouse_push_ax;
                   17289:                REG16(BX) = mouse_push_bx;
                   17290:                REG16(CX) = mouse_push_cx;
                   17291:                REG16(DX) = mouse_push_dx;
                   17292:                REG16(SI) = mouse_push_si;
                   17293:                REG16(DI) = mouse_push_di;
                   17294:                
                   17295:                // EOI
                   17296:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17297:                        pic[0].isr &= ~(1 << 2); // master
                   17298:                }
                   17299:                pic_update();
                   17300:                break;
                   17301:        case 0x6c:
1.1.1.19  root     17302:                // dummy interrupt for case map routine pointed in the country info
                   17303:                if(REG8(AL) >= 0x80) {
                   17304:                        char tmp[2] = {0};
                   17305:                        tmp[0] = REG8(AL);
                   17306:                        my_strupr(tmp);
                   17307:                        REG8(AL) = tmp[0];
                   17308:                }
                   17309:                break;
1.1.1.27  root     17310:        case 0x6d:
                   17311:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17312:                REG8(AL) = 0x86; // not supported
                   17313:                m_CF = 1;
                   17314:                break;
1.1.1.32  root     17315:        case 0x6e:
                   17316:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17317:                {
                   17318:                        UINT16 code = REG16(AX);
                   17319:                        if(code & 0xf0) {
                   17320:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17321:                        }
                   17322:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17323:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17324:                                        const char *message = NULL;
                   17325:                                        if(active_code_page == 932) {
                   17326:                                                message = param_error_table[i].message_japanese;
                   17327:                                        }
                   17328:                                        if(message == NULL) {
                   17329:                                                message = param_error_table[i].message_english;
                   17330:                                        }
                   17331:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   17332:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   17333:                                        
                   17334:                                        SREG(ES) = WORK_TOP >> 4;
                   17335:                                        i386_load_segment_descriptor(ES);
                   17336:                                        REG16(DI) = 0x0000;
                   17337:                                        break;
                   17338:                                }
                   17339:                        }
                   17340:                }
                   17341:                break;
1.1.1.49  root     17342:        case 0x6f:
                   17343:                // dummy interrupt for end of alter page map and call
                   17344:                {
                   17345:                        UINT16 handles[4], pages[4];
                   17346:                        
                   17347:                        // pop old mapping data in new mapping
                   17348:                        for(int i = 0; i < 4; i++) {
                   17349:                                pages  [3 - i] = i386_pop16();
                   17350:                                handles[3 - i] = i386_pop16();
                   17351:                        }
                   17352:                        
                   17353:                        // restore old mapping
                   17354:                        for(int i = 0; i < 4; i++) {
                   17355:                                UINT16 handle = handles[i];
                   17356:                                UINT16 page   = pages  [i];
                   17357:                                
                   17358:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   17359:                                        ems_map_page(i, handle, page);
                   17360:                                } else {
                   17361:                                        ems_unmap_page(i);
                   17362:                                }
                   17363:                        }
                   17364:                        // do ret_far (pop cs/ip) in old mapping
                   17365:                }
                   17366:                break;
1.1.1.8   root     17367:        case 0x70:
                   17368:        case 0x71:
                   17369:        case 0x72:
                   17370:        case 0x73:
                   17371:        case 0x74:
                   17372:        case 0x75:
                   17373:        case 0x76:
                   17374:        case 0x77:
                   17375:                // EOI
                   17376:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   17377:                        pic[0].isr &= ~(1 << 2); // master
                   17378:                }
                   17379:                pic_update();
                   17380:                break;
1.1       root     17381:        default:
1.1.1.22  root     17382: //             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     17383:                break;
                   17384:        }
                   17385:        
                   17386:        // update cursor position
                   17387:        if(cursor_moved) {
1.1.1.36  root     17388:                pcbios_update_cursor_position();
1.1       root     17389:                cursor_moved = false;
                   17390:        }
                   17391: }
                   17392: 
                   17393: // init
                   17394: 
                   17395: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   17396: {
                   17397:        // init file handler
                   17398:        memset(file_handler, 0, sizeof(file_handler));
                   17399:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   17400:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   17401:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     17402: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17403:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     17404: #else
                   17405:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   17406: #endif
                   17407:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     17408:        }
1.1.1.21  root     17409:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     17410: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   17411:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     17412:        }
1.1       root     17413:        _dup2(0, DUP_STDIN);
                   17414:        _dup2(1, DUP_STDOUT);
                   17415:        _dup2(2, DUP_STDERR);
1.1.1.21  root     17416:        _dup2(3, DUP_STDAUX);
                   17417:        _dup2(4, DUP_STDPRN);
1.1       root     17418:        
1.1.1.24  root     17419:        // init mouse
                   17420:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     17421:        mouse.enabled = true;   // from DOSBox
                   17422:        mouse.hidden = 1;       // hidden in default ???
                   17423:        mouse.old_hidden = 1;   // from DOSBox
                   17424:        mouse.max_position.x = 8 * (scr_width  - 1);
                   17425:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     17426:        mouse.mickey.x = 8;
                   17427:        mouse.mickey.y = 16;
                   17428:        
1.1.1.26  root     17429: #ifdef SUPPORT_XMS
                   17430:        // init xms
                   17431:        msdos_xms_init();
                   17432: #endif
                   17433:        
1.1       root     17434:        // init process
                   17435:        memset(process, 0, sizeof(process));
                   17436:        
1.1.1.13  root     17437:        // init dtainfo
                   17438:        msdos_dta_info_init();
                   17439:        
1.1       root     17440:        // init memory
                   17441:        memset(mem, 0, sizeof(mem));
                   17442:        
                   17443:        // bios data area
1.1.1.23  root     17444:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     17445:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   17446:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     17447:        CONSOLE_FONT_INFO cfi;
                   17448:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   17449:        
                   17450:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   17451:        text_vram_top_address = TEXT_VRAM_TOP;
                   17452:        text_vram_end_address = text_vram_top_address + regen;
                   17453:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   17454:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     17455:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     17456:        
                   17457:        if(regen > 0x4000) {
                   17458:                regen = 0x8000;
                   17459:                vram_pages = 1;
                   17460:        } else if(regen > 0x2000) {
                   17461:                regen = 0x4000;
                   17462:                vram_pages = 2;
                   17463:        } else if(regen > 0x1000) {
                   17464:                regen = 0x2000;
                   17465:                vram_pages = 4;
                   17466:        } else {
                   17467:                regen = 0x1000;
                   17468:                vram_pages = 8;
                   17469:        }
1.1       root     17470:        
1.1.1.25  root     17471:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   17472:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     17473:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   17474:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     17475:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     17476:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   17477:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     17478: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17479:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     17480: #endif
1.1.1.26  root     17481:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     17482:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     17483:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   17484:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     17485:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     17486:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   17487:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     17488:        *(UINT16 *)(mem + 0x44e) = 0;
                   17489:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     17490:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     17491:        *(UINT8  *)(mem + 0x460) = 7;
                   17492:        *(UINT8  *)(mem + 0x461) = 7;
                   17493:        *(UINT8  *)(mem + 0x462) = 0;
                   17494:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     17495:        *(UINT8  *)(mem + 0x465) = 0x09;
                   17496:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     17497:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     17498:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   17499:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     17500:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   17501:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   17502:        *(UINT8  *)(mem + 0x487) = 0x60;
                   17503:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     17504: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17505:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     17506: #endif
1.1.1.14  root     17507:        
                   17508:        // initial screen
                   17509:        SMALL_RECT rect;
                   17510:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   17511:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   17512:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   17513:                for(int x = 0; x < scr_width; x++) {
                   17514:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   17515:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   17516:                }
                   17517:        }
1.1       root     17518:        
1.1.1.19  root     17519:        // init mcb
1.1       root     17520:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     17521:        
                   17522:        // iret table
                   17523:        // note: int 2eh vector should address the routine in command.com,
                   17524:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   17525:        // so move iret table into allocated memory block
                   17526:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     17527:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     17528:        IRET_TOP = seg << 4;
                   17529:        seg += IRET_SIZE >> 4;
1.1.1.25  root     17530:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     17531:        
                   17532:        // dummy xms/ems device
1.1.1.33  root     17533:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     17534:        XMS_TOP = seg << 4;
                   17535:        seg += XMS_SIZE >> 4;
                   17536:        
                   17537:        // environment
1.1.1.33  root     17538:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     17539:        int env_seg = seg;
                   17540:        int ofs = 0;
1.1.1.32  root     17541:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   17542:        char comspec_added = 0;
1.1.1.33  root     17543:        char lastdrive_added = 0;
1.1.1.32  root     17544:        char env_msdos_path[ENV_SIZE] = {0};
                   17545:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     17546:        char prompt_added = 0;
1.1.1.32  root     17547:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     17548:        char tz_added = 0;
1.1.1.45  root     17549:        const char *path, *short_path;
1.1.1.32  root     17550:        
                   17551:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   17552:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17553:                        strcpy(env_append, short_path);
                   17554:                }
                   17555:        }
                   17556:        if((path = getenv("APPEND")) != NULL) {
                   17557:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17558:                        if(env_append[0] != '\0') {
                   17559:                                strcat(env_append, ";");
                   17560:                        }
                   17561:                        strcat(env_append, short_path);
                   17562:                }
                   17563:        }
                   17564:        
                   17565:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   17566:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17567:                        strcpy(comspec_path, short_path);
                   17568:                }
                   17569:        }
                   17570:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   17571:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17572:                        strcpy(comspec_path, short_path);
                   17573:                }
                   17574:        }
1.1       root     17575:        
1.1.1.28  root     17576:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     17577:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17578:                        strcpy(env_msdos_path, short_path);
                   17579:                        strcpy(env_path, short_path);
1.1.1.14  root     17580:                }
                   17581:        }
1.1.1.28  root     17582:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     17583:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17584:                        if(env_path[0] != '\0') {
                   17585:                                strcat(env_path, ";");
                   17586:                        }
                   17587:                        strcat(env_path, short_path);
1.1.1.9   root     17588:                }
                   17589:        }
1.1.1.32  root     17590:        
                   17591:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   17592:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     17593:        }
1.1.1.32  root     17594:        for(int i = 0; i < 4; i++) {
                   17595:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   17596:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   17597:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17598:                                strcpy(env_temp, short_path);
                   17599:                                break;
                   17600:                        }
                   17601:                }
1.1.1.24  root     17602:        }
1.1.1.32  root     17603:        
1.1.1.9   root     17604:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     17605:                // lower to upper
1.1.1.28  root     17606:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     17607:                strcpy(tmp, *p);
                   17608:                for(int i = 0;; i++) {
                   17609:                        if(tmp[i] == '=') {
                   17610:                                tmp[i] = '\0';
                   17611:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     17612:                                my_strupr(name);
1.1       root     17613:                                tmp[i] = '=';
                   17614:                                break;
                   17615:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     17616:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     17617:                        }
                   17618:                }
1.1.1.33  root     17619:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   17620:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   17621:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     17622:                        // ignore non standard environments
                   17623:                } else {
1.1.1.33  root     17624:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     17625:                                if(env_append[0] != '\0') {
                   17626:                                        sprintf(tmp, "APPEND=%s", env_append);
                   17627:                                } else {
                   17628:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   17629:                                }
                   17630:                                append_added = 1;
                   17631:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     17632:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     17633:                                comspec_added = 1;
1.1.1.33  root     17634:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   17635:                                char *env = getenv("MSDOS_LASTDRIVE");
                   17636:                                if(env != NULL) {
                   17637:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   17638:                                }
                   17639:                                lastdrive_added = 1;
                   17640:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     17641:                                if(env_msdos_path[0] != '\0') {
                   17642:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   17643:                                } else {
                   17644:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   17645:                                }
1.1.1.33  root     17646:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     17647:                                if(env_path[0] != '\0') {
                   17648:                                        sprintf(tmp, "PATH=%s", env_path);
                   17649:                                } else {
                   17650:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   17651:                                }
1.1.1.32  root     17652:                                path_added = 1;
1.1.1.33  root     17653:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   17654:                                prompt_added = 1;
1.1.1.28  root     17655:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   17656:                                if(env_temp[0] != '\0') {
                   17657:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   17658:                                } else {
                   17659:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   17660:                                }
1.1.1.32  root     17661:                                temp_added = 1;
1.1.1.33  root     17662:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     17663:                                if(env_temp[0] != '\0') {
                   17664:                                        sprintf(tmp, "TMP=%s", env_temp);
                   17665:                                } else {
                   17666:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     17667:                                }
1.1.1.32  root     17668:                                tmp_added = 1;
1.1.1.33  root     17669:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   17670:                                char *env = getenv("MSDOS_TZ");
                   17671:                                if(env != NULL) {
                   17672:                                        sprintf(tmp, "TZ=%s", env);
                   17673:                                }
                   17674:                                tz_added = 1;
1.1       root     17675:                        }
                   17676:                        int len = strlen(tmp);
1.1.1.14  root     17677:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     17678:                                fatalerror("too many environments\n");
                   17679:                        }
                   17680:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   17681:                        ofs += len + 1;
                   17682:                }
                   17683:        }
1.1.1.32  root     17684:        if(!append_added && env_append[0] != '\0') {
                   17685:                #define SET_ENV(name, value) { \
                   17686:                        char tmp[ENV_SIZE]; \
                   17687:                        sprintf(tmp, "%s=%s", name, value); \
                   17688:                        int len = strlen(tmp); \
                   17689:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   17690:                                fatalerror("too many environments\n"); \
                   17691:                        } \
                   17692:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   17693:                        ofs += len + 1; \
                   17694:                }
                   17695:                SET_ENV("APPEND", env_append);
                   17696:        }
                   17697:        if(!comspec_added) {
                   17698:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   17699:        }
1.1.1.33  root     17700:        if(!lastdrive_added) {
                   17701:                SET_ENV("LASTDRIVE", "Z");
                   17702:        }
1.1.1.32  root     17703:        if(!path_added) {
                   17704:                SET_ENV("PATH", env_path);
                   17705:        }
1.1.1.33  root     17706:        if(!prompt_added) {
                   17707:                SET_ENV("PROMPT", "$P$G");
                   17708:        }
1.1.1.32  root     17709:        if(!temp_added) {
                   17710:                SET_ENV("TEMP", env_temp);
                   17711:        }
                   17712:        if(!tmp_added) {
                   17713:                SET_ENV("TMP", env_temp);
                   17714:        }
1.1.1.33  root     17715:        if(!tz_added) {
                   17716:                TIME_ZONE_INFORMATION tzi;
                   17717:                HKEY hKey, hSubKey;
                   17718:                char tzi_std_name[64];
                   17719:                char tz_std[8] = "GMT";
                   17720:                char tz_dlt[8] = "GST";
                   17721:                char tz_value[32];
                   17722:                
                   17723:                // timezone name from GetTimeZoneInformation may not be english
                   17724:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   17725:                setlocale(LC_CTYPE, "");
                   17726:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   17727:                
                   17728:                // get english timezone name from registry
                   17729:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   17730:                        for(DWORD i = 0; !tz_added; i++) {
                   17731:                                char reg_name[256], sub_key[1024], std_name[256];
                   17732:                                DWORD size;
                   17733:                                FILETIME ftTime;
                   17734:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   17735:                                
                   17736:                                if(result == ERROR_SUCCESS) {
                   17737:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   17738:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   17739:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   17740:                                                        // search english timezone name from table
1.1.1.37  root     17741:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     17742:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   17743:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   17744:                                                                                if(tz_table[j].std != NULL) {
                   17745:                                                                                        strcpy(tz_std, tz_table[j].std);
                   17746:                                                                                }
                   17747:                                                                                if(tz_table[j].dlt != NULL) {
                   17748:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   17749:                                                                                }
                   17750:                                                                                tz_added = 1;
                   17751:                                                                                break;
                   17752:                                                                        }
                   17753:                                                                }
                   17754:                                                        }
                   17755:                                                }
                   17756:                                                RegCloseKey(hSubKey);
                   17757:                                        }
                   17758:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   17759:                                        break;
                   17760:                                }
                   17761:                        }
                   17762:                        RegCloseKey(hKey);
                   17763:                }
                   17764:                if((tzi.Bias % 60) != 0) {
                   17765:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   17766:                } else {
                   17767:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   17768:                }
                   17769:                if(daylight) {
                   17770:                        strcat(tz_value, tz_dlt);
                   17771:                }
                   17772:                SET_ENV("TZ", tz_value);
                   17773:        }
1.1       root     17774:        seg += (ENV_SIZE >> 4);
                   17775:        
                   17776:        // psp
1.1.1.33  root     17777:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     17778:        current_psp = seg;
1.1.1.35  root     17779:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   17780:        psp->parent_psp = current_psp;
1.1       root     17781:        seg += (PSP_SIZE >> 4);
                   17782:        
1.1.1.19  root     17783:        // first free mcb in conventional memory
1.1.1.33  root     17784:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     17785:        first_mcb = seg;
                   17786:        
1.1.1.19  root     17787:        // dummy mcb to link to umb
1.1.1.33  root     17788: #if 0
1.1.1.39  root     17789:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     17790: #else
1.1.1.39  root     17791:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     17792: #endif
1.1.1.19  root     17793:        
                   17794:        // first free mcb in upper memory block
1.1.1.8   root     17795:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     17796:        
1.1.1.29  root     17797: #ifdef SUPPORT_HMA
                   17798:        // first free mcb in high memory area
                   17799:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   17800: #endif
                   17801:        
1.1.1.26  root     17802:        // interrupt vector
                   17803:        for(int i = 0; i < 0x80; i++) {
                   17804:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   17805:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   17806:        }
1.1.1.49  root     17807:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   17808:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17809:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   17810:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   17811:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   17812:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     17813:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   17814:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17815:        
1.1.1.29  root     17816:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     17817:        static const struct {
                   17818:                UINT16 attributes;
                   17819:                char *dev_name;
                   17820:        } dummy_devices[] = {
                   17821:                {0x8013, "CON     "},
                   17822:                {0x8000, "AUX     "},
                   17823:                {0xa0c0, "PRN     "},
                   17824:                {0x8008, "CLOCK$  "},
                   17825:                {0x8000, "COM1    "},
                   17826:                {0xa0c0, "LPT1    "},
                   17827:                {0xa0c0, "LPT2    "},
                   17828:                {0xa0c0, "LPT3    "},
                   17829:                {0x8000, "COM2    "},
                   17830:                {0x8000, "COM3    "},
                   17831:                {0x8000, "COM4    "},
1.1.1.30  root     17832: //             {0xc000, "CONFIG$ "},
                   17833:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     17834:        };
                   17835:        static const UINT8 dummy_device_routine[] = {
                   17836:                // from NUL device of Windows 98 SE
                   17837:                // or word ptr ES:[BX+03],0100
                   17838:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   17839:                // retf
                   17840:                0xcb,
                   17841:        };
1.1.1.29  root     17842:        device_t *last = NULL;
1.1.1.32  root     17843:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     17844:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     17845:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   17846:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17847:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     17848:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   17849:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     17850:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     17851:                last = device;
                   17852:        }
                   17853:        if(last != NULL) {
                   17854:                last->next_driver.w.l = 0;
                   17855:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     17856:        }
1.1.1.29  root     17857:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     17858:        
1.1.1.25  root     17859:        // dos info
                   17860:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   17861:        dos_info->magic_word = 1;
                   17862:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   17863:        dos_info->first_dpb.w.l = 0;
                   17864:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   17865:        dos_info->first_sft.w.l = 0;
                   17866:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     17867:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     17868:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17869:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     17870:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   17871:        dos_info->max_sector_len = 512;
                   17872:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   17873:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   17874:        dos_info->cds.w.l = 0;
                   17875:        dos_info->cds.w.h = CDS_TOP >> 4;
                   17876:        dos_info->fcb_table.w.l = 0;
                   17877:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   17878:        dos_info->last_drive = 'Z' - 'A' + 1;
                   17879:        dos_info->buffers_x = 20;
                   17880:        dos_info->buffers_y = 0;
                   17881:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     17882:        dos_info->nul_device.next_driver.w.l = 22;
                   17883:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     17884:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     17885:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   17886:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17887:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   17888:        dos_info->disk_buf_heads.w.l = 0;
                   17889:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     17890:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     17891:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   17892:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     17893:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     17894:        
                   17895:        char *env;
                   17896:        if((env = getenv("LASTDRIVE")) != NULL) {
                   17897:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17898:                        dos_info->last_drive = env[0] - 'A' + 1;
                   17899:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17900:                        dos_info->last_drive = env[0] - 'a' + 1;
                   17901:                }
                   17902:        }
                   17903:        if((env = getenv("windir")) != NULL) {
                   17904:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17905:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   17906:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17907:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   17908:                }
                   17909:        }
                   17910: #if defined(HAS_I386)
                   17911:        dos_info->i386_or_later = 1;
                   17912: #else
                   17913:        dos_info->i386_or_later = 0;
                   17914: #endif
                   17915:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   17916:        
1.1.1.27  root     17917:        // ems (int 67h) and xms
1.1.1.25  root     17918:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   17919:        xms_device->next_driver.w.l = 0xffff;
                   17920:        xms_device->next_driver.w.h = 0xffff;
                   17921:        xms_device->attributes = 0xc000;
1.1.1.29  root     17922:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   17923:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17924:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   17925:        
1.1.1.26  root     17926:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   17927:        mem[XMS_TOP + 0x13] = 0x68;
                   17928:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     17929: #ifdef SUPPORT_XMS
                   17930:        if(support_xms) {
1.1.1.26  root     17931:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   17932:                mem[XMS_TOP + 0x16] = 0x69;
                   17933:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     17934:        } else
                   17935: #endif
1.1.1.26  root     17936:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     17937:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     17938:        
1.1.1.26  root     17939:        // irq12 routine (mouse)
1.1.1.49  root     17940:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 6ah (dummy)
                   17941:        mem[DUMMY_TOP + 0x01] = 0x6a;
                   17942:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   17943:        mem[DUMMY_TOP + 0x03] = 0xff;
                   17944:        mem[DUMMY_TOP + 0x04] = 0xff;
                   17945:        mem[DUMMY_TOP + 0x05] = 0xff;
                   17946:        mem[DUMMY_TOP + 0x06] = 0xff;
                   17947:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17948:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   17949:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     17950:        
1.1.1.27  root     17951:        // case map routine
1.1.1.49  root     17952:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   17953:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   17954:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     17955:        
                   17956:        // font read routine
1.1.1.49  root     17957:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   17958:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   17959:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     17960:        
1.1.1.32  root     17961:        // error message read routine
1.1.1.49  root     17962:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   17963:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   17964:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     17965:        
1.1.1.35  root     17966:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     17967:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   17968:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   17969:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   17970:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   17971:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     17972:        
1.1.1.50  root     17973:        // irq0 routine (system timer)
1.1.1.49  root     17974:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   17975:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   17976:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   17977:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   17978:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   17979:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   17980:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   17981:        
                   17982:        // alter page map and call routine
                   17983:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   17984:        mem[DUMMY_TOP + 0x20] = 0xff;
                   17985:        mem[DUMMY_TOP + 0x21] = 0xff;
                   17986:        mem[DUMMY_TOP + 0x22] = 0xff;
                   17987:        mem[DUMMY_TOP + 0x23] = 0xff;
                   17988:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   17989:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   17990:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     17991:        
1.1.1.50  root     17992:        // call int 29h routine
                   17993:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   17994:        mem[DUMMY_TOP + 0x28] = 0x29;
                   17995:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   17996:        
1.1.1.26  root     17997:        // boot routine
1.1.1.49  root     17998:        mem[0xffff0 + 0x00] = 0xf4;     // halt
                   17999:        mem[0xffff0 + 0x05] = '0';      // rom date
                   18000:        mem[0xffff0 + 0x06] = '2';
                   18001:        mem[0xffff0 + 0x07] = '/';
                   18002:        mem[0xffff0 + 0x08] = '2';
                   18003:        mem[0xffff0 + 0x09] = '2';
                   18004:        mem[0xffff0 + 0x0a] = '/';
                   18005:        mem[0xffff0 + 0x0b] = '0';
                   18006:        mem[0xffff0 + 0x0c] = '6';
                   18007:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id
                   18008:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.24  root     18009:        
1.1       root     18010:        // param block
                   18011:        // + 0: param block (22bytes)
                   18012:        // +24: fcb1/2 (20bytes)
                   18013:        // +44: command tail (128bytes)
                   18014:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18015:        param->env_seg = 0;
                   18016:        param->cmd_line.w.l = 44;
                   18017:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18018:        param->fcb1.w.l = 24;
                   18019:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18020:        param->fcb2.w.l = 24;
                   18021:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18022:        
                   18023:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18024:        
                   18025:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18026:        if(argc > 1) {
                   18027:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18028:                for(int i = 2; i < argc; i++) {
                   18029:                        char tmp[128];
                   18030:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18031:                        strcpy(cmd_line->cmd, tmp);
                   18032:                }
                   18033:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18034:        } else {
                   18035:                cmd_line->len = 0;
                   18036:        }
                   18037:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18038:        
                   18039:        // system file table
1.1.1.21  root     18040:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18041:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18042:        
1.1.1.19  root     18043:        // disk buffer header (from DOSBox)
                   18044:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18045:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18046:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18047:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18048:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18049:        
1.1       root     18050:        // fcb table
                   18051:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18052:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18053:        
1.1.1.41  root     18054:        // drive parameter block
1.1.1.42  root     18055:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18056:                // may be a floppy drive
1.1.1.44  root     18057:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18058:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18059:                cds->drive_attrib = 0x4000;     // physical drive
                   18060:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18061:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18062:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18063:                cds->bs_offset = 2;
                   18064:                
1.1.1.41  root     18065:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18066:                dpb->drive_num = i;
                   18067:                dpb->unit_num = i;
1.1.1.43  root     18068:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18069:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18070:        }
                   18071:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18072:                msdos_cds_update(i);
1.1.1.42  root     18073:                UINT16 seg, ofs;
                   18074:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18075:        }
                   18076:        
1.1.1.17  root     18077:        // nls stuff
                   18078:        msdos_nls_tables_init();
1.1       root     18079:        
                   18080:        // execute command
1.1.1.28  root     18081:        try {
1.1.1.52  root     18082:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18083:                        fatalerror("'%s' not found\n", argv[0]);
                   18084:                }
                   18085:        } catch(...) {
                   18086:                // we should not reach here :-(
                   18087:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18088:        }
                   18089:        retval = 0;
                   18090:        return(0);
                   18091: }
                   18092: 
                   18093: #define remove_std_file(path) { \
                   18094:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18095:        if(fd != -1) { \
                   18096:                _lseek(fd, 0, SEEK_END); \
                   18097:                int size = _tell(fd); \
                   18098:                _close(fd); \
                   18099:                if(size == 0) { \
                   18100:                        remove(path); \
                   18101:                } \
                   18102:        } \
                   18103: }
                   18104: 
                   18105: void msdos_finish()
                   18106: {
                   18107:        for(int i = 0; i < MAX_FILES; i++) {
                   18108:                if(file_handler[i].valid) {
                   18109:                        _close(i);
                   18110:                }
                   18111:        }
1.1.1.21  root     18112: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18113:        remove_std_file("stdaux.txt");
1.1.1.21  root     18114: #endif
1.1.1.30  root     18115: #ifdef SUPPORT_XMS
                   18116:        msdos_xms_finish();
                   18117: #endif
1.1       root     18118:        msdos_dbcs_table_finish();
                   18119: }
                   18120: 
                   18121: /* ----------------------------------------------------------------------------
                   18122:        PC/AT hardware emulation
                   18123: ---------------------------------------------------------------------------- */
                   18124: 
                   18125: void hardware_init()
                   18126: {
1.1.1.3   root     18127:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18128:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18129:        m_IF = 1;
1.1.1.3   root     18130: #if defined(HAS_I386)
1.1       root     18131:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18132:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18133: #endif
                   18134:        i386_set_a20_line(0);
1.1.1.14  root     18135:        
1.1.1.19  root     18136:        ems_init();
1.1.1.25  root     18137:        dma_init();
1.1       root     18138:        pic_init();
1.1.1.25  root     18139:        pio_init();
1.1.1.8   root     18140: #ifdef PIT_ALWAYS_RUNNING
                   18141:        pit_init();
                   18142: #else
1.1       root     18143:        pit_active = 0;
                   18144: #endif
1.1.1.25  root     18145:        sio_init();
1.1.1.8   root     18146:        cmos_init();
                   18147:        kbd_init();
1.1       root     18148: }
                   18149: 
1.1.1.10  root     18150: void hardware_finish()
                   18151: {
                   18152: #if defined(HAS_I386)
                   18153:        vtlb_free(m_vtlb);
                   18154: #endif
1.1.1.19  root     18155:        ems_finish();
1.1.1.37  root     18156:        pio_finish();
1.1.1.25  root     18157:        sio_finish();
1.1.1.10  root     18158: }
                   18159: 
1.1.1.28  root     18160: void hardware_release()
                   18161: {
                   18162:        // release hardware resources when this program will be terminated abnormally
                   18163: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18164:        if(fp_debug_log != NULL) {
                   18165:                fclose(fp_debug_log);
                   18166:                fp_debug_log = NULL;
1.1.1.28  root     18167:        }
                   18168: #endif
                   18169: #if defined(HAS_I386)
                   18170:        vtlb_free(m_vtlb);
                   18171: #endif
                   18172:        ems_release();
1.1.1.37  root     18173:        pio_release();
1.1.1.28  root     18174:        sio_release();
                   18175: }
                   18176: 
1.1       root     18177: void hardware_run()
                   18178: {
1.1.1.22  root     18179: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18180:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18181:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18182: #endif
1.1.1.51  root     18183: #ifdef USE_DEBUGGER
                   18184:        m_int_num = -1;
                   18185: #endif
1.1.1.54  root     18186:        while(!m_exit) {
1.1.1.50  root     18187:                hardware_run_cpu();
1.1       root     18188:        }
1.1.1.22  root     18189: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18190:        if(fp_debug_log != NULL) {
                   18191:                fclose(fp_debug_log);
                   18192:                fp_debug_log = NULL;
1.1.1.28  root     18193:        }
1.1.1.22  root     18194: #endif
1.1       root     18195: }
                   18196: 
1.1.1.50  root     18197: inline void hardware_run_cpu()
                   18198: {
                   18199: #if defined(HAS_I386)
                   18200:        CPU_EXECUTE_CALL(i386);
                   18201:        if(m_eip != m_prev_eip) {
                   18202:                idle_ops++;
                   18203:        }
                   18204: #else
                   18205:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18206:        if(m_pc != m_prevpc) {
                   18207:                idle_ops++;
                   18208:        }
                   18209: #endif
                   18210: #ifdef USE_DEBUGGER
                   18211:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18212:        if(m_int_num >= 0) {
                   18213:                unsigned num = (unsigned)m_int_num;
                   18214:                m_int_num = -1;
                   18215:                msdos_syscall(num);
                   18216:        }
                   18217: #endif
                   18218:        if(++update_ops == UPDATE_OPS) {
                   18219:                update_ops = 0;
                   18220:                hardware_update();
                   18221:        }
                   18222: }
                   18223: 
1.1       root     18224: void hardware_update()
                   18225: {
1.1.1.8   root     18226:        static UINT32 prev_time = 0;
                   18227:        UINT32 cur_time = timeGetTime();
                   18228:        
                   18229:        if(prev_time != cur_time) {
                   18230:                // update pit and raise irq0
                   18231: #ifndef PIT_ALWAYS_RUNNING
                   18232:                if(pit_active)
                   18233: #endif
                   18234:                {
                   18235:                        if(pit_run(0, cur_time)) {
                   18236:                                pic_req(0, 0, 1);
                   18237:                        }
                   18238:                        pit_run(1, cur_time);
                   18239:                        pit_run(2, cur_time);
                   18240:                }
1.1.1.24  root     18241:                
1.1.1.25  root     18242:                // update sio and raise irq4/3
1.1.1.29  root     18243:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18244:                        sio_update(c);
                   18245:                }
                   18246:                
1.1.1.24  root     18247:                // update keyboard and mouse
1.1.1.14  root     18248:                static UINT32 prev_tick = 0;
                   18249:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     18250:                
1.1.1.14  root     18251:                if(prev_tick != cur_tick) {
                   18252:                        // update keyboard flags
                   18253:                        UINT8 state;
1.1.1.24  root     18254:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   18255:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   18256:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   18257:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   18258:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   18259:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   18260:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   18261:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     18262:                        mem[0x417] = state;
                   18263:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   18264:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   18265:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   18266:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     18267: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   18268: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     18269:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   18270:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   18271:                        mem[0x418] = state;
                   18272:                        
1.1.1.24  root     18273:                        // update console input if needed
1.1.1.34  root     18274:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     18275:                                update_console_input();
                   18276:                        }
1.1.1.55! root     18277:                        key_changed = false;
1.1.1.24  root     18278:                        
1.1.1.55! root     18279:                        // raise irq1 if key buffer is not empty
        !          18280: #ifdef USE_SERVICE_THREAD
        !          18281:                        EnterCriticalSection(&key_buf_crit_sect);
        !          18282: #endif
        !          18283:                        bool empty = pcbios_is_key_buffer_empty();
        !          18284: #ifdef USE_SERVICE_THREAD
        !          18285:                        LeaveCriticalSection(&key_buf_crit_sect);
        !          18286: #endif
        !          18287:                        if(!empty) {
1.1.1.8   root     18288:                                pic_req(0, 1, 1);
1.1.1.24  root     18289:                        }
                   18290:                        
                   18291:                        // raise irq12 if mouse status is changed
1.1.1.54  root     18292:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw) {
                   18293:                                mouse.status_irq = 0; // ???
                   18294:                                mouse.status_irq_alt = 0; // ???
                   18295:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
                   18296:                                mouse.status = 0;
                   18297:                                pic_req(1, 4, 1);
                   18298:                        } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     18299:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   18300:                                mouse.status_irq_alt = 0; // ???
1.1.1.54  root     18301:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     18302:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     18303:                                pic_req(1, 4, 1);
                   18304:                        } else {
                   18305:                                for(int i = 0; i < 8; i++) {
                   18306:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   18307:                                                mouse.status_irq = 0; // ???
                   18308:                                                mouse.status_irq_alt = 0;
1.1.1.54  root     18309:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     18310:                                                for(int j = 0; j < 8; j++) {
                   18311:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   18312:                                                                mouse.status_irq_alt |=  (1 << j);
                   18313:                                                                mouse.status_alt     &= ~(1 << j);
                   18314:                                                        }
                   18315:                                                }
                   18316:                                                pic_req(1, 4, 1);
                   18317:                                                break;
                   18318:                                        }
                   18319:                                }
1.1.1.8   root     18320:                        }
1.1.1.24  root     18321:                        
1.1.1.14  root     18322:                        prev_tick = cur_tick;
1.1.1.8   root     18323:                }
1.1.1.24  root     18324:                
1.1.1.19  root     18325:                // update daily timer counter
                   18326:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     18327:                
1.1.1.8   root     18328:                prev_time = cur_time;
1.1       root     18329:        }
                   18330: }
                   18331: 
1.1.1.19  root     18332: // ems
                   18333: 
                   18334: void ems_init()
                   18335: {
                   18336:        memset(ems_handles, 0, sizeof(ems_handles));
                   18337:        memset(ems_pages, 0, sizeof(ems_pages));
                   18338:        free_ems_pages = MAX_EMS_PAGES;
                   18339: }
                   18340: 
                   18341: void ems_finish()
                   18342: {
1.1.1.28  root     18343:        ems_release();
                   18344: }
                   18345: 
                   18346: void ems_release()
                   18347: {
1.1.1.31  root     18348:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     18349:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     18350:                        free(ems_handles[i].buffer);
                   18351:                        ems_handles[i].buffer = NULL;
                   18352:                }
                   18353:        }
                   18354: }
                   18355: 
                   18356: void ems_allocate_pages(int handle, int pages)
                   18357: {
                   18358:        if(pages > 0) {
                   18359:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18360:        } else {
                   18361:                ems_handles[handle].buffer = NULL;
                   18362:        }
                   18363:        ems_handles[handle].pages = pages;
                   18364:        ems_handles[handle].allocated = true;
                   18365:        free_ems_pages -= pages;
                   18366: }
                   18367: 
                   18368: void ems_reallocate_pages(int handle, int pages)
                   18369: {
                   18370:        if(ems_handles[handle].allocated) {
                   18371:                if(ems_handles[handle].pages != pages) {
                   18372:                        UINT8 *new_buffer = NULL;
                   18373:                        
                   18374:                        if(pages > 0) {
                   18375:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18376:                        }
1.1.1.32  root     18377:                        if(ems_handles[handle].buffer != NULL) {
                   18378:                                if(new_buffer != NULL) {
1.1.1.19  root     18379:                                        if(pages > ems_handles[handle].pages) {
                   18380:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   18381:                                        } else {
                   18382:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   18383:                                        }
                   18384:                                }
                   18385:                                free(ems_handles[handle].buffer);
                   18386:                                ems_handles[handle].buffer = NULL;
                   18387:                        }
                   18388:                        free_ems_pages += ems_handles[handle].pages;
                   18389:                        
                   18390:                        ems_handles[handle].buffer = new_buffer;
                   18391:                        ems_handles[handle].pages = pages;
                   18392:                        free_ems_pages -= pages;
                   18393:                }
                   18394:        } else {
                   18395:                ems_allocate_pages(handle, pages);
                   18396:        }
                   18397: }
                   18398: 
                   18399: void ems_release_pages(int handle)
                   18400: {
                   18401:        if(ems_handles[handle].allocated) {
1.1.1.32  root     18402:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     18403:                        free(ems_handles[handle].buffer);
                   18404:                        ems_handles[handle].buffer = NULL;
                   18405:                }
                   18406:                free_ems_pages += ems_handles[handle].pages;
                   18407:                ems_handles[handle].allocated = false;
                   18408:        }
                   18409: }
                   18410: 
                   18411: void ems_map_page(int physical, int handle, int logical)
                   18412: {
                   18413:        if(ems_pages[physical].mapped) {
                   18414:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   18415:                        return;
                   18416:                }
                   18417:                ems_unmap_page(physical);
                   18418:        }
1.1.1.32  root     18419:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18420:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   18421:        }
                   18422:        ems_pages[physical].handle = handle;
                   18423:        ems_pages[physical].page = logical;
                   18424:        ems_pages[physical].mapped = true;
                   18425: }
                   18426: 
                   18427: void ems_unmap_page(int physical)
                   18428: {
                   18429:        if(ems_pages[physical].mapped) {
                   18430:                int handle = ems_pages[physical].handle;
                   18431:                int logical = ems_pages[physical].page;
                   18432:                
1.1.1.32  root     18433:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18434:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   18435:                }
                   18436:                ems_pages[physical].mapped = false;
                   18437:        }
                   18438: }
                   18439: 
1.1.1.25  root     18440: // dma
1.1       root     18441: 
1.1.1.25  root     18442: void dma_init()
1.1       root     18443: {
1.1.1.26  root     18444:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     18445:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     18446: //             for(int ch = 0; ch < 4; ch++) {
                   18447: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   18448: //             }
1.1.1.25  root     18449:                dma_reset(c);
                   18450:        }
1.1       root     18451: }
                   18452: 
1.1.1.25  root     18453: void dma_reset(int c)
1.1       root     18454: {
1.1.1.25  root     18455:        dma[c].low_high = false;
                   18456:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   18457:        dma[c].mask = 0xff;
                   18458: }
                   18459: 
                   18460: void dma_write(int c, UINT32 addr, UINT8 data)
                   18461: {
                   18462:        int ch = (addr >> 1) & 3;
                   18463:        UINT8 bit = 1 << (data & 3);
                   18464:        
                   18465:        switch(addr & 0x0f) {
                   18466:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18467:                if(dma[c].low_high) {
                   18468:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     18469:                } else {
1.1.1.25  root     18470:                        dma[c].ch[ch].bareg.b.l = data;
                   18471:                }
                   18472:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18473:                dma[c].low_high = !dma[c].low_high;
                   18474:                break;
                   18475:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18476:                if(dma[c].low_high) {
                   18477:                        dma[c].ch[ch].bcreg.b.h = data;
                   18478:                } else {
                   18479:                        dma[c].ch[ch].bcreg.b.l = data;
                   18480:                }
                   18481:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18482:                dma[c].low_high = !dma[c].low_high;
                   18483:                break;
                   18484:        case 0x08:
                   18485:                // command register
                   18486:                dma[c].cmd = data;
                   18487:                break;
                   18488:        case 0x09:
                   18489:                // dma[c].request register
                   18490:                if(data & 4) {
                   18491:                        if(!(dma[c].req & bit)) {
                   18492:                                dma[c].req |= bit;
                   18493: //                             dma_run(c, ch);
                   18494:                        }
                   18495:                } else {
                   18496:                        dma[c].req &= ~bit;
                   18497:                }
                   18498:                break;
                   18499:        case 0x0a:
                   18500:                // single mask register
                   18501:                if(data & 4) {
                   18502:                        dma[c].mask |= bit;
                   18503:                } else {
                   18504:                        dma[c].mask &= ~bit;
                   18505:                }
                   18506:                break;
                   18507:        case 0x0b:
                   18508:                // mode register
                   18509:                dma[c].ch[data & 3].mode = data;
                   18510:                break;
                   18511:        case 0x0c:
                   18512:                dma[c].low_high = false;
                   18513:                break;
                   18514:        case 0x0d:
                   18515:                // clear master
                   18516:                dma_reset(c);
                   18517:                break;
                   18518:        case 0x0e:
                   18519:                // clear mask register
                   18520:                dma[c].mask = 0;
                   18521:                break;
                   18522:        case 0x0f:
                   18523:                // all mask register
                   18524:                dma[c].mask = data & 0x0f;
                   18525:                break;
                   18526:        }
                   18527: }
                   18528: 
                   18529: UINT8 dma_read(int c, UINT32 addr)
                   18530: {
                   18531:        int ch = (addr >> 1) & 3;
                   18532:        UINT8 val = 0xff;
                   18533:        
                   18534:        switch(addr & 0x0f) {
                   18535:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18536:                if(dma[c].low_high) {
                   18537:                        val = dma[c].ch[ch].areg.b.h;
                   18538:                } else {
                   18539:                        val = dma[c].ch[ch].areg.b.l;
                   18540:                }
                   18541:                dma[c].low_high = !dma[c].low_high;
                   18542:                return(val);
                   18543:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18544:                if(dma[c].low_high) {
                   18545:                        val = dma[c].ch[ch].creg.b.h;
                   18546:                } else {
                   18547:                        val = dma[c].ch[ch].creg.b.l;
                   18548:                }
                   18549:                dma[c].low_high = !dma[c].low_high;
                   18550:                return(val);
                   18551:        case 0x08:
                   18552:                // status register
                   18553:                val = (dma[c].req << 4) | dma[c].tc;
                   18554:                dma[c].tc = 0;
                   18555:                return(val);
                   18556:        case 0x0d:
1.1.1.26  root     18557:                // temporary register (intel 82374 does not support)
1.1.1.25  root     18558:                return(dma[c].tmp & 0xff);
1.1.1.26  root     18559:        case 0x0f:
                   18560:                // mask register (intel 82374 does support)
                   18561:                return(dma[c].mask);
1.1.1.25  root     18562:        }
                   18563:        return(0xff);
                   18564: }
                   18565: 
                   18566: void dma_page_write(int c, int ch, UINT8 data)
                   18567: {
                   18568:        dma[c].ch[ch].pagereg = data;
                   18569: }
                   18570: 
                   18571: UINT8 dma_page_read(int c, int ch)
                   18572: {
                   18573:        return(dma[c].ch[ch].pagereg);
                   18574: }
                   18575: 
                   18576: void dma_run(int c, int ch)
                   18577: {
                   18578:        UINT8 bit = 1 << ch;
                   18579:        
                   18580:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   18581:                // execute dma
                   18582:                while(dma[c].req & bit) {
                   18583:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   18584:                                // memory -> memory
                   18585:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   18586:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   18587:                                
                   18588:                                if(c == 0) {
                   18589:                                        dma[c].tmp = read_byte(saddr);
                   18590:                                        write_byte(daddr, dma[c].tmp);
                   18591:                                } else {
                   18592:                                        dma[c].tmp = read_word(saddr << 1);
                   18593:                                        write_word(daddr << 1, dma[c].tmp);
                   18594:                                }
                   18595:                                if(!(dma[c].cmd & 0x02)) {
                   18596:                                        if(dma[c].ch[0].mode & 0x20) {
                   18597:                                                dma[c].ch[0].areg.w--;
                   18598:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   18599:                                                        dma[c].ch[0].pagereg--;
                   18600:                                                }
                   18601:                                        } else {
                   18602:                                                dma[c].ch[0].areg.w++;
                   18603:                                                if(dma[c].ch[0].areg.w == 0) {
                   18604:                                                        dma[c].ch[0].pagereg++;
                   18605:                                                }
                   18606:                                        }
                   18607:                                }
                   18608:                                if(dma[c].ch[1].mode & 0x20) {
                   18609:                                        dma[c].ch[1].areg.w--;
                   18610:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   18611:                                                dma[c].ch[1].pagereg--;
                   18612:                                        }
                   18613:                                } else {
                   18614:                                        dma[c].ch[1].areg.w++;
                   18615:                                        if(dma[c].ch[1].areg.w == 0) {
                   18616:                                                dma[c].ch[1].pagereg++;
                   18617:                                        }
                   18618:                                }
                   18619:                                
                   18620:                                // check dma condition
                   18621:                                if(dma[c].ch[0].creg.w-- == 0) {
                   18622:                                        if(dma[c].ch[0].mode & 0x10) {
                   18623:                                                // self initialize
                   18624:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   18625:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   18626:                                        } else {
                   18627: //                                             dma[c].mask |= bit;
                   18628:                                        }
                   18629:                                }
                   18630:                                if(dma[c].ch[1].creg.w-- == 0) {
                   18631:                                        // terminal count
                   18632:                                        if(dma[c].ch[1].mode & 0x10) {
                   18633:                                                // self initialize
                   18634:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   18635:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   18636:                                        } else {
                   18637:                                                dma[c].mask |= bit;
                   18638:                                        }
                   18639:                                        dma[c].req &= ~bit;
                   18640:                                        dma[c].tc |= bit;
                   18641:                                }
                   18642:                        } else {
                   18643:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   18644:                                
                   18645:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   18646:                                        // verify
                   18647:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   18648:                                        // io -> memory
                   18649:                                        if(c == 0) {
                   18650:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   18651:                                                write_byte(addr, dma[c].tmp);
                   18652:                                        } else {
                   18653:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   18654:                                                write_word(addr << 1, dma[c].tmp);
                   18655:                                        }
                   18656:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   18657:                                        // memory -> io
                   18658:                                        if(c == 0) {
                   18659:                                                dma[c].tmp = read_byte(addr);
                   18660:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   18661:                                        } else {
                   18662:                                                dma[c].tmp = read_word(addr << 1);
                   18663:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   18664:                                        }
                   18665:                                }
                   18666:                                if(dma[c].ch[ch].mode & 0x20) {
                   18667:                                        dma[c].ch[ch].areg.w--;
                   18668:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   18669:                                                dma[c].ch[ch].pagereg--;
                   18670:                                        }
                   18671:                                } else {
                   18672:                                        dma[c].ch[ch].areg.w++;
                   18673:                                        if(dma[c].ch[ch].areg.w == 0) {
                   18674:                                                dma[c].ch[ch].pagereg++;
                   18675:                                        }
                   18676:                                }
                   18677:                                
                   18678:                                // check dma condition
                   18679:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   18680:                                        // terminal count
                   18681:                                        if(dma[c].ch[ch].mode & 0x10) {
                   18682:                                                // self initialize
                   18683:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18684:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18685:                                        } else {
                   18686:                                                dma[c].mask |= bit;
                   18687:                                        }
                   18688:                                        dma[c].req &= ~bit;
                   18689:                                        dma[c].tc |= bit;
                   18690:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   18691:                                        // single mode
                   18692:                                        break;
                   18693:                                }
                   18694:                        }
                   18695:                }
                   18696:        }
                   18697: }
                   18698: 
                   18699: // pic
                   18700: 
                   18701: void pic_init()
                   18702: {
                   18703:        memset(pic, 0, sizeof(pic));
                   18704:        pic[0].imr = pic[1].imr = 0xff;
                   18705:        
                   18706:        // from bochs bios
                   18707:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   18708:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   18709:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   18710:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   18711:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   18712:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   18713:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   18714:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   18715:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   18716: }
                   18717: 
                   18718: void pic_write(int c, UINT32 addr, UINT8 data)
                   18719: {
                   18720:        if(addr & 1) {
                   18721:                if(pic[c].icw2_r) {
                   18722:                        // icw2
                   18723:                        pic[c].icw2 = data;
                   18724:                        pic[c].icw2_r = 0;
                   18725:                } else if(pic[c].icw3_r) {
                   18726:                        // icw3
                   18727:                        pic[c].icw3 = data;
                   18728:                        pic[c].icw3_r = 0;
                   18729:                } else if(pic[c].icw4_r) {
                   18730:                        // icw4
                   18731:                        pic[c].icw4 = data;
                   18732:                        pic[c].icw4_r = 0;
                   18733:                } else {
                   18734:                        // ocw1
1.1       root     18735:                        pic[c].imr = data;
                   18736:                }
                   18737:        } else {
                   18738:                if(data & 0x10) {
                   18739:                        // icw1
                   18740:                        pic[c].icw1 = data;
                   18741:                        pic[c].icw2_r = 1;
                   18742:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   18743:                        pic[c].icw4_r = data & 1;
                   18744:                        pic[c].irr = 0;
                   18745:                        pic[c].isr = 0;
                   18746:                        pic[c].imr = 0;
                   18747:                        pic[c].prio = 0;
                   18748:                        if(!(pic[c].icw1 & 1)) {
                   18749:                                pic[c].icw4 = 0;
                   18750:                        }
                   18751:                        pic[c].ocw3 = 0;
                   18752:                } else if(data & 8) {
                   18753:                        // ocw3
                   18754:                        if(!(data & 2)) {
                   18755:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   18756:                        }
                   18757:                        if(!(data & 0x40)) {
                   18758:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   18759:                        }
                   18760:                        pic[c].ocw3 = data;
                   18761:                } else {
                   18762:                        // ocw2
                   18763:                        int level = 0;
                   18764:                        if(data & 0x40) {
                   18765:                                level = data & 7;
                   18766:                        } else {
                   18767:                                if(!pic[c].isr) {
                   18768:                                        return;
                   18769:                                }
                   18770:                                level = pic[c].prio;
                   18771:                                while(!(pic[c].isr & (1 << level))) {
                   18772:                                        level = (level + 1) & 7;
                   18773:                                }
                   18774:                        }
                   18775:                        if(data & 0x80) {
                   18776:                                pic[c].prio = (level + 1) & 7;
                   18777:                        }
                   18778:                        if(data & 0x20) {
                   18779:                                pic[c].isr &= ~(1 << level);
                   18780:                        }
                   18781:                }
                   18782:        }
                   18783:        pic_update();
                   18784: }
                   18785: 
                   18786: UINT8 pic_read(int c, UINT32 addr)
                   18787: {
                   18788:        if(addr & 1) {
                   18789:                return(pic[c].imr);
                   18790:        } else {
                   18791:                // polling mode is not supported...
                   18792:                //if(pic[c].ocw3 & 4) {
                   18793:                //      return ???;
                   18794:                //}
                   18795:                if(pic[c].ocw3 & 1) {
                   18796:                        return(pic[c].isr);
                   18797:                } else {
                   18798:                        return(pic[c].irr);
                   18799:                }
                   18800:        }
                   18801: }
                   18802: 
                   18803: void pic_req(int c, int level, int signal)
                   18804: {
                   18805:        if(signal) {
                   18806:                pic[c].irr |= (1 << level);
                   18807:        } else {
                   18808:                pic[c].irr &= ~(1 << level);
                   18809:        }
                   18810:        pic_update();
                   18811: }
                   18812: 
                   18813: int pic_ack()
                   18814: {
                   18815:        // ack (INTA=L)
                   18816:        pic[pic_req_chip].isr |= pic_req_bit;
                   18817:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   18818:        if(pic_req_chip > 0) {
                   18819:                // update isr and irr of master
                   18820:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   18821:                pic[pic_req_chip - 1].isr |= slave;
                   18822:                pic[pic_req_chip - 1].irr &= ~slave;
                   18823:        }
                   18824:        //if(pic[pic_req_chip].icw4 & 1) {
                   18825:                // 8086 mode
                   18826:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   18827:        //} else {
                   18828:        //      // 8080 mode
                   18829:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   18830:        //      if(pic[pic_req_chip].icw1 & 4) {
                   18831:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   18832:        //      } else {
                   18833:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   18834:        //      }
                   18835:        //      vector = 0xcd | (addr << 8);
                   18836:        //}
                   18837:        if(pic[pic_req_chip].icw4 & 2) {
                   18838:                // auto eoi
                   18839:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   18840:        }
                   18841:        return(vector);
                   18842: }
                   18843: 
                   18844: void pic_update()
                   18845: {
                   18846:        for(int c = 0; c < 2; c++) {
                   18847:                UINT8 irr = pic[c].irr;
                   18848:                if(c + 1 < 2) {
                   18849:                        // this is master
                   18850:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   18851:                                // request from slave
                   18852:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   18853:                        }
                   18854:                }
                   18855:                irr &= (~pic[c].imr);
                   18856:                if(!irr) {
                   18857:                        break;
                   18858:                }
                   18859:                if(!(pic[c].ocw3 & 0x20)) {
                   18860:                        irr |= pic[c].isr;
                   18861:                }
                   18862:                int level = pic[c].prio;
                   18863:                UINT8 bit = 1 << level;
                   18864:                while(!(irr & bit)) {
                   18865:                        level = (level + 1) & 7;
                   18866:                        bit = 1 << level;
                   18867:                }
                   18868:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   18869:                        // check slave
                   18870:                        continue;
                   18871:                }
                   18872:                if(pic[c].isr & bit) {
                   18873:                        break;
                   18874:                }
                   18875:                // interrupt request
                   18876:                pic_req_chip = c;
                   18877:                pic_req_level = level;
                   18878:                pic_req_bit = bit;
1.1.1.3   root     18879:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     18880:                return;
                   18881:        }
1.1.1.3   root     18882:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     18883: }
1.1       root     18884: 
1.1.1.25  root     18885: // pio
                   18886: 
                   18887: void pio_init()
                   18888: {
1.1.1.38  root     18889: //     bool conv_mode = (GetConsoleCP() == 932);
                   18890:        
1.1.1.26  root     18891:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     18892:        
1.1.1.25  root     18893:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     18894:                pio[c].stat = 0xdf;
1.1.1.25  root     18895:                pio[c].ctrl = 0x0c;
1.1.1.38  root     18896: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     18897:        }
                   18898: }
                   18899: 
1.1.1.37  root     18900: void pio_finish()
                   18901: {
                   18902:        pio_release();
                   18903: }
                   18904: 
                   18905: void pio_release()
                   18906: {
                   18907:        for(int c = 0; c < 2; c++) {
                   18908:                if(pio[c].fp != NULL) {
1.1.1.38  root     18909:                        if(pio[c].jis_mode) {
                   18910:                                fputc(0x1c, pio[c].fp);
                   18911:                                fputc(0x2e, pio[c].fp);
                   18912:                        }
1.1.1.37  root     18913:                        fclose(pio[c].fp);
                   18914:                        pio[c].fp = NULL;
                   18915:                }
                   18916:        }
                   18917: }
                   18918: 
1.1.1.25  root     18919: void pio_write(int c, UINT32 addr, UINT8 data)
                   18920: {
                   18921:        switch(addr & 3) {
                   18922:        case 0:
                   18923:                pio[c].data = data;
                   18924:                break;
                   18925:        case 2:
1.1.1.37  root     18926:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   18927:                        // strobe H -> L
                   18928:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   18929:                                // auto feed
                   18930:                                printer_out(c, 0x0d);
                   18931:                                printer_out(c, 0x0a);
                   18932:                        } else {
                   18933:                                printer_out(c, pio[c].data);
                   18934:                        }
                   18935:                        pio[c].stat &= ~0x40; // set ack
                   18936:                }
1.1.1.25  root     18937:                pio[c].ctrl = data;
                   18938:                break;
                   18939:        }
                   18940: }
                   18941: 
                   18942: UINT8 pio_read(int c, UINT32 addr)
                   18943: {
                   18944:        switch(addr & 3) {
                   18945:        case 0:
1.1.1.37  root     18946:                if(pio[c].ctrl & 0x20) {
                   18947:                        // input mode
                   18948:                        return(0xff);
                   18949:                }
1.1.1.25  root     18950:                return(pio[c].data);
                   18951:        case 1:
1.1.1.37  root     18952:                {
                   18953:                        UINT8 stat = pio[c].stat;
                   18954:                        pio[c].stat |= 0x40; // clear ack
                   18955:                        return(stat);
                   18956:                }
1.1.1.25  root     18957:        case 2:
                   18958:                return(pio[c].ctrl);
                   18959:        }
                   18960:        return(0xff);
                   18961: }
                   18962: 
1.1.1.37  root     18963: void printer_out(int c, UINT8 data)
                   18964: {
                   18965:        SYSTEMTIME time;
1.1.1.38  root     18966:        bool jis_mode = false;
1.1.1.37  root     18967:        
                   18968:        GetLocalTime(&time);
                   18969:        
                   18970:        if(pio[c].fp != NULL) {
                   18971:                // if at least 1000ms passed from last written, close the current file
                   18972:                FILETIME ftime1;
                   18973:                FILETIME ftime2;
                   18974:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   18975:                SystemTimeToFileTime(&time, &ftime2);
                   18976:                INT64 *time1 = (INT64 *)&ftime1;
                   18977:                INT64 *time2 = (INT64 *)&ftime2;
                   18978:                INT64 msec = (*time2 - *time1) / 10000;
                   18979:                
                   18980:                if(msec >= 1000) {
1.1.1.38  root     18981:                        if(pio[c].jis_mode) {
                   18982:                                fputc(0x1c, pio[c].fp);
                   18983:                                fputc(0x2e, pio[c].fp);
                   18984:                                jis_mode = true;
                   18985:                        }
1.1.1.37  root     18986:                        fclose(pio[c].fp);
                   18987:                        pio[c].fp = NULL;
                   18988:                }
                   18989:        }
                   18990:        if(pio[c].fp == NULL) {
                   18991:                // create a new file in the temp folder
                   18992:                char file_name[MAX_PATH];
                   18993:                
                   18994:                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);
                   18995:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   18996:                        strcat(pio[c].path, file_name);
                   18997:                } else {
                   18998:                        strcpy(pio[c].path, file_name);
                   18999:                }
1.1.1.38  root     19000:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     19001:        }
                   19002:        if(pio[c].fp != NULL) {
1.1.1.38  root     19003:                if(jis_mode) {
                   19004:                        fputc(0x1c, pio[c].fp);
                   19005:                        fputc(0x26, pio[c].fp);
                   19006:                }
1.1.1.37  root     19007:                fputc(data, pio[c].fp);
1.1.1.38  root     19008:                
                   19009:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   19010:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   19011:                        UINT8 buffer[4];
                   19012:                        fseek(pio[c].fp, 0, SEEK_SET);
                   19013:                        fread(buffer, 4, 1, pio[c].fp);
                   19014:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   19015:                                fclose(pio[c].fp);
                   19016:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   19017:                        }
                   19018:                }
1.1.1.37  root     19019:                pio[c].time = time;
                   19020:        }
                   19021: }
                   19022: 
1.1       root     19023: // pit
                   19024: 
1.1.1.22  root     19025: #define PIT_FREQ 1193182ULL
1.1       root     19026: #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)
                   19027: 
                   19028: void pit_init()
                   19029: {
1.1.1.8   root     19030:        memset(pit, 0, sizeof(pit));
1.1       root     19031:        for(int ch = 0; ch < 3; ch++) {
                   19032:                pit[ch].count = 0x10000;
                   19033:                pit[ch].ctrl_reg = 0x34;
                   19034:                pit[ch].mode = 3;
                   19035:        }
                   19036:        
                   19037:        // from bochs bios
                   19038:        pit_write(3, 0x34);
                   19039:        pit_write(0, 0x00);
                   19040:        pit_write(0, 0x00);
                   19041: }
                   19042: 
                   19043: void pit_write(int ch, UINT8 val)
                   19044: {
1.1.1.8   root     19045: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19046:        if(!pit_active) {
                   19047:                pit_active = 1;
                   19048:                pit_init();
                   19049:        }
1.1.1.8   root     19050: #endif
1.1       root     19051:        switch(ch) {
                   19052:        case 0:
                   19053:        case 1:
                   19054:        case 2:
                   19055:                // write count register
                   19056:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19057:                        if(pit[ch].ctrl_reg & 0x10) {
                   19058:                                pit[ch].low_write = 1;
                   19059:                        }
                   19060:                        if(pit[ch].ctrl_reg & 0x20) {
                   19061:                                pit[ch].high_write = 1;
                   19062:                        }
                   19063:                }
                   19064:                if(pit[ch].low_write) {
                   19065:                        pit[ch].count_reg = val;
                   19066:                        pit[ch].low_write = 0;
                   19067:                } else if(pit[ch].high_write) {
                   19068:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19069:                                pit[ch].count_reg = val << 8;
                   19070:                        } else {
                   19071:                                pit[ch].count_reg |= val << 8;
                   19072:                        }
                   19073:                        pit[ch].high_write = 0;
                   19074:                }
                   19075:                // start count
1.1.1.8   root     19076:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19077:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19078:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19079:                                pit[ch].prev_time = timeGetTime();
                   19080:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19081:                        }
                   19082:                }
                   19083:                break;
                   19084:        case 3: // ctrl reg
                   19085:                if((val & 0xc0) == 0xc0) {
                   19086:                        // i8254 read-back command
                   19087:                        for(ch = 0; ch < 3; ch++) {
                   19088:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19089:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19090:                                        pit[ch].status_latched = 1;
                   19091:                                }
                   19092:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19093:                                        pit_latch_count(ch);
                   19094:                                }
                   19095:                        }
                   19096:                        break;
                   19097:                }
                   19098:                ch = (val >> 6) & 3;
                   19099:                if(val & 0x30) {
1.1.1.35  root     19100:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19101:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19102:                        pit[ch].count_latched = 0;
                   19103:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19104:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19105:                        pit[ch].ctrl_reg = val;
                   19106:                        // stop count
1.1.1.8   root     19107:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19108:                        pit[ch].count_reg = 0;
                   19109:                } else if(!pit[ch].count_latched) {
                   19110:                        pit_latch_count(ch);
                   19111:                }
                   19112:                break;
                   19113:        }
                   19114: }
                   19115: 
                   19116: UINT8 pit_read(int ch)
                   19117: {
1.1.1.8   root     19118: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19119:        if(!pit_active) {
                   19120:                pit_active = 1;
                   19121:                pit_init();
                   19122:        }
1.1.1.8   root     19123: #endif
1.1       root     19124:        switch(ch) {
                   19125:        case 0:
                   19126:        case 1:
                   19127:        case 2:
                   19128:                if(pit[ch].status_latched) {
                   19129:                        pit[ch].status_latched = 0;
                   19130:                        return(pit[ch].status);
                   19131:                }
                   19132:                // if not latched, through current count
                   19133:                if(!pit[ch].count_latched) {
                   19134:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19135:                                pit_latch_count(ch);
                   19136:                        }
                   19137:                }
                   19138:                // return latched count
                   19139:                if(pit[ch].low_read) {
                   19140:                        pit[ch].low_read = 0;
                   19141:                        if(!pit[ch].high_read) {
                   19142:                                pit[ch].count_latched = 0;
                   19143:                        }
                   19144:                        return(pit[ch].latch & 0xff);
                   19145:                } else if(pit[ch].high_read) {
                   19146:                        pit[ch].high_read = 0;
                   19147:                        pit[ch].count_latched = 0;
                   19148:                        return((pit[ch].latch >> 8) & 0xff);
                   19149:                }
                   19150:        }
                   19151:        return(0xff);
                   19152: }
                   19153: 
1.1.1.8   root     19154: int pit_run(int ch, UINT32 cur_time)
1.1       root     19155: {
1.1.1.8   root     19156:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19157:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19158:                pit[ch].prev_time = pit[ch].expired_time;
                   19159:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19160:                if(cur_time >= pit[ch].expired_time) {
                   19161:                        pit[ch].prev_time = cur_time;
                   19162:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19163:                }
1.1.1.8   root     19164:                return(1);
1.1       root     19165:        }
1.1.1.8   root     19166:        return(0);
1.1       root     19167: }
                   19168: 
                   19169: void pit_latch_count(int ch)
                   19170: {
1.1.1.8   root     19171:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19172:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19173:                pit_run(ch, cur_time);
                   19174:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19175:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19176:                
                   19177:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19178:                        // decrement counter in 1msec period
                   19179:                        if(pit[ch].next_latch == 0) {
                   19180:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   19181:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19182:                        }
                   19183:                        if(pit[ch].latch > pit[ch].next_latch) {
                   19184:                                pit[ch].latch--;
                   19185:                        }
                   19186:                } else {
                   19187:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   19188:                        pit[ch].next_latch = 0;
                   19189:                }
1.1.1.8   root     19190:        } else {
                   19191:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     19192:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     19193:        }
                   19194:        pit[ch].count_latched = 1;
                   19195:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   19196:                // lower byte
                   19197:                pit[ch].low_read = 1;
                   19198:                pit[ch].high_read = 0;
                   19199:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19200:                // upper byte
                   19201:                pit[ch].low_read = 0;
                   19202:                pit[ch].high_read = 1;
                   19203:        } else {
                   19204:                // lower -> upper
1.1.1.14  root     19205:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     19206:        }
                   19207: }
                   19208: 
1.1.1.8   root     19209: int pit_get_expired_time(int ch)
1.1       root     19210: {
1.1.1.22  root     19211:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   19212:        UINT64 val = pit[ch].accum >> 10;
                   19213:        pit[ch].accum -= val << 10;
                   19214:        return((val != 0) ? val : 1);
1.1.1.8   root     19215: }
                   19216: 
1.1.1.25  root     19217: // sio
                   19218: 
                   19219: void sio_init()
                   19220: {
1.1.1.26  root     19221:        memset(sio, 0, sizeof(sio));
                   19222:        memset(sio_mt, 0, sizeof(sio_mt));
                   19223:        
1.1.1.29  root     19224:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19225:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19226:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19227:                
                   19228:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   19229:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     19230:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   19231:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     19232:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   19233:                sio[c].irq_identify = 0x01;     // no pending irq
                   19234:                
                   19235:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   19236:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   19237:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   19238:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   19239:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   19240:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   19241:                
1.1.1.26  root     19242:                if(sio_port_number[c] != 0) {
1.1.1.25  root     19243:                        sio[c].channel = c;
                   19244:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   19245:                }
                   19246:        }
                   19247: }
                   19248: 
                   19249: void sio_finish()
                   19250: {
1.1.1.29  root     19251:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19252:                if(sio_mt[c].hThread != NULL) {
                   19253:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   19254:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     19255:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     19256:                }
                   19257:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   19258:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   19259:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   19260:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   19261:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   19262:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     19263:        }
                   19264:        sio_release();
                   19265: }
                   19266: 
                   19267: void sio_release()
                   19268: {
1.1.1.29  root     19269:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     19270:                // sio_thread() may access the resources :-(
1.1.1.32  root     19271:                bool running = (sio_mt[c].hThread != NULL);
                   19272:                
                   19273:                if(running) {
                   19274:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   19275:                }
                   19276:                if(sio[c].send_buffer != NULL) {
                   19277:                        sio[c].send_buffer->release();
                   19278:                        delete sio[c].send_buffer;
                   19279:                        sio[c].send_buffer = NULL;
                   19280:                }
                   19281:                if(running) {
                   19282:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19283:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   19284:                }
                   19285:                if(sio[c].recv_buffer != NULL) {
                   19286:                        sio[c].recv_buffer->release();
                   19287:                        delete sio[c].recv_buffer;
                   19288:                        sio[c].recv_buffer = NULL;
                   19289:                }
                   19290:                if(running) {
                   19291:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     19292:                }
1.1.1.25  root     19293:        }
                   19294: }
                   19295: 
                   19296: void sio_write(int c, UINT32 addr, UINT8 data)
                   19297: {
                   19298:        switch(addr & 7) {
                   19299:        case 0:
                   19300:                if(sio[c].selector & 0x80) {
                   19301:                        if(sio[c].divisor.b.l != data) {
                   19302:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19303:                                sio[c].divisor.b.l = data;
                   19304:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19305:                        }
                   19306:                } else {
                   19307:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19308:                        if(sio[c].send_buffer != NULL) {
                   19309:                                sio[c].send_buffer->write(data);
                   19310:                        }
1.1.1.25  root     19311:                        // transmitter holding/shift registers are not empty
                   19312:                        sio[c].line_stat_buf &= ~0x60;
                   19313:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19314:                        
                   19315:                        if(sio[c].irq_enable & 0x02) {
                   19316:                                sio_update_irq(c);
                   19317:                        }
                   19318:                }
                   19319:                break;
                   19320:        case 1:
                   19321:                if(sio[c].selector & 0x80) {
                   19322:                        if(sio[c].divisor.b.h != data) {
                   19323:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19324:                                sio[c].divisor.b.h = data;
                   19325:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19326:                        }
                   19327:                } else {
                   19328:                        if(sio[c].irq_enable != data) {
                   19329:                                sio[c].irq_enable = data;
                   19330:                                sio_update_irq(c);
                   19331:                        }
                   19332:                }
                   19333:                break;
                   19334:        case 3:
                   19335:                {
                   19336:                        UINT8 line_ctrl = data & 0x3f;
                   19337:                        bool set_brk = ((data & 0x40) != 0);
                   19338:                        
                   19339:                        if(sio[c].line_ctrl != line_ctrl) {
                   19340:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19341:                                sio[c].line_ctrl = line_ctrl;
                   19342:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19343:                        }
                   19344:                        if(sio[c].set_brk != set_brk) {
                   19345:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   19346:                                sio[c].set_brk = set_brk;
                   19347:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19348:                        }
                   19349:                }
                   19350:                sio[c].selector = data;
                   19351:                break;
                   19352:        case 4:
                   19353:                {
                   19354:                        bool set_dtr = ((data & 0x01) != 0);
                   19355:                        bool set_rts = ((data & 0x02) != 0);
                   19356:                        
                   19357:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     19358: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     19359:                                sio[c].set_dtr = set_dtr;
                   19360:                                sio[c].set_rts = set_rts;
1.1.1.26  root     19361: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19362:                                
                   19363:                                bool state_changed = false;
                   19364:                                
                   19365:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19366:                                if(set_dtr) {
                   19367:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   19368:                                } else {
                   19369:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   19370:                                }
                   19371:                                if(set_rts) {
                   19372:                                        sio[c].modem_stat |= 0x10;      // cts on
                   19373:                                } else {
                   19374:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   19375:                                }
                   19376:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   19377:                                        if(!(sio[c].modem_stat & 0x02)) {
                   19378:                                                if(sio[c].irq_enable & 0x08) {
                   19379:                                                        state_changed = true;
                   19380:                                                }
                   19381:                                                sio[c].modem_stat |= 0x02;
                   19382:                                        }
                   19383:                                }
                   19384:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   19385:                                        if(!(sio[c].modem_stat & 0x01)) {
                   19386:                                                if(sio[c].irq_enable & 0x08) {
                   19387:                                                        state_changed = true;
                   19388:                                                }
                   19389:                                                sio[c].modem_stat |= 0x01;
                   19390:                                        }
                   19391:                                }
                   19392:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19393:                                
                   19394:                                if(state_changed) {
                   19395:                                        sio_update_irq(c);
                   19396:                                }
1.1.1.25  root     19397:                        }
                   19398:                }
                   19399:                sio[c].modem_ctrl = data;
                   19400:                break;
                   19401:        case 7:
                   19402:                sio[c].scratch = data;
                   19403:                break;
                   19404:        }
                   19405: }
                   19406: 
                   19407: UINT8 sio_read(int c, UINT32 addr)
                   19408: {
                   19409:        switch(addr & 7) {
                   19410:        case 0:
                   19411:                if(sio[c].selector & 0x80) {
                   19412:                        return(sio[c].divisor.b.l);
                   19413:                } else {
                   19414:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19415:                        UINT8 data = 0;
                   19416:                        if(sio[c].recv_buffer != NULL) {
                   19417:                                data = sio[c].recv_buffer->read();
                   19418:                        }
1.1.1.25  root     19419:                        // data is not ready
                   19420:                        sio[c].line_stat_buf &= ~0x01;
                   19421:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19422:                        
                   19423:                        if(sio[c].irq_enable & 0x01) {
                   19424:                                sio_update_irq(c);
                   19425:                        }
                   19426:                        return(data);
                   19427:                }
                   19428:        case 1:
                   19429:                if(sio[c].selector & 0x80) {
                   19430:                        return(sio[c].divisor.b.h);
                   19431:                } else {
                   19432:                        return(sio[c].irq_enable);
                   19433:                }
                   19434:        case 2:
                   19435:                return(sio[c].irq_identify);
                   19436:        case 3:
                   19437:                return(sio[c].selector);
                   19438:        case 4:
                   19439:                return(sio[c].modem_ctrl);
                   19440:        case 5:
                   19441:                {
                   19442:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   19443:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   19444:                        sio[c].line_stat_err = 0x00;
                   19445:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19446:                        
                   19447:                        bool state_changed = false;
                   19448:                        
                   19449:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19450:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19451:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19452:                                        // transmitter holding register will be empty first
                   19453:                                        if(sio[c].irq_enable & 0x02) {
                   19454:                                                state_changed = true;
                   19455:                                        }
                   19456:                                        sio[c].line_stat_buf |= 0x20;
                   19457:                                }
                   19458:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19459:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19460:                                // transmitter shift register will be empty later
                   19461:                                sio[c].line_stat_buf |= 0x40;
                   19462:                        }
                   19463:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   19464:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19465:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19466:                                        // data is ready
                   19467:                                        if(sio[c].irq_enable & 0x01) {
                   19468:                                                state_changed = true;
                   19469:                                        }
                   19470:                                        sio[c].line_stat_buf |= 0x01;
                   19471:                                }
                   19472:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19473:                        }
                   19474:                        if(state_changed) {
                   19475:                                sio_update_irq(c);
                   19476:                        }
                   19477:                        return(val);
                   19478:                }
                   19479:        case 6:
                   19480:                {
                   19481:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   19482:                        UINT8 val = sio[c].modem_stat;
                   19483:                        sio[c].modem_stat &= 0xf0;
                   19484:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   19485:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19486:                        
                   19487:                        if(sio[c].modem_ctrl & 0x10) {
                   19488:                                // loop-back
                   19489:                                val &= 0x0f;
                   19490:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   19491:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   19492:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   19493:                        }
                   19494:                        return(val);
                   19495:                }
                   19496:        case 7:
                   19497:                return(sio[c].scratch);
                   19498:        }
                   19499:        return(0xff);
                   19500: }
                   19501: 
                   19502: void sio_update(int c)
                   19503: {
                   19504:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19505:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19506:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19507:                        // transmitter holding/shift registers will be empty
                   19508:                        sio[c].line_stat_buf |= 0x60;
                   19509:                }
                   19510:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19511:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19512:                // transmitter shift register will be empty
                   19513:                sio[c].line_stat_buf |= 0x40;
                   19514:        }
                   19515:        if(!(sio[c].line_stat_buf & 0x01)) {
                   19516:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19517:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19518:                        // data is ready
                   19519:                        sio[c].line_stat_buf |= 0x01;
                   19520:                }
                   19521:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19522:        }
                   19523:        sio_update_irq(c);
                   19524: }
                   19525: 
                   19526: void sio_update_irq(int c)
                   19527: {
                   19528:        int level = -1;
                   19529:        
                   19530:        if(sio[c].irq_enable & 0x08) {
                   19531:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19532:                if((sio[c].modem_stat & 0x0f) != 0) {
                   19533:                        level = 0;
                   19534:                }
                   19535:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19536:        }
                   19537:        if(sio[c].irq_enable & 0x02) {
                   19538:                if(sio[c].line_stat_buf & 0x20) {
                   19539:                        level = 1;
                   19540:                }
                   19541:        }
                   19542:        if(sio[c].irq_enable & 0x01) {
                   19543:                if(sio[c].line_stat_buf & 0x01) {
                   19544:                        level = 2;
                   19545:                }
                   19546:        }
                   19547:        if(sio[c].irq_enable & 0x04) {
                   19548:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   19549:                if(sio[c].line_stat_err != 0) {
                   19550:                        level = 3;
                   19551:                }
                   19552:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19553:        }
1.1.1.29  root     19554:        
                   19555:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     19556:        if(level != -1) {
                   19557:                sio[c].irq_identify = level << 1;
1.1.1.29  root     19558:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     19559:        } else {
                   19560:                sio[c].irq_identify = 1;
1.1.1.29  root     19561:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     19562:        }
                   19563: }
                   19564: 
                   19565: DWORD WINAPI sio_thread(void *lpx)
                   19566: {
                   19567:        volatile sio_t *p = (sio_t *)lpx;
                   19568:        sio_mt_t *q = &sio_mt[p->channel];
                   19569:        
                   19570:        char name[] = "COM1";
1.1.1.26  root     19571:        name[3] = '0' + sio_port_number[p->channel];
                   19572:        HANDLE hComm = NULL;
                   19573:        COMMPROP commProp;
                   19574:        DCB dcb;
                   19575:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   19576:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   19577:        
                   19578:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   19579:                if(GetCommProperties(hComm, &commProp)) {
                   19580:                        dwSettableBaud = commProp.dwSettableBaud;
                   19581:                }
1.1.1.25  root     19582:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     19583: //             EscapeCommFunction(hComm, SETRTS);
                   19584: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     19585:                
1.1.1.54  root     19586:                while(!m_exit) {
1.1.1.25  root     19587:                        // setup comm port
                   19588:                        bool comm_state_changed = false;
                   19589:                        
                   19590:                        EnterCriticalSection(&q->csLineCtrl);
                   19591:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   19592:                                p->prev_divisor = p->divisor.w;
                   19593:                                p->prev_line_ctrl = p->line_ctrl;
                   19594:                                comm_state_changed = true;
                   19595:                        }
                   19596:                        LeaveCriticalSection(&q->csLineCtrl);
                   19597:                        
                   19598:                        if(comm_state_changed) {
1.1.1.26  root     19599:                                if(GetCommState(hComm, &dcb)) {
                   19600: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   19601:                                        DWORD baud = 115200 / p->prev_divisor;
                   19602:                                        dcb.BaudRate = 9600; // default
                   19603:                                        
                   19604:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   19605:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   19606:                                        // 134.5bps is not supported ???
                   19607: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   19608:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   19609:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   19610:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   19611:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   19612:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   19613:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   19614:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   19615:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   19616:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   19617: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   19618: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   19619: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   19620:                                        
                   19621:                                        switch(p->prev_line_ctrl & 0x03) {
                   19622:                                        case 0x00: dcb.ByteSize = 5; break;
                   19623:                                        case 0x01: dcb.ByteSize = 6; break;
                   19624:                                        case 0x02: dcb.ByteSize = 7; break;
                   19625:                                        case 0x03: dcb.ByteSize = 8; break;
                   19626:                                        }
                   19627:                                        switch(p->prev_line_ctrl & 0x04) {
                   19628:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   19629:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   19630:                                        }
                   19631:                                        switch(p->prev_line_ctrl & 0x38) {
                   19632:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   19633:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   19634:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   19635:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   19636:                                        default:   dcb.Parity = NOPARITY;    break;
                   19637:                                        }
                   19638:                                        dcb.fBinary = TRUE;
                   19639:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   19640:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   19641:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   19642:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   19643:                                        dcb.fTXContinueOnXoff = TRUE;
                   19644:                                        dcb.fOutX = dcb.fInX = FALSE;
                   19645:                                        dcb.fErrorChar = FALSE;
                   19646:                                        dcb.fNull = FALSE;
                   19647:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   19648:                                        dcb.fAbortOnError = FALSE;
                   19649:                                        
                   19650:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     19651:                                }
                   19652:                                
                   19653:                                // check again to apply all comm state changes
                   19654:                                Sleep(10);
                   19655:                                continue;
                   19656:                        }
                   19657:                        
                   19658:                        // set comm pins
                   19659:                        bool change_brk = false;
1.1.1.26  root     19660: //                     bool change_rts = false;
                   19661: //                     bool change_dtr = false;
1.1.1.25  root     19662:                        
                   19663:                        EnterCriticalSection(&q->csModemCtrl);
                   19664:                        if(p->prev_set_brk != p->set_brk) {
                   19665:                                p->prev_set_brk = p->set_brk;
                   19666:                                change_brk = true;
                   19667:                        }
1.1.1.26  root     19668: //                     if(p->prev_set_rts != p->set_rts) {
                   19669: //                             p->prev_set_rts = p->set_rts;
                   19670: //                             change_rts = true;
                   19671: //                     }
                   19672: //                     if(p->prev_set_dtr != p->set_dtr) {
                   19673: //                             p->prev_set_dtr = p->set_dtr;
                   19674: //                             change_dtr = true;
                   19675: //                     }
1.1.1.25  root     19676:                        LeaveCriticalSection(&q->csModemCtrl);
                   19677:                        
                   19678:                        if(change_brk) {
1.1.1.26  root     19679:                                static UINT32 clear_time = 0;
                   19680:                                if(p->prev_set_brk) {
                   19681:                                        EscapeCommFunction(hComm, SETBREAK);
                   19682:                                        clear_time = timeGetTime() + 200;
                   19683:                                } else {
                   19684:                                        // keep break for at least 200msec
                   19685:                                        UINT32 cur_time = timeGetTime();
                   19686:                                        if(clear_time > cur_time) {
                   19687:                                                Sleep(clear_time - cur_time);
                   19688:                                        }
                   19689:                                        EscapeCommFunction(hComm, CLRBREAK);
                   19690:                                }
1.1.1.25  root     19691:                        }
1.1.1.26  root     19692: //                     if(change_rts) {
                   19693: //                             if(p->prev_set_rts) {
                   19694: //                                     EscapeCommFunction(hComm, SETRTS);
                   19695: //                             } else {
                   19696: //                                     EscapeCommFunction(hComm, CLRRTS);
                   19697: //                             }
                   19698: //                     }
                   19699: //                     if(change_dtr) {
                   19700: //                             if(p->prev_set_dtr) {
                   19701: //                                     EscapeCommFunction(hComm, SETDTR);
                   19702: //                             } else {
                   19703: //                                     EscapeCommFunction(hComm, CLRDTR);
                   19704: //                             }
                   19705: //                     }
1.1.1.25  root     19706:                        
                   19707:                        // get comm pins
                   19708:                        DWORD dwModemStat = 0;
                   19709:                        
                   19710:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   19711:                                EnterCriticalSection(&q->csModemStat);
                   19712:                                if(dwModemStat & MS_RLSD_ON) {
                   19713:                                        p->modem_stat |= 0x80;
                   19714:                                } else {
                   19715:                                        p->modem_stat &= ~0x80;
                   19716:                                }
                   19717:                                if(dwModemStat & MS_RING_ON) {
                   19718:                                        p->modem_stat |= 0x40;
                   19719:                                } else {
                   19720:                                        p->modem_stat &= ~0x40;
                   19721:                                }
1.1.1.26  root     19722: //                             if(dwModemStat & MS_DSR_ON) {
                   19723: //                                     p->modem_stat |= 0x20;
                   19724: //                             } else {
                   19725: //                                     p->modem_stat &= ~0x20;
                   19726: //                             }
                   19727: //                             if(dwModemStat & MS_CTS_ON) {
                   19728: //                                     p->modem_stat |= 0x10;
                   19729: //                             } else {
                   19730: //                                     p->modem_stat &= ~0x10;
                   19731: //                             }
1.1.1.25  root     19732:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   19733:                                        p->modem_stat |= 0x08;
                   19734:                                }
                   19735:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   19736:                                        p->modem_stat |= 0x04;
                   19737:                                }
1.1.1.26  root     19738: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   19739: //                                     p->modem_stat |= 0x02;
                   19740: //                             }
                   19741: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   19742: //                                     p->modem_stat |= 0x01;
                   19743: //                             }
1.1.1.25  root     19744:                                LeaveCriticalSection(&q->csModemStat);
                   19745:                        }
                   19746:                        
                   19747:                        // send data
                   19748:                        DWORD dwSend = 0;
                   19749:                        
                   19750:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     19751:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     19752:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   19753:                        }
                   19754:                        LeaveCriticalSection(&q->csSendData);
                   19755:                        
                   19756:                        if(dwSend != 0) {
                   19757:                                DWORD dwWritten = 0;
                   19758:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   19759:                        }
                   19760:                        
                   19761:                        // get line status and recv data
                   19762:                        DWORD dwLineStat = 0;
                   19763:                        COMSTAT comStat;
                   19764:                        
                   19765:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   19766:                                EnterCriticalSection(&q->csLineStat);
                   19767:                                if(dwLineStat & CE_BREAK) {
                   19768:                                        p->line_stat_err |= 0x10;
                   19769:                                }
                   19770:                                if(dwLineStat & CE_FRAME) {
                   19771:                                        p->line_stat_err |= 0x08;
                   19772:                                }
                   19773:                                if(dwLineStat & CE_RXPARITY) {
                   19774:                                        p->line_stat_err |= 0x04;
                   19775:                                }
                   19776:                                if(dwLineStat & CE_OVERRUN) {
                   19777:                                        p->line_stat_err |= 0x02;
                   19778:                                }
                   19779:                                LeaveCriticalSection(&q->csLineStat);
                   19780:                                
                   19781:                                if(comStat.cbInQue != 0) {
                   19782:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19783:                                        DWORD dwRecv = 0;
                   19784:                                        if(p->recv_buffer != NULL) {
                   19785:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   19786:                                        }
1.1.1.25  root     19787:                                        LeaveCriticalSection(&q->csRecvData);
                   19788:                                        
                   19789:                                        if(dwRecv != 0) {
                   19790:                                                DWORD dwRead = 0;
                   19791:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   19792:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19793:                                                        if(p->recv_buffer != NULL) {
                   19794:                                                                for(int i = 0; i < dwRead; i++) {
                   19795:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   19796:                                                                }
1.1.1.25  root     19797:                                                        }
                   19798:                                                        LeaveCriticalSection(&q->csRecvData);
                   19799:                                                }
                   19800:                                        }
                   19801:                                }
                   19802:                        }
                   19803:                        Sleep(10);
                   19804:                }
                   19805:                CloseHandle(hComm);
                   19806:        }
                   19807:        return 0;
                   19808: }
                   19809: 
1.1.1.8   root     19810: // cmos
                   19811: 
                   19812: void cmos_init()
                   19813: {
                   19814:        memset(cmos, 0, sizeof(cmos));
                   19815:        cmos_addr = 0;
1.1       root     19816:        
1.1.1.8   root     19817:        // from DOSBox
                   19818:        cmos_write(0x0a, 0x26);
                   19819:        cmos_write(0x0b, 0x02);
                   19820:        cmos_write(0x0d, 0x80);
1.1       root     19821: }
                   19822: 
1.1.1.8   root     19823: void cmos_write(int addr, UINT8 val)
1.1       root     19824: {
1.1.1.8   root     19825:        cmos[addr & 0x7f] = val;
                   19826: }
                   19827: 
                   19828: #define CMOS_GET_TIME() { \
                   19829:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   19830:        if(prev_sec != cur_sec) { \
                   19831:                GetLocalTime(&time); \
                   19832:                prev_sec = cur_sec; \
                   19833:        } \
1.1       root     19834: }
1.1.1.8   root     19835: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     19836: 
1.1.1.8   root     19837: UINT8 cmos_read(int addr)
1.1       root     19838: {
1.1.1.8   root     19839:        static SYSTEMTIME time;
                   19840:        static UINT32 prev_sec = 0;
1.1       root     19841:        
1.1.1.8   root     19842:        switch(addr & 0x7f) {
                   19843:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   19844:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   19845:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   19846:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   19847:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   19848:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   19849:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   19850: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   19851:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   19852:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   19853:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   19854:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   19855:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   19856:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   19857:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   19858:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     19859:        }
1.1.1.8   root     19860:        return(cmos[addr & 0x7f]);
1.1       root     19861: }
                   19862: 
1.1.1.7   root     19863: // kbd (a20)
                   19864: 
                   19865: void kbd_init()
                   19866: {
1.1.1.8   root     19867:        kbd_data = kbd_command = 0;
1.1.1.7   root     19868:        kbd_status = 0x18;
                   19869: }
                   19870: 
                   19871: UINT8 kbd_read_data()
                   19872: {
1.1.1.8   root     19873:        kbd_status &= ~1;
1.1.1.7   root     19874:        return(kbd_data);
                   19875: }
                   19876: 
                   19877: void kbd_write_data(UINT8 val)
                   19878: {
                   19879:        switch(kbd_command) {
                   19880:        case 0xd1:
                   19881:                i386_set_a20_line((val >> 1) & 1);
                   19882:                break;
                   19883:        }
                   19884:        kbd_command = 0;
1.1.1.8   root     19885:        kbd_status &= ~8;
1.1.1.7   root     19886: }
                   19887: 
                   19888: UINT8 kbd_read_status()
                   19889: {
                   19890:        return(kbd_status);
                   19891: }
                   19892: 
                   19893: void kbd_write_command(UINT8 val)
                   19894: {
                   19895:        switch(val) {
                   19896:        case 0xd0:
                   19897:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     19898:                kbd_status |= 1;
1.1.1.7   root     19899:                break;
                   19900:        case 0xdd:
                   19901:                i386_set_a20_line(0);
                   19902:                break;
                   19903:        case 0xdf:
                   19904:                i386_set_a20_line(1);
                   19905:                break;
1.1.1.26  root     19906:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   19907:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     19908:                if(!(val & 1)) {
1.1.1.8   root     19909:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     19910:                                // reset pic
                   19911:                                pic_init();
                   19912:                                pic[0].irr = pic[1].irr = 0x00;
                   19913:                                pic[0].imr = pic[1].imr = 0xff;
                   19914:                        }
                   19915:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     19916:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   19917:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   19918:                        i386_jmp_far(selector, address);
1.1.1.7   root     19919:                }
                   19920:                i386_set_a20_line((val >> 1) & 1);
                   19921:                break;
                   19922:        }
                   19923:        kbd_command = val;
1.1.1.8   root     19924:        kbd_status |= 8;
1.1.1.7   root     19925: }
                   19926: 
1.1.1.9   root     19927: // vga
                   19928: 
                   19929: UINT8 vga_read_status()
                   19930: {
                   19931:        // 60hz
                   19932:        static const int period[3] = {16, 17, 17};
                   19933:        static int index = 0;
                   19934:        UINT32 time = timeGetTime() % period[index];
                   19935:        
                   19936:        index = (index + 1) % 3;
1.1.1.14  root     19937:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     19938: }
                   19939: 
1.1       root     19940: // i/o bus
                   19941: 
1.1.1.29  root     19942: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   19943: //#define SW1US_PATCH
                   19944: 
1.1.1.25  root     19945: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     19946: #ifdef USE_DEBUGGER
1.1.1.25  root     19947: {
1.1.1.33  root     19948:        if(now_debugging) {
                   19949:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19950:                        if(in_break_point.table[i].status == 1) {
                   19951:                                if(addr == in_break_point.table[i].addr) {
                   19952:                                        in_break_point.hit = i + 1;
                   19953:                                        now_suspended = true;
                   19954:                                        break;
                   19955:                                }
                   19956:                        }
                   19957:                }
1.1.1.25  root     19958:        }
1.1.1.33  root     19959:        return(debugger_read_io_byte(addr));
1.1.1.25  root     19960: }
1.1.1.33  root     19961: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     19962: #endif
1.1       root     19963: {
1.1.1.33  root     19964:        UINT8 val = 0xff;
                   19965:        
1.1       root     19966:        switch(addr) {
1.1.1.29  root     19967: #ifdef SW1US_PATCH
                   19968:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19969:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     19970:                val = sio_read(0, addr - 1);
                   19971:                break;
1.1.1.29  root     19972: #else
1.1.1.25  root     19973:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19974:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     19975:                val = dma_read(0, addr);
                   19976:                break;
1.1.1.29  root     19977: #endif
1.1.1.25  root     19978:        case 0x20: case 0x21:
1.1.1.33  root     19979:                val = pic_read(0, addr);
                   19980:                break;
1.1.1.25  root     19981:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     19982:                val = pit_read(addr & 0x03);
                   19983:                break;
1.1.1.7   root     19984:        case 0x60:
1.1.1.33  root     19985:                val = kbd_read_data();
                   19986:                break;
1.1.1.9   root     19987:        case 0x61:
1.1.1.33  root     19988:                val = system_port;
                   19989:                break;
1.1.1.7   root     19990:        case 0x64:
1.1.1.33  root     19991:                val = kbd_read_status();
                   19992:                break;
1.1       root     19993:        case 0x71:
1.1.1.33  root     19994:                val = cmos_read(cmos_addr);
                   19995:                break;
1.1.1.25  root     19996:        case 0x81:
1.1.1.33  root     19997:                val = dma_page_read(0, 2);
                   19998:                break;
1.1.1.25  root     19999:        case 0x82:
1.1.1.33  root     20000:                val = dma_page_read(0, 3);
                   20001:                break;
1.1.1.25  root     20002:        case 0x83:
1.1.1.33  root     20003:                val = dma_page_read(0, 1);
                   20004:                break;
1.1.1.25  root     20005:        case 0x87:
1.1.1.33  root     20006:                val = dma_page_read(0, 0);
                   20007:                break;
1.1.1.25  root     20008:        case 0x89:
1.1.1.33  root     20009:                val = dma_page_read(1, 2);
                   20010:                break;
1.1.1.25  root     20011:        case 0x8a:
1.1.1.33  root     20012:                val = dma_page_read(1, 3);
                   20013:                break;
1.1.1.25  root     20014:        case 0x8b:
1.1.1.33  root     20015:                val = dma_page_read(1, 1);
                   20016:                break;
1.1.1.25  root     20017:        case 0x8f:
1.1.1.33  root     20018:                val = dma_page_read(1, 0);
                   20019:                break;
1.1       root     20020:        case 0x92:
1.1.1.33  root     20021:                val = (m_a20_mask >> 19) & 2;
                   20022:                break;
1.1.1.25  root     20023:        case 0xa0: case 0xa1:
1.1.1.33  root     20024:                val = pic_read(1, addr);
                   20025:                break;
1.1.1.25  root     20026:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20027:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20028:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20029:                break;
1.1.1.37  root     20030:        case 0x278: case 0x279: case 0x27a:
                   20031:                val = pio_read(1, addr);
                   20032:                break;
1.1.1.29  root     20033:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20034:                val = sio_read(3, addr);
                   20035:                break;
1.1.1.25  root     20036:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20037:                val = sio_read(1, addr);
                   20038:                break;
1.1.1.25  root     20039:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20040:                val = pio_read(0, addr);
                   20041:                break;
1.1.1.25  root     20042:        case 0x3ba: case 0x3da:
1.1.1.33  root     20043:                val = vga_read_status();
                   20044:                break;
1.1.1.37  root     20045:        case 0x3bc: case 0x3bd: case 0x3be:
                   20046:                val = pio_read(2, addr);
                   20047:                break;
1.1.1.29  root     20048:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20049:                val = sio_read(2, addr);
                   20050:                break;
1.1.1.25  root     20051:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20052:                val = sio_read(0, addr);
                   20053:                break;
1.1       root     20054:        default:
1.1.1.33  root     20055: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20056:                break;
                   20057:        }
1.1.1.33  root     20058: #ifdef ENABLE_DEBUG_IOPORT
                   20059:        if(fp_debug_log != NULL) {
                   20060:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20061:        }
                   20062: #endif
                   20063:        return(val);
1.1       root     20064: }
                   20065: 
                   20066: UINT16 read_io_word(offs_t addr)
                   20067: {
                   20068:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20069: }
                   20070: 
1.1.1.33  root     20071: #ifdef USE_DEBUGGER
                   20072: UINT16 debugger_read_io_word(offs_t addr)
                   20073: {
                   20074:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20075: }
                   20076: #endif
                   20077: 
1.1       root     20078: UINT32 read_io_dword(offs_t addr)
                   20079: {
                   20080:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20081: }
                   20082: 
1.1.1.33  root     20083: #ifdef USE_DEBUGGER
                   20084: UINT32 debugger_read_io_dword(offs_t addr)
                   20085: {
                   20086:        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));
                   20087: }
                   20088: #endif
                   20089: 
1.1       root     20090: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20091: #ifdef USE_DEBUGGER
                   20092: {
                   20093:        if(now_debugging) {
                   20094:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20095:                        if(out_break_point.table[i].status == 1) {
                   20096:                                if(addr == out_break_point.table[i].addr) {
                   20097:                                        out_break_point.hit = i + 1;
                   20098:                                        now_suspended = true;
                   20099:                                        break;
                   20100:                                }
                   20101:                        }
                   20102:                }
                   20103:        }
                   20104:        debugger_write_io_byte(addr, val);
                   20105: }
                   20106: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20107: #endif
1.1       root     20108: {
1.1.1.25  root     20109: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20110:        if(fp_debug_log != NULL) {
1.1.1.43  root     20111: #ifdef USE_SERVICE_THREAD
                   20112:                if(addr != 0xf7)
                   20113: #endif
1.1.1.33  root     20114:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20115:        }
                   20116: #endif
1.1       root     20117:        switch(addr) {
1.1.1.29  root     20118: #ifdef SW1US_PATCH
                   20119:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20120:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20121:                sio_write(0, addr - 1, val);
                   20122:                break;
                   20123: #else
1.1.1.25  root     20124:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20125:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20126:                dma_write(0, addr, val);
                   20127:                break;
1.1.1.29  root     20128: #endif
1.1.1.25  root     20129:        case 0x20: case 0x21:
1.1       root     20130:                pic_write(0, addr, val);
                   20131:                break;
1.1.1.25  root     20132:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20133:                pit_write(addr & 0x03, val);
                   20134:                break;
1.1.1.7   root     20135:        case 0x60:
                   20136:                kbd_write_data(val);
                   20137:                break;
1.1.1.9   root     20138:        case 0x61:
                   20139:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20140:                        // beep on
                   20141: //                     MessageBeep(-1);
                   20142:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20143:                        // beep off
                   20144:                }
                   20145:                system_port = val;
                   20146:                break;
1.1       root     20147:        case 0x64:
1.1.1.7   root     20148:                kbd_write_command(val);
1.1       root     20149:                break;
                   20150:        case 0x70:
                   20151:                cmos_addr = val;
                   20152:                break;
                   20153:        case 0x71:
1.1.1.8   root     20154:                cmos_write(cmos_addr, val);
1.1       root     20155:                break;
1.1.1.25  root     20156:        case 0x81:
                   20157:                dma_page_write(0, 2, val);
                   20158:        case 0x82:
                   20159:                dma_page_write(0, 3, val);
                   20160:        case 0x83:
                   20161:                dma_page_write(0, 1, val);
                   20162:        case 0x87:
                   20163:                dma_page_write(0, 0, val);
                   20164:        case 0x89:
                   20165:                dma_page_write(1, 2, val);
                   20166:        case 0x8a:
                   20167:                dma_page_write(1, 3, val);
                   20168:        case 0x8b:
                   20169:                dma_page_write(1, 1, val);
                   20170:        case 0x8f:
                   20171:                dma_page_write(1, 0, val);
1.1       root     20172:        case 0x92:
1.1.1.7   root     20173:                i386_set_a20_line((val >> 1) & 1);
1.1       root     20174:                break;
1.1.1.25  root     20175:        case 0xa0: case 0xa1:
1.1       root     20176:                pic_write(1, addr, val);
                   20177:                break;
1.1.1.25  root     20178:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20179:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     20180:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     20181:                break;
1.1.1.35  root     20182: #ifdef USE_SERVICE_THREAD
                   20183:        case 0xf7:
                   20184:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     20185:                if(in_service && cursor_moved) {
                   20186:                        // update cursor position before service is done
                   20187:                        pcbios_update_cursor_position();
                   20188:                        cursor_moved = false;
                   20189:                }
1.1.1.35  root     20190:                finish_service_loop();
                   20191:                break;
                   20192: #endif
1.1.1.37  root     20193:        case 0x278: case 0x279: case 0x27a:
                   20194:                pio_write(1, addr, val);
                   20195:                break;
1.1.1.29  root     20196:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   20197:                sio_write(3, addr, val);
                   20198:                break;
1.1.1.25  root     20199:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   20200:                sio_write(1, addr, val);
                   20201:                break;
                   20202:        case 0x378: case 0x379: case 0x37a:
                   20203:                pio_write(0, addr, val);
                   20204:                break;
1.1.1.37  root     20205:        case 0x3bc: case 0x3bd: case 0x3be:
                   20206:                pio_write(2, addr, val);
                   20207:                break;
1.1.1.29  root     20208:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   20209:                sio_write(2, addr, val);
                   20210:                break;
1.1.1.25  root     20211:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   20212:                sio_write(0, addr, val);
                   20213:                break;
1.1       root     20214:        default:
1.1.1.33  root     20215: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     20216:                break;
                   20217:        }
                   20218: }
                   20219: 
                   20220: void write_io_word(offs_t addr, UINT16 val)
                   20221: {
                   20222:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20223:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20224: }
                   20225: 
1.1.1.33  root     20226: #ifdef USE_DEBUGGER
                   20227: void debugger_write_io_word(offs_t addr, UINT16 val)
                   20228: {
                   20229:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20230:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20231: }
                   20232: #endif
                   20233: 
1.1       root     20234: void write_io_dword(offs_t addr, UINT32 val)
                   20235: {
                   20236:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20237:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20238:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20239:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20240: }
1.1.1.33  root     20241: 
                   20242: #ifdef USE_DEBUGGER
                   20243: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   20244: {
                   20245:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20246:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20247:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20248:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20249: }
                   20250: #endif

unix.superglobalmegacorp.com

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