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

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.51  root      380:                        bool empty = key_buf_char->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.52  root      895: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true);
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
                   3343:                bool empty = key_buf_char->empty();
                   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
                   4955:                bool empty = key_buf_char->empty();
                   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
                   5005:                                bool empty = key_buf_char->empty();
                   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.51  root     8435: void pcbios_clear_key_buffer()
                   8436: {
                   8437:        key_buf_char->clear();
                   8438:        key_buf_scan->clear();
                   8439:        
                   8440:        // update key buffer
                   8441:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8442: }
                   8443: 
                   8444: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8445: {
                   8446:        key_buf_char->write(key_char);
                   8447:        key_buf_scan->write(key_scan);
                   8448:        
                   8449:        // update key buffer
                   8450:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8451:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8452:        UINT16 next = tail + 2;
                   8453:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8454:                next = *(UINT16 *)(mem + 0x480);
                   8455:        }
                   8456:        if(next != head) {
                   8457:                *(UINT16 *)(mem + 0x41c) = next;
                   8458:                mem[0x400 + (tail++)] = key_char;
                   8459:                mem[0x400 + (tail++)] = key_scan;
                   8460:        }
                   8461: }
                   8462: 
                   8463: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8464: {
                   8465:        if(key_buf_char->empty()) {
                   8466:                *key_char = 0x00;
                   8467:                *key_scan = 0x00;
                   8468:                return(false);
                   8469:        }
                   8470:        *key_char = key_buf_char->read();
                   8471:        *key_scan = key_buf_scan->read();
                   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;
                   8482: //             *key_char = mem[0x400 + (head++)];
                   8483: //             *key_scan = mem[0x400 + (head++)];
                   8484:        }
                   8485:        return(true);
                   8486: }
                   8487: 
1.1.1.33  root     8488: void pcbios_update_key_code(bool wait)
1.1       root     8489: {
1.1.1.32  root     8490:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8491: #ifdef USE_SERVICE_THREAD
                   8492:                EnterCriticalSection(&key_buf_crit_sect);
                   8493: #endif
                   8494:                bool empty = key_buf_char->empty();
                   8495: #ifdef USE_SERVICE_THREAD
                   8496:                LeaveCriticalSection(&key_buf_crit_sect);
                   8497: #endif
                   8498:                if(empty) {
1.1.1.32  root     8499:                        if(!update_key_buffer()) {
1.1.1.33  root     8500:                                if(wait) {
1.1.1.32  root     8501:                                        Sleep(10);
                   8502:                                } else {
                   8503:                                        maybe_idle();
                   8504:                                }
1.1.1.14  root     8505:                        }
                   8506:                }
1.1.1.34  root     8507:        }
                   8508:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8509: #ifdef USE_SERVICE_THREAD
                   8510:                EnterCriticalSection(&key_buf_crit_sect);
                   8511: #endif
1.1.1.51  root     8512:                int key_char, key_scan;
                   8513:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8514:                        key_code  = key_char << 0;
                   8515:                        key_code |= key_scan << 8;
1.1.1.35  root     8516:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8517:                }
                   8518:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8519:                        key_code |= key_char << 16;
                   8520:                        key_code |= key_scan << 24;
1.1.1.33  root     8521:                        key_recv |= 0xffff0000;
1.1.1.32  root     8522:                }
1.1.1.35  root     8523: #ifdef USE_SERVICE_THREAD
                   8524:                LeaveCriticalSection(&key_buf_crit_sect);
                   8525: #endif
1.1       root     8526:        }
                   8527: }
                   8528: 
1.1.1.35  root     8529: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8530: {
1.1.1.54! root     8531:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     8532:                pcbios_update_key_code(true);
1.1       root     8533:        }
1.1.1.33  root     8534:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8535:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8536:                        if(REG8(AH) == 0x10) {
                   8537:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8538:                        } else {
                   8539:                                key_code = ((key_code >> 16) & 0xff00);
                   8540:                        }
                   8541:                        key_recv >>= 16;
1.1       root     8542:                }
                   8543:        }
                   8544:        REG16(AX) = key_code & 0xffff;
                   8545:        key_code >>= 16;
1.1.1.33  root     8546:        key_recv >>= 16;
1.1.1.35  root     8547:        
                   8548: #ifdef USE_SERVICE_THREAD
                   8549:        service_exit = true;
                   8550: #endif
                   8551:        return(0);
                   8552: }
                   8553: 
                   8554: inline void pcbios_int_16h_00h()
                   8555: {
                   8556: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8557:        if(!in_service && !in_service_29h) {
                   8558:                start_service_loop(pcbios_int_16h_00h_thread);
                   8559:        } else {
                   8560: #endif
                   8561:                pcbios_int_16h_00h_thread(NULL);
                   8562:                REQUEST_HARDWRE_UPDATE();
                   8563: #ifdef USE_SERVICE_THREAD
                   8564:        }
1.1.1.35  root     8565: #endif
1.1       root     8566: }
                   8567: 
                   8568: inline void pcbios_int_16h_01h()
                   8569: {
1.1.1.33  root     8570:        if(key_recv == 0) {
                   8571:                pcbios_update_key_code(false);
1.1.1.5   root     8572:        }
1.1.1.33  root     8573:        if(key_recv != 0) {
                   8574:                UINT32 key_code_tmp = key_code;
                   8575:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8576:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8577:                                if(REG8(AH) == 0x11) {
                   8578:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8579:                                } else {
                   8580:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8581:                                }
                   8582:                        }
1.1       root     8583:                }
1.1.1.5   root     8584:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8585: #if defined(HAS_I386)
1.1.1.33  root     8586:                m_ZF = 0;
                   8587: #else
                   8588:                m_ZeroVal = 1;
                   8589: #endif
                   8590:        } else {
                   8591: #if defined(HAS_I386)
                   8592:                m_ZF = 1;
1.1.1.3   root     8593: #else
1.1.1.33  root     8594:                m_ZeroVal = 0;
1.1.1.3   root     8595: #endif
1.1.1.33  root     8596:        }
1.1       root     8597: }
                   8598: 
                   8599: inline void pcbios_int_16h_02h()
                   8600: {
                   8601:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8602:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8603:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8604:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8605:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8606:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8607:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8608:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8609: }
                   8610: 
                   8611: inline void pcbios_int_16h_03h()
                   8612: {
                   8613:        static UINT16 status = 0;
                   8614:        
                   8615:        switch(REG8(AL)) {
                   8616:        case 0x05:
                   8617:                status = REG16(BX);
                   8618:                break;
                   8619:        case 0x06:
                   8620:                REG16(BX) = status;
                   8621:                break;
                   8622:        default:
1.1.1.3   root     8623:                m_CF = 1;
1.1       root     8624:                break;
                   8625:        }
                   8626: }
                   8627: 
                   8628: inline void pcbios_int_16h_05h()
                   8629: {
1.1.1.32  root     8630:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8631: #ifdef USE_SERVICE_THREAD
                   8632:                EnterCriticalSection(&key_buf_crit_sect);
                   8633: #endif
1.1.1.51  root     8634:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     8635: #ifdef USE_SERVICE_THREAD
                   8636:                LeaveCriticalSection(&key_buf_crit_sect);
                   8637: #endif
1.1.1.32  root     8638:        }
1.1       root     8639:        REG8(AL) = 0x00;
                   8640: }
                   8641: 
                   8642: inline void pcbios_int_16h_12h()
                   8643: {
                   8644:        pcbios_int_16h_02h();
                   8645:        
                   8646:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8647:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8648:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8649:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8650:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8651:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8652:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8653:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8654: }
                   8655: 
                   8656: inline void pcbios_int_16h_13h()
                   8657: {
                   8658:        static UINT16 status = 0;
                   8659:        
                   8660:        switch(REG8(AL)) {
                   8661:        case 0x00:
                   8662:                status = REG16(DX);
                   8663:                break;
                   8664:        case 0x01:
                   8665:                REG16(DX) = status;
                   8666:                break;
                   8667:        default:
1.1.1.22  root     8668:                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     8669:                m_CF = 1;
1.1       root     8670:                break;
                   8671:        }
                   8672: }
                   8673: 
                   8674: inline void pcbios_int_16h_14h()
                   8675: {
                   8676:        static UINT8 status = 0;
                   8677:        
                   8678:        switch(REG8(AL)) {
                   8679:        case 0x00:
                   8680:        case 0x01:
                   8681:                status = REG8(AL);
                   8682:                break;
                   8683:        case 0x02:
                   8684:                REG8(AL) = status;
                   8685:                break;
                   8686:        default:
1.1.1.22  root     8687:                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     8688:                m_CF = 1;
1.1       root     8689:                break;
                   8690:        }
                   8691: }
                   8692: 
1.1.1.24  root     8693: inline void pcbios_int_16h_55h()
                   8694: {
                   8695:        switch(REG8(AL)) {
                   8696:        case 0x00:
                   8697:                // keyboard tsr is not present
                   8698:                break;
                   8699:        case 0xfe:
                   8700:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8701:                break;
                   8702:        case 0xff:
                   8703:                break;
                   8704:        default:
                   8705:                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));
                   8706:                m_CF = 1;
                   8707:                break;
                   8708:        }
                   8709: }
                   8710: 
1.1.1.30  root     8711: inline void pcbios_int_16h_6fh()
                   8712: {
                   8713:        switch(REG8(AL)) {
                   8714:        case 0x00:
                   8715:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8716:                break;
                   8717:        default:
                   8718:                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));
                   8719:                m_CF = 1;
                   8720:                break;
                   8721:        }
                   8722: }
                   8723: 
1.1.1.37  root     8724: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8725: {
                   8726:        UINT8 hi = jis >> 8;
                   8727:        UINT8 lo = jis & 0xff;
                   8728:        
                   8729:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8730:        hi = (hi - 0x21) / 2 + 0x81;
                   8731:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8732:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8733:        
                   8734:        return((hi << 8) + lo);
                   8735: }
                   8736: 
                   8737: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8738: {
                   8739:        UINT8 hi = sjis >> 8;
                   8740:        UINT8 lo = sjis & 0xff;
                   8741:        
                   8742:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8743:                return(0x2121);
                   8744:        }
                   8745:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8746:                return(0x2121);
                   8747:        }
                   8748:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8749:                // gaiji
                   8750:                if(lo >= 0x40 && lo <= 0x7e) {
                   8751:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   8752:                }
                   8753:                if(lo >= 0x80 && lo <= 0x9e) {
                   8754:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   8755:                }
                   8756:                if(lo >= 0x9f && lo <= 0xfc) {
                   8757:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   8758:                }
                   8759:        }
                   8760:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   8761:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   8762:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   8763:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   8764:        
                   8765:        return((hi << 8) + lo);
                   8766: }
                   8767: 
1.1.1.38  root     8768: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   8769: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   8770: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     8771: 
                   8772: void pcbios_printer_out(int c, UINT8 data)
                   8773: {
                   8774:        if(pio[c].conv_mode) {
                   8775:                if(pio[c].sjis_hi != 0) {
                   8776:                        if(!pio[c].jis_mode) {
                   8777:                                printer_out(c, 0x1c);
                   8778:                                printer_out(c, 0x26);
                   8779:                                pio[c].jis_mode = true;
                   8780:                        }
                   8781:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   8782:                        printer_out(c, jis >> 8);
                   8783:                        printer_out(c, jis & 0xff);
                   8784:                        pio[c].sjis_hi = 0;
                   8785:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   8786:                        printer_out(c, data);
                   8787:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8788:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8789:                        }
                   8790:                        pio[c].esc_len++;
                   8791:                        
                   8792:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8793:                        case 0x33: // 1Bh 33h XX
                   8794:                        case 0x4a: // 1Bh 4Ah XX
                   8795:                        case 0x4e: // 1Bh 4Eh XX
                   8796:                        case 0x51: // 1Bh 51h XX
                   8797:                        case 0x55: // 1Bh 55h XX
                   8798:                        case 0x6c: // 1Bh 6Ch XX
                   8799:                        case 0x71: // 1Bh 71h XX
                   8800:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     8801:                                if(pio[c].esc_len == 3) {
                   8802:                                        pio[c].esc_buf[0] = 0x00;
                   8803:                                }
                   8804:                                break;
1.1.1.38  root     8805:                        case 0x24: // 1Bh 24h XX XX
                   8806:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     8807:                                if(pio[c].esc_len == 4) {
                   8808:                                        pio[c].esc_buf[0] = 0x00;
                   8809:                                }
                   8810:                                break;
1.1.1.38  root     8811:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     8812:                                if(pio[c].esc_len >= 3) {
                   8813:                                        switch(pio[c].esc_buf[2]) {
                   8814:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   8815:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   8816:                                                        pio[c].esc_buf[0] = 0x00;
                   8817:                                                }
                   8818:                                                break;
                   8819:                                        case 32: case 33: case 38: case 39: case 40:
                   8820:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   8821:                                                        pio[c].esc_buf[0] = 0x00;
                   8822:                                                }
                   8823:                                                break;
1.1.1.38  root     8824:                                        case 71: case 72: case 73:
                   8825:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   8826:                                                        pio[c].esc_buf[0] = 0x00;
                   8827:                                                }
                   8828:                                                break;
1.1.1.37  root     8829:                                        default:
                   8830:                                                pio[c].esc_buf[0] = 0x00;
                   8831:                                                break;
                   8832:                                        }
                   8833:                                }
                   8834:                                break;
1.1.1.38  root     8835:                        case 0x40: // 1Bh 40h
1.1.1.37  root     8836:                                if(pio[c].jis_mode) {
                   8837:                                        printer_out(c, 0x1c);
                   8838:                                        printer_out(c, 0x2e);
                   8839:                                        pio[c].jis_mode = false;
                   8840:                                }
                   8841:                                pio[c].esc_buf[0] = 0x00;
                   8842:                                break;
1.1.1.38  root     8843:                        case 0x42: // 1Bh 42h data 00h
                   8844:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8845:                                if(pio[c].esc_len >= 3 && data == 0) {
                   8846:                                        pio[c].esc_buf[0] = 0x00;
                   8847:                                }
                   8848:                                break;
1.1.1.38  root     8849:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     8850:                                if(pio[c].esc_len >= 3 && data != 0) {
                   8851:                                        pio[c].esc_buf[0] = 0x00;
                   8852:                                }
                   8853:                                break;
1.1.1.38  root     8854:                        default: // 1Bh XX
1.1.1.37  root     8855:                                pio[c].esc_buf[0] = 0x00;
                   8856:                                break;
                   8857:                        }
                   8858:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   8859:                        printer_out(c, data);
                   8860:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8861:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8862:                        }
                   8863:                        pio[c].esc_len++;
                   8864:                        
                   8865:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8866:                        case 0x21: // 1Ch 21h XX
                   8867:                        case 0x2d: // 1Ch 2Dh XX
                   8868:                        case 0x57: // 1Ch 57h XX
                   8869:                        case 0x6b: // 1Ch 6Bh XX
                   8870:                        case 0x72: // 1Ch 72h XX
                   8871:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     8872:                                if(pio[c].esc_len == 3) {
                   8873:                                        pio[c].esc_buf[0] = 0x00;
                   8874:                                }
                   8875:                                break;
1.1.1.38  root     8876:                        case 0x26: // 1Ch 26h
1.1.1.37  root     8877:                                pio[c].jis_mode = true;
                   8878:                                pio[c].esc_buf[0] = 0x00;
                   8879:                                break;
1.1.1.38  root     8880:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     8881:                                pio[c].jis_mode = false;
                   8882:                                pio[c].esc_buf[0] = 0x00;
                   8883:                                break;
1.1.1.38  root     8884:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     8885:                                if(pio[c].esc_len == 76) {
                   8886:                                        pio[c].esc_buf[0] = 0x00;
                   8887:                                }
                   8888:                                break;
1.1.1.38  root     8889:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8890:                                if(pio[c].esc_len == 6) {
                   8891:                                        pio[c].esc_buf[0] = 0x00;
                   8892:                                }
                   8893:                                break;
1.1.1.38  root     8894:                        case 0x53: // 1Ch 53h XX XX
                   8895:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     8896:                                if(pio[c].esc_len == 4) {
                   8897:                                        pio[c].esc_buf[0] = 0x00;
                   8898:                                }
                   8899:                                break;
1.1.1.38  root     8900:                        default: // 1Ch XX
1.1.1.37  root     8901:                                pio[c].esc_buf[0] = 0x00;
                   8902:                                break;
                   8903:                        }
                   8904:                } else if(data == 0x1b || data == 0x1c) {
                   8905:                        printer_out(c, data);
                   8906:                        pio[c].esc_buf[0] = data;
                   8907:                        pio[c].esc_len = 1;
                   8908:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   8909:                        pio[c].sjis_hi = data;
                   8910:                } else {
                   8911:                        if(pio[c].jis_mode) {
                   8912:                                printer_out(c, 0x1c);
                   8913:                                printer_out(c, 0x2e);
                   8914:                                pio[c].jis_mode = false;
                   8915:                        }
                   8916:                        printer_out(c, data);
                   8917:                }
                   8918:        } else {
                   8919:                if(pio[c].jis_mode) {
                   8920:                        printer_out(c, 0x1c);
                   8921:                        printer_out(c, 0x2e);
                   8922:                        pio[c].jis_mode = false;
                   8923:                }
                   8924:                printer_out(c, data);
                   8925:        }
                   8926: }
                   8927: 
                   8928: inline void pcbios_int_17h_00h()
                   8929: {
                   8930:        if(REG16(DX) < 3) {
                   8931:                pcbios_printer_out(REG16(DX), REG8(AL));
                   8932:                REG8(AH) = 0xd0;
                   8933:        }
                   8934: }
                   8935: 
                   8936: inline void pcbios_int_17h_01h()
                   8937: {
                   8938:        if(REG16(DX) < 3) {
                   8939:                REG8(AH) = 0xd0;
                   8940:        }
                   8941: }
                   8942: 
                   8943: inline void pcbios_int_17h_02h()
                   8944: {
                   8945:        if(REG16(DX) < 3) {
                   8946:                REG8(AH) = 0xd0;
                   8947:        }
                   8948: }
                   8949: 
                   8950: inline void pcbios_int_17h_03h()
                   8951: {
                   8952:        switch(REG8(AL)) {
                   8953:        case 0x00:
                   8954:                if(REG16(DX) < 3) {
                   8955:                        if(pio[REG16(DX)].jis_mode) {
                   8956:                                printer_out(REG16(DX), 0x1c);
                   8957:                                printer_out(REG16(DX), 0x2e);
                   8958:                                pio[REG16(DX)].jis_mode = false;
                   8959:                        }
                   8960:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   8961:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   8962:                        }
                   8963:                        REG16(CX) = 0x0000;
                   8964:                        REG8(AH) = 0xd0;
                   8965:                }
                   8966:                break;
                   8967:        default:
                   8968:                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));
                   8969:                break;
                   8970:        }
                   8971: }
                   8972: 
                   8973: inline void pcbios_int_17h_50h()
                   8974: {
                   8975:        switch(REG8(AL)) {
                   8976:        case 0x00:
                   8977:                if(REG16(DX) < 3) {
                   8978:                        if(REG16(BX) = 0x0001) {
                   8979:                                pio[REG16(DX)].conv_mode = false;
                   8980:                                REG8(AL) = 0x00;
                   8981:                        } else if(REG16(BX) = 0x0051) {
                   8982:                                pio[REG16(DX)].conv_mode = true;
                   8983:                                REG8(AL) = 0x00;
                   8984:                        } else {
                   8985:                                REG8(AL) = 0x01;
                   8986:                        }
                   8987:                } else {
                   8988:                        REG8(AL) = 0x02;
                   8989:                }
                   8990:                break;
                   8991:        case 0x01:
                   8992:                if(REG16(DX) < 3) {
                   8993:                        if(pio[REG16(DX)].conv_mode) {
                   8994:                                REG16(BX) = 0x0051;
                   8995:                        } else {
                   8996:                                REG16(BX) = 0x0001;
                   8997:                        }
                   8998:                        REG8(AL) = 0x00;
                   8999:                } else {
                   9000:                        REG8(AL) = 0x02;
                   9001:                }
                   9002:                break;
                   9003:        default:
                   9004:                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));
                   9005:                break;
                   9006:        }
                   9007: }
                   9008: 
                   9009: inline void pcbios_int_17h_51h()
                   9010: {
                   9011:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9012:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9013:        } else {
                   9014:                REG16(DX) = 0x0000;
                   9015:        }
                   9016: }
                   9017: 
                   9018: inline void pcbios_int_17h_52h()
                   9019: {
                   9020:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9021:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9022:        } else {
                   9023:                REG16(DX) = 0x0000;
                   9024:        }
                   9025: }
                   9026: 
                   9027: inline void pcbios_int_17h_84h()
                   9028: {
                   9029:        if(REG16(DX) < 3) {
                   9030:                if(pio[REG16(DX)].jis_mode) {
                   9031:                        printer_out(REG16(DX), 0x1c);
                   9032:                        printer_out(REG16(DX), 0x2e);
                   9033:                        pio[REG16(DX)].jis_mode = false;
                   9034:                }
                   9035:                printer_out(REG16(DX), REG8(AL));
                   9036:                REG8(AH) = 0xd0;
                   9037:        }
                   9038: }
                   9039: 
                   9040: inline void pcbios_int_17h_85h()
                   9041: {
                   9042:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9043: }
                   9044: 
1.1       root     9045: inline void pcbios_int_1ah_00h()
                   9046: {
1.1.1.19  root     9047:        pcbios_update_daily_timer_counter(timeGetTime());
                   9048:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9049:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9050:        REG8(AL) = mem[0x470];
                   9051:        mem[0x470] = 0;
1.1       root     9052: }
                   9053: 
                   9054: inline int to_bcd(int t)
                   9055: {
                   9056:        int u = (t % 100) / 10;
                   9057:        return (u << 4) | (t % 10);
                   9058: }
                   9059: 
                   9060: inline void pcbios_int_1ah_02h()
                   9061: {
                   9062:        SYSTEMTIME time;
                   9063:        
                   9064:        GetLocalTime(&time);
                   9065:        REG8(CH) = to_bcd(time.wHour);
                   9066:        REG8(CL) = to_bcd(time.wMinute);
                   9067:        REG8(DH) = to_bcd(time.wSecond);
                   9068:        REG8(DL) = 0x00;
                   9069: }
                   9070: 
                   9071: inline void pcbios_int_1ah_04h()
                   9072: {
                   9073:        SYSTEMTIME time;
                   9074:        
                   9075:        GetLocalTime(&time);
                   9076:        REG8(CH) = to_bcd(time.wYear / 100);
                   9077:        REG8(CL) = to_bcd(time.wYear);
                   9078:        REG8(DH) = to_bcd(time.wMonth);
                   9079:        REG8(DL) = to_bcd(time.wDay);
                   9080: }
                   9081: 
                   9082: inline void pcbios_int_1ah_0ah()
                   9083: {
                   9084:        SYSTEMTIME time;
                   9085:        FILETIME file_time;
                   9086:        WORD dos_date, dos_time;
                   9087:        
                   9088:        GetLocalTime(&time);
                   9089:        SystemTimeToFileTime(&time, &file_time);
                   9090:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9091:        REG16(CX) = dos_date;
                   9092: }
                   9093: 
                   9094: // msdos system call
                   9095: 
1.1.1.43  root     9096: inline void msdos_int_21h_56h(int lfn);
                   9097: 
1.1       root     9098: inline void msdos_int_21h_00h()
                   9099: {
1.1.1.3   root     9100:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9101: }
                   9102: 
1.1.1.35  root     9103: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9104: {
                   9105:        REG8(AL) = msdos_getche();
1.1.1.33  root     9106:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9107:        
1.1.1.35  root     9108: #ifdef USE_SERVICE_THREAD
                   9109:        service_exit = true;
                   9110: #endif
                   9111:        return(0);
                   9112: }
                   9113: 
                   9114: inline void msdos_int_21h_01h()
                   9115: {
                   9116: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9117:        if(!in_service && !in_service_29h &&
                   9118:           *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     9119:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9120:                // msdos_putch() will be used in this service
                   9121:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9122:                start_service_loop(msdos_int_21h_01h_thread);
                   9123:        } else {
                   9124: #endif
                   9125:                msdos_int_21h_01h_thread(NULL);
                   9126:                REQUEST_HARDWRE_UPDATE();
                   9127: #ifdef USE_SERVICE_THREAD
                   9128:        }
1.1.1.35  root     9129: #endif
1.1       root     9130: }
                   9131: 
                   9132: inline void msdos_int_21h_02h()
                   9133: {
1.1.1.33  root     9134:        UINT8 data = REG8(DL);
                   9135:        msdos_putch(data);
                   9136:        REG8(AL) = data;
                   9137:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9138: }
                   9139: 
                   9140: inline void msdos_int_21h_03h()
                   9141: {
                   9142:        REG8(AL) = msdos_aux_in();
                   9143: }
                   9144: 
                   9145: inline void msdos_int_21h_04h()
                   9146: {
                   9147:        msdos_aux_out(REG8(DL));
                   9148: }
                   9149: 
                   9150: inline void msdos_int_21h_05h()
                   9151: {
                   9152:        msdos_prn_out(REG8(DL));
                   9153: }
                   9154: 
                   9155: inline void msdos_int_21h_06h()
                   9156: {
                   9157:        if(REG8(DL) == 0xff) {
                   9158:                if(msdos_kbhit()) {
                   9159:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9160: #if defined(HAS_I386)
                   9161:                        m_ZF = 0;
                   9162: #else
                   9163:                        m_ZeroVal = 1;
                   9164: #endif
1.1       root     9165:                } else {
                   9166:                        REG8(AL) = 0;
1.1.1.3   root     9167: #if defined(HAS_I386)
                   9168:                        m_ZF = 1;
                   9169: #else
                   9170:                        m_ZeroVal = 0;
                   9171: #endif
1.1.1.14  root     9172:                        maybe_idle();
1.1       root     9173:                }
                   9174:        } else {
1.1.1.33  root     9175:                UINT8 data = REG8(DL);
                   9176:                msdos_putch(data);
                   9177:                REG8(AL) = data;
1.1       root     9178:        }
                   9179: }
                   9180: 
1.1.1.35  root     9181: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9182: {
                   9183:        REG8(AL) = msdos_getch();
1.1.1.26  root     9184:        
1.1.1.35  root     9185: #ifdef USE_SERVICE_THREAD
                   9186:        service_exit = true;
                   9187: #endif
                   9188:        return(0);
1.1       root     9189: }
                   9190: 
1.1.1.35  root     9191: inline void msdos_int_21h_07h()
                   9192: {
                   9193: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9194:        if(!in_service && !in_service_29h) {
                   9195:                start_service_loop(msdos_int_21h_07h_thread);
                   9196:        } else {
                   9197: #endif
                   9198:                msdos_int_21h_07h_thread(NULL);
                   9199:                REQUEST_HARDWRE_UPDATE();
                   9200: #ifdef USE_SERVICE_THREAD
                   9201:        }
1.1.1.35  root     9202: #endif
                   9203: }
                   9204: 
                   9205: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9206: {
                   9207:        REG8(AL) = msdos_getch();
1.1.1.33  root     9208:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9209:        
1.1.1.35  root     9210: #ifdef USE_SERVICE_THREAD
                   9211:        service_exit = true;
                   9212: #endif
                   9213:        return(0);
                   9214: }
                   9215: 
                   9216: inline void msdos_int_21h_08h()
                   9217: {
                   9218: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9219:        if(!in_service && !in_service_29h) {
                   9220:                start_service_loop(msdos_int_21h_08h_thread);
                   9221:        } else {
                   9222: #endif
                   9223:                msdos_int_21h_08h_thread(NULL);
                   9224:                REQUEST_HARDWRE_UPDATE();
                   9225: #ifdef USE_SERVICE_THREAD
                   9226:        }
1.1.1.35  root     9227: #endif
1.1       root     9228: }
                   9229: 
                   9230: inline void msdos_int_21h_09h()
                   9231: {
1.1.1.21  root     9232:        msdos_stdio_reopen();
                   9233:        
1.1.1.20  root     9234:        process_t *process = msdos_process_info_get(current_psp);
                   9235:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9236:        
1.1.1.14  root     9237:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9238:        int len = 0;
1.1       root     9239:        
1.1.1.14  root     9240:        while(str[len] != '$' && len < 0x10000) {
                   9241:                len++;
                   9242:        }
1.1.1.20  root     9243:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9244:                // stdout is redirected to file
1.1.1.20  root     9245:                msdos_write(fd, str, len);
1.1       root     9246:        } else {
                   9247:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9248:                        msdos_putch(str[i]);
1.1       root     9249:                }
                   9250:        }
1.1.1.33  root     9251:        REG8(AL) = '$';
                   9252:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9253: }
                   9254: 
1.1.1.35  root     9255: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9256: {
1.1.1.3   root     9257:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9258:        int max = mem[ofs] - 1;
                   9259:        UINT8 *buf = mem + ofs + 2;
                   9260:        int chr, p = 0;
                   9261:        
                   9262:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9263:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9264:                        p = 0;
1.1.1.33  root     9265:                        msdos_putch(0x03);
                   9266:                        msdos_putch(0x0d);
                   9267:                        msdos_putch(0x0a);
1.1.1.26  root     9268:                        break;
1.1.1.33  root     9269:                } else if(ctrl_break_pressed) {
                   9270:                        // skip this byte
1.1.1.26  root     9271:                } else if(chr == 0x00) {
1.1       root     9272:                        // skip 2nd byte
                   9273:                        msdos_getch();
                   9274:                } else if(chr == 0x08) {
                   9275:                        // back space
                   9276:                        if(p > 0) {
                   9277:                                p--;
1.1.1.20  root     9278:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9279:                                        msdos_putch(0x08);
                   9280:                                        msdos_putch(0x08);
                   9281:                                        msdos_putch(0x20);
                   9282:                                        msdos_putch(0x20);
                   9283:                                        msdos_putch(0x08);
                   9284:                                        msdos_putch(0x08);
1.1.1.36  root     9285:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9286:                                        p--;
                   9287:                                        msdos_putch(0x08);
                   9288:                                        msdos_putch(0x08);
                   9289:                                        msdos_putch(0x20);
                   9290:                                        msdos_putch(0x20);
                   9291:                                        msdos_putch(0x08);
                   9292:                                        msdos_putch(0x08);
1.1.1.34  root     9293:                                } else {
                   9294:                                        msdos_putch(0x08);
                   9295:                                        msdos_putch(0x20);
                   9296:                                        msdos_putch(0x08);
                   9297:                                }
                   9298:                        }
                   9299:                } else if(chr == 0x1b) {
                   9300:                        // escape
                   9301:                        while(p > 0) {
                   9302:                                p--;
                   9303:                                if(msdos_ctrl_code_check(buf[p])) {
                   9304:                                        msdos_putch(0x08);
                   9305:                                        msdos_putch(0x08);
                   9306:                                        msdos_putch(0x20);
                   9307:                                        msdos_putch(0x20);
                   9308:                                        msdos_putch(0x08);
                   9309:                                        msdos_putch(0x08);
1.1.1.20  root     9310:                                } else {
1.1.1.34  root     9311:                                        msdos_putch(0x08);
                   9312:                                        msdos_putch(0x20);
                   9313:                                        msdos_putch(0x08);
1.1.1.20  root     9314:                                }
1.1       root     9315:                        }
                   9316:                } else if(p < max) {
                   9317:                        buf[p++] = chr;
                   9318:                        msdos_putch(chr);
                   9319:                }
                   9320:        }
                   9321:        buf[p] = 0x0d;
                   9322:        mem[ofs + 1] = p;
1.1.1.33  root     9323:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9324:        
1.1.1.35  root     9325: #ifdef USE_SERVICE_THREAD
                   9326:        service_exit = true;
                   9327: #endif
                   9328:        return(0);
                   9329: }
                   9330: 
                   9331: inline void msdos_int_21h_0ah()
                   9332: {
                   9333:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9334: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9335:                if(!in_service && !in_service_29h &&
                   9336:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     9337:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9338:                        // msdos_putch() will be used in this service
                   9339:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9340:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9341:                } else {
                   9342: #endif
                   9343:                        msdos_int_21h_0ah_thread(NULL);
                   9344:                        REQUEST_HARDWRE_UPDATE();
                   9345: #ifdef USE_SERVICE_THREAD
                   9346:                }
1.1.1.35  root     9347: #endif
                   9348:        }
1.1       root     9349: }
                   9350: 
                   9351: inline void msdos_int_21h_0bh()
                   9352: {
                   9353:        if(msdos_kbhit()) {
                   9354:                REG8(AL) = 0xff;
                   9355:        } else {
                   9356:                REG8(AL) = 0x00;
1.1.1.14  root     9357:                maybe_idle();
1.1       root     9358:        }
1.1.1.33  root     9359:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9360: }
                   9361: 
                   9362: inline void msdos_int_21h_0ch()
                   9363: {
                   9364:        // clear key buffer
1.1.1.21  root     9365:        msdos_stdio_reopen();
                   9366:        
1.1.1.20  root     9367:        process_t *process = msdos_process_info_get(current_psp);
                   9368:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9369:        
                   9370:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9371:                // stdin is redirected to file
                   9372:        } else {
                   9373:                while(msdos_kbhit()) {
                   9374:                        msdos_getch();
                   9375:                }
                   9376:        }
                   9377:        
                   9378:        switch(REG8(AL)) {
                   9379:        case 0x01:
                   9380:                msdos_int_21h_01h();
                   9381:                break;
                   9382:        case 0x06:
                   9383:                msdos_int_21h_06h();
                   9384:                break;
                   9385:        case 0x07:
                   9386:                msdos_int_21h_07h();
                   9387:                break;
                   9388:        case 0x08:
                   9389:                msdos_int_21h_08h();
                   9390:                break;
                   9391:        case 0x0a:
                   9392:                msdos_int_21h_0ah();
                   9393:                break;
                   9394:        default:
1.1.1.48  root     9395:                // the buffer is flushed but no input is attempted
1.1       root     9396:                break;
                   9397:        }
                   9398: }
                   9399: 
                   9400: inline void msdos_int_21h_0dh()
                   9401: {
                   9402: }
                   9403: 
                   9404: inline void msdos_int_21h_0eh()
                   9405: {
                   9406:        if(REG8(DL) < 26) {
                   9407:                _chdrive(REG8(DL) + 1);
                   9408:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9409:                msdos_sda_update(current_psp);
1.1       root     9410:        }
                   9411:        REG8(AL) = 26; // zdrive
                   9412: }
                   9413: 
1.1.1.14  root     9414: inline void msdos_int_21h_0fh()
                   9415: {
                   9416:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9417:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9418:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9419:        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     9420:        
1.1.1.14  root     9421:        if(hFile == INVALID_HANDLE_VALUE) {
                   9422:                REG8(AL) = 0xff;
                   9423:        } else {
                   9424:                REG8(AL) = 0;
                   9425:                fcb->current_block = 0;
                   9426:                fcb->record_size = 128;
                   9427:                fcb->file_size = GetFileSize(hFile, NULL);
                   9428:                fcb->handle = hFile;
                   9429:                fcb->cur_record = 0;
                   9430:        }
                   9431: }
                   9432: 
                   9433: inline void msdos_int_21h_10h()
                   9434: {
                   9435:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9436:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9437:        
                   9438:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9439: }
                   9440: 
1.1       root     9441: inline void msdos_int_21h_11h()
                   9442: {
1.1.1.3   root     9443:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9444:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9445:        
                   9446:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9447:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9448:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9449:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9450:        const char *path = msdos_fcb_path(fcb);
1.1       root     9451:        WIN32_FIND_DATA fd;
                   9452:        
1.1.1.13  root     9453:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9454:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9455:                FindClose(dtainfo->find_handle);
                   9456:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9457:        }
                   9458:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9459:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9460:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9461:        
1.1.1.14  root     9462:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9463:                dtainfo->allowable_mask &= ~8;
1.1       root     9464:        }
1.1.1.14  root     9465:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9466:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9467:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9468:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9469:                                FindClose(dtainfo->find_handle);
                   9470:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9471:                                break;
                   9472:                        }
                   9473:                }
                   9474:        }
1.1.1.13  root     9475:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9476:                if(ext_fcb->flag == 0xff) {
                   9477:                        ext_find->flag = 0xff;
                   9478:                        memset(ext_find->reserved, 0, 5);
                   9479:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9480:                }
                   9481:                find->drive = _getdrive();
1.1.1.13  root     9482:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9483:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9484:                find->nt_res = 0;
                   9485:                msdos_find_file_conv_local_time(&fd);
                   9486:                find->create_time_ms = 0;
                   9487:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9488:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9489:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9490:                find->cluster_hi = find->cluster_lo = 0;
                   9491:                find->file_size = fd.nFileSizeLow;
                   9492:                REG8(AL) = 0x00;
1.1.1.14  root     9493:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9494:                if(ext_fcb->flag == 0xff) {
                   9495:                        ext_find->flag = 0xff;
                   9496:                        memset(ext_find->reserved, 0, 5);
                   9497:                        ext_find->attribute = 8;
                   9498:                }
                   9499:                find->drive = _getdrive();
                   9500:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9501:                find->attribute = 8;
                   9502:                find->nt_res = 0;
                   9503:                msdos_find_file_conv_local_time(&fd);
                   9504:                find->create_time_ms = 0;
                   9505:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9506:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9507:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9508:                find->cluster_hi = find->cluster_lo = 0;
                   9509:                find->file_size = 0;
1.1.1.14  root     9510:                dtainfo->allowable_mask &= ~8;
1.1       root     9511:                REG8(AL) = 0x00;
                   9512:        } else {
                   9513:                REG8(AL) = 0xff;
                   9514:        }
                   9515: }
                   9516: 
                   9517: inline void msdos_int_21h_12h()
                   9518: {
1.1.1.3   root     9519:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9520: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9521:        
                   9522:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9523:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9524:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9525:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9526:        WIN32_FIND_DATA fd;
                   9527:        
1.1.1.13  root     9528:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9529:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9530:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9531:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9532:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9533:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9534:                                        FindClose(dtainfo->find_handle);
                   9535:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9536:                                        break;
                   9537:                                }
                   9538:                        }
                   9539:                } else {
1.1.1.13  root     9540:                        FindClose(dtainfo->find_handle);
                   9541:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9542:                }
                   9543:        }
1.1.1.13  root     9544:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9545:                if(ext_fcb->flag == 0xff) {
                   9546:                        ext_find->flag = 0xff;
                   9547:                        memset(ext_find->reserved, 0, 5);
                   9548:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9549:                }
                   9550:                find->drive = _getdrive();
1.1.1.13  root     9551:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9552:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9553:                find->nt_res = 0;
                   9554:                msdos_find_file_conv_local_time(&fd);
                   9555:                find->create_time_ms = 0;
                   9556:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9557:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9558:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9559:                find->cluster_hi = find->cluster_lo = 0;
                   9560:                find->file_size = fd.nFileSizeLow;
                   9561:                REG8(AL) = 0x00;
1.1.1.14  root     9562:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9563:                if(ext_fcb->flag == 0xff) {
                   9564:                        ext_find->flag = 0xff;
                   9565:                        memset(ext_find->reserved, 0, 5);
                   9566:                        ext_find->attribute = 8;
                   9567:                }
                   9568:                find->drive = _getdrive();
                   9569:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9570:                find->attribute = 8;
                   9571:                find->nt_res = 0;
                   9572:                msdos_find_file_conv_local_time(&fd);
                   9573:                find->create_time_ms = 0;
                   9574:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9575:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9576:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9577:                find->cluster_hi = find->cluster_lo = 0;
                   9578:                find->file_size = 0;
1.1.1.14  root     9579:                dtainfo->allowable_mask &= ~8;
1.1       root     9580:                REG8(AL) = 0x00;
                   9581:        } else {
                   9582:                REG8(AL) = 0xff;
                   9583:        }
                   9584: }
                   9585: 
                   9586: inline void msdos_int_21h_13h()
                   9587: {
1.1.1.3   root     9588:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9589:                REG8(AL) = 0xff;
                   9590:        } else {
                   9591:                REG8(AL) = 0x00;
                   9592:        }
                   9593: }
                   9594: 
1.1.1.16  root     9595: inline void msdos_int_21h_14h()
                   9596: {
                   9597:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9598:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9599:        process_t *process = msdos_process_info_get(current_psp);
                   9600:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9601:        DWORD num = 0;
                   9602:        
                   9603:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9604:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9605:                REG8(AL) = 1;
                   9606:        } else {
                   9607:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9608:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9609:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9610:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9611:        }
                   9612: }
                   9613: 
                   9614: inline void msdos_int_21h_15h()
1.1.1.14  root     9615: {
                   9616:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9617:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9618:        process_t *process = msdos_process_info_get(current_psp);
                   9619:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9620:        DWORD num = 0;
1.1.1.14  root     9621:        
1.1.1.16  root     9622:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9623:                REG8(AL) = 1;
                   9624:        } else {
                   9625:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9626:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9627:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9628:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9629:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9630:        }
                   9631: }
                   9632: 
                   9633: inline void msdos_int_21h_16h()
                   9634: {
                   9635:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9636:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9637:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9638:        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     9639:        
1.1.1.14  root     9640:        if(hFile == INVALID_HANDLE_VALUE) {
                   9641:                REG8(AL) = 0xff;
                   9642:        } else {
                   9643:                REG8(AL) = 0;
                   9644:                fcb->current_block = 0;
                   9645:                fcb->record_size = 128;
                   9646:                fcb->file_size = 0;
                   9647:                fcb->handle = hFile;
                   9648:                fcb->cur_record = 0;
                   9649:        }
                   9650: }
                   9651: 
1.1.1.16  root     9652: inline void msdos_int_21h_17h()
                   9653: {
                   9654:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9655:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     9656: //     const char *path_src = msdos_fcb_path(fcb_src);
                   9657:        char path_src[MAX_PATH];
                   9658:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   9659:        
1.1.1.16  root     9660:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9661:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     9662: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   9663:        char path_dst[MAX_PATH];
                   9664:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9665:        
                   9666:        if(rename(path_src, path_dst)) {
                   9667:                REG8(AL) = 0xff;
                   9668:        } else {
                   9669:                REG8(AL) = 0;
                   9670:        }
                   9671: }
                   9672: 
1.1       root     9673: inline void msdos_int_21h_18h()
                   9674: {
                   9675:        REG8(AL) = 0x00;
                   9676: }
                   9677: 
                   9678: inline void msdos_int_21h_19h()
                   9679: {
                   9680:        REG8(AL) = _getdrive() - 1;
                   9681: }
                   9682: 
                   9683: inline void msdos_int_21h_1ah()
                   9684: {
                   9685:        process_t *process = msdos_process_info_get(current_psp);
                   9686:        
                   9687:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9688:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9689:        msdos_sda_update(current_psp);
1.1       root     9690: }
                   9691: 
                   9692: inline void msdos_int_21h_1bh()
                   9693: {
                   9694:        int drive_num = _getdrive() - 1;
                   9695:        UINT16 seg, ofs;
                   9696:        
                   9697:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9698:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9699:                REG8(AL) = dpb->highest_sector_num + 1;
                   9700:                REG16(CX) = dpb->bytes_per_sector;
                   9701:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9702:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9703:        } else {
                   9704:                REG8(AL) = 0xff;
1.1.1.3   root     9705:                m_CF = 1;
1.1       root     9706:        }
                   9707: 
                   9708: }
                   9709: 
                   9710: inline void msdos_int_21h_1ch()
                   9711: {
1.1.1.41  root     9712:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9713:        UINT16 seg, ofs;
                   9714:        
                   9715:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9716:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9717:                REG8(AL) = dpb->highest_sector_num + 1;
                   9718:                REG16(CX) = dpb->bytes_per_sector;
                   9719:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9720:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9721:        } else {
                   9722:                REG8(AL) = 0xff;
1.1.1.3   root     9723:                m_CF = 1;
1.1       root     9724:        }
                   9725: 
                   9726: }
                   9727: 
                   9728: inline void msdos_int_21h_1dh()
                   9729: {
                   9730:        REG8(AL) = 0;
                   9731: }
                   9732: 
                   9733: inline void msdos_int_21h_1eh()
                   9734: {
                   9735:        REG8(AL) = 0;
                   9736: }
                   9737: 
                   9738: inline void msdos_int_21h_1fh()
                   9739: {
                   9740:        int drive_num = _getdrive() - 1;
                   9741:        UINT16 seg, ofs;
                   9742:        
                   9743:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9744:                REG8(AL) = 0;
1.1.1.3   root     9745:                SREG(DS) = seg;
                   9746:                i386_load_segment_descriptor(DS);
1.1       root     9747:                REG16(BX) = ofs;
                   9748:        } else {
                   9749:                REG8(AL) = 0xff;
1.1.1.3   root     9750:                m_CF = 1;
1.1       root     9751:        }
                   9752: }
                   9753: 
                   9754: inline void msdos_int_21h_20h()
                   9755: {
                   9756:        REG8(AL) = 0;
                   9757: }
                   9758: 
1.1.1.14  root     9759: inline void msdos_int_21h_21h()
                   9760: {
                   9761:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9762:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9763:        
                   9764:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9765:                REG8(AL) = 1;
                   9766:        } else {
                   9767:                process_t *process = msdos_process_info_get(current_psp);
                   9768:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9769:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9770:                DWORD num = 0;
1.1.1.14  root     9771:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9772:                        REG8(AL) = 1;
                   9773:                } else {
                   9774:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9775:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9776:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9777:                }
                   9778:        }
                   9779: }
                   9780: 
                   9781: inline void msdos_int_21h_22h()
                   9782: {
                   9783:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9784:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9785:        
                   9786:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9787:                REG8(AL) = 0xff;
                   9788:        } else {
                   9789:                process_t *process = msdos_process_info_get(current_psp);
                   9790:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9791:                DWORD num = 0;
1.1.1.14  root     9792:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9793:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9794:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9795:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9796:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9797:        }
                   9798: }
                   9799: 
1.1.1.16  root     9800: inline void msdos_int_21h_23h()
                   9801: {
                   9802:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9803:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9804:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     9805:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9806:        
                   9807:        if(hFile == INVALID_HANDLE_VALUE) {
                   9808:                REG8(AL) = 0xff;
                   9809:        } else {
                   9810:                UINT32 size = GetFileSize(hFile, NULL);
                   9811:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9812:                REG8(AL) = 0;
                   9813:        }
                   9814: }
                   9815: 
                   9816: inline void msdos_int_21h_24h()
                   9817: {
                   9818:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9819:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9820:        
                   9821:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9822: }
                   9823: 
1.1       root     9824: inline void msdos_int_21h_25h()
                   9825: {
                   9826:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9827:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9828: }
                   9829: 
                   9830: inline void msdos_int_21h_26h()
                   9831: {
                   9832:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9833:        
                   9834:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     9835:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     9836:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9837:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9838:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9839:        psp->parent_psp = 0;
                   9840: }
                   9841: 
1.1.1.16  root     9842: inline void msdos_int_21h_27h()
                   9843: {
                   9844:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9845:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9846:        
                   9847:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9848:                REG8(AL) = 1;
                   9849:        } else {
                   9850:                process_t *process = msdos_process_info_get(current_psp);
                   9851:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9852:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   9853:                DWORD num = 0;
                   9854:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   9855:                        REG8(AL) = 1;
                   9856:                } else {
                   9857:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9858:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9859:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9860:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9861:                }
                   9862:        }
                   9863: }
                   9864: 
                   9865: inline void msdos_int_21h_28h()
                   9866: {
                   9867:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9868:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9869:        
                   9870:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9871:                REG8(AL) = 0xff;
                   9872:        } else {
                   9873:                process_t *process = msdos_process_info_get(current_psp);
                   9874:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9875:                DWORD num = 0;
                   9876:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   9877:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9878:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9879:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9880:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9881:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9882:        }
                   9883: }
                   9884: 
1.1       root     9885: inline void msdos_int_21h_29h()
                   9886: {
1.1.1.20  root     9887:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   9888:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     9889:        UINT8 drv = 0;
                   9890:        char sep_chars[] = ":.;,=+";
                   9891:        char end_chars[] = "\\<>|/\"[]";
                   9892:        char spc_chars[] = " \t";
                   9893:        
1.1.1.20  root     9894:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   9895:        buffer[1023] = 0;
                   9896:        memset(name, 0x20, sizeof(name));
                   9897:        memset(ext, 0x20, sizeof(ext));
                   9898:        
1.1       root     9899:        if(REG8(AL) & 1) {
1.1.1.20  root     9900:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   9901:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     9902:                        ofs++;
                   9903:                }
                   9904:        }
1.1.1.20  root     9905:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     9906:        
1.1.1.24  root     9907:        if(buffer[ofs + 1] == ':') {
                   9908:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   9909:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     9910:                        ofs += 2;
1.1.1.24  root     9911:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9912:                                ofs++;
                   9913:                        }
                   9914:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   9915:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     9916:                        ofs += 2;
1.1.1.24  root     9917:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9918:                                ofs++;
                   9919:                        }
1.1       root     9920:                }
                   9921:        }
1.1.1.20  root     9922:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9923:                UINT8 c = buffer[ofs];
                   9924:                if(is_kanji) {
                   9925:                        is_kanji = 0;
                   9926:                } else if(msdos_lead_byte_check(c)) {
                   9927:                        is_kanji = 1;
                   9928:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9929:                        break;
                   9930:                } else if(c >= 'a' && c <= 'z') {
                   9931:                        c -= 0x20;
                   9932:                }
                   9933:                ofs++;
                   9934:                name[i] = c;
                   9935:        }
1.1.1.20  root     9936:        if(buffer[ofs] == '.') {
1.1       root     9937:                ofs++;
1.1.1.20  root     9938:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9939:                        UINT8 c = buffer[ofs];
                   9940:                        if(is_kanji) {
                   9941:                                is_kanji = 0;
                   9942:                        } else if(msdos_lead_byte_check(c)) {
                   9943:                                is_kanji = 1;
                   9944:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9945:                                break;
                   9946:                        } else if(c >= 'a' && c <= 'z') {
                   9947:                                c -= 0x20;
                   9948:                        }
                   9949:                        ofs++;
                   9950:                        ext[i] = c;
                   9951:                }
                   9952:        }
1.1.1.20  root     9953:        int si = REG16(SI) + ofs;
1.1.1.3   root     9954:        int ds = SREG(DS);
1.1       root     9955:        while(si > 0xffff) {
                   9956:                si -= 0x10;
                   9957:                ds++;
                   9958:        }
                   9959:        REG16(SI) = si;
1.1.1.3   root     9960:        SREG(DS) = ds;
                   9961:        i386_load_segment_descriptor(DS);
1.1       root     9962:        
1.1.1.3   root     9963:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     9964:        if(!(REG8(AL) & 2) || drv != 0) {
                   9965:                fcb[0] = drv;
                   9966:        }
                   9967:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   9968:                memcpy(fcb + 1, name, 8);
                   9969:        }
                   9970:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   9971:                memcpy(fcb + 9, ext, 3);
                   9972:        }
                   9973:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     9974:                if(fcb[i] == '*') {
                   9975:                        found_star = 1;
                   9976:                }
                   9977:                if(found_star) {
                   9978:                        fcb[i] = '?';
                   9979:                }
                   9980:        }
1.1.1.20  root     9981:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9982:                if(fcb[i] == '*') {
                   9983:                        found_star = 1;
                   9984:                }
                   9985:                if(found_star) {
                   9986:                        fcb[i] = '?';
                   9987:                }
                   9988:        }
                   9989:        
1.1.1.44  root     9990:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     9991:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   9992:                        REG8(AL) = 0x01;
1.1.1.20  root     9993:                } else {
                   9994:                        REG8(AL) = 0x00;
1.1       root     9995:                }
                   9996:        } else {
                   9997:                REG8(AL) = 0xff;
                   9998:        }
                   9999: }
                   10000: 
                   10001: inline void msdos_int_21h_2ah()
                   10002: {
                   10003:        SYSTEMTIME sTime;
                   10004:        
                   10005:        GetLocalTime(&sTime);
                   10006:        REG16(CX) = sTime.wYear;
                   10007:        REG8(DH) = (UINT8)sTime.wMonth;
                   10008:        REG8(DL) = (UINT8)sTime.wDay;
                   10009:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10010: }
                   10011: 
                   10012: inline void msdos_int_21h_2bh()
                   10013: {
1.1.1.14  root     10014:        REG8(AL) = 0xff;
1.1       root     10015: }
                   10016: 
                   10017: inline void msdos_int_21h_2ch()
                   10018: {
                   10019:        SYSTEMTIME sTime;
                   10020:        
                   10021:        GetLocalTime(&sTime);
                   10022:        REG8(CH) = (UINT8)sTime.wHour;
                   10023:        REG8(CL) = (UINT8)sTime.wMinute;
                   10024:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10025:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10026: }
                   10027: 
                   10028: inline void msdos_int_21h_2dh()
                   10029: {
                   10030:        REG8(AL) = 0x00;
                   10031: }
                   10032: 
                   10033: inline void msdos_int_21h_2eh()
                   10034: {
                   10035:        process_t *process = msdos_process_info_get(current_psp);
                   10036:        
                   10037:        process->verify = REG8(AL);
                   10038: }
                   10039: 
                   10040: inline void msdos_int_21h_2fh()
                   10041: {
                   10042:        process_t *process = msdos_process_info_get(current_psp);
                   10043:        
                   10044:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10045:        SREG(ES) = process->dta.w.h;
                   10046:        i386_load_segment_descriptor(ES);
1.1       root     10047: }
                   10048: 
                   10049: inline void msdos_int_21h_30h()
                   10050: {
                   10051:        // Version Flag / OEM
1.1.1.27  root     10052:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10053: #ifdef SUPPORT_HMA
                   10054:                REG16(BX) = 0x0000;
                   10055: #else
                   10056:                REG16(BX) = 0x1000; // DOS is in HMA
                   10057: #endif
1.1       root     10058:        } else {
1.1.1.27  root     10059:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10060:                // but this is not correct on Windows 98 SE
                   10061: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10062:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10063:        }
1.1.1.27  root     10064:        REG16(CX) = 0x0000;
1.1.1.30  root     10065:        REG8(AL) = dos_major_version;   // 7
                   10066:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10067: }
                   10068: 
                   10069: inline void msdos_int_21h_31h()
                   10070: {
1.1.1.29  root     10071:        try {
                   10072:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10073:        } catch(...) {
                   10074:                // recover the broken mcb
                   10075:                int mcb_seg = current_psp - 1;
                   10076:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10077:                
1.1.1.29  root     10078:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10079:                        mcb->mz = 'M';
                   10080:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10081:                        
1.1.1.29  root     10082:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10083:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10084:                        } else {
1.1.1.39  root     10085:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10086:                        }
                   10087:                } else {
                   10088:                        mcb->mz = 'Z';
1.1.1.30  root     10089:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10090:                }
                   10091:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10092:        }
1.1       root     10093:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10094: }
                   10095: 
                   10096: inline void msdos_int_21h_32h()
                   10097: {
                   10098:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10099:        UINT16 seg, ofs;
                   10100:        
                   10101:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10102:                REG8(AL) = 0;
1.1.1.3   root     10103:                SREG(DS) = seg;
                   10104:                i386_load_segment_descriptor(DS);
1.1       root     10105:                REG16(BX) = ofs;
                   10106:        } else {
                   10107:                REG8(AL) = 0xff;
1.1.1.3   root     10108:                m_CF = 1;
1.1       root     10109:        }
                   10110: }
                   10111: 
                   10112: inline void msdos_int_21h_33h()
                   10113: {
                   10114:        char path[MAX_PATH];
1.1.1.48  root     10115:        char drive = 3; // C:
1.1       root     10116:        
                   10117:        switch(REG8(AL)) {
                   10118:        case 0x00:
1.1.1.33  root     10119:                REG8(DL) = ctrl_break_checking;
1.1       root     10120:                break;
                   10121:        case 0x01:
1.1.1.33  root     10122:                ctrl_break_checking = REG8(DL);
                   10123:                break;
                   10124:        case 0x02:
                   10125:                {
                   10126:                        UINT8 old = ctrl_break_checking;
                   10127:                        ctrl_break_checking = REG8(DL);
                   10128:                        REG8(DL) = old;
                   10129:                }
                   10130:                break;
                   10131:        case 0x03:
                   10132:        case 0x04:
                   10133:                // DOS 4.0+ - Unused
1.1       root     10134:                break;
                   10135:        case 0x05:
1.1.1.48  root     10136:                if(GetSystemDirectory(path, MAX_PATH) != 0) {
                   10137:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10138:                                drive = path[0] - 'a' + 1;
                   10139:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10140:                                drive = path[0] - 'A' + 1;
                   10141:                        }
1.1       root     10142:                }
1.1.1.48  root     10143:                REG8(DL) = (UINT8)drive;
1.1       root     10144:                break;
                   10145:        case 0x06:
1.1.1.2   root     10146:                // MS-DOS version (7.10)
1.1       root     10147:                REG8(BL) = 7;
1.1.1.2   root     10148:                REG8(BH) = 10;
1.1       root     10149:                REG8(DL) = 0;
1.1.1.29  root     10150: #ifdef SUPPORT_HMA
                   10151:                REG8(DH) = 0x00;
                   10152: #else
                   10153:                REG8(DH) = 0x10; // DOS is in HMA
                   10154: #endif
1.1       root     10155:                break;
1.1.1.6   root     10156:        case 0x07:
                   10157:                if(REG8(DL) == 0) {
                   10158:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10159:                } else if(REG8(DL) == 1) {
                   10160:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10161:                }
                   10162:                break;
1.1       root     10163:        default:
1.1.1.22  root     10164:                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     10165: //             REG16(AX) = 0x01;
                   10166: //             m_CF = 1;
                   10167:                REG8(AL) = 0xff;
1.1       root     10168:                break;
                   10169:        }
                   10170: }
                   10171: 
1.1.1.23  root     10172: inline void msdos_int_21h_34h()
                   10173: {
                   10174:        SREG(ES) = SDA_TOP >> 4;
                   10175:        i386_load_segment_descriptor(ES);
                   10176:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   10177: }
                   10178: 
1.1       root     10179: inline void msdos_int_21h_35h()
                   10180: {
                   10181:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10182:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10183:        i386_load_segment_descriptor(ES);
1.1       root     10184: }
                   10185: 
                   10186: inline void msdos_int_21h_36h()
                   10187: {
                   10188:        struct _diskfree_t df = {0};
                   10189:        
                   10190:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10191:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10192:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10193:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10194:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10195:        } else {
                   10196:                REG16(AX) = 0xffff;
                   10197:        }
                   10198: }
                   10199: 
                   10200: inline void msdos_int_21h_37h()
                   10201: {
1.1.1.22  root     10202:        static UINT8 dev_flag = 0xff;
1.1       root     10203:        
                   10204:        switch(REG8(AL)) {
                   10205:        case 0x00:
1.1.1.22  root     10206:                {
                   10207:                        process_t *process = msdos_process_info_get(current_psp);
                   10208:                        REG8(AL) = 0x00;
                   10209:                        REG8(DL) = process->switchar;
                   10210:                }
1.1       root     10211:                break;
                   10212:        case 0x01:
1.1.1.22  root     10213:                {
                   10214:                        process_t *process = msdos_process_info_get(current_psp);
                   10215:                        REG8(AL) = 0x00;
                   10216:                        process->switchar = REG8(DL);
1.1.1.23  root     10217:                        msdos_sda_update(current_psp);
1.1.1.22  root     10218:                }
                   10219:                break;
                   10220:        case 0x02:
                   10221:                REG8(DL) = dev_flag;
                   10222:                break;
                   10223:        case 0x03:
                   10224:                dev_flag = REG8(DL);
                   10225:                break;
                   10226:        case 0xd0:
                   10227:        case 0xd1:
                   10228:        case 0xd2:
                   10229:        case 0xd3:
                   10230:        case 0xd4:
                   10231:        case 0xd5:
                   10232:        case 0xd6:
                   10233:        case 0xd7:
                   10234:        case 0xdc:
                   10235:        case 0xdd:
                   10236:        case 0xde:
                   10237:        case 0xdf:
1.1.1.48  root     10238:                // DIET v1.43e
                   10239: //             REG16(AX) = 1;
                   10240:                REG8(AL) = 0xff;
1.1       root     10241:                break;
                   10242:        default:
1.1.1.22  root     10243:                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     10244: //             REG16(AX) = 1;
                   10245:                REG8(AL) = 0xff;
1.1       root     10246:                break;
                   10247:        }
                   10248: }
                   10249: 
1.1.1.52  root     10250: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10251: {
                   10252:        char LCdata[80];
                   10253:        
1.1.1.19  root     10254:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     10255:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10256:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     10257:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10258:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     10259:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10260:        ci->date_format = *LCdata - '0';
1.1.1.42  root     10261:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10262:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     10263:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10264:        *ci->date_sep = *LCdata;
1.1.1.42  root     10265:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10266:        *ci->dec_sep = *LCdata;
1.1.1.42  root     10267:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10268:        *ci->list_sep = *LCdata;
1.1.1.42  root     10269:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10270:        *ci->thou_sep = *LCdata;
1.1.1.42  root     10271:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10272:        *ci->time_sep = *LCdata;
1.1.1.42  root     10273:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10274:        if(strchr(LCdata, 'H') != NULL) {
                   10275:                ci->time_format = 1;
                   10276:        }
1.1.1.49  root     10277:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10278:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.42  root     10279:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10280:        return atoi(LCdata);
                   10281: }
                   10282: 
1.1.1.42  root     10283: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10284: {
                   10285:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10286: }
                   10287: 
1.1.1.43  root     10288: void set_country_info(country_info_t *ci, int size)
                   10289: {
                   10290:        char LCdata[80];
                   10291:        
                   10292:        if(size >= 0x00 + 2) {
                   10293:                memset(LCdata, 0, sizeof(LCdata));
                   10294:                *LCdata = '0' + ci->date_format;
                   10295:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   10296:        }
                   10297:        if(size >= 0x02 + 5) {
                   10298:                memset(LCdata, 0, sizeof(LCdata));
                   10299:                memcpy(LCdata, &ci->currency_symbol, 4);
                   10300:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   10301:        }
                   10302:        if(size >= 0x07 + 2) {
                   10303:                memset(LCdata, 0, sizeof(LCdata));
                   10304:                *LCdata = *ci->thou_sep;
                   10305:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   10306:        }
                   10307:        if(size >= 0x09 + 2) {
                   10308:                memset(LCdata, 0, sizeof(LCdata));
                   10309:                *LCdata = *ci->dec_sep;
                   10310:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   10311:        }
                   10312:        if(size >= 0x0b + 2) {
                   10313:                memset(LCdata, 0, sizeof(LCdata));
                   10314:                *LCdata = *ci->date_sep;
                   10315:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   10316:        }
                   10317:        if(size >= 0x0d + 2) {
                   10318:                memset(LCdata, 0, sizeof(LCdata));
                   10319:                *LCdata = *ci->time_sep;
                   10320:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   10321:        }
                   10322:        if(size >= 0x0f + 1) {
                   10323:                memset(LCdata, 0, sizeof(LCdata));
                   10324:                *LCdata = '0' + ci->currency_format;
                   10325:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   10326:        }
                   10327:        if(size >= 0x10 + 1) {
                   10328:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   10329:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   10330:        }
                   10331:        if(size >= 0x11 + 1) {
                   10332:                // FIXME: is time format always H/h:mm:ss ???
                   10333:                if(ci->time_format & 1) {
                   10334:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10335:                } else {
                   10336:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10337:                }
                   10338:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   10339:        }
                   10340:        if(size >= 0x12 + 4) {
                   10341:                // 12h  DWORD   address of case map routine
                   10342:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10343:        }
                   10344:        if(size >= 0x16 + 2) {
                   10345:                memset(LCdata, 0, sizeof(LCdata));
                   10346:                *LCdata = *ci->list_sep;
                   10347:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10348:        }
                   10349: }
                   10350: 
1.1.1.42  root     10351: #ifndef SUBLANG_SWAHILI
                   10352:        #define SUBLANG_SWAHILI 0x01
                   10353: #endif
                   10354: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10355:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10356: #endif
                   10357: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10358:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10359: #endif
                   10360: #ifndef LANG_BANGLA
                   10361:        #define LANG_BANGLA 0x45
                   10362: #endif
                   10363: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10364:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10365: #endif
                   10366: 
                   10367: static const struct {
                   10368:        int code;
                   10369:        USHORT usPrimaryLanguage;
                   10370:        USHORT usSubLanguage;
                   10371: } country_table[] = {
                   10372:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10373:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10374:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10375:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10376:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10377:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10378: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10379: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10380: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10381: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10382: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10383:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10384:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10385: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10386:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10387: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10388:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10389:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10390:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10391:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10392:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10393:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10394:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10395: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10396: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10397:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10398:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10399:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10400:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10401:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10402: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10403: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10404: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10405:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10406: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10407: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10408: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10409: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10410:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10411:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10412:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10413: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10414:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10415:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10416:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10417:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10418:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10419:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10420:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10421:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10422:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10423:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10424:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10425:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10426:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10427: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10428:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10429:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10430:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10431:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10432:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10433:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10434:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10435:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10436:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10437:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10438: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10439:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10440: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10441:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10442:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10443:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10444:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10445:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10446:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10447:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10448:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10449: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10450:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10451: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10452: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10453:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10454: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10455:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10456:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10457:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10458:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10459:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10460:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10461:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10462: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10463: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10464:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10465: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10466:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10467:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10468:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10469:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10470: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10471: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10472: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10473:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10474:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10475:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10476:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10477:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10478:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10479:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10480:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10481:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10482:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10483:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10484:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10485:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10486:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10487:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10488:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10489:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10490:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10491:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10492:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10493:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10494: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10495:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10496: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10497:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10498:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10499:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10500:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10501:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10502:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10503:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10504:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10505:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10506:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10507:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10508:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10509:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10510:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10511:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10512:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10513:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10514:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10515:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10516:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10517:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10518:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10519:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10520:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10521:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10522: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10523:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10524:        {-1, 0, 0},
                   10525: };
                   10526: 
1.1.1.14  root     10527: inline void msdos_int_21h_38h()
                   10528: {
                   10529:        switch(REG8(AL)) {
                   10530:        case 0x00:
1.1.1.19  root     10531:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10532:                break;
                   10533:        default:
1.1.1.42  root     10534:                for(int i = 0;; i++) {
                   10535:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10536:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10537:                                break;
                   10538:                        } else if(country_table[i].code == -1) {
                   10539: //                             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));
                   10540: //                             REG16(AX) = 2;
                   10541: //                             m_CF = 1;
1.1.1.48  root     10542:                                // get current coutry info
1.1.1.42  root     10543:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10544:                                break;
                   10545:                        }
                   10546:                }
1.1.1.14  root     10547:                break;
                   10548:        }
                   10549: }
                   10550: 
1.1       root     10551: inline void msdos_int_21h_39h(int lfn)
                   10552: {
1.1.1.3   root     10553:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10554:                REG16(AX) = errno;
1.1.1.3   root     10555:                m_CF = 1;
1.1       root     10556:        }
                   10557: }
                   10558: 
                   10559: inline void msdos_int_21h_3ah(int lfn)
                   10560: {
1.1.1.3   root     10561:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10562:                REG16(AX) = errno;
1.1.1.3   root     10563:                m_CF = 1;
1.1       root     10564:        }
                   10565: }
                   10566: 
                   10567: inline void msdos_int_21h_3bh(int lfn)
                   10568: {
1.1.1.45  root     10569:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10570:        
                   10571:        if(_chdir(path)) {
1.1.1.17  root     10572:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10573:                m_CF = 1;
1.1.1.44  root     10574:        } else {
                   10575:                int drv = _getdrive() - 1;
                   10576:                if(path[1] == ':') {
                   10577:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10578:                                drv = path[0] - 'A';
                   10579:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10580:                                drv = path[0] - 'a';
                   10581:                        }
                   10582:                }
                   10583:                msdos_cds_update(drv, path);
1.1       root     10584:        }
                   10585: }
                   10586: 
                   10587: inline void msdos_int_21h_3ch()
                   10588: {
1.1.1.45  root     10589:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10590:        int attr = GetFileAttributes(path);
1.1.1.37  root     10591:        int fd = -1;
                   10592:        int sio_port = 0;
                   10593:        int lpt_port = 0;
1.1       root     10594:        
1.1.1.45  root     10595:        if(msdos_is_device_path(path)) {
                   10596:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10597:        } else {
                   10598:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10599:        }
                   10600:        if(fd != -1) {
                   10601:                if(attr == -1) {
                   10602:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10603:                }
                   10604:                SetFileAttributes(path, attr);
                   10605:                REG16(AX) = fd;
1.1.1.45  root     10606:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10607:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10608:        } else {
                   10609:                REG16(AX) = errno;
1.1.1.3   root     10610:                m_CF = 1;
1.1       root     10611:        }
                   10612: }
                   10613: 
                   10614: inline void msdos_int_21h_3dh()
                   10615: {
1.1.1.45  root     10616:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10617:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10618:        int fd = -1;
                   10619:        int sio_port = 0;
                   10620:        int lpt_port = 0;
1.1       root     10621:        
                   10622:        if(mode < 0x03) {
1.1.1.45  root     10623:                if(msdos_is_device_path(path)) {
                   10624:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10625:                } else {
1.1.1.13  root     10626:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10627:                }
1.1       root     10628:                if(fd != -1) {
                   10629:                        REG16(AX) = fd;
1.1.1.45  root     10630:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10631:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10632:                } else {
                   10633:                        REG16(AX) = errno;
1.1.1.3   root     10634:                        m_CF = 1;
1.1       root     10635:                }
                   10636:        } else {
                   10637:                REG16(AX) = 0x0c;
1.1.1.3   root     10638:                m_CF = 1;
1.1       root     10639:        }
                   10640: }
                   10641: 
                   10642: inline void msdos_int_21h_3eh()
                   10643: {
                   10644:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10645:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10646:        
1.1.1.20  root     10647:        if(fd < process->max_files && file_handler[fd].valid) {
                   10648:                _close(fd);
                   10649:                msdos_file_handler_close(fd);
                   10650:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10651:        } else {
                   10652:                REG16(AX) = 0x06;
1.1.1.3   root     10653:                m_CF = 1;
1.1       root     10654:        }
                   10655: }
                   10656: 
1.1.1.35  root     10657: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10658: {
                   10659:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10660:        int max = REG16(CX);
                   10661:        int p = 0;
                   10662:        
                   10663:        while(max > p) {
                   10664:                int chr = msdos_getch();
                   10665:                
                   10666:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10667:                        p = 0;
                   10668:                        buf[p++] = 0x0d;
                   10669:                        if(max > p) {
                   10670:                                buf[p++] = 0x0a;
                   10671:                        }
                   10672:                        msdos_putch(0x03);
                   10673:                        msdos_putch(0x0d);
                   10674:                        msdos_putch(0x0a);
                   10675:                        break;
                   10676:                } else if(ctrl_break_pressed) {
                   10677:                        // skip this byte
                   10678:                } else if(chr == 0x00) {
                   10679:                        // skip 2nd byte
                   10680:                        msdos_getch();
                   10681:                } else if(chr == 0x0d) {
                   10682:                        // carriage return
                   10683:                        buf[p++] = 0x0d;
                   10684:                        if(max > p) {
                   10685:                                buf[p++] = 0x0a;
                   10686:                        }
                   10687:                        msdos_putch('\n');
                   10688:                        break;
                   10689:                } else if(chr == 0x08) {
                   10690:                        // back space
                   10691:                        if(p > 0) {
                   10692:                                p--;
                   10693:                                if(msdos_ctrl_code_check(buf[p])) {
                   10694:                                        msdos_putch(0x08);
                   10695:                                        msdos_putch(0x08);
                   10696:                                        msdos_putch(0x20);
                   10697:                                        msdos_putch(0x20);
                   10698:                                        msdos_putch(0x08);
                   10699:                                        msdos_putch(0x08);
1.1.1.36  root     10700:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10701:                                        p--;
                   10702:                                        msdos_putch(0x08);
                   10703:                                        msdos_putch(0x08);
                   10704:                                        msdos_putch(0x20);
                   10705:                                        msdos_putch(0x20);
                   10706:                                        msdos_putch(0x08);
                   10707:                                        msdos_putch(0x08);
1.1.1.35  root     10708:                                } else {
                   10709:                                        msdos_putch(0x08);
                   10710:                                        msdos_putch(0x20);
                   10711:                                        msdos_putch(0x08);
                   10712:                                }
                   10713:                        }
                   10714:                } else if(chr == 0x1b) {
                   10715:                        // escape
                   10716:                        while(p > 0) {
                   10717:                                p--;
                   10718:                                if(msdos_ctrl_code_check(buf[p])) {
                   10719:                                        msdos_putch(0x08);
                   10720:                                        msdos_putch(0x08);
                   10721:                                        msdos_putch(0x20);
                   10722:                                        msdos_putch(0x20);
                   10723:                                        msdos_putch(0x08);
                   10724:                                        msdos_putch(0x08);
                   10725:                                } else {
                   10726:                                        msdos_putch(0x08);
                   10727:                                        msdos_putch(0x20);
                   10728:                                        msdos_putch(0x08);
                   10729:                                }
                   10730:                        }
                   10731:                } else {
                   10732:                        buf[p++] = chr;
                   10733:                        msdos_putch(chr);
                   10734:                }
                   10735:        }
                   10736:        REG16(AX) = p;
                   10737:        
                   10738: #ifdef USE_SERVICE_THREAD
                   10739:        service_exit = true;
                   10740: #endif
                   10741:        return(0);
                   10742: }
                   10743: 
1.1       root     10744: inline void msdos_int_21h_3fh()
                   10745: {
                   10746:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10747:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10748:        
1.1.1.20  root     10749:        if(fd < process->max_files && file_handler[fd].valid) {
                   10750:                if(file_mode[file_handler[fd].mode].in) {
                   10751:                        if(file_handler[fd].atty) {
1.1       root     10752:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10753:                                if(REG16(CX) != 0) {
                   10754: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     10755:                                        if(!in_service && !in_service_29h &&
                   10756:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     10757:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   10758:                                                // msdos_putch() will be used in this service
                   10759:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   10760:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   10761:                                        } else {
                   10762: #endif
                   10763:                                                msdos_int_21h_3fh_thread(NULL);
                   10764:                                                REQUEST_HARDWRE_UPDATE();
                   10765: #ifdef USE_SERVICE_THREAD
                   10766:                                        }
1.1.1.35  root     10767: #endif
                   10768:                                } else {
                   10769:                                        REG16(AX) = 0;
1.1       root     10770:                                }
                   10771:                        } else {
1.1.1.37  root     10772:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10773:                        }
                   10774:                } else {
                   10775:                        REG16(AX) = 0x05;
1.1.1.3   root     10776:                        m_CF = 1;
1.1       root     10777:                }
                   10778:        } else {
                   10779:                REG16(AX) = 0x06;
1.1.1.3   root     10780:                m_CF = 1;
1.1       root     10781:        }
                   10782: }
                   10783: 
                   10784: inline void msdos_int_21h_40h()
                   10785: {
                   10786:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10787:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10788:        
1.1.1.20  root     10789:        if(fd < process->max_files && file_handler[fd].valid) {
                   10790:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10791:                        if(REG16(CX)) {
1.1.1.20  root     10792:                                if(file_handler[fd].atty) {
1.1       root     10793:                                        // BX is stdout/stderr or is redirected to stdout
                   10794:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10795:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10796:                                        }
                   10797:                                        REG16(AX) = REG16(CX);
                   10798:                                } else {
1.1.1.20  root     10799:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10800:                                }
                   10801:                        } else {
1.1.1.20  root     10802:                                UINT32 pos = _tell(fd);
                   10803:                                _lseek(fd, 0, SEEK_END);
                   10804:                                UINT32 size = _tell(fd);
1.1.1.12  root     10805:                                if(pos < size) {
1.1.1.20  root     10806:                                        _lseek(fd, pos, SEEK_SET);
                   10807:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10808:                                } else {
                   10809:                                        for(UINT32 i = size; i < pos; i++) {
                   10810:                                                UINT8 tmp = 0;
1.1.1.23  root     10811:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10812:                                        }
1.1.1.20  root     10813:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10814:                                }
1.1.1.23  root     10815:                                REG16(AX) = 0;
1.1       root     10816:                        }
                   10817:                } else {
                   10818:                        REG16(AX) = 0x05;
1.1.1.3   root     10819:                        m_CF = 1;
1.1       root     10820:                }
                   10821:        } else {
                   10822:                REG16(AX) = 0x06;
1.1.1.3   root     10823:                m_CF = 1;
1.1       root     10824:        }
                   10825: }
                   10826: 
                   10827: inline void msdos_int_21h_41h(int lfn)
                   10828: {
1.1.1.3   root     10829:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10830:                REG16(AX) = errno;
1.1.1.3   root     10831:                m_CF = 1;
1.1       root     10832:        }
                   10833: }
                   10834: 
                   10835: inline void msdos_int_21h_42h()
                   10836: {
                   10837:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10838:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10839:        
1.1.1.20  root     10840:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10841:                if(REG8(AL) < 0x03) {
1.1.1.35  root     10842:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     10843:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   10844:                        UINT32 pos = _tell(fd);
1.1       root     10845:                        REG16(AX) = pos & 0xffff;
                   10846:                        REG16(DX) = (pos >> 16);
                   10847:                } else {
                   10848:                        REG16(AX) = 0x01;
1.1.1.3   root     10849:                        m_CF = 1;
1.1       root     10850:                }
                   10851:        } else {
                   10852:                REG16(AX) = 0x06;
1.1.1.3   root     10853:                m_CF = 1;
1.1       root     10854:        }
                   10855: }
                   10856: 
                   10857: inline void msdos_int_21h_43h(int lfn)
                   10858: {
1.1.1.45  root     10859:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     10860:        int attr;
                   10861:        
1.1.1.14  root     10862:        if(!lfn && REG8(AL) > 2) {
                   10863:                REG16(AX) = 0x01;
                   10864:                m_CF = 1;
                   10865:                return;
                   10866:        }
                   10867:        switch(REG8(lfn ? BL : AL)) {
1.1       root     10868:        case 0x00:
                   10869:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     10870:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   10871:                } else {
                   10872:                        REG16(AX) = (UINT16)GetLastError();
                   10873:                        m_CF = 1;
                   10874:                }
                   10875:                break;
                   10876:        case 0x01:
                   10877:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   10878:                        REG16(AX) = (UINT16)GetLastError();
                   10879:                        m_CF = 1;
                   10880:                }
                   10881:                break;
                   10882:        case 0x02:
                   10883:                {
1.1.1.45  root     10884:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
                   10885:                        if(compressed_size != INVALID_FILE_SIZE) {
                   10886:                                if(compressed_size != 0) {
                   10887:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10888:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   10889:                                                file_size = GetFileSize(hFile, NULL);
                   10890:                                                CloseHandle(hFile);
                   10891:                                        }
                   10892:                                        if(compressed_size == file_size) {
                   10893:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   10894:                                                // this isn't correct if the file is in the NTFS MFT
                   10895:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   10896:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   10897:                                                }
1.1.1.14  root     10898:                                        }
                   10899:                                }
1.1.1.45  root     10900:                                REG16(AX) = LOWORD(compressed_size);
                   10901:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     10902:                        } else {
                   10903:                                REG16(AX) = (UINT16)GetLastError();
                   10904:                                m_CF = 1;
1.1       root     10905:                        }
1.1.1.14  root     10906:                }
                   10907:                break;
                   10908:        case 0x03:
                   10909:        case 0x05:
                   10910:        case 0x07:
1.1.1.48  root     10911:                if(lfn) {
1.1.1.14  root     10912:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10913:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10914:                                FILETIME local, time;
                   10915:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   10916:                                if(REG8(BL) == 7) {
                   10917:                                        ULARGE_INTEGER hund;
                   10918:                                        hund.LowPart = local.dwLowDateTime;
                   10919:                                        hund.HighPart = local.dwHighDateTime;
                   10920:                                        hund.QuadPart += REG16(SI) * 100000;
                   10921:                                        local.dwLowDateTime = hund.LowPart;
                   10922:                                        local.dwHighDateTime = hund.HighPart;
                   10923:                                }
                   10924:                                LocalFileTimeToFileTime(&local, &time);
                   10925:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   10926:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   10927:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   10928:                                        REG16(AX) = (UINT16)GetLastError();
                   10929:                                        m_CF = 1;
                   10930:                                }
                   10931:                                CloseHandle(hFile);
                   10932:                        } else {
                   10933:                                REG16(AX) = (UINT16)GetLastError();
                   10934:                                m_CF = 1;
1.1       root     10935:                        }
1.1.1.48  root     10936:                } else {
                   10937:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   10938:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   10939:                        // 214307 DR DOS 6.0 - Set File Owner
                   10940: //                     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));
                   10941:                        REG16(AX) = 0x01;
                   10942:                        m_CF = 1;
1.1.1.14  root     10943:                }
                   10944:                break;
                   10945:        case 0x04:
                   10946:        case 0x06:
                   10947:        case 0x08:
1.1.1.48  root     10948:                if(lfn) {
1.1.1.14  root     10949:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   10950:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   10951:                                FILETIME *time, local;
                   10952:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   10953:                                                   0x06 ? &fad.ftLastAccessTime :
                   10954:                                                          &fad.ftCreationTime;
                   10955:                                FileTimeToLocalFileTime(time, &local);
                   10956:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   10957:                                if(REG8(BL) == 0x08) {
                   10958:                                        ULARGE_INTEGER hund;
                   10959:                                        hund.LowPart = local.dwLowDateTime;
                   10960:                                        hund.HighPart = local.dwHighDateTime;
                   10961:                                        hund.QuadPart /= 100000;
                   10962:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   10963:                                }
                   10964:                        } else {
                   10965:                                REG16(AX) = (UINT16)GetLastError();
                   10966:                                m_CF = 1;
1.1       root     10967:                        }
1.1.1.48  root     10968:                } else {
                   10969:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   10970:                        // 214306 DR DOS 6.0 - Get File Owner
                   10971: //                     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));
                   10972:                        REG16(AX) = 0x01;
                   10973:                        m_CF = 1;
1.1.1.14  root     10974:                }
                   10975:                break;
1.1.1.43  root     10976:        case 0xff:
1.1.1.48  root     10977:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     10978:                        if(REG8(CL) == 0x39) {
                   10979:                                msdos_int_21h_39h(1);
                   10980:                                break;
                   10981:                        } else if(REG8(CL) == 0x56) {
                   10982:                                msdos_int_21h_56h(1);
                   10983:                                break;
                   10984:                        }
                   10985:                }
1.1.1.14  root     10986:        default:
1.1.1.22  root     10987:                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     10988:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     10989:                m_CF = 1;
                   10990:                break;
                   10991:        }
                   10992: }
                   10993: 
                   10994: inline void msdos_int_21h_44h()
                   10995: {
1.1.1.22  root     10996:        static UINT16 iteration_count = 0;
                   10997:        
1.1.1.44  root     10998:        process_t *process;
                   10999:        int fd, drv;
1.1.1.14  root     11000:        
                   11001:        switch(REG8(AL)) {
                   11002:        case 0x00:
                   11003:        case 0x01:
                   11004:        case 0x02:
                   11005:        case 0x03:
                   11006:        case 0x04:
                   11007:        case 0x05:
                   11008:        case 0x06:
                   11009:        case 0x07:
1.1.1.44  root     11010:                process = msdos_process_info_get(current_psp);
                   11011:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11012:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11013:                        REG16(AX) = 0x06;
                   11014:                        m_CF = 1;
                   11015:                        return;
1.1.1.14  root     11016:                }
                   11017:                break;
                   11018:        case 0x08:
                   11019:        case 0x09:
1.1.1.44  root     11020:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11021:                if(!msdos_is_valid_drive(drv)) {
                   11022:                        // invalid drive
1.1.1.14  root     11023:                        REG16(AX) = 0x0f;
                   11024:                        m_CF = 1;
                   11025:                        return;
1.1       root     11026:                }
                   11027:                break;
                   11028:        }
                   11029:        switch(REG8(AL)) {
1.1.1.48  root     11030:        case 0x00: // Get Device Information
1.1.1.20  root     11031:                REG16(DX) = file_handler[fd].info;
1.1       root     11032:                break;
1.1.1.48  root     11033:        case 0x01: // Set Device Information
1.1.1.45  root     11034:                if(REG8(DH) != 0) {
                   11035: //                     REG16(AX) = 0x0d; // data invalid
                   11036: //                     m_CF = 1;
                   11037:                        file_handler[fd].info = REG16(DX);
                   11038:                } else {
                   11039:                        file_handler[fd].info &= 0xff00;
                   11040:                        file_handler[fd].info |= REG8(DL);
                   11041:                }
1.1       root     11042:                break;
1.1.1.48  root     11043:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11044:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11045:                        // from DOSBox
                   11046:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11047:                        case 0x00:
                   11048:                                if(REG16(CX) >= 6) {
                   11049:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11050:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11051:                                        REG16(AX) = 6; // number of bytes actually read
                   11052:                                } else {
                   11053:                                        REG16(AX) = 0x0d; // data invalid
                   11054:                                        m_CF = 1;
                   11055:                                }
                   11056:                                break;
                   11057:                        case 0x01:
                   11058:                                if(REG16(CX) >= 6) {
                   11059:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11060:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11061:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11062:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11063:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11064:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11065:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11066:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11067:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11068:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11069:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11070:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11071:                                                } else {
                   11072:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11073:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11074:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11075:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11076:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11077:                                                }
                   11078:                                        }
                   11079:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11080:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11081:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11082:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11083:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11084:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11085:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11086:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11087:                                        
                   11088:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   11089:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   11090:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11091:                                        REG16(AX) = 6; // number of bytes actually read
                   11092:                                } else {
                   11093:                                        REG16(AX) = 0x0d; // data invalid
                   11094:                                        m_CF = 1;
                   11095:                                }
                   11096:                                break;
                   11097:                        case 0x02:
                   11098:                                if(REG16(CX) >= 2) {
                   11099:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11100:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11101:                                        REG16(AX) = 2; // number of bytes actually read
                   11102:                                } else {
                   11103:                                        REG16(AX) = 0x0d; // data invalid
                   11104:                                        m_CF = 1;
                   11105:                                }
                   11106:                                break;
                   11107:                        case 0x03:
                   11108:                                if(REG16(CX) >= 4) {
                   11109:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11110:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11111:                                        REG16(AX) = 4; // number of bytes actually read
                   11112:                                } else {
                   11113:                                        REG16(AX) = 0x0d; // data invalid
                   11114:                                        m_CF = 1;
                   11115:                                }
                   11116:                                break;
                   11117:                        default:
                   11118:                                REG16(AX) = 0x01; // function number invalid
                   11119:                                m_CF = 1;
                   11120:                        }
                   11121:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11122:                        if(REG16(CX) >= 5) {
                   11123:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11124:                                REG16(AX) = 5; // number of bytes actually read
                   11125:                        } else {
                   11126:                                REG16(AX) = 0x0d; // data invalid
                   11127:                                m_CF = 1;
                   11128:                        }
                   11129:                } else {
                   11130: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11131: //                     REG16(AX) = REG16(CX);
                   11132:                        REG16(AX) = 0x05; // access denied
                   11133:                        m_CF = 1;
                   11134:                }
                   11135:                break;
1.1.1.48  root     11136:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11137: //             REG16(AX) = 0x05;
                   11138: //             m_CF = 1;
                   11139:                REG16(AX) = 0x00; // success
                   11140:                break;
1.1.1.48  root     11141:        case 0x04: // Read From Block Device Control Channel
                   11142:        case 0x05: // Write To Block Device Control Channel
1.1       root     11143:                REG16(AX) = 0x05;
1.1.1.3   root     11144:                m_CF = 1;
1.1       root     11145:                break;
1.1.1.48  root     11146:        case 0x06: // Get Input Status
1.1.1.20  root     11147:                if(file_mode[file_handler[fd].mode].in) {
                   11148:                        if(file_handler[fd].atty) {
1.1.1.14  root     11149:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11150:                        } else {
1.1.1.20  root     11151:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11152:                        }
1.1.1.14  root     11153:                } else {
                   11154:                        REG8(AL) = 0x00;
1.1       root     11155:                }
                   11156:                break;
1.1.1.48  root     11157:        case 0x07: // Get Output Status
1.1.1.20  root     11158:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11159:                        REG8(AL) = 0xff;
                   11160:                } else {
                   11161:                        REG8(AL) = 0x00;
1.1       root     11162:                }
                   11163:                break;
1.1.1.48  root     11164:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11165:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11166:                        // removable drive
                   11167:                        REG16(AX) = 0x00;
1.1       root     11168:                } else {
1.1.1.14  root     11169:                        // fixed drive
                   11170:                        REG16(AX) = 0x01;
1.1       root     11171:                }
                   11172:                break;
1.1.1.48  root     11173:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11174:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11175:                        // remote drive
                   11176:                        REG16(DX) = 0x1000;
1.1.1.44  root     11177:                } else if(msdos_is_subst_drive(drv)) {
                   11178:                        // subst drive
                   11179:                        REG16(DX) = 0x8000;
1.1       root     11180:                } else {
1.1.1.14  root     11181:                        // local drive
1.1.1.44  root     11182:                        REG16(DX) = 0x0000;
1.1       root     11183:                }
                   11184:                break;
1.1.1.48  root     11185:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11186:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11187:                        REG16(DX) = 0x8000;
                   11188:                } else {
                   11189:                        REG16(DX) = 0x0000;
                   11190:                }
1.1.1.21  root     11191:                break;
1.1.1.48  root     11192:        case 0x0b: // Set Sharing Retry Count
1.1       root     11193:                break;
1.1.1.48  root     11194:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11195:                if(REG8(CL) == 0x45) {
                   11196:                        // set iteration (retry) count
                   11197:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11198:                } else if(REG8(CL) == 0x4a) {
                   11199:                        // select code page
                   11200:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11201:                        msdos_nls_tables_update();
1.1.1.44  root     11202:                } else if(REG8(CL) == 0x4c) {
                   11203:                        // start code-page preparation
                   11204:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11205:                        int count = 1, offset = 0;
                   11206:                        if(active_code_page != 437) {
                   11207:                                ids[count++] = active_code_page;
                   11208:                        }
                   11209:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11210:                                ids[count++] = system_code_page;
                   11211:                        }
                   11212:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11213:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11214:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11215:                        for(int i = 0; i < count; i++) {
                   11216:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11217:                        }
                   11218:                } else if(REG8(CL) == 0x4d) {
                   11219:                        // end code-page preparation
                   11220:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11221:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11222:                } else if(REG8(CL) == 0x5f) {
                   11223:                        // set display information
                   11224:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11225:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11226:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11227:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11228:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11229:                                
                   11230:                                if(cur_width != new_width || cur_height != new_height) {
                   11231:                                        pcbios_set_console_size(new_width, new_height, true);
                   11232:                                }
                   11233:                        }
1.1.1.22  root     11234:                } else if(REG8(CL) == 0x65) {
                   11235:                        // get iteration (retry) count
                   11236:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11237:                } else if(REG8(CL) == 0x6a) {
                   11238:                        // query selected code page
                   11239:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11240:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11241:                        
                   11242:                        CPINFO info;
                   11243:                        GetCPInfo(active_code_page, &info);
                   11244:                        
                   11245:                        if(info.MaxCharSize != 1) {
                   11246:                                for(int i = 0;; i++) {
                   11247:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11248:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11249:                                        
                   11250:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11251:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11252:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11253:                                        
                   11254:                                        if(lo == 0 && hi == 0) {
                   11255:                                                break;
                   11256:                                        }
                   11257:                                }
                   11258:                        }
1.1.1.44  root     11259:                } else if(REG8(CL) == 0x6b) {
                   11260:                        // query prepare list
                   11261:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11262:                        int count = 1, offset = 0;
                   11263:                        if(active_code_page != 437) {
                   11264:                                ids[count++] = active_code_page;
                   11265:                        }
                   11266:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11267:                                ids[count++] = system_code_page;
                   11268:                        }
                   11269:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11270:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11271:                        for(int i = 0; i < count; i++) {
                   11272:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11273:                        }
                   11274:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11275:                        for(int i = 0; i < count; i++) {
                   11276:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11277:                        }
1.1.1.22  root     11278:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11279:                        // get display information
1.1.1.50  root     11280:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11281:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11282:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11283:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11284:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11285:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11286:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11287:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11288:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11289:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11290:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11291:                } else {
                   11292:                        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));
                   11293:                        REG16(AX) = 0x01; // invalid function
                   11294:                        m_CF = 1;
                   11295:                }
                   11296:                break;
1.1.1.48  root     11297:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11298:                if(REG8(CL) == 0x40) {
                   11299:                        // set device parameters
1.1.1.48  root     11300: //             } else if(REG8(CL) == 0x41) {
                   11301: //                     // write logical device track
                   11302: //             } else if(REG8(CL) == 0x42) {
                   11303: //                     // format and verify logical device track
1.1.1.22  root     11304:                } else if(REG8(CL) == 0x46) {
                   11305:                        // set volume serial number
1.1.1.48  root     11306:                } else if(REG8(CL) == 0x47) {
                   11307:                        // set access flag
                   11308: //             } else if(REG8(CL) == 0x48) {
                   11309: //                     // set media lock state
                   11310: //             } else if(REG8(CL) == 0x49) {
                   11311: //                     // eject media in drive
1.1.1.22  root     11312:                } else if(REG8(CL) == 0x4a) {
                   11313:                        // lock logical volume
                   11314:                } else if(REG8(CL) == 0x4b) {
                   11315:                        // lock physical volume
                   11316:                } else if(REG8(CL) == 0x60) {
                   11317:                        // get device parameters
1.1.1.42  root     11318:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11319:                        
1.1.1.42  root     11320:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11321:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11322:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11323:                                
                   11324:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11325:                                switch(geo->MediaType) {
                   11326:                                case F5_360_512:
                   11327:                                case F5_320_512:
                   11328:                                case F5_320_1024:
                   11329:                                case F5_180_512:
                   11330:                                case F5_160_512:
                   11331:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11332:                                        break;
                   11333:                                case F5_1Pt2_512:
                   11334:                                case F3_1Pt2_512:
                   11335:                                case F3_1Pt23_1024:
                   11336:                                case F5_1Pt23_1024:
                   11337:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11338:                                        break;
                   11339:                                case F3_720_512:
                   11340:                                case F3_640_512:
                   11341:                                case F5_640_512:
                   11342:                                case F5_720_512:
                   11343:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11344:                                        break;
                   11345:                                case F8_256_128:
                   11346:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11347:                                        break;
                   11348:                                case FixedMedia:
                   11349:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11350:                                        break;
                   11351:                                case F3_1Pt44_512:
                   11352:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11353:                                        break;
                   11354:                                case F3_2Pt88_512:
                   11355:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11356:                                        break;
                   11357:                                default:
                   11358:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11359: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11360:                                        break;
1.1.1.22  root     11361:                                }
1.1.1.42  root     11362:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11363:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11364:                                switch(geo->MediaType) {
                   11365:                                case F5_360_512:
                   11366:                                case F5_320_512:
                   11367:                                case F5_320_1024:
                   11368:                                case F5_180_512:
                   11369:                                case F5_160_512:
                   11370:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11371:                                        break;
                   11372:                                default:
                   11373:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11374:                                        break;
                   11375:                                }
                   11376:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11377:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11378:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11379:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11380:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11381:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11382:                                switch(geo->MediaType) {
                   11383:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11384:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11385:                                        break;
                   11386:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11387:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11388:                                        break;
                   11389:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11390:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11391:                                        break;
                   11392:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11393:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11394:                                        break;
                   11395:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11396:                                case F3_1Pt2_512:
                   11397:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11398:                                case F5_720_512:
                   11399:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11400:                                        break;
                   11401:                                case FixedMedia:        // hard disk
                   11402:                                case RemovableMedia:
                   11403:                                case Unknown:
                   11404:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11405:                                        break;
                   11406:                                default:
                   11407:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11408:                                        break;
                   11409:                                }
                   11410:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11411:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11412:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11413:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11414:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11415:                                // 21h  BYTE    device type
                   11416:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11417:                        } else {
                   11418:                                REG16(AX) = 0x0f; // invalid drive
                   11419:                                m_CF = 1;
                   11420:                        }
1.1.1.48  root     11421: //             } else if(REG8(CL) == 0x61) {
                   11422: //                     // read logical device track
                   11423: //             } else if(REG8(CL) == 0x62) {
                   11424: //                     // verify logical device track
1.1.1.22  root     11425:                } else if(REG8(CL) == 0x66) {
                   11426:                        // get volume serial number
                   11427:                        char path[] = "A:\\";
                   11428:                        char volume_label[MAX_PATH];
                   11429:                        DWORD serial_number = 0;
                   11430:                        char file_system[MAX_PATH];
                   11431:                        
                   11432:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11433:                        
                   11434:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11435:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11436:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11437:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11438:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11439:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11440:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11441:                        } else {
                   11442:                                REG16(AX) = 0x0f; // invalid drive
                   11443:                                m_CF = 1;
                   11444:                        }
                   11445:                } else if(REG8(CL) == 0x67) {
                   11446:                        // get access flag
                   11447:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11448:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11449:                } else if(REG8(CL) == 0x68) {
                   11450:                        // sense media type
1.1.1.42  root     11451:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11452:                        
1.1.1.42  root     11453:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11454:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11455:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11456:                                
                   11457:                                switch(geo->MediaType) {
                   11458:                                case F3_720_512:
                   11459:                                case F5_720_512:
                   11460:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11461:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11462:                                        break;
                   11463:                                case F3_1Pt44_512:
                   11464:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11465:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11466:                                        break;
                   11467:                                case F3_2Pt88_512:
                   11468:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11469:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11470:                                        break;
                   11471:                                default:
                   11472:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11473:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11474:                                        break;
1.1.1.22  root     11475:                                }
                   11476:                        } else {
                   11477:                                REG16(AX) = 0x0f; // invalid drive
                   11478:                                m_CF = 1;
                   11479:                        }
                   11480:                } else if(REG8(CL) == 0x6a) {
                   11481:                        // unlock logical volume
                   11482:                } else if(REG8(CL) == 0x6b) {
                   11483:                        // unlock physical volume
1.1.1.48  root     11484: //             } else if(REG8(CL) == 0x6c) {
                   11485: //                     // get lock flag
                   11486: //             } else if(REG8(CL) == 0x6d) {
                   11487: //                     // enumerate open files
                   11488: //             } else if(REG8(CL) == 0x6e) {
                   11489: //                     // find swap file
                   11490: //             } else if(REG8(CL) == 0x6f) {
                   11491: //                     // get drive map information
                   11492: //             } else if(REG8(CL) == 0x70) {
                   11493: //                     // get current lock state
                   11494: //             } else if(REG8(CL) == 0x71) {
                   11495: //                     // get first cluster
1.1.1.22  root     11496:                } else {
                   11497:                        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));
                   11498:                        REG16(AX) = 0x01; // invalid function
                   11499:                        m_CF = 1;
                   11500:                }
                   11501:                break;
1.1.1.48  root     11502:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11503:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11504:                        REG16(AX) = 0x0f; // invalid drive
                   11505:                        m_CF = 1;
                   11506:                } else {
                   11507:                        REG8(AL) = 0;
1.1.1.22  root     11508:                }
                   11509:                break;
1.1.1.48  root     11510:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11511:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11512:                        REG16(AX) = 0x0f; // invalid drive
                   11513:                        m_CF = 1;
1.1.1.22  root     11514:                }
                   11515:                break;
1.1.1.48  root     11516:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11517:                switch(REG8(CL)) {
                   11518:                case 0x45:
                   11519:                case 0x4a:
1.1.1.48  root     11520:                case 0x4c:
                   11521:                case 0x4d:
1.1.1.22  root     11522:                case 0x65:
                   11523:                case 0x6a:
1.1.1.48  root     11524:                case 0x6b:
1.1.1.22  root     11525:                case 0x7f:
                   11526:                        REG16(AX) = 0x0000; // supported
                   11527:                        break;
                   11528:                default:
                   11529:                        REG8(AL) = 0x01; // ioctl capability not available
                   11530:                        m_CF = 1;
                   11531:                        break;
                   11532:                }
                   11533:                break;
1.1.1.48  root     11534:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     11535:                switch(REG8(CL)) {
                   11536:                case 0x40:
                   11537:                case 0x46:
                   11538:                case 0x4a:
                   11539:                case 0x4b:
                   11540:                case 0x60:
                   11541:                case 0x66:
                   11542:                case 0x67:
                   11543:                case 0x68:
                   11544:                case 0x6a:
                   11545:                case 0x6b:
1.1.1.48  root     11546:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   11547:                                // CH = 00h     Unknown
                   11548:                                // CH = 01h     COMn:
                   11549:                                // CH = 03h     CON
                   11550:                                // CH = 05h     LPTn:
                   11551:                                REG16(AX) = 0x0000; // supported
                   11552:                                break;
                   11553:                        }
1.1.1.22  root     11554:                default:
                   11555:                        REG8(AL) = 0x01; // ioctl capability not available
                   11556:                        m_CF = 1;
                   11557:                        break;
                   11558:                }
                   11559:                break;
1.1.1.48  root     11560:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   11561:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   11562:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   11563:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   11564:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   11565:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   11566:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   11567:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   11568:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   11569:        case 0x59: // DR Multiuser DOS 5.0 - API
                   11570:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     11571:                m_CF = 1;
                   11572:                break;
1.1       root     11573:        default:
1.1.1.22  root     11574:                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     11575:                REG16(AX) = 0x01;
1.1.1.3   root     11576:                m_CF = 1;
1.1       root     11577:                break;
                   11578:        }
                   11579: }
                   11580: 
                   11581: inline void msdos_int_21h_45h()
                   11582: {
                   11583:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11584:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11585:        
1.1.1.20  root     11586:        if(fd < process->max_files && file_handler[fd].valid) {
                   11587:                int dup_fd = _dup(fd);
                   11588:                if(dup_fd != -1) {
                   11589:                        REG16(AX) = dup_fd;
                   11590:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11591: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11592:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11593:                } else {
                   11594:                        REG16(AX) = errno;
1.1.1.3   root     11595:                        m_CF = 1;
1.1       root     11596:                }
                   11597:        } else {
                   11598:                REG16(AX) = 0x06;
1.1.1.3   root     11599:                m_CF = 1;
1.1       root     11600:        }
                   11601: }
                   11602: 
                   11603: inline void msdos_int_21h_46h()
                   11604: {
                   11605:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11606:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11607:        int dup_fd = REG16(CX);
                   11608:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11609:        
1.1.1.20  root     11610:        if(REG16(BX) == REG16(CX)) {
                   11611:                REG16(AX) = 0x06;
                   11612:                m_CF = 1;
                   11613:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11614:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11615:                        _close(tmp_fd);
                   11616:                        msdos_file_handler_close(tmp_fd);
                   11617:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11618:                }
                   11619:                if(_dup2(fd, dup_fd) != -1) {
                   11620:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11621: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11622:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11623:                } else {
                   11624:                        REG16(AX) = errno;
1.1.1.3   root     11625:                        m_CF = 1;
1.1       root     11626:                }
                   11627:        } else {
                   11628:                REG16(AX) = 0x06;
1.1.1.3   root     11629:                m_CF = 1;
1.1       root     11630:        }
                   11631: }
                   11632: 
                   11633: inline void msdos_int_21h_47h(int lfn)
                   11634: {
                   11635:        char path[MAX_PATH];
                   11636:        
                   11637:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     11638:                if(!lfn) {
                   11639:                        strcpy(path, msdos_short_path(path));
                   11640:                }
1.1       root     11641:                if(path[1] == ':') {
                   11642:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     11643:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11644:                } else {
1.1.1.45  root     11645:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11646:                }
                   11647:        } else {
                   11648:                REG16(AX) = errno;
1.1.1.3   root     11649:                m_CF = 1;
1.1       root     11650:        }
                   11651: }
                   11652: 
                   11653: inline void msdos_int_21h_48h()
                   11654: {
1.1.1.19  root     11655:        int seg, umb_linked;
1.1       root     11656:        
1.1.1.8   root     11657:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11658:                // unlink umb not to allocate memory in umb
                   11659:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11660:                        msdos_mem_unlink_umb();
                   11661:                }
1.1.1.8   root     11662:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11663:                        REG16(AX) = seg;
                   11664:                } else {
                   11665:                        REG16(AX) = 0x08;
                   11666:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11667:                        m_CF = 1;
                   11668:                }
1.1.1.19  root     11669:                if(umb_linked != 0) {
                   11670:                        msdos_mem_link_umb();
                   11671:                }
1.1.1.8   root     11672:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11673:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11674:                        REG16(AX) = seg;
                   11675:                } else {
                   11676:                        REG16(AX) = 0x08;
                   11677:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11678:                        m_CF = 1;
                   11679:                }
                   11680:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11681:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11682:                        REG16(AX) = seg;
                   11683:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11684:                        REG16(AX) = seg;
                   11685:                } else {
                   11686:                        REG16(AX) = 0x08;
                   11687:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11688:                        m_CF = 1;
                   11689:                }
1.1       root     11690:        }
                   11691: }
                   11692: 
                   11693: inline void msdos_int_21h_49h()
                   11694: {
1.1.1.14  root     11695:        int mcb_seg = SREG(ES) - 1;
                   11696:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11697:        
                   11698:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11699:                msdos_mem_free(SREG(ES));
                   11700:        } else {
1.1.1.33  root     11701:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11702:                m_CF = 1;
                   11703:        }
1.1       root     11704: }
                   11705: 
                   11706: inline void msdos_int_21h_4ah()
                   11707: {
1.1.1.14  root     11708:        int mcb_seg = SREG(ES) - 1;
                   11709:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11710:        int max_paragraphs;
                   11711:        
1.1.1.14  root     11712:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11713:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11714:                        REG16(AX) = 0x08;
                   11715:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11716:                        m_CF = 1;
                   11717:                }
                   11718:        } else {
1.1.1.33  root     11719:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11720:                m_CF = 1;
1.1       root     11721:        }
                   11722: }
                   11723: 
                   11724: inline void msdos_int_21h_4bh()
                   11725: {
1.1.1.3   root     11726:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11727:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11728:        
                   11729:        switch(REG8(AL)) {
                   11730:        case 0x00:
                   11731:        case 0x01:
                   11732:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11733:                        REG16(AX) = 0x02;
1.1.1.3   root     11734:                        m_CF = 1;
1.1       root     11735:                }
                   11736:                break;
1.1.1.14  root     11737:        case 0x03:
                   11738:                {
                   11739:                        int fd;
                   11740:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11741:                                REG16(AX) = 0x02;
                   11742:                                m_CF = 1;
                   11743:                                break;
                   11744:                        }
                   11745:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11746:                        _close(fd);
                   11747:                        
                   11748:                        UINT16 *overlay = (UINT16 *)param;
                   11749:                        
                   11750:                        // check exe header
                   11751:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11752:                        int header_size = 0;
                   11753:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11754:                                header_size = header->header_size * 16;
                   11755:                                // relocation
                   11756:                                int start_seg = overlay[1];
                   11757:                                for(int i = 0; i < header->relocations; i++) {
                   11758:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11759:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11760:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11761:                                }
                   11762:                        }
                   11763:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11764:                }
                   11765:                break;
1.1.1.48  root     11766:        case 0x04:
                   11767:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   11768: //     case 0x05:
                   11769: //             // DOS 5+ - Set Execution State
                   11770:        case 0x80:
                   11771:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   11772:        case 0xf0:
                   11773:        case 0xf1:
                   11774:                // DIET v1.10+
1.1.1.43  root     11775:        case 0xfd:
                   11776:        case 0xfe:
                   11777:                // unknown function called in FreeCOM
                   11778:                REG16(AX) = 0x01;
                   11779:                m_CF = 1;
                   11780:                break;
1.1       root     11781:        default:
1.1.1.22  root     11782:                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     11783:                REG16(AX) = 0x01;
1.1.1.3   root     11784:                m_CF = 1;
1.1       root     11785:                break;
                   11786:        }
                   11787: }
                   11788: 
                   11789: inline void msdos_int_21h_4ch()
                   11790: {
                   11791:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11792: }
                   11793: 
                   11794: inline void msdos_int_21h_4dh()
                   11795: {
                   11796:        REG16(AX) = retval;
                   11797: }
                   11798: 
                   11799: inline void msdos_int_21h_4eh()
                   11800: {
                   11801:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11802:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11803:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     11804:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11805:        WIN32_FIND_DATA fd;
                   11806:        
1.1.1.14  root     11807:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11808:        find->find_magic = FIND_MAGIC;
                   11809:        find->dta_index = dtainfo - dtalist;
1.1       root     11810:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11811:        dtainfo->allowable_mask = REG8(CL);
                   11812:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11813:        
1.1.1.14  root     11814:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11815:                dtainfo->allowable_mask &= ~8;
1.1       root     11816:        }
1.1.1.14  root     11817:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11818:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11819:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11820:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11821:                                FindClose(dtainfo->find_handle);
                   11822:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11823:                                break;
                   11824:                        }
                   11825:                }
                   11826:        }
1.1.1.13  root     11827:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11828:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11829:                msdos_find_file_conv_local_time(&fd);
                   11830:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11831:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11832:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11833:                REG16(AX) = 0;
1.1.1.14  root     11834:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11835:                find->attrib = 8;
                   11836:                find->size = 0;
                   11837:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11838:                dtainfo->allowable_mask &= ~8;
1.1       root     11839:                REG16(AX) = 0;
                   11840:        } else {
                   11841:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11842:                m_CF = 1;
1.1       root     11843:        }
                   11844: }
                   11845: 
                   11846: inline void msdos_int_21h_4fh()
                   11847: {
                   11848:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11849:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11850:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     11851:        WIN32_FIND_DATA fd;
                   11852:        
1.1.1.14  root     11853:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   11854:                REG16(AX) = 0x12;
                   11855:                m_CF = 1;
                   11856:                return;
                   11857:        }
                   11858:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     11859:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11860:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11861:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11862:                              !msdos_find_file_has_8dot3name(&fd)) {
                   11863:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11864:                                        FindClose(dtainfo->find_handle);
                   11865:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11866:                                        break;
                   11867:                                }
                   11868:                        }
                   11869:                } else {
1.1.1.13  root     11870:                        FindClose(dtainfo->find_handle);
                   11871:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11872:                }
                   11873:        }
1.1.1.13  root     11874:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11875:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11876:                msdos_find_file_conv_local_time(&fd);
                   11877:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11878:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11879:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11880:                REG16(AX) = 0;
1.1.1.14  root     11881:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11882:                find->attrib = 8;
                   11883:                find->size = 0;
                   11884:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11885:                dtainfo->allowable_mask &= ~8;
1.1       root     11886:                REG16(AX) = 0;
                   11887:        } else {
                   11888:                REG16(AX) = 0x12;
1.1.1.3   root     11889:                m_CF = 1;
1.1       root     11890:        }
                   11891: }
                   11892: 
                   11893: inline void msdos_int_21h_50h()
                   11894: {
1.1.1.8   root     11895:        if(current_psp != REG16(BX)) {
                   11896:                process_t *process = msdos_process_info_get(current_psp);
                   11897:                if(process != NULL) {
                   11898:                        process->psp = REG16(BX);
                   11899:                }
                   11900:                current_psp = REG16(BX);
1.1.1.23  root     11901:                msdos_sda_update(current_psp);
1.1.1.8   root     11902:        }
1.1       root     11903: }
                   11904: 
                   11905: inline void msdos_int_21h_51h()
                   11906: {
                   11907:        REG16(BX) = current_psp;
                   11908: }
                   11909: 
                   11910: inline void msdos_int_21h_52h()
                   11911: {
1.1.1.25  root     11912:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     11913:        i386_load_segment_descriptor(ES);
1.1.1.25  root     11914:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     11915: }
                   11916: 
1.1.1.43  root     11917: inline void msdos_int_21h_53h()
                   11918: {
                   11919:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   11920:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   11921:        
                   11922:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   11923:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   11924:        dpb->shift_count = 0;
                   11925:        dpb->reserved_sectors = 0;
                   11926:        dpb->fat_num = bpb->fat_num;
                   11927:        dpb->root_entries = bpb->root_entries;
                   11928:        dpb->first_data_sector = 0;
                   11929:        if(bpb->sectors_per_cluster != 0) {
                   11930:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   11931:        } else {
                   11932:                dpb->highest_cluster_num = 0;
                   11933:        }
                   11934:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   11935:        dpb->first_dir_sector = 0;
                   11936:        dpb->device_driver_header = 0;
                   11937:        dpb->media_type = bpb->media_type;
                   11938:        dpb->drive_accessed = 0;
                   11939:        dpb->next_dpb_ofs = 0xffff;
                   11940:        dpb->next_dpb_seg = 0xffff;
                   11941:        dpb->first_free_cluster = 0;
                   11942:        dpb->free_clusters = 0xffff;
                   11943: }
                   11944: 
1.1       root     11945: inline void msdos_int_21h_54h()
                   11946: {
                   11947:        process_t *process = msdos_process_info_get(current_psp);
                   11948:        
                   11949:        REG8(AL) = process->verify;
                   11950: }
                   11951: 
                   11952: inline void msdos_int_21h_55h()
                   11953: {
                   11954:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   11955:        
                   11956:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   11957:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   11958:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   11959:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   11960:        psp->parent_psp = current_psp;
                   11961: }
                   11962: 
                   11963: inline void msdos_int_21h_56h(int lfn)
                   11964: {
                   11965:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     11966:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   11967:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     11968:        
                   11969:        if(rename(src, dst)) {
                   11970:                REG16(AX) = errno;
1.1.1.3   root     11971:                m_CF = 1;
1.1       root     11972:        }
                   11973: }
                   11974: 
                   11975: inline void msdos_int_21h_57h()
                   11976: {
                   11977:        FILETIME time, local;
1.1.1.14  root     11978:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     11979:        HANDLE hHandle;
1.1       root     11980:        
1.1.1.21  root     11981:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     11982:                REG16(AX) = (UINT16)GetLastError();
                   11983:                m_CF = 1;
                   11984:                return;
                   11985:        }
                   11986:        ctime = atime = mtime = NULL;
                   11987:        
1.1       root     11988:        switch(REG8(AL)) {
                   11989:        case 0x00:
1.1.1.6   root     11990:        case 0x01:
1.1.1.14  root     11991:                mtime = &time;
1.1.1.6   root     11992:                break;
                   11993:        case 0x04:
                   11994:        case 0x05:
1.1.1.14  root     11995:                atime = &time;
1.1       root     11996:                break;
1.1.1.6   root     11997:        case 0x06:
                   11998:        case 0x07:
1.1.1.14  root     11999:                ctime = &time;
                   12000:                break;
                   12001:        default:
1.1.1.22  root     12002:                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     12003:                REG16(AX) = 0x01;
                   12004:                m_CF = 1;
                   12005:                return;
                   12006:        }
                   12007:        if(REG8(AL) & 1) {
1.1       root     12008:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12009:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12010:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12011:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12012:                        m_CF = 1;
1.1       root     12013:                }
1.1.1.14  root     12014:        } else {
1.1.1.21  root     12015:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12016:                        // assume a device and use the current time
                   12017:                        GetSystemTimeAsFileTime(&time);
                   12018:                }
                   12019:                FileTimeToLocalFileTime(&time, &local);
                   12020:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12021:        }
                   12022: }
                   12023: 
                   12024: inline void msdos_int_21h_58h()
                   12025: {
                   12026:        switch(REG8(AL)) {
                   12027:        case 0x00:
1.1.1.7   root     12028:                REG16(AX) = malloc_strategy;
                   12029:                break;
                   12030:        case 0x01:
1.1.1.24  root     12031: //             switch(REG16(BX)) {
                   12032:                switch(REG8(BL)) {
1.1.1.7   root     12033:                case 0x0000:
                   12034:                case 0x0001:
                   12035:                case 0x0002:
                   12036:                case 0x0040:
                   12037:                case 0x0041:
                   12038:                case 0x0042:
                   12039:                case 0x0080:
                   12040:                case 0x0081:
                   12041:                case 0x0082:
                   12042:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12043:                        msdos_sda_update(current_psp);
1.1.1.7   root     12044:                        break;
                   12045:                default:
1.1.1.22  root     12046:                        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     12047:                        REG16(AX) = 0x01;
                   12048:                        m_CF = 1;
                   12049:                        break;
                   12050:                }
                   12051:                break;
                   12052:        case 0x02:
1.1.1.19  root     12053:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12054:                break;
                   12055:        case 0x03:
1.1.1.24  root     12056: //             switch(REG16(BX)) {
                   12057:                switch(REG8(BL)) {
1.1.1.7   root     12058:                case 0x0000:
1.1.1.19  root     12059:                        msdos_mem_unlink_umb();
                   12060:                        break;
1.1.1.7   root     12061:                case 0x0001:
1.1.1.19  root     12062:                        msdos_mem_link_umb();
1.1.1.7   root     12063:                        break;
                   12064:                default:
1.1.1.22  root     12065:                        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     12066:                        REG16(AX) = 0x01;
                   12067:                        m_CF = 1;
                   12068:                        break;
                   12069:                }
1.1       root     12070:                break;
                   12071:        default:
1.1.1.22  root     12072:                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     12073:                REG16(AX) = 0x01;
1.1.1.3   root     12074:                m_CF = 1;
1.1       root     12075:                break;
                   12076:        }
                   12077: }
                   12078: 
                   12079: inline void msdos_int_21h_59h()
                   12080: {
1.1.1.47  root     12081:        if(REG16(BX) == 0x0000) {
                   12082:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12083:                
                   12084:                REG16(AX) = sda->extended_error_code;
                   12085:                REG8(BH)  = sda->error_class;
                   12086:                REG8(BL)  = sda->suggested_action;
                   12087:                REG8(CH)  = sda->locus_of_last_error;
                   12088:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12089:                if(sda->int21h_5d0ah_called != 0) {
                   12090:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12091:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12092: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12093:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12094: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12095: //                     i386_load_segment_descriptor(DS);
                   12096:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12097:                        i386_load_segment_descriptor(ES);
                   12098:                }
                   12099:                sda->int21h_5d0ah_called = 0;
                   12100: //     } else if(REG16(BX) == 0x0001) {
                   12101: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12102:        } else {
                   12103:                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));
                   12104:                REG16(AX) = 0x01;
                   12105:                m_CF = 1;
                   12106:        }
1.1       root     12107: }
                   12108: 
                   12109: inline void msdos_int_21h_5ah()
                   12110: {
1.1.1.3   root     12111:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12112:        int len = strlen(path);
                   12113:        char tmp[MAX_PATH];
                   12114:        
                   12115:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   12116:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12117:                
                   12118:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12119:                REG16(AX) = fd;
                   12120:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12121:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12122:                
                   12123:                strcpy(path, tmp);
                   12124:                int dx = REG16(DX) + len;
1.1.1.3   root     12125:                int ds = SREG(DS);
1.1       root     12126:                while(dx > 0xffff) {
                   12127:                        dx -= 0x10;
                   12128:                        ds++;
                   12129:                }
                   12130:                REG16(DX) = dx;
1.1.1.3   root     12131:                SREG(DS) = ds;
                   12132:                i386_load_segment_descriptor(DS);
1.1       root     12133:        } else {
                   12134:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12135:                m_CF = 1;
1.1       root     12136:        }
                   12137: }
                   12138: 
                   12139: inline void msdos_int_21h_5bh()
                   12140: {
1.1.1.45  root     12141:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12142:        
1.1.1.45  root     12143: //     if(msdos_is_existing_file(path)) {
                   12144:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12145:                // already exists
                   12146:                REG16(AX) = 0x50;
1.1.1.3   root     12147:                m_CF = 1;
1.1       root     12148:        } else {
                   12149:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12150:                
                   12151:                if(fd != -1) {
                   12152:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12153:                        REG16(AX) = fd;
                   12154:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12155:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12156:                } else {
                   12157:                        REG16(AX) = errno;
1.1.1.3   root     12158:                        m_CF = 1;
1.1       root     12159:                }
                   12160:        }
                   12161: }
                   12162: 
                   12163: inline void msdos_int_21h_5ch()
                   12164: {
                   12165:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12166:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12167:        
1.1.1.20  root     12168:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12169:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12170:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12171:                        UINT32 pos = _tell(fd);
                   12172:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12173:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12174:                                REG16(AX) = errno;
1.1.1.3   root     12175:                                m_CF = 1;
1.1       root     12176:                        }
1.1.1.20  root     12177:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12178:                        
1.1       root     12179:                        // some seconds may be passed in _locking()
1.1.1.35  root     12180:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12181:                } else {
                   12182:                        REG16(AX) = 0x01;
1.1.1.3   root     12183:                        m_CF = 1;
1.1       root     12184:                }
                   12185:        } else {
                   12186:                REG16(AX) = 0x06;
1.1.1.3   root     12187:                m_CF = 1;
1.1       root     12188:        }
                   12189: }
                   12190: 
1.1.1.22  root     12191: inline void msdos_int_21h_5dh()
                   12192: {
                   12193:        switch(REG8(AL)) {
1.1.1.45  root     12194:        case 0x00:
                   12195:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12196:                        // current system
                   12197:                        static bool reenter = false;
                   12198:                        if(!reenter) {
                   12199:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12200:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12201:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12202:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12203:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12204:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12205:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12206:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12207:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12208:                                i386_load_segment_descriptor(DS);
                   12209:                                i386_load_segment_descriptor(ES);
                   12210:                                reenter = true;
                   12211:                                try {
                   12212:                                        msdos_syscall(0x21);
                   12213:                                } catch(...) {
                   12214:                                }
                   12215:                                reenter = false;
                   12216:                        }
                   12217:                } else {
                   12218:                        REG16(AX) = 0x49; //  network software not installed
                   12219:                        m_CF = 1;
                   12220:                }
                   12221:                break;
1.1.1.22  root     12222:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     12223:                SREG(DS) = (SDA_TOP >> 4);
                   12224:                i386_load_segment_descriptor(DS);
                   12225:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12226:                REG16(CX) = 0x80;
                   12227:                REG16(DX) = 0x1a;
                   12228:                break;
1.1.1.45  root     12229:        case 0x07: // get redirected printer mode
                   12230:        case 0x08: // set redirected printer mode
                   12231:        case 0x09: // flush redirected printer output
                   12232:                REG16(AX) = 0x49; //  network software not installed
                   12233:                m_CF = 1;
                   12234:                break;
1.1.1.43  root     12235:        case 0x0a: // set extended error information
                   12236:                {
                   12237:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12238:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12239:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12240:                        // XXX: which one is correct ???
                   12241: #if 1
                   12242:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12243:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12244:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12245:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12246:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12247: #else
                   12248:                        // PC DOS 7 Technical Update
                   12249:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12250:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12251:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12252:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12253: #endif
                   12254:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12255: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12256:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12257: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12258:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12259:                }
                   12260:                break;
1.1.1.23  root     12261:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12262:                REG16(AX) = 0x01;
                   12263:                m_CF = 1;
                   12264:                break;
                   12265:        default:
                   12266:                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));
                   12267:                REG16(AX) = 0x01;
                   12268:                m_CF = 1;
                   12269:                break;
                   12270:        }
                   12271: }
                   12272: 
1.1.1.42  root     12273: inline void msdos_int_21h_5eh()
                   12274: {
                   12275:        switch(REG8(AL)) {
                   12276:        case 0x00:
                   12277:                {
                   12278:                        char name[256] = {0};
                   12279:                        DWORD dwSize = 256;
                   12280:                        
                   12281:                        if(GetComputerName(name, &dwSize)) {
                   12282:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12283:                                for(int i = 0; i < 15; i++) {
                   12284:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12285:                                }
                   12286:                                dest[15] = '\0';
                   12287:                                REG8(CH) = 0x01; // nonzero valid
                   12288:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12289:                        } else {
                   12290:                                REG16(AX) = 0x01;
                   12291:                                m_CF = 1;
                   12292:                        }
                   12293:                }
                   12294:                break;
                   12295:        default:
1.1.1.45  root     12296: //             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));
                   12297: //             REG16(AX) = 0x01;
                   12298:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12299:                m_CF = 1;
                   12300:                break;
                   12301:        }
                   12302: }
                   12303: 
1.1.1.30  root     12304: inline void msdos_int_21h_5fh()
                   12305: {
                   12306:        switch(REG8(AL)) {
1.1.1.42  root     12307:        case 0x05:
1.1.1.44  root     12308:                REG16(BP) = 0;
                   12309:                for(int i = 0; i < 26; i++) {
                   12310:                        if(msdos_is_remote_drive(i)) {
                   12311:                                REG16(BP)++;
1.1.1.42  root     12312:                        }
                   12313:                }
1.1.1.30  root     12314:        case 0x02:
1.1.1.44  root     12315:                for(int i = 0, index = 0; i < 26; i++) {
                   12316:                        if(msdos_is_remote_drive(i)) {
                   12317:                                if(index == REG16(BX)) {
                   12318:                                        char volume[] = "A:";
1.1.1.30  root     12319:                                        volume[0] = 'A' + i;
1.1.1.44  root     12320:                                        DWORD dwSize = 128;
                   12321:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   12322:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   12323:                                        REG8(BH) = 0x00; // valid
                   12324:                                        REG8(BL) = 0x04; // disk drive
                   12325:                                        REG16(CX) = 0x00; // LANtastic
                   12326:                                        return;
1.1.1.30  root     12327:                                }
1.1.1.44  root     12328:                                index++;
1.1.1.30  root     12329:                        }
                   12330:                }
                   12331:                REG16(AX) = 0x12; // no more files
                   12332:                m_CF = 1;
                   12333:                break;
1.1.1.44  root     12334:        case 0x07:
                   12335:                if(msdos_is_valid_drive(REG8(DL))) {
                   12336:                        msdos_cds_update(REG8(DL));
                   12337:                } else {
                   12338:                        REG16(AX) = 0x0f; // invalid drive
                   12339:                        m_CF = 1;
                   12340:                }
                   12341:                break;
                   12342:        case 0x08:
                   12343:                if(msdos_is_valid_drive(REG8(DL))) {
                   12344:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12345:                        cds->drive_attrib = 0x0000;
                   12346:                } else {
                   12347:                        REG16(AX) = 0x0f; // invalid drive
                   12348:                        m_CF = 1;
                   12349:                }
                   12350:                break;
1.1.1.30  root     12351:        default:
1.1.1.45  root     12352: //             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));
                   12353: //             REG16(AX) = 0x01;
                   12354:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12355:                m_CF = 1;
                   12356:                break;
                   12357:        }
                   12358: }
                   12359: 
1.1       root     12360: inline void msdos_int_21h_60h(int lfn)
                   12361: {
1.1.1.45  root     12362:        char full[MAX_PATH];
                   12363:        const char *path = NULL;
1.1.1.14  root     12364:        
1.1       root     12365:        if(lfn) {
1.1.1.14  root     12366:                char *name;
                   12367:                *full = '\0';
1.1.1.3   root     12368:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12369:                switch(REG8(CL)) {
                   12370:                case 1:
                   12371:                        GetShortPathName(full, full, MAX_PATH);
                   12372:                        my_strupr(full);
                   12373:                        break;
                   12374:                case 2:
                   12375:                        GetLongPathName(full, full, MAX_PATH);
                   12376:                        break;
                   12377:                }
                   12378:                path = full;
                   12379:        } else {
                   12380:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12381:        }
                   12382:        if(*path != '\0') {
                   12383:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12384:        } else {
1.1.1.14  root     12385:                REG16(AX) = (UINT16)GetLastError();
                   12386:                m_CF = 1;
1.1       root     12387:        }
                   12388: }
                   12389: 
                   12390: inline void msdos_int_21h_61h()
                   12391: {
                   12392:        REG8(AL) = 0;
                   12393: }
                   12394: 
                   12395: inline void msdos_int_21h_62h()
                   12396: {
                   12397:        REG16(BX) = current_psp;
                   12398: }
                   12399: 
                   12400: inline void msdos_int_21h_63h()
                   12401: {
                   12402:        switch(REG8(AL)) {
                   12403:        case 0x00:
1.1.1.3   root     12404:                SREG(DS) = (DBCS_TABLE >> 4);
                   12405:                i386_load_segment_descriptor(DS);
1.1       root     12406:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12407:                REG8(AL) = 0x00;
                   12408:                break;
1.1.1.22  root     12409:        case 0x01: // set korean input mode
                   12410:        case 0x02: // get korean input mode
                   12411:                REG8(AL) = 0xff; // not supported
                   12412:                break;
1.1       root     12413:        default:
1.1.1.22  root     12414:                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     12415:                REG16(AX) = 0x01;
1.1.1.3   root     12416:                m_CF = 1;
1.1       root     12417:                break;
                   12418:        }
                   12419: }
                   12420: 
1.1.1.25  root     12421: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12422: {
1.1.1.25  root     12423:        switch(func) {
1.1.1.17  root     12424:        case 0x01:
                   12425:                if(REG16(CX) >= 5) {
1.1.1.19  root     12426:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12427:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12428:                                REG16(CX) = sizeof(data);
                   12429:                        ZeroMemory(data, sizeof(data));
                   12430:                        data[0] = 0x01;
                   12431:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12432:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12433:                        *(UINT16 *)(data + 5) = active_code_page;
                   12434:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12435: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12436:                } else {
1.1.1.25  root     12437:                        return(0x08); // insufficient memory
1.1.1.17  root     12438:                }
                   12439:                break;
                   12440:        case 0x02:
                   12441:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12442:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12443:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12444: //             REG16(AX) = active_code_page;
1.1.1.17  root     12445:                REG16(CX) = 0x05;
                   12446:                break;
1.1.1.23  root     12447:        case 0x03:
                   12448:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12449:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12450:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12451: //             REG16(AX) = active_code_page;
1.1.1.23  root     12452:                REG16(CX) = 0x05;
                   12453:                break;
1.1.1.17  root     12454:        case 0x04:
                   12455:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12456:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12457:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12458: //             REG16(AX) = active_code_page;
1.1.1.17  root     12459:                REG16(CX) = 0x05;
                   12460:                break;
                   12461:        case 0x05:
                   12462:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12463:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12464:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12465: //             REG16(AX) = active_code_page;
1.1.1.17  root     12466:                REG16(CX) = 0x05;
                   12467:                break;
                   12468:        case 0x06:
                   12469:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12470:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12471:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12472: //             REG16(AX) = active_code_page;
1.1.1.17  root     12473:                REG16(CX) = 0x05;
                   12474:                break;
1.1       root     12475:        case 0x07:
1.1.1.3   root     12476:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12477:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12478:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12479: //             REG16(AX) = active_code_page;
1.1       root     12480:                REG16(CX) = 0x05;
                   12481:                break;
1.1.1.25  root     12482:        default:
                   12483:                return(0x01); // function number invalid
                   12484:        }
                   12485:        return(0x00);
                   12486: }
                   12487: 
                   12488: inline void msdos_int_21h_65h()
                   12489: {
                   12490:        char tmp[0x10000];
                   12491:        
                   12492:        switch(REG8(AL)) {
1.1.1.43  root     12493:        case 0x00:
                   12494:                if(REG16(CX) >= 7) {
                   12495:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12496:                        REG16(AX) = system_code_page;
                   12497:                } else {
                   12498:                        REG16(AX) = 0x0c;
                   12499:                        m_CF = 1;
                   12500:                }
                   12501:                break;
1.1.1.25  root     12502:        case 0x01:
                   12503:        case 0x02:
                   12504:        case 0x03:
                   12505:        case 0x04:
                   12506:        case 0x05:
                   12507:        case 0x06:
                   12508:        case 0x07:
                   12509:                {
                   12510:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12511:                        if(result) {
                   12512:                                REG16(AX) = result;
                   12513:                                m_CF = 1;
                   12514:                        } else {
                   12515:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12516:                        }
                   12517:                }
                   12518:                break;
1.1       root     12519:        case 0x20:
1.1.1.25  root     12520:        case 0xa0:
1.1.1.19  root     12521:                memset(tmp, 0, sizeof(tmp));
                   12522:                tmp[0] = REG8(DL);
1.1       root     12523:                my_strupr(tmp);
                   12524:                REG8(DL) = tmp[0];
                   12525:                break;
                   12526:        case 0x21:
1.1.1.25  root     12527:        case 0xa1:
1.1       root     12528:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12529:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12530:                my_strupr(tmp);
1.1.1.3   root     12531:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12532:                break;
                   12533:        case 0x22:
1.1.1.25  root     12534:        case 0xa2:
1.1.1.3   root     12535:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12536:                break;
1.1.1.25  root     12537:        case 0x23:
                   12538:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12539:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12540:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12541:                        REG16(AX) = 0x00;
                   12542:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12543:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12544:                        REG16(AX) = 0x01;
                   12545:                } else {
                   12546:                        REG16(AX) = 0x02;
                   12547:                }
                   12548:                break;
1.1       root     12549:        default:
1.1.1.22  root     12550:                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     12551:                REG16(AX) = 0x01;
1.1.1.3   root     12552:                m_CF = 1;
1.1       root     12553:                break;
                   12554:        }
                   12555: }
                   12556: 
                   12557: inline void msdos_int_21h_66h()
                   12558: {
                   12559:        switch(REG8(AL)) {
                   12560:        case 0x01:
                   12561:                REG16(BX) = active_code_page;
                   12562:                REG16(DX) = system_code_page;
                   12563:                break;
                   12564:        case 0x02:
                   12565:                if(active_code_page == REG16(BX)) {
                   12566:                        REG16(AX) = 0xeb41;
                   12567:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12568:                        active_code_page = REG16(BX);
1.1.1.17  root     12569:                        msdos_nls_tables_update();
1.1       root     12570:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12571:                        SetConsoleCP(active_code_page);
                   12572:                        SetConsoleOutputCP(active_code_page);
1.1       root     12573:                } else {
                   12574:                        REG16(AX) = 0x25;
1.1.1.3   root     12575:                        m_CF = 1;
1.1       root     12576:                }
                   12577:                break;
                   12578:        default:
1.1.1.22  root     12579:                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     12580:                REG16(AX) = 0x01;
1.1.1.3   root     12581:                m_CF = 1;
1.1       root     12582:                break;
                   12583:        }
                   12584: }
                   12585: 
                   12586: inline void msdos_int_21h_67h()
                   12587: {
                   12588:        process_t *process = msdos_process_info_get(current_psp);
                   12589:        
                   12590:        if(REG16(BX) <= MAX_FILES) {
                   12591:                process->max_files = max(REG16(BX), 20);
                   12592:        } else {
                   12593:                REG16(AX) = 0x08;
1.1.1.3   root     12594:                m_CF = 1;
1.1       root     12595:        }
                   12596: }
                   12597: 
                   12598: inline void msdos_int_21h_68h()
                   12599: {
                   12600:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12601:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12602:        
1.1.1.20  root     12603:        if(fd < process->max_files && file_handler[fd].valid) {
                   12604:                // fflush(_fdopen(fd, ""));
1.1       root     12605:        } else {
                   12606:                REG16(AX) = 0x06;
1.1.1.3   root     12607:                m_CF = 1;
1.1       root     12608:        }
                   12609: }
                   12610: 
                   12611: inline void msdos_int_21h_69h()
                   12612: {
1.1.1.3   root     12613:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12614:        char path[] = "A:\\";
                   12615:        char volume_label[MAX_PATH];
                   12616:        DWORD serial_number = 0;
                   12617:        char file_system[MAX_PATH];
                   12618:        
                   12619:        if(REG8(BL) == 0) {
                   12620:                path[0] = 'A' + _getdrive() - 1;
                   12621:        } else {
                   12622:                path[0] = 'A' + REG8(BL) - 1;
                   12623:        }
                   12624:        
                   12625:        switch(REG8(AL)) {
                   12626:        case 0x00:
                   12627:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12628:                        info->info_level = 0;
                   12629:                        info->serial_number = serial_number;
                   12630:                        memset(info->volume_label, 0x20, 11);
                   12631:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12632:                        memset(info->file_system, 0x20, 8);
                   12633:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12634:                } else {
                   12635:                        REG16(AX) = errno;
1.1.1.3   root     12636:                        m_CF = 1;
1.1       root     12637:                }
                   12638:                break;
                   12639:        case 0x01:
                   12640:                REG16(AX) = 0x03;
1.1.1.3   root     12641:                m_CF = 1;
1.1.1.45  root     12642:                break;
                   12643:        default:
                   12644:                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));
                   12645:                REG16(AX) = 0x01;
                   12646:                m_CF = 1;
                   12647:                break;
1.1       root     12648:        }
                   12649: }
                   12650: 
                   12651: inline void msdos_int_21h_6ah()
                   12652: {
                   12653:        REG8(AH) = 0x68;
                   12654:        msdos_int_21h_68h();
                   12655: }
                   12656: 
                   12657: inline void msdos_int_21h_6bh()
                   12658: {
1.1.1.45  root     12659:        REG8(AL) = 0x00;
1.1       root     12660: }
                   12661: 
                   12662: inline void msdos_int_21h_6ch(int lfn)
                   12663: {
1.1.1.45  root     12664:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12665:        int mode = REG8(BL) & 0x03;
                   12666:        
                   12667:        if(mode < 0x03) {
1.1.1.29  root     12668:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12669:                        // file exists
                   12670:                        if(REG8(DL) & 1) {
1.1.1.37  root     12671:                                int fd = -1;
                   12672:                                int sio_port = 0;
                   12673:                                int lpt_port = 0;
1.1       root     12674:                                
1.1.1.45  root     12675:                                if(msdos_is_device_path(path)) {
                   12676:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12677:                                } else {
1.1.1.13  root     12678:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12679:                                }
1.1       root     12680:                                if(fd != -1) {
                   12681:                                        REG16(AX) = fd;
                   12682:                                        REG16(CX) = 1;
1.1.1.45  root     12683:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12684:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12685:                                } else {
                   12686:                                        REG16(AX) = errno;
1.1.1.3   root     12687:                                        m_CF = 1;
1.1       root     12688:                                }
                   12689:                        } else if(REG8(DL) & 2) {
                   12690:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12691:                                int fd = -1;
                   12692:                                int sio_port = 0;
                   12693:                                int lpt_port = 0;
1.1       root     12694:                                
1.1.1.45  root     12695:                                if(msdos_is_device_path(path)) {
                   12696:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12697:                                } else {
                   12698:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12699:                                }
                   12700:                                if(fd != -1) {
                   12701:                                        if(attr == -1) {
                   12702:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12703:                                        }
                   12704:                                        SetFileAttributes(path, attr);
                   12705:                                        REG16(AX) = fd;
                   12706:                                        REG16(CX) = 3;
1.1.1.45  root     12707:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12708:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12709:                                } else {
                   12710:                                        REG16(AX) = errno;
1.1.1.3   root     12711:                                        m_CF = 1;
1.1       root     12712:                                }
                   12713:                        } else {
                   12714:                                REG16(AX) = 0x50;
1.1.1.3   root     12715:                                m_CF = 1;
1.1       root     12716:                        }
                   12717:                } else {
                   12718:                        // file not exists
                   12719:                        if(REG8(DL) & 0x10) {
                   12720:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12721:                                
                   12722:                                if(fd != -1) {
                   12723:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12724:                                        REG16(AX) = fd;
                   12725:                                        REG16(CX) = 2;
                   12726:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12727:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12728:                                } else {
                   12729:                                        REG16(AX) = errno;
1.1.1.3   root     12730:                                        m_CF = 1;
1.1       root     12731:                                }
                   12732:                        } else {
                   12733:                                REG16(AX) = 0x02;
1.1.1.3   root     12734:                                m_CF = 1;
1.1       root     12735:                        }
                   12736:                }
                   12737:        } else {
                   12738:                REG16(AX) = 0x0c;
1.1.1.3   root     12739:                m_CF = 1;
1.1       root     12740:        }
                   12741: }
                   12742: 
1.1.1.43  root     12743: inline void msdos_int_21h_70h()
                   12744: {
                   12745:        switch(REG8(AL)) {
1.1.1.48  root     12746:        case 0x00: // get ??? info
                   12747:        case 0x01: // set above info
                   12748: //             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));
                   12749:                REG16(AX) = 0x7000;
                   12750:                m_CF = 1;
                   12751:                break;
                   12752:        case 0x02: // set general internationalization info
1.1.1.43  root     12753:                if(REG16(CX) >= 7) {
                   12754:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12755:                        msdos_nls_tables_update();
                   12756:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12757:                        REG16(AX) = system_code_page;
                   12758:                } else {
                   12759:                        REG16(AX) = 0x0c;
                   12760:                        m_CF = 1;
                   12761:                }
                   12762:                break;
                   12763:        default:
                   12764:                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     12765:                REG16(AX) = 0x7000;
1.1.1.43  root     12766:                m_CF = 1;
                   12767:                break;
                   12768:        }
                   12769: }
                   12770: 
1.1       root     12771: inline void msdos_int_21h_710dh()
                   12772: {
                   12773:        // reset drive
                   12774: }
                   12775: 
1.1.1.48  root     12776: inline void msdos_int_21h_7141h()
1.1.1.17  root     12777: {
                   12778:        if(REG16(SI) == 0) {
1.1.1.48  root     12779:                msdos_int_21h_41h(1);
1.1.1.17  root     12780:                return;
                   12781:        }
                   12782:        if(REG16(SI) != 1) {
                   12783:                REG16(AX) = 5;
                   12784:                m_CF = 1;
                   12785:        }
                   12786:        /* wild card and matching attributes... */
                   12787:        char tmp[MAX_PATH * 2];
                   12788:        // copy search pathname (and quick check overrun)
                   12789:        ZeroMemory(tmp, sizeof(tmp));
                   12790:        tmp[MAX_PATH - 1] = '\0';
                   12791:        tmp[MAX_PATH] = 1;
                   12792:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12793:        
                   12794:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12795:                REG16(AX) = 1;
                   12796:                m_CF = 1;
                   12797:                return;
                   12798:        }
                   12799:        for(char *s = tmp; *s; ++s) {
                   12800:                if(*s == '/') {
                   12801:                        *s = '\\';
                   12802:                }
                   12803:        }
                   12804:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12805:        if(tmp_name) {
                   12806:                ++tmp_name;
                   12807:        } else {
                   12808:                tmp_name = strchr(tmp, ':');
                   12809:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12810:        }
                   12811:        
                   12812:        WIN32_FIND_DATAA fd;
                   12813:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12814:        if(fh == INVALID_HANDLE_VALUE) {
                   12815:                REG16(AX) = 2;
                   12816:                m_CF = 1;
                   12817:                return;
                   12818:        }
                   12819:        do {
                   12820:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12821:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     12822:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     12823:                                REG16(AX) = 5;
                   12824:                                m_CF = 1;
                   12825:                                break;
                   12826:                        }
                   12827:                }
                   12828:        } while(FindNextFileA(fh, &fd));
                   12829:        if(!m_CF) {
                   12830:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   12831:                        m_CF = 1;
                   12832:                        REG16(AX) = 2;
                   12833:                }
                   12834:        }
                   12835:        FindClose(fh);
                   12836: }
                   12837: 
1.1       root     12838: inline void msdos_int_21h_714eh()
                   12839: {
                   12840:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12841:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   12842:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12843:        WIN32_FIND_DATA fd;
                   12844:        
1.1.1.13  root     12845:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12846:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12847:                FindClose(dtainfo->find_handle);
                   12848:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12849:        }
                   12850:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12851:        dtainfo->allowable_mask = REG8(CL);
                   12852:        dtainfo->required_mask = REG8(CH);
                   12853:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12854:        
1.1.1.14  root     12855:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12856:                dtainfo->allowable_mask &= ~8;
1.1       root     12857:        }
1.1.1.14  root     12858:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12859:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12860:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12861:                                FindClose(dtainfo->find_handle);
                   12862:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12863:                                break;
                   12864:                        }
                   12865:                }
                   12866:        }
1.1.1.13  root     12867:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12868:                find->attrib = fd.dwFileAttributes;
                   12869:                msdos_find_file_conv_local_time(&fd);
                   12870:                if(REG16(SI) == 0) {
                   12871:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12872:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12873:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12874:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12875:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12876:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12877:                } else {
                   12878:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12879:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12880:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12881:                }
                   12882:                find->size_hi = fd.nFileSizeHigh;
                   12883:                find->size_lo = fd.nFileSizeLow;
                   12884:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12885:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12886:                REG16(AX) = dtainfo - dtalist + 1;
                   12887:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12888:                // volume label
                   12889:                find->attrib = 8;
                   12890:                find->size_hi = find->size_lo = 0;
                   12891:                strcpy(find->full_name, process->volume_label);
                   12892:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12893:                dtainfo->allowable_mask &= ~8;
                   12894:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     12895:        } else {
                   12896:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12897:                m_CF = 1;
1.1       root     12898:        }
                   12899: }
                   12900: 
                   12901: inline void msdos_int_21h_714fh()
                   12902: {
                   12903:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12904:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12905:        WIN32_FIND_DATA fd;
                   12906:        
1.1.1.14  root     12907:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12908:                REG16(AX) = 6;
1.1.1.13  root     12909:                m_CF = 1;
                   12910:                return;
                   12911:        }
1.1.1.14  root     12912:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12913:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12914:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12915:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12916:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12917:                                        FindClose(dtainfo->find_handle);
                   12918:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12919:                                        break;
                   12920:                                }
                   12921:                        }
                   12922:                } else {
1.1.1.13  root     12923:                        FindClose(dtainfo->find_handle);
                   12924:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12925:                }
                   12926:        }
1.1.1.13  root     12927:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12928:                find->attrib = fd.dwFileAttributes;
                   12929:                msdos_find_file_conv_local_time(&fd);
                   12930:                if(REG16(SI) == 0) {
                   12931:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12932:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12933:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12934:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12935:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12936:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12937:                } else {
                   12938:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12939:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12940:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12941:                }
                   12942:                find->size_hi = fd.nFileSizeHigh;
                   12943:                find->size_lo = fd.nFileSizeLow;
                   12944:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12945:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12946:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12947:                // volume label
                   12948:                find->attrib = 8;
                   12949:                find->size_hi = find->size_lo = 0;
                   12950:                strcpy(find->full_name, process->volume_label);
                   12951:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12952:                dtainfo->allowable_mask &= ~8;
1.1       root     12953:        } else {
                   12954:                REG16(AX) = 0x12;
1.1.1.3   root     12955:                m_CF = 1;
1.1       root     12956:        }
                   12957: }
                   12958: 
                   12959: inline void msdos_int_21h_71a0h()
                   12960: {
                   12961:        DWORD max_component_len, file_sys_flag;
                   12962:        
1.1.1.14  root     12963:        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))) {
                   12964:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   12965:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     12966:                REG16(CX) = (UINT16)max_component_len;          // 255
                   12967:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   12968:        } else {
                   12969:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12970:                m_CF = 1;
1.1       root     12971:        }
                   12972: }
                   12973: 
                   12974: inline void msdos_int_21h_71a1h()
                   12975: {
1.1.1.14  root     12976:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12977:                REG16(AX) = 6;
1.1.1.13  root     12978:                m_CF = 1;
                   12979:                return;
                   12980:        }
1.1.1.14  root     12981:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12982:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12983:                FindClose(dtainfo->find_handle);
                   12984:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12985:        }
                   12986: }
                   12987: 
                   12988: inline void msdos_int_21h_71a6h()
                   12989: {
                   12990:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12991:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12992:        
1.1.1.3   root     12993:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12994:        struct _stat64 status;
                   12995:        DWORD serial_number = 0;
                   12996:        
1.1.1.20  root     12997:        if(fd < process->max_files && file_handler[fd].valid) {
                   12998:                if(_fstat64(fd, &status) == 0) {
                   12999:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13000:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13001:                                char volume[] = "A:\\";
1.1.1.20  root     13002:                                volume[0] = file_handler[fd].path[1];
1.1       root     13003:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   13004:                        }
1.1.1.20  root     13005:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     13006:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13007:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13008:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13009:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13010:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13011:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13012:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13013:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13014:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13015:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13016:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13017:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13018:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13019:                } else {
                   13020:                        REG16(AX) = errno;
1.1.1.3   root     13021:                        m_CF = 1;
1.1       root     13022:                }
                   13023:        } else {
                   13024:                REG16(AX) = 0x06;
1.1.1.3   root     13025:                m_CF = 1;
1.1       root     13026:        }
                   13027: }
                   13028: 
                   13029: inline void msdos_int_21h_71a7h()
                   13030: {
                   13031:        switch(REG8(BL)) {
                   13032:        case 0x00:
1.1.1.3   root     13033:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13034:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13035:                        m_CF = 1;
1.1       root     13036:                }
                   13037:                break;
                   13038:        case 0x01:
                   13039:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13040:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13041:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13042:                        m_CF = 1;
1.1       root     13043:                }
                   13044:                break;
                   13045:        default:
1.1.1.22  root     13046:                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     13047:                REG16(AX) = 0x7100;
1.1.1.3   root     13048:                m_CF = 1;
1.1       root     13049:                break;
                   13050:        }
                   13051: }
                   13052: 
                   13053: inline void msdos_int_21h_71a8h()
                   13054: {
                   13055:        if(REG8(DH) == 0) {
                   13056:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13057:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13058:                memset(fcb, 0x20, sizeof(fcb));
                   13059:                int len = strlen(tmp);
1.1.1.21  root     13060:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13061:                        if(tmp[i] == '.') {
                   13062:                                pos = 8;
                   13063:                        } else {
                   13064:                                if(msdos_lead_byte_check(tmp[i])) {
                   13065:                                        fcb[pos++] = tmp[i++];
                   13066:                                }
                   13067:                                fcb[pos++] = tmp[i];
                   13068:                        }
                   13069:                }
1.1.1.3   root     13070:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13071:        } else {
1.1.1.3   root     13072:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13073:        }
                   13074: }
                   13075: 
1.1.1.22  root     13076: inline void msdos_int_21h_71aah()
                   13077: {
                   13078:        char drv[] = "A:", path[MAX_PATH];
                   13079:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13080:        
                   13081:        if(REG8(BL) == 0) {
                   13082:                drv[0] = 'A' + _getdrive() - 1;
                   13083:        } else {
                   13084:                drv[0] = 'A' + REG8(BL) - 1;
                   13085:        }
                   13086:        switch(REG8(BH)) {
                   13087:        case 0x00:
1.1.1.44  root     13088:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13089:                        REG16(AX) = 0x0f; // invalid drive
                   13090:                        m_CF = 1;
                   13091:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   13092:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13093:                        m_CF = 1;
                   13094:                }
                   13095:                break;
                   13096:        case 0x01:
1.1.1.44  root     13097:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13098:                        REG16(AX) = 0x0f; // invalid drive
                   13099:                        m_CF = 1;
                   13100:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13101:                        REG16(AX) = 0x0f; // invalid drive
                   13102:                        m_CF = 1;
                   13103:                }
                   13104:                break;
                   13105:        case 0x02:
1.1.1.44  root     13106:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13107:                        REG16(AX) = 0x0f; // invalid drive
                   13108:                        m_CF = 1;
                   13109:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13110:                        REG16(AX) = 0x0f; // invalid drive
                   13111:                        m_CF = 1;
                   13112:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13113:                        REG16(AX) = 0x0f; // invalid drive
                   13114:                        m_CF = 1;
                   13115:                } else {
                   13116:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13117:                }
                   13118:                break;
                   13119:        default:
                   13120:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     13121:                REG16(AX) = 0x7100;
1.1.1.22  root     13122:                m_CF = 1;
                   13123:                break;
                   13124:        }
                   13125: }
                   13126: 
1.1.1.14  root     13127: inline void msdos_int_21h_7300h()
                   13128: {
1.1.1.44  root     13129:        REG8(AL) = REG8(CL);
                   13130:        REG8(AH) = 0;
1.1.1.14  root     13131: }
                   13132: 
                   13133: inline void msdos_int_21h_7302h()
                   13134: {
                   13135:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13136:        UINT16 seg, ofs;
                   13137:        
                   13138:        if(REG16(CX) < 0x3f) {
                   13139:                REG8(AL) = 0x18;
                   13140:                m_CF = 1;
                   13141:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13142:                REG8(AL) = 0xff;
                   13143:                m_CF = 1;
                   13144:        } else {
                   13145:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13146:        }
                   13147: }
                   13148: 
1.1       root     13149: inline void msdos_int_21h_7303h()
                   13150: {
1.1.1.3   root     13151:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13152:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13153:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13154:        
                   13155:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   13156:                info->size_of_structure = sizeof(ext_space_info_t);
                   13157:                info->structure_version = 0;
                   13158:                info->sectors_per_cluster = sectors_per_cluster;
                   13159:                info->bytes_per_sector = bytes_per_sector;
                   13160:                info->available_clusters_on_drive = free_clusters;
                   13161:                info->total_clusters_on_drive = total_clusters;
                   13162:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13163:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13164:                info->available_allocation_units = free_clusters;       // ???
                   13165:                info->total_allocation_units = total_clusters;          // ???
                   13166:        } else {
                   13167:                REG16(AX) = errno;
1.1.1.3   root     13168:                m_CF = 1;
1.1       root     13169:        }
                   13170: }
                   13171: 
1.1.1.30  root     13172: inline void msdos_int_21h_dbh()
                   13173: {
                   13174:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13175:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13176:        REG8(AL) = dos_info->last_drive;
                   13177: }
                   13178: 
                   13179: inline void msdos_int_21h_dch()
                   13180: {
                   13181:        // Novell NetWare - Connection Services - Get Connection Number
                   13182:        REG8(AL) = 0x00;
                   13183: }
                   13184: 
1.1.1.32  root     13185: inline void msdos_int_24h()
                   13186: {
                   13187:        const char *message = NULL;
                   13188:        int key = 0;
                   13189:        
                   13190:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13191:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13192:                        if(active_code_page == 932) {
                   13193:                                message = critical_error_table[i].message_japanese;
                   13194:                        }
                   13195:                        if(message == NULL) {
                   13196:                                message = critical_error_table[i].message_english;
                   13197:                        }
                   13198:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13199:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13200:                        
                   13201:                        SREG(ES) = WORK_TOP >> 4;
                   13202:                        i386_load_segment_descriptor(ES);
                   13203:                        REG16(DI) = 0x0000;
                   13204:                        break;
                   13205:                }
                   13206:        }
                   13207:        fprintf(stderr, "\n%s", message);
                   13208:        if(!(REG8(AH) & 0x80)) {
                   13209:                if(REG8(AH) & 0x01) {
                   13210:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13211:                } else {
                   13212:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13213:                }
                   13214:        }
                   13215:        fprintf(stderr, "\n");
                   13216:        
1.1.1.33  root     13217:        {
1.1.1.32  root     13218:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13219:        }
1.1.1.32  root     13220:        if(REG8(AH) & 0x10) {
                   13221:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13222:        }
                   13223:        if(REG8(AH) & 0x20) {
                   13224:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13225:        }
                   13226:        if(REG8(AH) & 0x08) {
                   13227:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13228:        }
                   13229:        fprintf(stderr, "? ");
                   13230:        
                   13231:        while(1) {
                   13232:                while(!_kbhit()) {
                   13233:                        Sleep(10);
                   13234:                }
                   13235:                key = _getch();
                   13236:                
                   13237:                if(key == 'I' || key == 'i') {
                   13238:                        if(REG8(AH) & 0x20) {
                   13239:                                REG8(AL) = 0;
                   13240:                                break;
                   13241:                        }
                   13242:                } else if(key == 'R' || key == 'r') {
                   13243:                        if(REG8(AH) & 0x10) {
                   13244:                                REG8(AL) = 1;
                   13245:                                break;
                   13246:                        }
                   13247:                } else if(key == 'A' || key == 'a') {
                   13248:                        REG8(AL) = 2;
                   13249:                        break;
                   13250:                } else if(key == 'F' || key == 'f') {
                   13251:                        if(REG8(AH) & 0x08) {
                   13252:                                REG8(AL) = 3;
                   13253:                                break;
                   13254:                        }
                   13255:                }
                   13256:        }
                   13257:        fprintf(stderr, "%c\n", key);
                   13258: }
                   13259: 
1.1       root     13260: inline void msdos_int_25h()
                   13261: {
                   13262:        UINT16 seg, ofs;
                   13263:        DWORD dwSize;
                   13264:        
1.1.1.3   root     13265: #if defined(HAS_I386)
                   13266:        I386OP(pushf)();
                   13267: #else
                   13268:        PREFIX86(_pushf());
                   13269: #endif
1.1       root     13270:        
                   13271:        if(!(REG8(AL) < 26)) {
                   13272:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13273:                m_CF = 1;
1.1       root     13274:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13275:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13276:                m_CF = 1;
1.1       root     13277:        } else {
                   13278:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13279:                char dev[64];
                   13280:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13281:                
                   13282:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13283:                if(hFile == INVALID_HANDLE_VALUE) {
                   13284:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13285:                        m_CF = 1;
1.1       root     13286:                } else {
1.1.1.19  root     13287:                        UINT32 top_sector  = REG16(DX);
                   13288:                        UINT16 sector_num  = REG16(CX);
                   13289:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13290:                        
                   13291:                        if(sector_num == 0xffff) {
                   13292:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13293:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13294:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13295:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13296:                                buffer_addr = (seg << 4) + ofs;
                   13297:                        }
                   13298: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13299: //                             REG8(AL) = 0x02; // drive not ready
                   13300: //                             m_CF = 1;
                   13301: //                     } else 
                   13302:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13303:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13304:                                m_CF = 1;
1.1.1.19  root     13305:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13306:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13307:                                m_CF = 1;
1.1       root     13308:                        }
                   13309:                        CloseHandle(hFile);
                   13310:                }
                   13311:        }
                   13312: }
                   13313: 
                   13314: inline void msdos_int_26h()
                   13315: {
1.1.1.42  root     13316:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13317:        UINT16 seg, ofs;
                   13318:        DWORD dwSize;
                   13319:        
1.1.1.3   root     13320: #if defined(HAS_I386)
                   13321:        I386OP(pushf)();
                   13322: #else
                   13323:        PREFIX86(_pushf());
                   13324: #endif
1.1       root     13325:        
                   13326:        if(!(REG8(AL) < 26)) {
                   13327:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13328:                m_CF = 1;
1.1       root     13329:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13330:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13331:                m_CF = 1;
1.1       root     13332:        } else {
                   13333:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13334:                char dev[64];
                   13335:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13336:                
                   13337:                if(dpb->media_type == 0xf8) {
                   13338:                        // this drive is not a floppy
1.1.1.6   root     13339: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13340: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13341: //                     }
1.1       root     13342:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13343:                        m_CF = 1;
1.1       root     13344:                } else {
                   13345:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13346:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13347:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13348:                                m_CF = 1;
1.1       root     13349:                        } else {
1.1.1.19  root     13350:                                UINT32 top_sector  = REG16(DX);
                   13351:                                UINT16 sector_num  = REG16(CX);
                   13352:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13353:                                
                   13354:                                if(sector_num == 0xffff) {
                   13355:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13356:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13357:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13358:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13359:                                        buffer_addr = (seg << 4) + ofs;
                   13360:                                }
1.1       root     13361:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13362:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13363:                                        m_CF = 1;
1.1.1.19  root     13364:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13365:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13366:                                        m_CF = 1;
1.1.1.19  root     13367:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13368:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13369:                                        m_CF = 1;
1.1       root     13370:                                }
                   13371:                                CloseHandle(hFile);
                   13372:                        }
                   13373:                }
                   13374:        }
                   13375: }
                   13376: 
                   13377: inline void msdos_int_27h()
                   13378: {
1.1.1.29  root     13379:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13380:        try {
                   13381:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13382:        } catch(...) {
                   13383:                // recover the broken mcb
                   13384:                int mcb_seg = SREG(CS) - 1;
                   13385:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13386:                
1.1.1.29  root     13387:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13388:                        mcb->mz = 'M';
                   13389:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13390:                        
1.1.1.29  root     13391:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13392:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13393:                        } else {
1.1.1.39  root     13394:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13395:                        }
                   13396:                } else {
                   13397:                        mcb->mz = 'Z';
1.1.1.30  root     13398:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13399:                }
                   13400:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13401:        }
1.1.1.3   root     13402:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13403: }
                   13404: 
                   13405: inline void msdos_int_29h()
                   13406: {
1.1.1.50  root     13407:        msdos_putch_fast(REG8(AL));
1.1       root     13408: }
                   13409: 
                   13410: inline void msdos_int_2eh()
                   13411: {
                   13412:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13413:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13414:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13415:        char *token = my_strtok(tmp, " ");
                   13416:        strcpy(command, token);
                   13417:        strcpy(opt, token + strlen(token) + 1);
                   13418:        
                   13419:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13420:        param->env_seg = 0;
                   13421:        param->cmd_line.w.l = 44;
                   13422:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13423:        param->fcb1.w.l = 24;
                   13424:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13425:        param->fcb2.w.l = 24;
                   13426:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13427:        
                   13428:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13429:        
                   13430:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13431:        cmd_line->len = strlen(opt);
                   13432:        strcpy(cmd_line->cmd, opt);
                   13433:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13434:        
1.1.1.28  root     13435:        try {
                   13436:                if(msdos_process_exec(command, param, 0)) {
                   13437:                        REG16(AX) = 0xffff; // error before processing command
                   13438:                } else {
                   13439:                        // set flag to set retval to ax when the started process is terminated
                   13440:                        process_t *process = msdos_process_info_get(current_psp);
                   13441:                        process->called_by_int2eh = true;
                   13442:                }
                   13443:        } catch(...) {
                   13444:                REG16(AX) = 0xffff; // error before processing command
                   13445:        }
1.1       root     13446: }
                   13447: 
1.1.1.29  root     13448: inline void msdos_int_2fh_05h()
                   13449: {
                   13450:        switch(REG8(AL)) {
                   13451:        case 0x00:
1.1.1.49  root     13452:                // critical error handler is installed
1.1.1.32  root     13453:                REG8(AL) = 0xff;
                   13454:                break;
                   13455:        case 0x01:
                   13456:        case 0x02:
                   13457:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13458:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13459:                                const char *message = NULL;
                   13460:                                if(active_code_page == 932) {
                   13461:                                        message = standard_error_table[i].message_japanese;
                   13462:                                }
                   13463:                                if(message == NULL) {
                   13464:                                        message = standard_error_table[i].message_english;
                   13465:                                }
                   13466:                                strcpy((char *)(mem + WORK_TOP), message);
                   13467:                                
                   13468:                                SREG(ES) = WORK_TOP >> 4;
                   13469:                                i386_load_segment_descriptor(ES);
                   13470:                                REG16(DI) = 0x0000;
                   13471:                                REG8(AL) = 0x01;
                   13472:                                break;
                   13473:                        }
                   13474:                }
1.1.1.29  root     13475:                break;
                   13476:        default:
                   13477:                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     13478:                REG16(AX) = 0x01;
1.1.1.29  root     13479:                m_CF = 1;
                   13480:        }
                   13481: }
                   13482: 
1.1.1.44  root     13483: inline void msdos_int_2fh_06h()
                   13484: {
                   13485:        switch(REG8(AL)) {
                   13486:        case 0x00:
                   13487:                // ASSIGN is not installed
1.1.1.49  root     13488: //             REG8(AL) = 0x00;
1.1.1.44  root     13489:                break;
                   13490:        case 0x01:
                   13491:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13492:                REG16(AX) = 0x01;
                   13493:                m_CF = 1;
                   13494:                break;
                   13495:        default:
                   13496:                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));
                   13497:                REG16(AX) = 0x01;
                   13498:                m_CF = 1;
                   13499:                break;
                   13500:        }
                   13501: }
                   13502: 
1.1.1.22  root     13503: inline void msdos_int_2fh_11h()
                   13504: {
                   13505:        switch(REG8(AL)) {
                   13506:        case 0x00:
1.1.1.29  root     13507:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     13508: #ifdef SUPPORT_MSCDEX
                   13509:                        // MSCDEX is installed
                   13510:                        REG8(AL) = 0xff;
                   13511:                        i386_write_stack(0xadad);
                   13512: #else
1.1.1.29  root     13513:                        // MSCDEX is not installed
                   13514: //                     REG8(AL) = 0x00;
1.1.1.53  root     13515: #endif
1.1.1.29  root     13516:                } else {
                   13517:                        // Network Redirector is not installed
                   13518: //                     REG8(AL) = 0x00;
                   13519:                }
1.1.1.22  root     13520:                break;
                   13521:        default:
1.1.1.43  root     13522: //             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     13523:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13524:                m_CF = 1;
                   13525:                break;
                   13526:        }
                   13527: }
                   13528: 
1.1.1.21  root     13529: inline void msdos_int_2fh_12h()
                   13530: {
                   13531:        switch(REG8(AL)) {
1.1.1.22  root     13532:        case 0x00:
1.1.1.29  root     13533:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13534:                REG8(AL) = 0xff;
                   13535:                break;
1.1.1.29  root     13536: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13537:        case 0x02:
                   13538:                {
                   13539:                        UINT16 stack = i386_read_stack();
                   13540:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13541:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13542:                        i386_load_segment_descriptor(ES);
                   13543:                }
                   13544:                break;
1.1.1.30  root     13545:        case 0x03:
                   13546:                SREG(DS) = (DEVICE_TOP >> 4);
                   13547:                i386_load_segment_descriptor(DS);
                   13548:                break;
1.1.1.29  root     13549:        case 0x04:
                   13550:                {
                   13551:                        UINT16 stack = i386_read_stack();
                   13552:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13553: #if defined(HAS_I386)
                   13554:                        m_ZF = (REG8(AL) == '\\');
                   13555: #else
                   13556:                        m_ZeroVal = (REG8(AL) != '\\');
                   13557: #endif
                   13558:                }
                   13559:                break;
                   13560:        case 0x05:
1.1.1.49  root     13561:                {
                   13562:                        UINT16 c = i386_read_stack();
                   13563:                        if((c >> 0) & 0xff) {
                   13564:                                msdos_putch((c >> 0) & 0xff);
                   13565:                        }
                   13566:                        if((c >> 8) & 0xff) {
                   13567:                                msdos_putch((c >> 8) & 0xff);
                   13568:                        }
                   13569:                }
1.1.1.29  root     13570:                break;
1.1.1.49  root     13571: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     13572: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13573: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     13574: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     13575: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13576: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13577: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13578:        case 0x0d:
                   13579:                {
                   13580:                        SYSTEMTIME time;
                   13581:                        FILETIME file_time;
                   13582:                        WORD dos_date, dos_time;
                   13583:                        GetLocalTime(&time);
                   13584:                        SystemTimeToFileTime(&time, &file_time);
                   13585:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13586:                        REG16(AX) = dos_date;
                   13587:                        REG16(DX) = dos_time;
                   13588:                }
                   13589:                break;
                   13590: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13591: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13592: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13593:        case 0x11:
                   13594:                {
                   13595:                        char path[MAX_PATH], *p;
                   13596:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13597:                        my_strupr(path);
                   13598:                        while((p = my_strchr(path, '/')) != NULL) {
                   13599:                                *p = '\\';
                   13600:                        }
                   13601:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13602:                }
                   13603:                break;
                   13604:        case 0x12:
                   13605:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13606:                break;
                   13607:        case 0x13:
                   13608:                {
                   13609:                        char tmp[2] = {0};
                   13610:                        tmp[0] = i386_read_stack();
                   13611:                        my_strupr(tmp);
                   13612:                        REG8(AL) = tmp[0];
                   13613:                }
                   13614:                break;
                   13615:        case 0x14:
                   13616: #if defined(HAS_I386)
                   13617:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13618: #else
                   13619:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13620: #endif
                   13621:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13622:                break;
                   13623: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13624:        case 0x16:
                   13625:                if(REG16(BX) < 20) {
                   13626:                        SREG(ES) = SFT_TOP >> 4;
                   13627:                        i386_load_segment_descriptor(ES);
                   13628:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13629:                        
                   13630:                        // update system file table
                   13631:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13632:                        if(file_handler[REG16(BX)].valid) {
                   13633:                                int count = 0;
                   13634:                                for(int i = 0; i < 20; i++) {
                   13635:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13636:                                                count++;
                   13637:                                        }
                   13638:                                }
                   13639:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13640:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13641:                                _lseek(REG16(BX), 0, SEEK_END);
                   13642:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13643:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13644:                        } else {
                   13645:                                memset(sft, 0, 0x3b);
                   13646:                        }
                   13647:                } else {
                   13648:                        REG16(AX) = 0x06;
                   13649:                        m_CF = 1;
                   13650:                }
                   13651:                break;
1.1.1.49  root     13652:        case 0x17:
                   13653:                {
                   13654:                        UINT16 drive = i386_read_stack();
                   13655:                        if(msdos_is_valid_drive(drive)) {
                   13656:                                msdos_cds_update(drive);
                   13657:                        }
                   13658:                        REG16(SI) = 88 * drive;
                   13659:                        SREG(DS) = (CDS_TOP >> 4);
                   13660:                        i386_load_segment_descriptor(DS);
                   13661:                }
                   13662:                break;
1.1.1.29  root     13663: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13664: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13665:        case 0x1a:
                   13666:                {
                   13667:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13668:                        if(path[1] == ':') {
                   13669:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13670:                                        REG8(AL) = path[0] - 'a' + 1;
                   13671:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13672:                                        REG8(AL) = path[0] - 'A' + 1;
                   13673:                                } else {
                   13674:                                        REG8(AL) = 0xff; // invalid
                   13675:                                }
                   13676:                                strcpy(full, path);
                   13677:                                strcpy(path, full + 2);
                   13678:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13679:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13680:                                        REG8(AL) = full[0] - 'a' + 1;
                   13681:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13682:                                        REG8(AL) = full[0] - 'A' + 1;
                   13683:                                } else {
                   13684:                                        REG8(AL) = 0xff; // invalid
                   13685:                                }
                   13686:                        } else {
                   13687:                                REG8(AL) = 0x00; // default
                   13688:                        }
                   13689:                }
                   13690:                break;
                   13691:        case 0x1b:
                   13692:                {
                   13693:                        int year = REG16(CX) + 1980;
                   13694:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13695:                }
                   13696:                break;
                   13697: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13698: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13699:        case 0x1e:
                   13700:                {
                   13701:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13702:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13703:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13704: #if defined(HAS_I386)
                   13705:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13706: #else
                   13707:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13708: #endif
                   13709:                        } else {
                   13710: #if defined(HAS_I386)
                   13711:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13712: #else
                   13713:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13714: #endif
                   13715:                        }
                   13716:                }
                   13717:                break;
1.1.1.49  root     13718:        case 0x1f:
                   13719:                {
                   13720:                        UINT16 drive = i386_read_stack();
                   13721:                        if(msdos_is_valid_drive(drive)) {
                   13722:                                msdos_cds_update(drive);
                   13723:                        }
                   13724:                        REG16(SI) = 88 * drive;
                   13725:                        SREG(ES) = (CDS_TOP >> 4);
                   13726:                        i386_load_segment_descriptor(ES);
                   13727:                }
                   13728:                break;
1.1.1.21  root     13729:        case 0x20:
                   13730:                {
                   13731:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13732:                        
                   13733:                        if(fd < 20) {
                   13734:                                SREG(ES) = current_psp;
                   13735:                                i386_load_segment_descriptor(ES);
                   13736:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13737:                        } else {
                   13738:                                REG16(AX) = 0x06;
                   13739:                                m_CF = 1;
                   13740:                        }
                   13741:                }
                   13742:                break;
1.1.1.29  root     13743:        case 0x21:
                   13744:                msdos_int_21h_60h(0);
                   13745:                break;
1.1.1.49  root     13746:        case 0x22:
                   13747:                {
                   13748:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13749:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   13750:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   13751:                        }
                   13752:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   13753:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   13754:                        }
                   13755:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   13756:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   13757:                        }
                   13758:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   13759:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   13760:                        }
                   13761:                }
                   13762:                break;
1.1.1.29  root     13763: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13764: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13765:        case 0x25:
                   13766:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13767:                break;
                   13768:        case 0x26:
                   13769:                REG8(AL) = REG8(CL);
                   13770:                msdos_int_21h_3dh();
                   13771:                break;
                   13772:        case 0x27:
                   13773:                msdos_int_21h_3eh();
                   13774:                break;
                   13775:        case 0x28:
                   13776:                REG16(AX) = REG16(BP);
                   13777:                msdos_int_21h_42h();
                   13778:                break;
                   13779:        case 0x29:
                   13780:                msdos_int_21h_3fh();
                   13781:                break;
                   13782: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13783:        case 0x2b:
                   13784:                REG16(AX) = REG16(BP);
                   13785:                msdos_int_21h_44h();
                   13786:                break;
                   13787:        case 0x2c:
                   13788:                REG16(BX) = DEVICE_TOP >> 4;
                   13789:                REG16(AX) = 22;
                   13790:                break;
                   13791:        case 0x2d:
                   13792:                {
                   13793:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13794:                        REG16(AX) = sda->extended_error_code;
                   13795:                }
                   13796:                break;
                   13797:        case 0x2e:
                   13798:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13799:                        SREG(ES) = 0x0001;
                   13800:                        i386_load_segment_descriptor(ES);
                   13801:                        REG16(DI) = 0x00;
                   13802:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     13803:                        // dummy parameter error message read routine is at fffc:0010
                   13804:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     13805:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13806:                        REG16(DI) = 0x0010;
1.1.1.22  root     13807:                }
                   13808:                break;
1.1.1.29  root     13809:        case 0x2f:
                   13810:                if(REG16(DX) != 0) {
1.1.1.30  root     13811:                        dos_major_version = REG8(DL);
                   13812:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13813:                } else {
                   13814:                        REG8(DL) = 7;
                   13815:                        REG8(DH) = 10;
                   13816:                }
                   13817:                break;
                   13818: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13819: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13820:        default:
                   13821:                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));
                   13822:                REG16(AX) = 0x01;
                   13823:                m_CF = 1;
                   13824:                break;
                   13825:        }
                   13826: }
                   13827: 
1.1.1.30  root     13828: inline void msdos_int_2fh_13h()
                   13829: {
                   13830:        static UINT16 prevDS = 0, prevDX = 0;
                   13831:        static UINT16 prevES = 0, prevBX = 0;
                   13832:        UINT16 tmp;
                   13833:        
                   13834:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   13835:        i386_load_segment_descriptor(DS);
                   13836:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   13837:        
                   13838:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   13839:        i386_load_segment_descriptor(ES);
                   13840:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   13841: }
                   13842: 
1.1.1.22  root     13843: inline void msdos_int_2fh_14h()
                   13844: {
                   13845:        switch(REG8(AL)) {
                   13846:        case 0x00:
1.1.1.29  root     13847:                // NLSFUNC.COM is installed
                   13848:                REG8(AL) = 0xff;
1.1.1.25  root     13849:                break;
                   13850:        case 0x01:
                   13851:        case 0x03:
                   13852:                REG8(AL) = 0x00;
                   13853:                active_code_page = REG16(BX);
                   13854:                msdos_nls_tables_update();
                   13855:                break;
                   13856:        case 0x02:
                   13857:                REG8(AL) = get_extended_country_info(REG16(BP));
                   13858:                break;
                   13859:        case 0x04:
1.1.1.42  root     13860:                for(int i = 0;; i++) {
                   13861:                        if(country_table[i].code == REG16(DX)) {
                   13862:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   13863:                                break;
                   13864:                        } else if(country_table[i].code == -1) {
                   13865:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13866:                                break;
                   13867:                        }
                   13868:                }
1.1.1.25  root     13869:                REG8(AL) = 0x00;
1.1.1.22  root     13870:                break;
                   13871:        default:
                   13872:                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));
                   13873:                REG16(AX) = 0x01;
                   13874:                m_CF = 1;
                   13875:                break;
                   13876:        }
                   13877: }
                   13878: 
                   13879: inline void msdos_int_2fh_15h()
                   13880: {
                   13881:        switch(REG8(AL)) {
1.1.1.29  root     13882:        case 0x00: // CD-ROM - Installation Check
                   13883:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     13884: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     13885:                        // MSCDEX is installed
                   13886:                        REG16(BX) = 0;
                   13887:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     13888:                                if(msdos_is_cdrom_drive(i)) {
                   13889:                                        if(REG16(BX) == 0) {
                   13890:                                                REG16(CX) = i;
1.1.1.43  root     13891:                                        }
1.1.1.44  root     13892:                                        REG16(BX)++;
1.1.1.43  root     13893:                                }
                   13894:                        }
                   13895: #else
1.1.1.29  root     13896:                        // MSCDEX is not installed
                   13897: //                     REG8(AL) = 0x00;
1.1.1.43  root     13898: #endif
1.1.1.29  root     13899:                } else {
                   13900:                        // GRAPHICS.COM is not installed
                   13901: //                     REG8(AL) = 0x00;
                   13902:                }
1.1.1.22  root     13903:                break;
1.1.1.43  root     13904:        case 0x0b:
1.1.1.44  root     13905:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   13906:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   13907:                REG16(BX) = 0xadad;
1.1.1.43  root     13908:                break;
                   13909:        case 0x0d:
1.1.1.44  root     13910:                for(int i = 0, n = 0; i < 26; i++) {
                   13911:                        if(msdos_is_cdrom_drive(i)) {
                   13912:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     13913:                        }
                   13914:                }
                   13915:                break;
1.1.1.22  root     13916:        case 0xff:
1.1.1.29  root     13917:                if(REG16(BX) == 0x0000) {
                   13918:                        // CORELCDX is not installed
                   13919:                } else {
                   13920:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   13921:                        REG16(AX) = 0x01;
                   13922:                        m_CF = 1;
                   13923:                }
1.1.1.22  root     13924:                break;
1.1.1.21  root     13925:        default:
1.1.1.22  root     13926:                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     13927:                REG16(AX) = 0x01;
                   13928:                m_CF = 1;
                   13929:                break;
                   13930:        }
                   13931: }
                   13932: 
1.1       root     13933: inline void msdos_int_2fh_16h()
                   13934: {
                   13935:        switch(REG8(AL)) {
                   13936:        case 0x00:
1.1.1.14  root     13937:                if(no_windows) {
1.1.1.29  root     13938:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   13939: //                     REG8(AL) = 0x00;
1.1.1.14  root     13940:                } else {
1.1.1.30  root     13941:                        REG8(AL) = win_major_version;
                   13942:                        REG8(AH) = win_minor_version;
1.1       root     13943:                }
                   13944:                break;
1.1.1.43  root     13945:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     13946:                // from DOSBox
                   13947:                i386_set_a20_line(1);
                   13948:                break;
1.1.1.49  root     13949:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     13950:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   13951:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   13952:                break;
                   13953:        case 0x07:
                   13954:                // Virtual Device Call API
                   13955:                break;
1.1.1.22  root     13956:        case 0x0a:
                   13957:                if(!no_windows) {
                   13958:                        REG16(AX) = 0x0000;
1.1.1.30  root     13959:                        REG8(BH) = win_major_version;
                   13960:                        REG8(BL) = win_minor_version;
1.1.1.49  root     13961: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     13962:                        REG16(CX) = 0x0003; // enhanced
                   13963:                }
                   13964:                break;
1.1.1.30  root     13965:        case 0x0b:
                   13966:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     13967:        case 0x0e:
                   13968:        case 0x0f:
1.1.1.30  root     13969:        case 0x10:
1.1.1.22  root     13970:        case 0x11:
                   13971:        case 0x12:
                   13972:        case 0x13:
                   13973:        case 0x14:
1.1.1.30  root     13974:        case 0x15:
1.1.1.43  root     13975:        case 0x81:
                   13976:        case 0x82:
1.1.1.44  root     13977:        case 0x84:
1.1.1.49  root     13978:        case 0x85:
1.1.1.33  root     13979:        case 0x86:
1.1.1.22  root     13980:        case 0x87:
1.1.1.30  root     13981:        case 0x89:
1.1.1.33  root     13982:        case 0x8a:
1.1.1.22  root     13983:                // function not supported, do not clear AX
                   13984:                break;
1.1.1.14  root     13985:        case 0x80:
                   13986:                Sleep(10);
1.1.1.35  root     13987:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     13988:                REG8(AL) = 0x00;
1.1.1.14  root     13989:                break;
1.1.1.33  root     13990:        case 0x83:
                   13991:                REG16(BX) = 0x01; // system vm id
                   13992:                break;
1.1.1.22  root     13993:        case 0x8e:
                   13994:                REG16(AX) = 0x00; // failed
                   13995:                break;
1.1.1.20  root     13996:        case 0x8f:
                   13997:                switch(REG8(DH)) {
                   13998:                case 0x01:
1.1.1.49  root     13999: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14000: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14001:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14002:                        break;
                   14003:                default:
                   14004:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14005:                        break;
                   14006:                }
                   14007:                break;
1.1       root     14008:        default:
1.1.1.22  root     14009:                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));
                   14010:                REG16(AX) = 0x01;
                   14011:                m_CF = 1;
                   14012:                break;
                   14013:        }
                   14014: }
                   14015: 
                   14016: inline void msdos_int_2fh_19h()
                   14017: {
                   14018:        switch(REG8(AL)) {
                   14019:        case 0x00:
1.1.1.29  root     14020:                // SHELLB.COM is not installed
                   14021: //             REG8(AL) = 0x00;
1.1.1.22  root     14022:                break;
                   14023:        case 0x01:
                   14024:        case 0x02:
                   14025:        case 0x03:
                   14026:        case 0x04:
                   14027:                REG16(AX) = 0x01;
                   14028:                m_CF = 1;
                   14029:                break;
1.1.1.29  root     14030:        case 0x80:
                   14031:                // IBM ROM-DOS v4.0 is not installed
                   14032: //             REG8(AL) = 0x00;
                   14033:                break;
1.1.1.22  root     14034:        default:
                   14035:                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     14036:                REG16(AX) = 0x01;
1.1.1.3   root     14037:                m_CF = 1;
1.1       root     14038:                break;
                   14039:        }
                   14040: }
                   14041: 
                   14042: inline void msdos_int_2fh_1ah()
                   14043: {
                   14044:        switch(REG8(AL)) {
                   14045:        case 0x00:
1.1.1.29  root     14046:                // ANSI.SYS is installed
1.1       root     14047:                REG8(AL) = 0xff;
                   14048:                break;
1.1.1.49  root     14049:        case 0x01:
1.1.1.50  root     14050:                if(REG8(CL) == 0x5f) {
                   14051:                        // set display information
                   14052:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14053:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14054:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14055:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14056:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14057:                                
                   14058:                                if(cur_width != new_width || cur_height != new_height) {
                   14059:                                        pcbios_set_console_size(new_width, new_height, true);
                   14060:                                }
                   14061:                        }
                   14062:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14063:                        // get display information
1.1.1.50  root     14064:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14065:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14066:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14067:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14068:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14069:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14070:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14071:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14072:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14073:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14074:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14075:                } else {
                   14076:                        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));
                   14077:                        REG16(AX) = 0x01;
                   14078:                        m_CF = 1;
                   14079:                }
                   14080:                break;
1.1       root     14081:        default:
1.1.1.22  root     14082:                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));
                   14083:                REG16(AX) = 0x01;
                   14084:                m_CF = 1;
                   14085:                break;
                   14086:        }
                   14087: }
                   14088: 
1.1.1.30  root     14089: inline void msdos_int_2fh_40h()
1.1.1.22  root     14090: {
                   14091:        switch(REG8(AL)) {
                   14092:        case 0x00:
1.1.1.30  root     14093:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14094:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14095:                break;
1.1.1.43  root     14096:        case 0x10:
                   14097:                // OS/2 v2.0+ - Installation Check
                   14098:                REG16(AX) = 0x01;
                   14099:                m_CF = 1;
                   14100:                break;
1.1.1.22  root     14101:        default:
                   14102:                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     14103:                REG16(AX) = 0x01;
1.1.1.3   root     14104:                m_CF = 1;
1.1       root     14105:                break;
                   14106:        }
                   14107: }
                   14108: 
                   14109: inline void msdos_int_2fh_43h()
                   14110: {
                   14111:        switch(REG8(AL)) {
                   14112:        case 0x00:
1.1.1.29  root     14113:                // XMS is installed ?
1.1.1.19  root     14114: #ifdef SUPPORT_XMS
                   14115:                if(support_xms) {
                   14116:                        REG8(AL) = 0x80;
1.1.1.44  root     14117:                }
                   14118: #endif
                   14119:                break;
                   14120:        case 0x08:
                   14121: #ifdef SUPPORT_XMS
                   14122:                if(support_xms) {
                   14123:                        REG8(AL) = 0x43;
                   14124:                        REG8(BL) = 0x01; // IBM PC/AT
                   14125:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14126:                }
1.1.1.19  root     14127: #endif
                   14128:                break;
                   14129:        case 0x10:
                   14130:                SREG(ES) = XMS_TOP >> 4;
                   14131:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14132:                REG16(BX) = 0x15;
1.1       root     14133:                break;
1.1.1.44  root     14134:        case 0xe0:
                   14135:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14136:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14137:                        break;
                   14138:                }
1.1       root     14139:        default:
1.1.1.22  root     14140:                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));
                   14141:                REG16(AX) = 0x01;
                   14142:                m_CF = 1;
                   14143:                break;
                   14144:        }
                   14145: }
                   14146: 
                   14147: inline void msdos_int_2fh_46h()
                   14148: {
                   14149:        switch(REG8(AL)) {
                   14150:        case 0x80:
1.1.1.29  root     14151:                // Windows v3.0 is not installed
                   14152: //             REG8(AL) = 0x00;
1.1.1.22  root     14153:                break;
                   14154:        default:
                   14155:                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));
                   14156:                REG16(AX) = 0x01;
                   14157:                m_CF = 1;
                   14158:                break;
                   14159:        }
                   14160: }
                   14161: 
                   14162: inline void msdos_int_2fh_48h()
                   14163: {
                   14164:        switch(REG8(AL)) {
                   14165:        case 0x00:
1.1.1.29  root     14166:                // DOSKEY is not installed
                   14167: //             REG8(AL) = 0x00;
1.1.1.22  root     14168:                break;
                   14169:        case 0x10:
                   14170:                msdos_int_21h_0ah();
                   14171:                REG16(AX) = 0x00;
                   14172:                break;
                   14173:        default:
                   14174:                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     14175:                REG16(AX) = 0x01;
1.1.1.3   root     14176:                m_CF = 1;
1.1       root     14177:                break;
                   14178:        }
                   14179: }
                   14180: 
                   14181: inline void msdos_int_2fh_4ah()
                   14182: {
                   14183:        switch(REG8(AL)) {
1.1.1.29  root     14184: #ifdef SUPPORT_HMA
                   14185:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14186:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14187:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14188:                                // restore first free mcb in high memory area
                   14189:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14190:                        }
                   14191:                        int offset = 0xffff;
                   14192:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14193:                                REG16(DI) = offset + 0x10;
                   14194:                        } else {
                   14195:                                REG16(DI) = 0xffff;
                   14196:                        }
                   14197:                } else {
                   14198:                        // HMA is already used
                   14199:                        REG16(BX) = 0;
                   14200:                        REG16(DI) = 0xffff;
                   14201:                }
                   14202:                SREG(ES) = 0xffff;
                   14203:                i386_load_segment_descriptor(ES);
                   14204:                break;
                   14205:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14206:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14207:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14208:                                // restore first free mcb in high memory area
                   14209:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14210:                        }
                   14211:                        int size = REG16(BX), offset;
                   14212:                        if((size % 16) != 0) {
                   14213:                                size &= ~15;
                   14214:                                size += 16;
                   14215:                        }
                   14216:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14217:                                REG16(BX) = size;
                   14218:                                REG16(DI) = offset + 0x10;
                   14219:                                is_hma_used_by_int_2fh = true;
                   14220:                        } else {
                   14221:                                REG16(BX) = 0;
                   14222:                                REG16(DI) = 0xffff;
                   14223:                        }
                   14224:                } else {
                   14225:                        // HMA is already used
                   14226:                        REG16(BX) = 0;
                   14227:                        REG16(DI) = 0xffff;
                   14228:                }
                   14229:                SREG(ES) = 0xffff;
                   14230:                i386_load_segment_descriptor(ES);
                   14231:                break;
                   14232:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14233:                if(REG8(DL) == 0x00) {
                   14234:                        if(!is_hma_used_by_xms) {
                   14235:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14236:                                        // restore first free mcb in high memory area
                   14237:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14238:                                        is_hma_used_by_int_2fh = false;
                   14239:                                }
                   14240:                                int size = REG16(BX), offset;
                   14241:                                if((size % 16) != 0) {
                   14242:                                        size &= ~15;
                   14243:                                        size += 16;
                   14244:                                }
                   14245:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14246: //                                     REG16(BX) = size;
                   14247:                                        SREG(ES) = 0xffff;
                   14248:                                        i386_load_segment_descriptor(ES);
                   14249:                                        REG16(DI) = offset + 0x10;
                   14250:                                        is_hma_used_by_int_2fh = true;
                   14251:                                } else {
                   14252:                                        REG16(DI) = 0xffff;
                   14253:                                }
                   14254:                        } else {
                   14255:                                REG16(DI) = 0xffff;
                   14256:                        }
                   14257:                } else if(REG8(DL) == 0x01) {
                   14258:                        if(!is_hma_used_by_xms) {
                   14259:                                int size = REG16(BX);
                   14260:                                if((size % 16) != 0) {
                   14261:                                        size &= ~15;
                   14262:                                        size += 16;
                   14263:                                }
                   14264:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14265:                                        // memory block address is not changed
                   14266:                                } else {
                   14267:                                        REG16(DI) = 0xffff;
                   14268:                                }
                   14269:                        } else {
                   14270:                                REG16(DI) = 0xffff;
                   14271:                        }
                   14272:                } else if(REG8(DL) == 0x02) {
                   14273:                        if(!is_hma_used_by_xms) {
                   14274:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14275:                                        // restore first free mcb in high memory area
                   14276:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14277:                                        is_hma_used_by_int_2fh = false;
                   14278:                                } else {
                   14279:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14280:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14281:                                                is_hma_used_by_int_2fh = false;
                   14282:                                        }
                   14283:                                }
                   14284:                        }
                   14285:                } else {
                   14286:                        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));
                   14287:                        REG16(AX) = 0x01;
                   14288:                        m_CF = 1;
                   14289:                }
                   14290:                break;
                   14291:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14292:                if(!is_hma_used_by_xms) {
                   14293:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14294:                                // restore first free mcb in high memory area
                   14295:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14296:                                is_hma_used_by_int_2fh = false;
                   14297:                        }
                   14298:                        REG16(AX) = 0x0000;
                   14299:                        SREG(ES) = 0xffff;
                   14300:                        i386_load_segment_descriptor(ES);
                   14301:                        REG16(DI) = 0x10;
                   14302:                }
                   14303:                break;
                   14304: #else
1.1       root     14305:        case 0x01:
                   14306:        case 0x02:
1.1.1.29  root     14307:                // HMA is already used
1.1.1.27  root     14308:                REG16(BX) = 0x0000;
1.1.1.3   root     14309:                SREG(ES) = 0xffff;
                   14310:                i386_load_segment_descriptor(ES);
1.1       root     14311:                REG16(DI) = 0xffff;
                   14312:                break;
1.1.1.19  root     14313:        case 0x03:
                   14314:                // unable to allocate
                   14315:                REG16(DI) = 0xffff;
                   14316:                break;
                   14317:        case 0x04:
                   14318:                // function not supported, do not clear AX
                   14319:                break;
1.1.1.29  root     14320: #endif
                   14321:        case 0x10:
1.1.1.42  root     14322:                switch(REG16(BX)) {
                   14323:                case 0x0000:
                   14324:                case 0x0001:
                   14325:                case 0x0002:
                   14326:                case 0x0003:
                   14327:                case 0x0004:
                   14328:                case 0x0005:
                   14329:                case 0x0006:
                   14330:                case 0x0007:
                   14331:                case 0x0008:
                   14332:                case 0x000a:
                   14333:                case 0x1234:
                   14334:                        // SMARTDRV v4.00+ is not installed
                   14335:                        break;
                   14336:                default:
1.1.1.29  root     14337:                        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));
                   14338:                        REG16(AX) = 0x01;
                   14339:                        m_CF = 1;
1.1.1.42  root     14340:                        break;
1.1.1.29  root     14341:                }
                   14342:                break;
                   14343:        case 0x11:
1.1.1.42  root     14344:                switch(REG16(BX)) {
                   14345:                case 0x0000:
                   14346:                case 0x0001:
                   14347:                case 0x0002:
                   14348:                case 0x0003:
                   14349:                case 0x0004:
                   14350:                case 0x0005:
                   14351:                case 0x0006:
                   14352:                case 0x0007:
                   14353:                case 0x0008:
                   14354:                case 0x0009:
                   14355:                case 0x000a:
                   14356:                case 0x000b:
                   14357:                case 0xfffe:
                   14358:                case 0xffff:
1.1.1.29  root     14359:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14360:                        break;
                   14361:                default:
                   14362:                        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));
                   14363:                        REG16(AX) = 0x01;
                   14364:                        m_CF = 1;
                   14365:                        break;
                   14366:                }
                   14367:                break;
                   14368:        case 0x12:
                   14369:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14370:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14371:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14372:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14373:                } else {
                   14374:                        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));
                   14375:                        REG16(AX) = 0x01;
                   14376:                        m_CF = 1;
                   14377:                }
1.1.1.22  root     14378:                break;
1.1.1.42  root     14379:        case 0x13:
                   14380:                // DBLSPACE.BIN is not installed
                   14381:                break;
1.1.1.22  root     14382:        default:
                   14383:                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));
                   14384:                REG16(AX) = 0x01;
                   14385:                m_CF = 1;
                   14386:                break;
                   14387:        }
                   14388: }
                   14389: 
                   14390: inline void msdos_int_2fh_4bh()
                   14391: {
                   14392:        switch(REG8(AL)) {
1.1.1.24  root     14393:        case 0x01:
1.1.1.22  root     14394:        case 0x02:
1.1.1.29  root     14395:                // Task Switcher is not installed
1.1.1.24  root     14396:                break;
                   14397:        case 0x03:
                   14398:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14399:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14400:                break;
1.1.1.30  root     14401:        case 0x04:
                   14402:                REG16(BX) = 0x0000; // free switcher id successfully
                   14403:                break;
1.1.1.43  root     14404:        case 0x05:
                   14405:                REG16(BX) = 0x0000; // no instance data chain
                   14406:                SREG(ES) = 0x0000;
                   14407:                i386_load_segment_descriptor(ES);
                   14408:                break;
1.1       root     14409:        default:
1.1.1.22  root     14410:                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     14411:                REG16(AX) = 0x01;
1.1.1.3   root     14412:                m_CF = 1;
1.1       root     14413:                break;
                   14414:        }
                   14415: }
                   14416: 
1.1.1.44  root     14417: inline void msdos_int_2fh_4dh()
                   14418: {
                   14419:        switch(REG8(AL)) {
                   14420:        case 0x00:
                   14421:                // KKCFUNC is not installed ???
                   14422:                break;
                   14423:        default:
                   14424: //             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));
                   14425:                REG16(AX) = 0x01; // invalid function
                   14426:                m_CF = 1;
                   14427:                break;
                   14428:        }
                   14429: }
                   14430: 
1.1       root     14431: inline void msdos_int_2fh_4fh()
                   14432: {
                   14433:        switch(REG8(AL)) {
                   14434:        case 0x00:
1.1.1.29  root     14435:                // BILING is installed
1.1.1.27  root     14436:                REG16(AX) = 0x0000;
                   14437:                REG8(DL) = 0x01;        // major version
                   14438:                REG8(DH) = 0x00;        // minor version
1.1       root     14439:                break;
                   14440:        case 0x01:
1.1.1.27  root     14441:                REG16(AX) = 0x0000;
1.1       root     14442:                REG16(BX) = active_code_page;
                   14443:                break;
                   14444:        default:
1.1.1.22  root     14445:                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));
                   14446:                REG16(AX) = 0x01;
                   14447:                m_CF = 1;
                   14448:                break;
                   14449:        }
                   14450: }
                   14451: 
                   14452: inline void msdos_int_2fh_55h()
                   14453: {
                   14454:        switch(REG8(AL)) {
                   14455:        case 0x00:
                   14456:        case 0x01:
                   14457: //             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));
                   14458:                break;
                   14459:        default:
                   14460:                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     14461:                REG16(AX) = 0x01;
1.1.1.3   root     14462:                m_CF = 1;
1.1       root     14463:                break;
                   14464:        }
                   14465: }
                   14466: 
1.1.1.44  root     14467: inline void msdos_int_2fh_56h()
                   14468: {
                   14469:        switch(REG8(AL)) {
                   14470:        case 0x00:
                   14471:                // INTERLNK is not installed
                   14472:                break;
                   14473:        case 0x01:
                   14474:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14475: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14476: //                     REG8(AL) = 0x00;
                   14477: //             }
                   14478:                break;
                   14479:        default:
                   14480:                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));
                   14481:                REG16(AX) = 0x01;
                   14482:                m_CF = 1;
                   14483:                break;
                   14484:        }
                   14485: }
                   14486: 
1.1.1.24  root     14487: inline void msdos_int_2fh_adh()
                   14488: {
                   14489:        switch(REG8(AL)) {
                   14490:        case 0x00:
1.1.1.29  root     14491:                // DISPLAY.SYS is installed
1.1.1.24  root     14492:                REG8(AL) = 0xff;
                   14493:                REG16(BX) = 0x100; // ???
                   14494:                break;
                   14495:        case 0x01:
                   14496:                active_code_page = REG16(BX);
                   14497:                msdos_nls_tables_update();
                   14498:                REG16(AX) = 0x01;
                   14499:                break;
                   14500:        case 0x02:
                   14501:                REG16(BX) = active_code_page;
                   14502:                break;
                   14503:        case 0x03:
                   14504:                // FIXME
                   14505:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14506:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14507:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14508:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14509:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14510:                break;
                   14511:        case 0x80:
1.1.1.49  root     14512:                // KEYB.COM is not installed
                   14513:                break;
1.1.1.24  root     14514:        default:
                   14515:                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));
                   14516:                REG16(AX) = 0x01;
                   14517:                m_CF = 1;
                   14518:                break;
                   14519:        }
                   14520: }
                   14521: 
1.1       root     14522: inline void msdos_int_2fh_aeh()
                   14523: {
                   14524:        switch(REG8(AL)) {
                   14525:        case 0x00:
1.1.1.28  root     14526:                // FIXME: we need to check the given command line
                   14527:                REG8(AL) = 0x00; // the command should be executed as usual
                   14528: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14529:                break;
                   14530:        case 0x01:
                   14531:                {
                   14532:                        char command[MAX_PATH];
                   14533:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14534:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14535:                        
                   14536:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14537:                        param->env_seg = 0;
                   14538:                        param->cmd_line.w.l = 44;
                   14539:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14540:                        param->fcb1.w.l = 24;
                   14541:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14542:                        param->fcb2.w.l = 24;
                   14543:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14544:                        
                   14545:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14546:                        
                   14547:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14548:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14549:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14550:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14551:                        
1.1.1.28  root     14552:                        try {
                   14553:                                msdos_process_exec(command, param, 0);
                   14554:                        } catch(...) {
                   14555:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14556:                        }
                   14557:                }
                   14558:                break;
                   14559:        default:
1.1.1.22  root     14560:                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     14561:                REG16(AX) = 0x01;
1.1.1.3   root     14562:                m_CF = 1;
1.1       root     14563:                break;
                   14564:        }
                   14565: }
                   14566: 
1.1.1.34  root     14567: inline void msdos_int_2fh_b7h()
                   14568: {
                   14569:        switch(REG8(AL)) {
                   14570:        case 0x00:
                   14571:                // APPEND is not installed
                   14572: //             REG8(AL) = 0x00;
                   14573:                break;
1.1.1.44  root     14574:        case 0x06:
                   14575:                REG16(BX) = 0x0000;
                   14576:                break;
1.1.1.34  root     14577:        case 0x07:
1.1.1.43  root     14578:        case 0x11:
1.1.1.34  root     14579:                // COMMAND.COM calls this service without checking APPEND is installed
                   14580:                break;
                   14581:        default:
                   14582:                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));
                   14583:                REG16(AX) = 0x01;
                   14584:                m_CF = 1;
                   14585:                break;
                   14586:        }
                   14587: }
                   14588: 
1.1.1.24  root     14589: inline void msdos_int_33h_0000h()
                   14590: {
                   14591:        REG16(AX) = 0xffff; // hardware/driver installed
                   14592:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14593: }
                   14594: 
                   14595: inline void msdos_int_33h_0001h()
                   14596: {
1.1.1.34  root     14597:        if(mouse.hidden > 0) {
                   14598:                mouse.hidden--;
                   14599:        }
                   14600:        if(mouse.hidden == 0) {
1.1.1.24  root     14601:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14602:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14603:                }
                   14604:                pic[1].imr &= ~0x10;    // enable irq12
                   14605:        }
                   14606: }
                   14607: 
                   14608: inline void msdos_int_33h_0002h()
                   14609: {
1.1.1.34  root     14610:        mouse.hidden++;
                   14611: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   14612:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     14613: }
                   14614: 
                   14615: inline void msdos_int_33h_0003h()
                   14616: {
1.1.1.34  root     14617: //     if(mouse.hidden > 0) {
                   14618:                update_console_input();
                   14619: //     }
1.1.1.24  root     14620:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14621:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14622:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14623: }
                   14624: 
                   14625: inline void msdos_int_33h_0004h()
                   14626: {
                   14627:        mouse.position.x = REG16(CX);
                   14628:        mouse.position.x = REG16(DX);
1.1.1.24  root     14629: }
                   14630: 
                   14631: inline void msdos_int_33h_0005h()
                   14632: {
1.1.1.34  root     14633: //     if(mouse.hidden > 0) {
                   14634:                update_console_input();
                   14635: //     }
1.1.1.24  root     14636:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14637:                int idx = REG16(BX);
1.1.1.34  root     14638:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14639:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14640:                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     14641:                mouse.buttons[idx].pressed_times = 0;
                   14642:        } else {
                   14643:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14644:        }
                   14645:        REG16(AX) = mouse.get_buttons();
                   14646: }
                   14647: 
                   14648: inline void msdos_int_33h_0006h()
                   14649: {
1.1.1.34  root     14650: //     if(mouse.hidden > 0) {
                   14651:                update_console_input();
                   14652: //     }
1.1.1.24  root     14653:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14654:                int idx = REG16(BX);
1.1.1.34  root     14655:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14656:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14657:                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     14658:                mouse.buttons[idx].released_times = 0;
                   14659:        } else {
                   14660:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14661:        }
                   14662:        REG16(AX) = mouse.get_buttons();
                   14663: }
                   14664: 
                   14665: inline void msdos_int_33h_0007h()
                   14666: {
                   14667:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14668:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14669: }
                   14670: 
                   14671: inline void msdos_int_33h_0008h()
                   14672: {
                   14673:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14674:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14675: }
                   14676: 
                   14677: inline void msdos_int_33h_0009h()
                   14678: {
                   14679:        mouse.hot_spot[0] = REG16(BX);
                   14680:        mouse.hot_spot[1] = REG16(CX);
                   14681: }
                   14682: 
1.1.1.49  root     14683: inline void msdos_int_33h_000ah()
                   14684: {
                   14685:        mouse.screen_mask = REG16(CX);
                   14686:        mouse.cursor_mask = REG16(DX);
                   14687: }
                   14688: 
1.1.1.24  root     14689: inline void msdos_int_33h_000bh()
                   14690: {
1.1.1.34  root     14691: //     if(mouse.hidden > 0) {
                   14692:                update_console_input();
                   14693: //     }
1.1.1.24  root     14694:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14695:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14696:        mouse.prev_position.x = mouse.position.x;
                   14697:        mouse.prev_position.y = mouse.position.y;
                   14698:        REG16(CX) = dx;
                   14699:        REG16(DX) = dy;
                   14700: }
                   14701: 
                   14702: inline void msdos_int_33h_000ch()
                   14703: {
                   14704:        mouse.call_mask = REG16(CX);
                   14705:        mouse.call_addr.w.l = REG16(DX);
                   14706:        mouse.call_addr.w.h = SREG(ES);
                   14707: }
                   14708: 
                   14709: inline void msdos_int_33h_000fh()
                   14710: {
                   14711:        mouse.mickey.x = REG16(CX);
                   14712:        mouse.mickey.y = REG16(DX);
                   14713: }
                   14714: 
                   14715: inline void msdos_int_33h_0011h()
                   14716: {
                   14717:        REG16(AX) = 0xffff;
                   14718:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14719: }
                   14720: 
                   14721: inline void msdos_int_33h_0014h()
                   14722: {
                   14723:        UINT16 old_mask = mouse.call_mask;
                   14724:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14725:        UINT16 old_seg = mouse.call_addr.w.h;
                   14726:        
                   14727:        mouse.call_mask = REG16(CX);
                   14728:        mouse.call_addr.w.l = REG16(DX);
                   14729:        mouse.call_addr.w.h = SREG(ES);
                   14730:        
                   14731:        REG16(CX) = old_mask;
                   14732:        REG16(DX) = old_ofs;
                   14733:        SREG(ES) = old_seg;
                   14734:        i386_load_segment_descriptor(ES);
                   14735: }
                   14736: 
                   14737: inline void msdos_int_33h_0015h()
                   14738: {
                   14739:        REG16(BX) = sizeof(mouse);
                   14740: }
                   14741: 
                   14742: inline void msdos_int_33h_0016h()
                   14743: {
                   14744:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14745: }
                   14746: 
                   14747: inline void msdos_int_33h_0017h()
                   14748: {
                   14749:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14750: }
                   14751: 
1.1.1.43  root     14752: inline void msdos_int_33h_0018h()
                   14753: {
                   14754:        for(int i = 0; i < 8; i++) {
                   14755:                if(REG16(CX) & (1 << i)) {
                   14756:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14757:                                // event handler already exists
                   14758:                                REG16(AX) = 0xffff;
                   14759:                                break;
                   14760:                        }
                   14761:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14762:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14763:                }
                   14764:        }
                   14765: }
                   14766: 
                   14767: inline void msdos_int_33h_0019h()
                   14768: {
                   14769:        UINT16 call_mask = REG16(CX);
                   14770:        
                   14771:        REG16(CX) = 0;
                   14772:        
                   14773:        for(int i = 0; i < 8; i++) {
                   14774:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14775:                        for(int j = 0; j < 8; j++) {
                   14776:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14777:                                        REG16(CX) |= (1 << j);
                   14778:                                }
                   14779:                        }
                   14780:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14781:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14782:                        break;
                   14783:                }
                   14784:        }
                   14785: }
                   14786: 
1.1.1.24  root     14787: inline void msdos_int_33h_001ah()
                   14788: {
                   14789:        mouse.sensitivity[0] = REG16(BX);
                   14790:        mouse.sensitivity[1] = REG16(CX);
                   14791:        mouse.sensitivity[2] = REG16(DX);
                   14792: }
                   14793: 
                   14794: inline void msdos_int_33h_001bh()
                   14795: {
                   14796:        REG16(BX) = mouse.sensitivity[0];
                   14797:        REG16(CX) = mouse.sensitivity[1];
                   14798:        REG16(DX) = mouse.sensitivity[2];
                   14799: }
                   14800: 
                   14801: inline void msdos_int_33h_001dh()
                   14802: {
                   14803:        mouse.display_page = REG16(BX);
                   14804: }
                   14805: 
                   14806: inline void msdos_int_33h_001eh()
                   14807: {
                   14808:        REG16(BX) = mouse.display_page;
                   14809: }
                   14810: 
1.1.1.34  root     14811: inline void msdos_int_33h_001fh()
                   14812: {
                   14813:        // from DOSBox
                   14814:        REG16(BX) = 0x0000;
                   14815:        SREG(ES) = 0x0000;
                   14816:        i386_load_segment_descriptor(ES);
                   14817:        mouse.enabled = false;
                   14818:        mouse.old_hidden = mouse.hidden;
                   14819:        mouse.hidden = 1;
                   14820: }
                   14821: 
                   14822: inline void msdos_int_33h_0020h()
                   14823: {
                   14824:        // from DOSBox
                   14825:        mouse.enabled = true;
                   14826:        mouse.hidden = mouse.old_hidden;
                   14827: }
                   14828: 
1.1.1.24  root     14829: inline void msdos_int_33h_0021h()
                   14830: {
                   14831:        REG16(AX) = 0xffff;
                   14832:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14833: }
                   14834: 
                   14835: inline void msdos_int_33h_0022h()
                   14836: {
                   14837:        mouse.language = REG16(BX);
                   14838: }
                   14839: 
                   14840: inline void msdos_int_33h_0023h()
                   14841: {
                   14842:        REG16(BX) = mouse.language;
                   14843: }
                   14844: 
                   14845: inline void msdos_int_33h_0024h()
                   14846: {
                   14847:        REG16(BX) = 0x0805; // V8.05
                   14848:        REG16(CX) = 0x0400; // PS/2
                   14849: }
                   14850: 
1.1.1.49  root     14851: inline void msdos_int_33h_0025h()
                   14852: {
                   14853:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   14854: }
                   14855: 
1.1.1.24  root     14856: inline void msdos_int_33h_0026h()
                   14857: {
                   14858:        REG16(BX) = 0x0000;
                   14859:        REG16(CX) = mouse.max_position.x;
                   14860:        REG16(DX) = mouse.max_position.y;
                   14861: }
                   14862: 
1.1.1.49  root     14863: inline void msdos_int_33h_0027h()
                   14864: {
                   14865: //     if(mouse.hidden > 0) {
                   14866:                update_console_input();
                   14867: //     }
                   14868:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14869:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14870:        mouse.prev_position.x = mouse.position.x;
                   14871:        mouse.prev_position.y = mouse.position.y;
                   14872:        REG16(AX) = mouse.screen_mask;
                   14873:        REG16(BX) = mouse.cursor_mask;
                   14874:        REG16(CX) = dx;
                   14875:        REG16(DX) = dy;
                   14876: }
                   14877: 
                   14878: inline void msdos_int_33h_0028h()
                   14879: {
                   14880:        if(REG16(CX) != 0) {
                   14881:                UINT8 tmp = REG8(AL);
                   14882:                REG8(AL) = REG8(CL);
                   14883:                pcbios_int_10h_00h();
                   14884:                REG8(AL) = tmp;
                   14885:        }
                   14886:        REG8(CL) = 0x00; // successful
                   14887: }
                   14888: 
                   14889: inline void msdos_int_33h_0029h()
                   14890: {
                   14891:        switch(REG16(CX)) {
                   14892:        case 0x0000:
                   14893:                REG16(CX) = 0x0003;
                   14894:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   14895:                break;
                   14896:        case 0x0003:
                   14897:                REG16(CX) = 0x0070;
                   14898:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   14899:                break;
                   14900:        case 0x0070:
                   14901:                REG16(CX) = 0x0071;
                   14902:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   14903:                break;
                   14904:        case 0x0071:
                   14905:                REG16(CX) = 0x0073;
                   14906:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   14907:                break;
                   14908:        default:
                   14909:                REG16(CX) = 0x0000;
                   14910:                break;
                   14911:        }
                   14912:        if(REG16(CX) != 0) {
                   14913:                SREG(DS) = (WORK_TOP >> 4);
                   14914:        } else {
                   14915:                SREG(DS) = 0x0000;
                   14916:        }
                   14917:        i386_load_segment_descriptor(DS);
                   14918:        REG16(DX) = 0x0000;
                   14919: }
                   14920: 
1.1.1.24  root     14921: inline void msdos_int_33h_002ah()
                   14922: {
1.1.1.34  root     14923:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     14924:        REG16(BX) = mouse.hot_spot[0];
                   14925:        REG16(CX) = mouse.hot_spot[1];
                   14926:        REG16(DX) = 4; // PS/2
                   14927: }
                   14928: 
                   14929: inline void msdos_int_33h_0031h()
                   14930: {
                   14931:        REG16(AX) = mouse.min_position.x;
                   14932:        REG16(BX) = mouse.min_position.y;
                   14933:        REG16(CX) = mouse.max_position.x;
                   14934:        REG16(DX) = mouse.max_position.y;
                   14935: }
                   14936: 
                   14937: inline void msdos_int_33h_0032h()
                   14938: {
                   14939:        REG16(AX) = 0;
1.1.1.49  root     14940:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     14941:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     14942:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     14943: //     REG16(AX) |= 0x1000; // 0028h
                   14944: //     REG16(AX) |= 0x0800; // 0029h
                   14945:        REG16(AX) |= 0x0400; // 002ah
                   14946: //     REG16(AX) |= 0x0200; // 002bh
                   14947: //     REG16(AX) |= 0x0100; // 002ch
                   14948: //     REG16(AX) |= 0x0080; // 002dh
                   14949: //     REG16(AX) |= 0x0040; // 002eh
                   14950:        REG16(AX) |= 0x0020; // 002fh
                   14951: //     REG16(AX) |= 0x0010; // 0030h
                   14952:        REG16(AX) |= 0x0008; // 0031h
                   14953:        REG16(AX) |= 0x0004; // 0032h
                   14954: //     REG16(AX) |= 0x0002; // 0033h
                   14955: //     REG16(AX) |= 0x0001; // 0034h
                   14956: }
                   14957: 
1.1.1.49  root     14958: inline void msdos_int_33h_004dh()
                   14959: {
                   14960:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   14961: }
                   14962: 
                   14963: inline void msdos_int_33h_006dh()
                   14964: {
                   14965:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   14966:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   14967: }
                   14968: 
1.1.1.19  root     14969: inline void msdos_int_67h_40h()
                   14970: {
                   14971:        if(!support_ems) {
                   14972:                REG8(AH) = 0x84;
                   14973:        } else {
                   14974:                REG8(AH) = 0x00;
                   14975:        }
                   14976: }
                   14977: 
                   14978: inline void msdos_int_67h_41h()
                   14979: {
                   14980:        if(!support_ems) {
                   14981:                REG8(AH) = 0x84;
                   14982:        } else {
                   14983:                REG8(AH) = 0x00;
                   14984:                REG16(BX) = EMS_TOP >> 4;
                   14985:        }
                   14986: }
                   14987: 
                   14988: inline void msdos_int_67h_42h()
                   14989: {
                   14990:        if(!support_ems) {
                   14991:                REG8(AH) = 0x84;
                   14992:        } else {
                   14993:                REG8(AH) = 0x00;
                   14994:                REG16(BX) = free_ems_pages;
                   14995:                REG16(DX) = MAX_EMS_PAGES;
                   14996:        }
                   14997: }
                   14998: 
                   14999: inline void msdos_int_67h_43h()
                   15000: {
                   15001:        if(!support_ems) {
                   15002:                REG8(AH) = 0x84;
                   15003:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15004:                REG8(AH) = 0x87;
                   15005:        } else if(REG16(BX) > free_ems_pages) {
                   15006:                REG8(AH) = 0x88;
                   15007:        } else if(REG16(BX) == 0) {
                   15008:                REG8(AH) = 0x89;
                   15009:        } else {
1.1.1.31  root     15010:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15011:                        if(!ems_handles[i].allocated) {
                   15012:                                ems_allocate_pages(i, REG16(BX));
                   15013:                                REG8(AH) = 0x00;
                   15014:                                REG16(DX) = i;
                   15015:                                return;
                   15016:                        }
                   15017:                }
                   15018:                REG8(AH) = 0x85;
                   15019:        }
                   15020: }
                   15021: 
                   15022: inline void msdos_int_67h_44h()
                   15023: {
                   15024:        if(!support_ems) {
                   15025:                REG8(AH) = 0x84;
1.1.1.31  root     15026:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15027:                REG8(AH) = 0x83;
                   15028:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15029:                REG8(AH) = 0x8a;
                   15030: //     } else if(!(REG8(AL) < 4)) {
                   15031: //             REG8(AH) = 0x8b;
                   15032:        } else if(REG16(BX) == 0xffff) {
                   15033:                ems_unmap_page(REG8(AL) & 3);
                   15034:                REG8(AH) = 0x00;
                   15035:        } else {
                   15036:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15037:                REG8(AH) = 0x00;
                   15038:        }
                   15039: }
                   15040: 
                   15041: inline void msdos_int_67h_45h()
                   15042: {
                   15043:        if(!support_ems) {
                   15044:                REG8(AH) = 0x84;
1.1.1.31  root     15045:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15046:                REG8(AH) = 0x83;
                   15047:        } else {
                   15048:                ems_release_pages(REG16(DX));
                   15049:                REG8(AH) = 0x00;
                   15050:        }
                   15051: }
                   15052: 
                   15053: inline void msdos_int_67h_46h()
                   15054: {
                   15055:        if(!support_ems) {
                   15056:                REG8(AH) = 0x84;
                   15057:        } else {
1.1.1.29  root     15058: //             REG16(AX) = 0x0032; // EMS 3.2
                   15059:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15060:        }
                   15061: }
                   15062: 
                   15063: inline void msdos_int_67h_47h()
                   15064: {
                   15065:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15066:        process_t *process = msdos_process_info_get(current_psp);
                   15067:        
                   15068:        if(!support_ems) {
                   15069:                REG8(AH) = 0x84;
1.1.1.31  root     15070: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15071: //             REG8(AH) = 0x83;
                   15072:        } else if(process->ems_pages_stored) {
                   15073:                REG8(AH) = 0x8d;
                   15074:        } else {
                   15075:                for(int i = 0; i < 4; i++) {
                   15076:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15077:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15078:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15079:                }
                   15080:                process->ems_pages_stored = true;
                   15081:                REG8(AH) = 0x00;
                   15082:        }
                   15083: }
                   15084: 
                   15085: inline void msdos_int_67h_48h()
                   15086: {
                   15087:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15088:        process_t *process = msdos_process_info_get(current_psp);
                   15089:        
                   15090:        if(!support_ems) {
                   15091:                REG8(AH) = 0x84;
1.1.1.31  root     15092: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15093: //             REG8(AH) = 0x83;
                   15094:        } else if(!process->ems_pages_stored) {
                   15095:                REG8(AH) = 0x8e;
                   15096:        } else {
                   15097:                for(int i = 0; i < 4; i++) {
                   15098:                        if(process->ems_pages[i].mapped) {
                   15099:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15100:                        } else {
                   15101:                                ems_unmap_page(i);
                   15102:                        }
                   15103:                }
                   15104:                process->ems_pages_stored = false;
                   15105:                REG8(AH) = 0x00;
                   15106:        }
                   15107: }
                   15108: 
                   15109: inline void msdos_int_67h_4bh()
                   15110: {
                   15111:        if(!support_ems) {
                   15112:                REG8(AH) = 0x84;
                   15113:        } else {
                   15114:                REG8(AH) = 0x00;
                   15115:                REG16(BX) = 0;
1.1.1.31  root     15116:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15117:                        if(ems_handles[i].allocated) {
                   15118:                                REG16(BX)++;
                   15119:                        }
                   15120:                }
                   15121:        }
                   15122: }
                   15123: 
                   15124: inline void msdos_int_67h_4ch()
                   15125: {
                   15126:        if(!support_ems) {
                   15127:                REG8(AH) = 0x84;
1.1.1.31  root     15128:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15129:                REG8(AH) = 0x83;
                   15130:        } else {
                   15131:                REG8(AH) = 0x00;
                   15132:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15133:        }
                   15134: }
                   15135: 
                   15136: inline void msdos_int_67h_4dh()
                   15137: {
                   15138:        if(!support_ems) {
                   15139:                REG8(AH) = 0x84;
                   15140:        } else {
                   15141:                REG8(AH) = 0x00;
                   15142:                REG16(BX) = 0;
1.1.1.31  root     15143:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15144:                        if(ems_handles[i].allocated) {
                   15145:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15146:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15147:                                REG16(BX)++;
                   15148:                        }
                   15149:                }
                   15150:        }
                   15151: }
                   15152: 
1.1.1.20  root     15153: inline void msdos_int_67h_4eh()
                   15154: {
                   15155:        if(!support_ems) {
                   15156:                REG8(AH) = 0x84;
                   15157:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15158:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15159:                        // save page map
                   15160:                        for(int i = 0; i < 4; i++) {
                   15161:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15162:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15163:                        }
                   15164:                }
                   15165:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15166:                        // restore page map
                   15167:                        for(int i = 0; i < 4; i++) {
                   15168:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15169:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15170:                                
1.1.1.31  root     15171:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15172:                                        ems_map_page(i, handle, page);
                   15173:                                } else {
                   15174:                                        ems_unmap_page(i);
                   15175:                                }
                   15176:                        }
                   15177:                }
                   15178:                REG8(AH) = 0x00;
                   15179:        } else if(REG8(AL) == 0x03) {
                   15180:                REG8(AH) = 0x00;
1.1.1.21  root     15181:                REG8(AL) = 4 * 4;
                   15182:        } else {
1.1.1.22  root     15183:                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     15184:                REG8(AH) = 0x8f;
                   15185:        }
                   15186: }
                   15187: 
                   15188: inline void msdos_int_67h_4fh()
                   15189: {
                   15190:        if(!support_ems) {
                   15191:                REG8(AH) = 0x84;
                   15192:        } else if(REG8(AL) == 0x00) {
                   15193:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15194:                
                   15195:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15196:                for(int i = 0; i < count; i++) {
                   15197:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15198:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15199:                        
                   15200: //                     if(!(physical < 4)) {
                   15201: //                             REG8(AH) = 0x8b;
                   15202: //                             return;
                   15203: //                     }
                   15204:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15205:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15206:                        *(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     15207:                }
                   15208:                REG8(AH) = 0x00;
                   15209:        } else if(REG8(AL) == 0x01) {
                   15210:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15211:                
                   15212:                for(int i = 0; i < count; i++) {
                   15213:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15214:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15215:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15216:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15217:                        
                   15218: //                     if(!(physical < 4)) {
                   15219: //                             REG8(AH) = 0x8b;
                   15220: //                             return;
                   15221: //                     } else
1.1.1.41  root     15222:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15223:                                ems_map_page(physical & 3, handle, logical);
                   15224:                        } else {
1.1.1.41  root     15225:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15226:                        }
                   15227:                }
                   15228:                REG8(AH) = 0x00;
                   15229:        } else if(REG8(AL) == 0x02) {
                   15230:                REG8(AH) = 0x00;
                   15231:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15232:        } else {
1.1.1.22  root     15233:                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     15234:                REG8(AH) = 0x8f;
                   15235:        }
                   15236: }
                   15237: 
                   15238: inline void msdos_int_67h_50h()
                   15239: {
                   15240:        if(!support_ems) {
                   15241:                REG8(AH) = 0x84;
1.1.1.31  root     15242:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15243:                REG8(AH) = 0x83;
                   15244:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15245:                for(int i = 0; i < REG16(CX); i++) {
                   15246:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15247:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15248:                        
                   15249:                        if(REG8(AL) == 0x01) {
                   15250:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15251:                        }
                   15252: //                     if(!(physical < 4)) {
                   15253: //                             REG8(AH) = 0x8b;
                   15254: //                             return;
                   15255: //                     } else
                   15256:                        if(logical == 0xffff) {
                   15257:                                ems_unmap_page(physical & 3);
                   15258:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15259:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15260:                        } else {
                   15261:                                REG8(AH) = 0x8a;
                   15262:                                return;
                   15263:                        }
                   15264:                }
                   15265:                REG8(AH) = 0x00;
                   15266:        } else {
1.1.1.22  root     15267:                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     15268:                REG8(AH) = 0x8f;
                   15269:        }
                   15270: }
                   15271: 
1.1.1.19  root     15272: inline void msdos_int_67h_51h()
                   15273: {
                   15274:        if(!support_ems) {
                   15275:                REG8(AH) = 0x84;
1.1.1.31  root     15276:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15277:                REG8(AH) = 0x83;
                   15278:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15279:                REG8(AH) = 0x87;
                   15280:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15281:                REG8(AH) = 0x88;
                   15282:        } else {
                   15283:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15284:                REG8(AH) = 0x00;
                   15285:        }
                   15286: }
                   15287: 
1.1.1.20  root     15288: inline void msdos_int_67h_52h()
                   15289: {
                   15290:        if(!support_ems) {
                   15291:                REG8(AH) = 0x84;
1.1.1.31  root     15292: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15293: //             REG8(AH) = 0x83;
1.1.1.20  root     15294:        } else if(REG8(AL) == 0x00) {
                   15295:                REG8(AL) = 0x00; // handle is volatile
                   15296:                REG8(AH) = 0x00;
                   15297:        } else if(REG8(AL) == 0x01) {
                   15298:                if(REG8(BL) == 0x00) {
                   15299:                        REG8(AH) = 0x00;
                   15300:                } else {
                   15301:                        REG8(AH) = 0x90; // undefined attribute type
                   15302:                }
                   15303:        } else if(REG8(AL) == 0x02) {
                   15304:                REG8(AL) = 0x00; // only volatile handles supported
                   15305:                REG8(AH) = 0x00;
                   15306:        } else {
1.1.1.22  root     15307:                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     15308:                REG8(AH) = 0x8f;
                   15309:        }
                   15310: }
                   15311: 
1.1.1.19  root     15312: inline void msdos_int_67h_53h()
                   15313: {
                   15314:        if(!support_ems) {
                   15315:                REG8(AH) = 0x84;
1.1.1.31  root     15316:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15317:                REG8(AH) = 0x83;
                   15318:        } else if(REG8(AL) == 0x00) {
                   15319:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15320:                REG8(AH) = 0x00;
                   15321:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15322:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15323:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15324:                                REG8(AH) = 0xa1;
                   15325:                                return;
                   15326:                        }
                   15327:                }
                   15328:                REG8(AH) = 0x00;
                   15329:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15330:        } else {
1.1.1.22  root     15331:                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     15332:                REG8(AH) = 0x8f;
1.1.1.19  root     15333:        }
                   15334: }
                   15335: 
                   15336: inline void msdos_int_67h_54h()
                   15337: {
                   15338:        if(!support_ems) {
                   15339:                REG8(AH) = 0x84;
                   15340:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15341:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15342:                        if(ems_handles[i].allocated) {
                   15343:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15344:                        } else {
                   15345:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15346:                        }
                   15347:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15348:                }
                   15349:                REG8(AH) = 0x00;
                   15350:                REG8(AL) = MAX_EMS_HANDLES;
                   15351:        } else if(REG8(AL) == 0x01) {
                   15352:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15353:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15354:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15355:                                REG8(AH) = 0x00;
                   15356:                                REG16(DX) = i;
                   15357:                                break;
                   15358:                        }
                   15359:                }
                   15360:        } else if(REG8(AL) == 0x02) {
                   15361:                REG8(AH) = 0x00;
                   15362:                REG16(BX) = MAX_EMS_HANDLES;
                   15363:        } else {
1.1.1.22  root     15364:                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     15365:                REG8(AH) = 0x8f;
                   15366:        }
                   15367: }
                   15368: 
1.1.1.49  root     15369: inline void msdos_int_67h_55h()
                   15370: {
                   15371:        if(!support_ems) {
                   15372:                REG8(AH) = 0x84;
                   15373:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15374:                REG8(AH) = 0x83;
                   15375:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15376:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15377:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15378:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15379:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15380:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15381:                
                   15382:                for(int i = 0; i < (int)entries; i++) {
                   15383:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15384:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15385:                        
                   15386:                        if(REG8(AL) == 0x01) {
                   15387:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15388:                        }
                   15389: //                     if(!(physical < 4)) {
                   15390: //                             REG8(AH) = 0x8b;
                   15391: //                             return;
                   15392: //                     } else
                   15393:                        if(logical == 0xffff) {
                   15394:                                ems_unmap_page(physical & 3);
                   15395:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15396:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15397:                        } else {
                   15398:                                REG8(AH) = 0x8a;
                   15399:                                return;
                   15400:                        }
                   15401:                }
                   15402:                i386_jmp_far(jump_seg, jump_ofs);
                   15403:                REG8(AH) = 0x00;
                   15404:        } else {
                   15405:                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));
                   15406:                REG8(AH) = 0x8f;
                   15407:        }
                   15408: }
                   15409: 
                   15410: inline void msdos_int_67h_56h()
                   15411: {
                   15412:        if(!support_ems) {
                   15413:                REG8(AH) = 0x84;
                   15414:        } else if(REG8(AL) == 0x02) {
                   15415:                REG16(BX) = (2 + 2) * 4;
                   15416:                REG8(AH) = 0x00;
                   15417:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15418:                REG8(AH) = 0x83;
                   15419:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15420:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15421:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15422:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15423:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15424:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15425: #if 0
                   15426:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15427:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15428:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15429: #endif
                   15430:                UINT16 handles[4], pages[4];
                   15431:                
                   15432:                // alter page map and call routine is at fffc:001f
                   15433:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15434:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15435:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15436:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15437:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15438:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15439:                } else {
                   15440:                        // invalid call addr :-(
                   15441:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15442:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15443:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15444:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15445:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15446:                }
                   15447:                // do call far (push cs/ip) in old mapping
                   15448:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15449:                
                   15450:                // get old mapping data
                   15451: #if 0
                   15452:                for(int i = 0; i < (int)old_entries; i++) {
                   15453:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15454:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15455:                        
                   15456:                        if(REG8(AL) == 0x01) {
                   15457:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15458:                        }
                   15459: //                     if(!(physical < 4)) {
                   15460: //                             REG8(AH) = 0x8b;
                   15461: //                             return;
                   15462: //                     } else
                   15463:                        if(logical == 0xffff) {
                   15464:                                ems_unmap_page(physical & 3);
                   15465:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15466:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15467:                        } else {
                   15468:                                REG8(AH) = 0x8a;
                   15469:                                return;
                   15470:                        }
                   15471:                }
                   15472: #endif
                   15473:                for(int i = 0; i < 4; i++) {
                   15474:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15475:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15476:                }
                   15477:                
                   15478:                // set new mapping
                   15479:                for(int i = 0; i < (int)new_entries; i++) {
                   15480:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   15481:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   15482:                        
                   15483:                        if(REG8(AL) == 0x01) {
                   15484:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15485:                        }
                   15486: //                     if(!(physical < 4)) {
                   15487: //                             REG8(AH) = 0x8b;
                   15488: //                             return;
                   15489: //                     } else
                   15490:                        if(logical == 0xffff) {
                   15491:                                ems_unmap_page(physical & 3);
                   15492:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15493:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15494:                        } else {
                   15495:                                REG8(AH) = 0x8a;
                   15496:                                return;
                   15497:                        }
                   15498:                }
                   15499:                
                   15500:                // push old mapping data in new mapping
                   15501:                for(int i = 0; i < 4; i++) {
                   15502:                        i386_push16(handles[i]);
                   15503:                        i386_push16(pages  [i]);
                   15504:                }
                   15505:                REG8(AH) = 0x00;
                   15506:        } else {
                   15507:                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));
                   15508:                REG8(AH) = 0x8f;
                   15509:        }
                   15510: }
                   15511: 
1.1.1.20  root     15512: inline void msdos_int_67h_57h_tmp()
                   15513: {
                   15514:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15515:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15516:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   15517:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   15518:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   15519:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   15520:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15521:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   15522:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   15523:        
1.1.1.32  root     15524:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     15525:        UINT32 src_addr, dest_addr;
                   15526:        UINT32 src_addr_max, dest_addr_max;
                   15527:        
                   15528:        if(src_type == 0) {
                   15529:                src_buffer = mem;
                   15530:                src_addr = (src_seg << 4) + src_ofs;
                   15531:                src_addr_max = MAX_MEM;
                   15532:        } else {
1.1.1.31  root     15533:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     15534:                        REG8(AH) = 0x83;
                   15535:                        return;
                   15536:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   15537:                        REG8(AH) = 0x8a;
                   15538:                        return;
                   15539:                }
1.1.1.32  root     15540:                if(ems_handles[src_handle].buffer != NULL) {
                   15541:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   15542:                }
1.1.1.20  root     15543:                src_addr = src_ofs;
1.1.1.32  root     15544:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     15545:        }
                   15546:        if(dest_type == 0) {
                   15547:                dest_buffer = mem;
                   15548:                dest_addr = (dest_seg << 4) + dest_ofs;
                   15549:                dest_addr_max = MAX_MEM;
                   15550:        } else {
1.1.1.31  root     15551:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     15552:                        REG8(AH) = 0x83;
                   15553:                        return;
                   15554:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   15555:                        REG8(AH) = 0x8a;
                   15556:                        return;
                   15557:                }
1.1.1.32  root     15558:                if(ems_handles[dest_handle].buffer != NULL) {
                   15559:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   15560:                }
1.1.1.20  root     15561:                dest_addr = dest_ofs;
1.1.1.32  root     15562:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     15563:        }
1.1.1.32  root     15564:        if(src_buffer != NULL && dest_buffer != NULL) {
                   15565:                for(int i = 0; i < copy_length; i++) {
                   15566:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15567:                                if(REG8(AL) == 0x00) {
                   15568:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15569:                                } else if(REG8(AL) == 0x01) {
                   15570:                                        UINT8 tmp = dest_buffer[dest_addr];
                   15571:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   15572:                                        src_buffer[src_addr++] = tmp;
                   15573:                                }
                   15574:                        } else {
                   15575:                                REG8(AH) = 0x93;
                   15576:                                return;
1.1.1.20  root     15577:                        }
                   15578:                }
1.1.1.32  root     15579:                REG8(AH) = 0x00;
                   15580:        } else {
                   15581:                REG8(AH) = 0x80;
1.1.1.20  root     15582:        }
                   15583: }
                   15584: 
                   15585: inline void msdos_int_67h_57h()
                   15586: {
                   15587:        if(!support_ems) {
                   15588:                REG8(AH) = 0x84;
                   15589:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15590:                struct {
                   15591:                        UINT16 handle;
                   15592:                        UINT16 page;
                   15593:                        bool mapped;
                   15594:                } tmp_pages[4];
                   15595:                
                   15596:                // unmap pages to copy memory data to ems buffer
                   15597:                for(int i = 0; i < 4; i++) {
                   15598:                        tmp_pages[i].handle = ems_pages[i].handle;
                   15599:                        tmp_pages[i].page   = ems_pages[i].page;
                   15600:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   15601:                        ems_unmap_page(i);
                   15602:                }
                   15603:                
                   15604:                // run move/exchange operation
                   15605:                msdos_int_67h_57h_tmp();
                   15606:                
                   15607:                // restore unmapped pages
                   15608:                for(int i = 0; i < 4; i++) {
                   15609:                        if(tmp_pages[i].mapped) {
                   15610:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   15611:                        }
                   15612:                }
                   15613:        } else {
1.1.1.22  root     15614:                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     15615:                REG8(AH) = 0x8f;
                   15616:        }
                   15617: }
                   15618: 
                   15619: inline void msdos_int_67h_58h()
                   15620: {
                   15621:        if(!support_ems) {
                   15622:                REG8(AH) = 0x84;
                   15623:        } else if(REG8(AL) == 0x00) {
                   15624:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     15625:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   15626:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     15627:                }
                   15628:                REG8(AH) = 0x00;
                   15629:                REG16(CX) = 4;
                   15630:        } else if(REG8(AL) == 0x01) {
                   15631:                REG8(AH) = 0x00;
                   15632:                REG16(CX) = 4;
                   15633:        } else {
1.1.1.22  root     15634:                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     15635:                REG8(AH) = 0x8f;
                   15636:        }
                   15637: }
                   15638: 
1.1.1.42  root     15639: inline void msdos_int_67h_59h()
                   15640: {
                   15641:        if(!support_ems) {
                   15642:                REG8(AH) = 0x84;
                   15643:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     15644:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   15645:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   15646:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   15647:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   15648:                REG8(AH) = 0x00;
                   15649: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     15650:        } else if(REG8(AL) == 0x01) {
                   15651:                REG8(AH) = 0x00;
                   15652:                REG16(BX) = free_ems_pages;
                   15653:                REG16(DX) = MAX_EMS_PAGES;
                   15654:        } else {
                   15655:                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));
                   15656:                REG8(AH) = 0x8f;
                   15657:        }
                   15658: }
                   15659: 
1.1.1.20  root     15660: inline void msdos_int_67h_5ah()
                   15661: {
                   15662:        if(!support_ems) {
1.1.1.19  root     15663:                REG8(AH) = 0x84;
1.1.1.20  root     15664:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15665:                REG8(AH) = 0x87;
                   15666:        } else if(REG16(BX) > free_ems_pages) {
                   15667:                REG8(AH) = 0x88;
                   15668: //     } else if(REG16(BX) == 0) {
                   15669: //             REG8(AH) = 0x89;
                   15670:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     15671:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     15672:                        if(!ems_handles[i].allocated) {
                   15673:                                ems_allocate_pages(i, REG16(BX));
                   15674:                                REG8(AH) = 0x00;
                   15675:                                REG16(DX) = i;
                   15676:                                return;
                   15677:                        }
                   15678:                }
                   15679:                REG8(AH) = 0x85;
                   15680:        } else {
1.1.1.22  root     15681:                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     15682:                REG8(AH) = 0x8f;
1.1.1.19  root     15683:        }
                   15684: }
                   15685: 
1.1.1.49  root     15686: inline void msdos_int_67h_5bh()
                   15687: {
                   15688:        static UINT8  stored_bl = 0x00;
                   15689:        static UINT16 stored_es = 0x0000;
                   15690:        static UINT16 stored_di = 0x0000;
                   15691:        
                   15692:        if(!support_ems) {
                   15693:                REG8(AH) = 0x84;
                   15694:        } else if(REG8(AL) == 0x00) {
                   15695:                if(stored_bl == 0x00) {
                   15696:                        if(!(stored_es == 0 && stored_di == 0)) {
                   15697:                                for(int i = 0; i < 4; i++) {
                   15698:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15699:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15700:                                }
                   15701:                        }
                   15702:                        SREG(ES) = stored_es;
                   15703:                        i386_load_segment_descriptor(ES);
                   15704:                        REG16(DI) = stored_di;
                   15705:                } else {
                   15706:                        REG8(BL) = stored_bl;
                   15707:                }
                   15708:                REG8(AH) = 0x00;
                   15709:        } else if(REG8(AL) == 0x01) {
                   15710:                if(REG8(BL) == 0x00) {
                   15711:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   15712:                                for(int i = 0; i < 4; i++) {
                   15713:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   15714:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   15715:                                        
                   15716:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   15717:                                                ems_map_page(i, handle, page);
                   15718:                                        } else {
                   15719:                                                ems_unmap_page(i);
                   15720:                                        }
                   15721:                                }
                   15722:                        }
                   15723:                }
                   15724:                stored_bl = REG8(BL);
                   15725:                stored_es = SREG(ES);
                   15726:                stored_di = REG16(DI);
                   15727:                REG8(AH) = 0x00;
                   15728:        } else if(REG8(AL) == 0x02) {
                   15729:                REG16(DX) = 4 * 4;
                   15730:                REG8(AH) = 0x00;
                   15731:        } else if(REG8(AL) == 0x03) {
                   15732:                REG8(BL) = 0x00; // not supported
                   15733:                REG8(AH) = 0x00;
                   15734:        } else if(REG8(AL) == 0x04) {
                   15735:                REG8(AH) = 0x00;
                   15736:        } else if(REG8(AL) == 0x05) {
                   15737:                REG8(BL) = 0x00; // not supported
                   15738:                REG8(AH) = 0x00;
                   15739:        } else {
                   15740:                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));
                   15741:                REG8(AH) = 0x8f;
                   15742:        }
                   15743: }
                   15744: 
1.1.1.43  root     15745: inline void msdos_int_67h_5dh()
                   15746: {
                   15747:        if(!support_ems) {
                   15748:                REG8(AH) = 0x84;
                   15749:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15750:                REG8(AH) = 0xa4; // operating system denied access
                   15751:        } else {
                   15752:                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));
                   15753:                REG8(AH) = 0x8f;
                   15754:        }
                   15755: }
                   15756: 
1.1.1.49  root     15757: inline void msdos_int_67h_70h()
                   15758: {
                   15759:        if(!support_ems) {
                   15760:                REG8(AH) = 0x84;
                   15761:        } else if(REG8(AL) == 0x00) {
                   15762:                REG8(AL) = 0x00;
                   15763:                REG8(AH) = 0x00;
                   15764:        } else if(REG8(AL) == 0x01) {
                   15765:                REG8(AL) = 0x00;
                   15766: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   15767:                REG8(AH) = 0x00;
                   15768:        } else {
                   15769:                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));
                   15770:                REG8(AH) = 0x8f;
                   15771:        }
                   15772: }
                   15773: 
1.1.1.30  root     15774: inline void msdos_int_67h_deh()
                   15775: {
                   15776:        REG8(AH) = 0x84;
                   15777: }
                   15778: 
1.1.1.19  root     15779: #ifdef SUPPORT_XMS
                   15780: 
1.1.1.32  root     15781: void msdos_xms_init()
1.1.1.26  root     15782: {
1.1.1.30  root     15783:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15784:        emb_handle_top->address = EMB_TOP;
                   15785:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     15786:        xms_a20_local_enb_count = 0;
                   15787: }
                   15788: 
1.1.1.32  root     15789: void msdos_xms_finish()
                   15790: {
                   15791:        msdos_xms_release();
                   15792: }
                   15793: 
                   15794: void msdos_xms_release()
1.1.1.30  root     15795: {
                   15796:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   15797:                emb_handle_t *next_handle = emb_handle->next;
                   15798:                free(emb_handle);
                   15799:                emb_handle = next_handle;
                   15800:        }
                   15801: }
                   15802: 
                   15803: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   15804: {
                   15805:        if(handle != 0) {
                   15806:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15807:                        if(emb_handle->handle == handle) {
                   15808:                                return(emb_handle);
                   15809:                        }
                   15810:                }
                   15811:        }
                   15812:        return(NULL);
                   15813: }
                   15814: 
                   15815: int msdos_xms_get_unused_emb_handle_id()
                   15816: {
                   15817:        for(int handle = 1;; handle++) {
                   15818:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   15819:                        return(handle);
                   15820:                }
                   15821:        }
                   15822:        return(0);
                   15823: }
                   15824: 
                   15825: int msdos_xms_get_unused_emb_handle_count()
                   15826: {
                   15827:        int count = 64; //255;
                   15828:        
                   15829:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15830:                if(emb_handle->handle != 0) {
                   15831:                        if(--count == 1) {
                   15832:                                break;
                   15833:                        }
                   15834:                }
                   15835:        }
                   15836:        return(count);
                   15837: }
                   15838: 
                   15839: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   15840: {
                   15841:        if(emb_handle->size_kb > size_kb) {
                   15842:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15843:                
                   15844:                new_handle->address = emb_handle->address + size_kb * 1024;
                   15845:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   15846:                emb_handle->size_kb = size_kb;
                   15847:                
                   15848:                new_handle->prev = emb_handle;
                   15849:                new_handle->next = emb_handle->next;
                   15850:                if(emb_handle->next != NULL) {
                   15851:                        emb_handle->next->prev = new_handle;
                   15852:                }
                   15853:                emb_handle->next = new_handle;
                   15854:        }
                   15855: }
                   15856: 
                   15857: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   15858: {
                   15859:        emb_handle_t *next_handle = emb_handle->next;
                   15860:        
                   15861:        if(next_handle != NULL) {
                   15862:                emb_handle->size_kb += next_handle->size_kb;
                   15863:                
                   15864:                if(next_handle->next != NULL) {
                   15865:                        next_handle->next->prev = emb_handle;
                   15866:                }
                   15867:                emb_handle->next = next_handle->next;
                   15868:                free(next_handle);
                   15869:        }
                   15870: }
                   15871: 
                   15872: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   15873: {
                   15874:        emb_handle_t *target_handle = NULL;
                   15875:        
                   15876:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15877:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   15878:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   15879:                                target_handle = emb_handle;
                   15880:                        }
                   15881:                }
                   15882:        }
                   15883:        if(target_handle != NULL) {
                   15884:                if(target_handle->size_kb > size_kb) {
                   15885:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   15886:                }
                   15887: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15888:                return(target_handle);
                   15889:        }
                   15890:        return(NULL);
                   15891: }
                   15892: 
                   15893: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   15894: {
                   15895:        emb_handle_t *prev_handle = emb_handle->prev;
                   15896:        emb_handle_t *next_handle = emb_handle->next;
                   15897:        
                   15898:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   15899:                msdos_xms_combine_emb_handles(prev_handle);
                   15900:                emb_handle = prev_handle;
                   15901:        }
                   15902:        if(next_handle != NULL && next_handle->handle == 0) {
                   15903:                msdos_xms_combine_emb_handles(emb_handle);
                   15904:        }
                   15905:        emb_handle->handle = 0;
                   15906: }
                   15907: 
1.1.1.19  root     15908: inline void msdos_call_xms_00h()
                   15909: {
1.1.1.29  root     15910: #if defined(HAS_I386)
                   15911:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     15912: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     15913:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   15914: #else
                   15915:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   15916:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   15917: #endif
                   15918: //     REG16(DX) = 0x0000; // HMA does not exist
                   15919:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     15920: }
                   15921: 
                   15922: inline void msdos_call_xms_01h()
                   15923: {
1.1.1.29  root     15924:        if(REG8(AL) == 0x40) {
                   15925:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   15926:                // DX=KB free extended memory returned by last call of function 08h
                   15927:                REG16(AX) = 0x0000;
                   15928:                REG8(BL) = 0x91;
                   15929:                REG16(DX) = xms_dx_after_call_08h;
                   15930:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15931:                REG16(AX) = 0x0000;
                   15932:                REG8(BL) = 0x81; // Vdisk was detected
                   15933: #ifdef SUPPORT_HMA
                   15934:        } else if(is_hma_used_by_int_2fh) {
                   15935:                REG16(AX) = 0x0000;
                   15936:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15937:        } else if(is_hma_used_by_xms) {
                   15938:                REG16(AX) = 0x0000;
                   15939:                REG8(BL) = 0x91; // HMA is already in use
                   15940:        } else {
                   15941:                REG16(AX) = 0x0001;
                   15942:                is_hma_used_by_xms = true;
                   15943: #else
                   15944:        } else {
                   15945:                REG16(AX) = 0x0000;
                   15946:                REG8(BL) = 0x91; // HMA is already in use
                   15947: #endif
                   15948:        }
1.1.1.19  root     15949: }
                   15950: 
                   15951: inline void msdos_call_xms_02h()
                   15952: {
1.1.1.29  root     15953:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15954:                REG16(AX) = 0x0000;
                   15955:                REG8(BL) = 0x81; // Vdisk was detected
                   15956: #ifdef SUPPORT_HMA
                   15957:        } else if(is_hma_used_by_int_2fh) {
                   15958:                REG16(AX) = 0x0000;
                   15959:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15960:        } else if(!is_hma_used_by_xms) {
                   15961:                REG16(AX) = 0x0000;
                   15962:                REG8(BL) = 0x93; // HMA is not allocated
                   15963:        } else {
                   15964:                REG16(AX) = 0x0001;
                   15965:                is_hma_used_by_xms = false;
                   15966:                // restore first free mcb in high memory area
                   15967:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15968: #else
                   15969:        } else {
                   15970:                REG16(AX) = 0x0000;
                   15971:                REG8(BL) = 0x91; // HMA is already in use
                   15972: #endif
                   15973:        }
1.1.1.19  root     15974: }
                   15975: 
                   15976: inline void msdos_call_xms_03h()
                   15977: {
                   15978:        i386_set_a20_line(1);
                   15979:        REG16(AX) = 0x0001;
                   15980:        REG8(BL) = 0x00;
                   15981: }
                   15982: 
                   15983: inline void msdos_call_xms_04h()
                   15984: {
1.1.1.21  root     15985:        i386_set_a20_line(0);
                   15986:        REG16(AX) = 0x0001;
                   15987:        REG8(BL) = 0x00;
1.1.1.19  root     15988: }
                   15989: 
                   15990: inline void msdos_call_xms_05h()
                   15991: {
                   15992:        i386_set_a20_line(1);
                   15993:        REG16(AX) = 0x0001;
                   15994:        REG8(BL) = 0x00;
1.1.1.21  root     15995:        xms_a20_local_enb_count++;
1.1.1.19  root     15996: }
                   15997: 
                   15998: void msdos_call_xms_06h()
                   15999: {
1.1.1.21  root     16000:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16001:                if(--xms_a20_local_enb_count == 0) {
                   16002:                        i386_set_a20_line(0);
                   16003:                        REG16(AX) = 0x0001;
                   16004:                        REG8(BL) = 0x00;
                   16005:                } else {
                   16006:                        REG16(AX) = 0x0000;
                   16007:                        REG8(BL) = 0x94;
                   16008:                }
1.1.1.21  root     16009:        } else {
1.1.1.45  root     16010:                i386_set_a20_line(0);
1.1.1.21  root     16011:                REG16(AX) = 0x0001;
                   16012:                REG8(BL) = 0x00;
1.1.1.19  root     16013:        }
                   16014: }
                   16015: 
                   16016: inline void msdos_call_xms_07h()
                   16017: {
                   16018:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16019:        REG8(BL) = 0x00;
                   16020: }
                   16021: 
                   16022: inline void msdos_call_xms_08h()
                   16023: {
1.1.1.45  root     16024:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16025:        
1.1.1.30  root     16026:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16027:                if(emb_handle->handle == 0) {
1.1.1.45  root     16028:                        if(eax < emb_handle->size_kb) {
                   16029:                                eax = emb_handle->size_kb;
1.1.1.19  root     16030:                        }
1.1.1.45  root     16031:                        edx += emb_handle->size_kb;
1.1.1.19  root     16032:                }
                   16033:        }
1.1.1.45  root     16034:        if(eax > 65535) {
                   16035:                eax = 65535;
                   16036:        }
                   16037:        if(edx > 65535) {
                   16038:                edx = 65535;
                   16039:        }
                   16040:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16041:                REG8(BL) = 0xa0;
                   16042:        } else {
                   16043:                REG8(BL) = 0x00;
                   16044:        }
1.1.1.45  root     16045: #if defined(HAS_I386)
                   16046:        REG32(EAX) = eax;
                   16047:        REG32(EDX) = edx;
                   16048: #else
                   16049:        REG16(AX) = (UINT16)eax;
                   16050:        REG16(DX) = (UINT16)edx;
                   16051: #endif
1.1.1.29  root     16052:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16053: }
                   16054: 
1.1.1.30  root     16055: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16056: {
1.1.1.30  root     16057:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16058:        
                   16059:        if(emb_handle != NULL) {
                   16060:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16061:                
                   16062:                REG16(AX) = 0x0001;
                   16063:                REG16(DX) = emb_handle->handle;
                   16064:                REG8(BL) = 0x00;
                   16065:        } else {
                   16066:                REG16(AX) = REG16(DX) = 0x0000;
                   16067:                REG8(BL) = 0xa0;
1.1.1.19  root     16068:        }
1.1.1.30  root     16069: }
                   16070: 
                   16071: inline void msdos_call_xms_09h()
                   16072: {
                   16073:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16074: }
                   16075: 
                   16076: inline void msdos_call_xms_0ah()
                   16077: {
1.1.1.30  root     16078:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16079:        
                   16080:        if(emb_handle == NULL) {
1.1.1.19  root     16081:                REG16(AX) = 0x0000;
                   16082:                REG8(BL) = 0xa2;
1.1.1.45  root     16083: //     } else if(emb_handle->lock > 0) {
                   16084: //             REG16(AX) = 0x0000;
                   16085: //             REG8(BL) = 0xab;
1.1.1.19  root     16086:        } else {
1.1.1.30  root     16087:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16088:                
                   16089:                REG16(AX) = 0x0001;
                   16090:                REG8(BL) = 0x00;
                   16091:        }
                   16092: }
                   16093: 
                   16094: inline void msdos_call_xms_0bh()
                   16095: {
                   16096:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16097:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16098:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16099:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16100:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16101:        
                   16102:        UINT8 *src_buffer, *dest_buffer;
                   16103:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16104:        emb_handle_t *emb_handle;
1.1.1.19  root     16105:        
                   16106:        if(src_handle == 0) {
                   16107:                src_buffer = mem;
                   16108:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16109:                src_addr_max = MAX_MEM;
                   16110:        } else {
1.1.1.30  root     16111:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16112:                        REG16(AX) = 0x0000;
                   16113:                        REG8(BL) = 0xa3;
                   16114:                        return;
                   16115:                }
1.1.1.30  root     16116:                src_buffer = mem + emb_handle->address;
                   16117:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16118:        }
                   16119:        if(dest_handle == 0) {
                   16120:                dest_buffer = mem;
                   16121:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16122:                dest_addr_max = MAX_MEM;
                   16123:        } else {
1.1.1.30  root     16124:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16125:                        REG16(AX) = 0x0000;
                   16126:                        REG8(BL) = 0xa5;
                   16127:                        return;
                   16128:                }
1.1.1.30  root     16129:                dest_buffer = mem + emb_handle->address;
                   16130:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16131:        }
                   16132:        for(int i = 0; i < copy_length; i++) {
                   16133:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16134:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16135:                } else {
                   16136:                        break;
                   16137:                }
                   16138:        }
                   16139:        REG16(AX) = 0x0001;
                   16140:        REG8(BL) = 0x00;
                   16141: }
                   16142: 
                   16143: inline void msdos_call_xms_0ch()
                   16144: {
1.1.1.30  root     16145:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16146:        
                   16147:        if(emb_handle == NULL) {
1.1.1.19  root     16148:                REG16(AX) = 0x0000;
                   16149:                REG8(BL) = 0xa2;
                   16150:        } else {
1.1.1.45  root     16151:                if(emb_handle->lock < 255) {
                   16152:                        emb_handle->lock++;
                   16153:                }
1.1.1.19  root     16154:                REG16(AX) = 0x0001;
1.1.1.30  root     16155:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16156:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16157:        }
                   16158: }
                   16159: 
                   16160: inline void msdos_call_xms_0dh()
                   16161: {
1.1.1.30  root     16162:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16163:        
                   16164:        if(emb_handle == NULL) {
1.1.1.19  root     16165:                REG16(AX) = 0x0000;
                   16166:                REG8(BL) = 0xa2;
1.1.1.30  root     16167:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16168:                REG16(AX) = 0x0000;
                   16169:                REG8(BL) = 0xaa;
                   16170:        } else {
1.1.1.30  root     16171:                emb_handle->lock--;
1.1.1.19  root     16172:                REG16(AX) = 0x0001;
                   16173:                REG8(BL) = 0x00;
                   16174:        }
                   16175: }
                   16176: 
                   16177: inline void msdos_call_xms_0eh()
                   16178: {
1.1.1.30  root     16179:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16180:        
                   16181:        if(emb_handle == NULL) {
1.1.1.19  root     16182:                REG16(AX) = 0x0000;
                   16183:                REG8(BL) = 0xa2;
                   16184:        } else {
                   16185:                REG16(AX) = 0x0001;
1.1.1.30  root     16186:                REG8(BH) = emb_handle->lock;
                   16187:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16188:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16189:        }
                   16190: }
                   16191: 
1.1.1.30  root     16192: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16193: {
1.1.1.30  root     16194:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16195:        
                   16196:        if(emb_handle == NULL) {
1.1.1.19  root     16197:                REG16(AX) = 0x0000;
                   16198:                REG8(BL) = 0xa2;
1.1.1.30  root     16199:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16200:                REG16(AX) = 0x0000;
                   16201:                REG8(BL) = 0xab;
                   16202:        } else {
1.1.1.30  root     16203:                if(emb_handle->size_kb < size_kb) {
                   16204:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16205:                                msdos_xms_combine_emb_handles(emb_handle);
                   16206:                                if(emb_handle->size_kb > size_kb) {
                   16207:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16208:                                }
                   16209:                        } else {
                   16210:                                int old_handle = emb_handle->handle;
                   16211:                                int old_size_kb = emb_handle->size_kb;
                   16212:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16213:                                
                   16214:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16215:                                msdos_xms_free_emb_handle(emb_handle);
                   16216:                                
                   16217:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16218:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16219:                                }
                   16220:                                emb_handle->handle = old_handle;
                   16221:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16222:                                free(buffer);
                   16223:                        }
                   16224:                } else if(emb_handle->size_kb > size_kb) {
                   16225:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16226:                }
                   16227:                if(emb_handle->size_kb != size_kb) {
                   16228:                        REG16(AX) = 0x0000;
                   16229:                        REG8(BL) = 0xa0;
                   16230:                } else {
                   16231:                        REG16(AX) = 0x0001;
                   16232:                        REG8(BL) = 0x00;
                   16233:                }
1.1.1.19  root     16234:        }
                   16235: }
                   16236: 
1.1.1.30  root     16237: inline void msdos_call_xms_0fh()
                   16238: {
                   16239:        msdos_call_xms_0fh(REG16(BX));
                   16240: }
                   16241: 
1.1.1.19  root     16242: inline void msdos_call_xms_10h()
                   16243: {
                   16244:        int seg;
                   16245:        
                   16246:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16247:                REG16(AX) = 0x0001;
                   16248:                REG16(BX) = seg;
                   16249:        } else {
                   16250:                REG16(AX) = 0x0000;
                   16251:                REG8(BL) = 0xb0;
                   16252:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16253:        }
                   16254: }
                   16255: 
                   16256: inline void msdos_call_xms_11h()
                   16257: {
                   16258:        int mcb_seg = REG16(DX) - 1;
                   16259:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16260:        
                   16261:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16262:                msdos_mem_free(REG16(DX));
                   16263:                REG16(AX) = 0x0001;
                   16264:                REG8(BL) = 0x00;
                   16265:        } else {
                   16266:                REG16(AX) = 0x0000;
                   16267:                REG8(BL) = 0xb2;
                   16268:        }
                   16269: }
                   16270: 
                   16271: inline void msdos_call_xms_12h()
                   16272: {
                   16273:        int mcb_seg = REG16(DX) - 1;
                   16274:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16275:        int max_paragraphs;
                   16276:        
                   16277:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16278:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16279:                        REG16(AX) = 0x0001;
                   16280:                        REG8(BL) = 0x00;
                   16281:                } else {
                   16282:                        REG16(AX) = 0x0000;
                   16283:                        REG8(BL) = 0xb0;
                   16284:                        REG16(DX) = max_paragraphs;
                   16285:                }
                   16286:        } else {
                   16287:                REG16(AX) = 0x0000;
                   16288:                REG8(BL) = 0xb2;
                   16289:        }
                   16290: }
                   16291: 
1.1.1.29  root     16292: #if defined(HAS_I386)
                   16293: 
                   16294: inline void msdos_call_xms_88h()
                   16295: {
                   16296:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16297:        
1.1.1.30  root     16298:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16299:                if(emb_handle->handle == 0) {
                   16300:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16301:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16302:                        }
1.1.1.30  root     16303:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16304:                }
                   16305:        }
                   16306:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16307:                REG8(BL) = 0xa0;
                   16308:        } else {
                   16309:                REG8(BL) = 0x00;
                   16310:        }
                   16311:        REG32(ECX) = EMB_END - 1;
                   16312: }
                   16313: 
                   16314: inline void msdos_call_xms_89h()
                   16315: {
1.1.1.30  root     16316:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16317: }
                   16318: 
                   16319: inline void msdos_call_xms_8eh()
                   16320: {
1.1.1.30  root     16321:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16322:        
                   16323:        if(emb_handle == NULL) {
1.1.1.29  root     16324:                REG16(AX) = 0x0000;
                   16325:                REG8(BL) = 0xa2;
                   16326:        } else {
                   16327:                REG16(AX) = 0x0001;
1.1.1.30  root     16328:                REG8(BH) = emb_handle->lock;
                   16329:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16330:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16331:        }
                   16332: }
                   16333: 
                   16334: inline void msdos_call_xms_8fh()
                   16335: {
1.1.1.30  root     16336:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16337: }
                   16338: 
                   16339: #endif
1.1.1.19  root     16340: #endif
                   16341: 
1.1.1.26  root     16342: UINT16 msdos_get_equipment()
                   16343: {
                   16344:        static UINT16 equip = 0;
                   16345:        
                   16346:        if(equip == 0) {
                   16347: #ifdef SUPPORT_FPU
                   16348:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   16349: #endif
                   16350:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   16351:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   16352: //             equip |= (1 << 8);      // 0 if DMA installed
                   16353:                equip |= (2 << 9);      // number of serial ports
                   16354:                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     16355:                
                   16356:                // check only A: and B: if it is floppy drive
                   16357:                int n = 0;
                   16358:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     16359:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   16360:                                n++;
1.1.1.28  root     16361:                        }
                   16362:                }
                   16363:                if(n != 0) {
                   16364:                        equip |= (1 << 0);      // floppy disk(s) installed
                   16365:                        n--;
                   16366:                        equip |= (n << 6);      // number of floppies installed less 1
                   16367:                }
                   16368: //             if(joyGetNumDevs() != 0) {
                   16369: //                     equip |= (1 << 12);     // game port installed
                   16370: //             }
1.1.1.26  root     16371:        }
                   16372:        return(equip);
                   16373: }
                   16374: 
1.1       root     16375: void msdos_syscall(unsigned num)
                   16376: {
1.1.1.22  root     16377: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     16378:        if(num == 0x08 || num == 0x1c) {
                   16379:                // don't log the timer interrupts
1.1.1.45  root     16380: //             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     16381:        } else if(num == 0x30) {
                   16382:                // dummy interrupt for call 0005h (call near)
                   16383:                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     16384:        } else if(num == 0x68) {
1.1.1.22  root     16385:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     16386:                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     16387:        } else if(num == 0x69) {
                   16388:                // dummy interrupt for XMS (call far)
1.1.1.33  root     16389:                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     16390:        } else if(num >= 0x6a && num <= 0x6f) {
1.1.1.45  root     16391:                // dummy interrupt
1.1.1.22  root     16392:        } else {
1.1.1.33  root     16393:                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     16394:        }
                   16395: #endif
1.1.1.36  root     16396:        // update cursor position
                   16397:        if(cursor_moved) {
                   16398:                pcbios_update_cursor_position();
                   16399:                cursor_moved = false;
                   16400:        }
1.1.1.50  root     16401: #ifdef USE_SERVICE_THREAD
                   16402:        // this is called from dummy loop to wait until a serive that waits input is done
                   16403:        if(!in_service)
                   16404: #endif
1.1.1.33  root     16405:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     16406:        
1.1       root     16407:        switch(num) {
                   16408:        case 0x00:
1.1.1.28  root     16409:                try {
                   16410:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16411:                        error("division by zero\n");
                   16412:                } catch(...) {
                   16413:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   16414:                }
1.1       root     16415:                break;
                   16416:        case 0x04:
1.1.1.28  root     16417:                try {
                   16418:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16419:                        error("overflow\n");
                   16420:                } catch(...) {
                   16421:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   16422:                }
1.1       root     16423:                break;
                   16424:        case 0x06:
                   16425:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     16426:                if(!ignore_illegal_insn) {
1.1.1.28  root     16427:                        try {
                   16428:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16429:                                error("illegal instruction\n");
                   16430:                        } catch(...) {
                   16431:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   16432:                        }
1.1.1.14  root     16433:                } else {
                   16434: #if defined(HAS_I386)
1.1.1.39  root     16435:                        m_eip = m_int6h_skip_eip;
                   16436: #elif defined(HAS_I286)
                   16437:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     16438: #else
1.1.1.39  root     16439:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     16440: #endif
                   16441:                }
1.1       root     16442:                break;
1.1.1.33  root     16443:        case 0x09:
                   16444:                // ctrl-break is pressed
                   16445:                if(raise_int_1bh) {
                   16446: #if defined(HAS_I386)
                   16447:                        m_ext = 0; // not an external interrupt
                   16448:                        i386_trap(0x1b, 1, 0);
                   16449:                        m_ext = 1;
                   16450: #else
                   16451:                        PREFIX86(_interrupt)(0x1b);
                   16452: #endif
                   16453:                        raise_int_1bh = false;
                   16454:                }
1.1.1.8   root     16455:        case 0x08:
1.1.1.14  root     16456: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     16457:        case 0x0b:
                   16458:        case 0x0c:
                   16459:        case 0x0d:
                   16460:        case 0x0e:
                   16461:        case 0x0f:
                   16462:                // EOI
                   16463:                pic[0].isr &= ~(1 << (num - 0x08));
                   16464:                pic_update();
                   16465:                break;
1.1       root     16466:        case 0x10:
                   16467:                // PC BIOS - Video
1.1.1.14  root     16468:                if(!restore_console_on_exit) {
1.1.1.15  root     16469:                        change_console_size(scr_width, scr_height);
1.1       root     16470:                }
1.1.1.3   root     16471:                m_CF = 0;
1.1       root     16472:                switch(REG8(AH)) {
1.1.1.16  root     16473:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     16474:                case 0x01: pcbios_int_10h_01h(); break;
                   16475:                case 0x02: pcbios_int_10h_02h(); break;
                   16476:                case 0x03: pcbios_int_10h_03h(); break;
                   16477:                case 0x05: pcbios_int_10h_05h(); break;
                   16478:                case 0x06: pcbios_int_10h_06h(); break;
                   16479:                case 0x07: pcbios_int_10h_07h(); break;
                   16480:                case 0x08: pcbios_int_10h_08h(); break;
                   16481:                case 0x09: pcbios_int_10h_09h(); break;
                   16482:                case 0x0a: pcbios_int_10h_0ah(); break;
                   16483:                case 0x0b: break;
1.1.1.40  root     16484:                case 0x0c: pcbios_int_10h_0ch(); break;
                   16485:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     16486:                case 0x0e: pcbios_int_10h_0eh(); break;
                   16487:                case 0x0f: pcbios_int_10h_0fh(); break;
                   16488:                case 0x10: break;
1.1.1.14  root     16489:                case 0x11: pcbios_int_10h_11h(); break;
                   16490:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     16491:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     16492:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     16493:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     16494:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   16495:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     16496:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     16497:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   16498:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     16499:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     16500:                case 0x6f: break;
1.1.1.22  root     16501:                case 0x80: m_CF = 1; break; // unknown
                   16502:                case 0x81: m_CF = 1; break; // unknown
1.1       root     16503:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     16504:                case 0x83: pcbios_int_10h_83h(); break;
                   16505:                case 0x8b: break;
                   16506:                case 0x8c: m_CF = 1; break; // unknown
                   16507:                case 0x8d: m_CF = 1; break; // unknown
                   16508:                case 0x8e: m_CF = 1; break; // unknown
                   16509:                case 0x90: pcbios_int_10h_90h(); break;
                   16510:                case 0x91: pcbios_int_10h_91h(); break;
                   16511:                case 0x92: break;
                   16512:                case 0x93: break;
                   16513:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     16514:                case 0xfa: break; // ega register interface library is not installed
1.1       root     16515:                case 0xfe: pcbios_int_10h_feh(); break;
                   16516:                case 0xff: pcbios_int_10h_ffh(); break;
                   16517:                default:
1.1.1.22  root     16518:                        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));
                   16519:                        m_CF = 1;
1.1       root     16520:                        break;
                   16521:                }
                   16522:                break;
                   16523:        case 0x11:
                   16524:                // PC BIOS - Get Equipment List
1.1.1.26  root     16525:                REG16(AX) = msdos_get_equipment();
1.1       root     16526:                break;
                   16527:        case 0x12:
                   16528:                // PC BIOS - Get Memory Size
1.1.1.33  root     16529:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     16530:                break;
                   16531:        case 0x13:
1.1.1.42  root     16532:                // PC BIOS - Disk I/O
                   16533:                {
                   16534:                        static UINT8 last = 0x00;
                   16535:                        switch(REG8(AH)) {
                   16536:                        case 0x00: pcbios_int_13h_00h(); break;
                   16537:                        case 0x01: // get last status
                   16538:                                REG8(AH) = last;
                   16539:                                break;
                   16540:                        case 0x02: pcbios_int_13h_02h(); break;
                   16541:                        case 0x03: pcbios_int_13h_03h(); break;
                   16542:                        case 0x04: pcbios_int_13h_04h(); break;
                   16543:                        case 0x08: pcbios_int_13h_08h(); break;
                   16544:                        case 0x0a: pcbios_int_13h_02h(); break;
                   16545:                        case 0x0b: pcbios_int_13h_03h(); break;
                   16546:                        case 0x0d: pcbios_int_13h_00h(); break;
                   16547:                        case 0x10: pcbios_int_13h_10h(); break;
                   16548:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     16549:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     16550:                        case 0x05: // format
                   16551:                        case 0x06:
                   16552:                        case 0x07:
                   16553:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   16554:                                m_CF = 1;
                   16555:                                break;
                   16556:                        case 0x09:
                   16557:                        case 0x0c: // seek
                   16558:                        case 0x11: // recalib
                   16559:                        case 0x14:
                   16560:                        case 0x17:
                   16561:                                REG8(AH) = 0x00; // successful completion
                   16562:                                break;
1.1.1.43  root     16563:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   16564:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   16565:                                REG8(AH) = 0x01; // invalid function
                   16566:                                m_CF = 1;
                   16567:                                break;
1.1.1.42  root     16568:                        default:
                   16569:                                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));
                   16570:                                REG8(AH) = 0x01; // invalid function
                   16571:                                m_CF = 1;
                   16572:                                break;
                   16573:                        }
                   16574:                        last = REG8(AH);
                   16575:                }
1.1       root     16576:                break;
                   16577:        case 0x14:
                   16578:                // PC BIOS - Serial I/O
1.1.1.25  root     16579:                switch(REG8(AH)) {
                   16580:                case 0x00: pcbios_int_14h_00h(); break;
                   16581:                case 0x01: pcbios_int_14h_01h(); break;
                   16582:                case 0x02: pcbios_int_14h_02h(); break;
                   16583:                case 0x03: pcbios_int_14h_03h(); break;
                   16584:                case 0x04: pcbios_int_14h_04h(); break;
                   16585:                case 0x05: pcbios_int_14h_05h(); break;
                   16586:                default:
                   16587:                        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));
                   16588:                        break;
                   16589:                }
1.1       root     16590:                break;
                   16591:        case 0x15:
                   16592:                // PC BIOS
1.1.1.3   root     16593:                m_CF = 0;
1.1       root     16594:                switch(REG8(AH)) {
1.1.1.14  root     16595:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     16596:                case 0x23: pcbios_int_15h_23h(); break;
                   16597:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     16598:                case 0x41: break;
1.1       root     16599:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     16600:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     16601:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     16602:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     16603:                case 0x86: pcbios_int_15h_86h(); break;
                   16604:                case 0x87: pcbios_int_15h_87h(); break;
                   16605:                case 0x88: pcbios_int_15h_88h(); break;
                   16606:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     16607:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     16608:                case 0xc0: // PS/2 ???
1.1.1.54! root     16609: #ifndef EXT_BIOS_TOP
1.1.1.22  root     16610:                case 0xc1:
1.1.1.54! root     16611: #endif
1.1.1.30  root     16612:                case 0xc3: // PS50+ ???
                   16613:                case 0xc4:
1.1.1.22  root     16614:                        REG8(AH) = 0x86;
                   16615:                        m_CF = 1;
                   16616:                        break;
1.1.1.54! root     16617: #ifdef EXT_BIOS_TOP
        !          16618:                case 0xc1: pcbios_int_15h_c1h(); break;
        !          16619: #endif
        !          16620:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     16621: #if defined(HAS_I386)
1.1       root     16622:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     16623: #endif
1.1       root     16624:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     16625:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     16626:                default:
1.1.1.22  root     16627:                        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));
                   16628:                        REG8(AH) = 0x86;
1.1.1.3   root     16629:                        m_CF = 1;
1.1       root     16630:                        break;
                   16631:                }
                   16632:                break;
                   16633:        case 0x16:
                   16634:                // PC BIOS - Keyboard
1.1.1.3   root     16635:                m_CF = 0;
1.1       root     16636:                switch(REG8(AH)) {
                   16637:                case 0x00: pcbios_int_16h_00h(); break;
                   16638:                case 0x01: pcbios_int_16h_01h(); break;
                   16639:                case 0x02: pcbios_int_16h_02h(); break;
                   16640:                case 0x03: pcbios_int_16h_03h(); break;
                   16641:                case 0x05: pcbios_int_16h_05h(); break;
                   16642:                case 0x10: pcbios_int_16h_00h(); break;
                   16643:                case 0x11: pcbios_int_16h_01h(); break;
                   16644:                case 0x12: pcbios_int_16h_12h(); break;
                   16645:                case 0x13: pcbios_int_16h_13h(); break;
                   16646:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     16647:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     16648:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     16649:                case 0xda: break; // unknown
1.1.1.43  root     16650:                case 0xdb: break; // unknown
1.1.1.22  root     16651:                case 0xff: break; // unknown
1.1       root     16652:                default:
1.1.1.22  root     16653:                        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     16654:                        break;
                   16655:                }
                   16656:                break;
                   16657:        case 0x17:
                   16658:                // PC BIOS - Printer
1.1.1.37  root     16659:                m_CF = 0;
                   16660:                switch(REG8(AH)) {
                   16661:                case 0x00: pcbios_int_17h_00h(); break;
                   16662:                case 0x01: pcbios_int_17h_01h(); break;
                   16663:                case 0x02: pcbios_int_17h_02h(); break;
                   16664:                case 0x03: pcbios_int_17h_03h(); break;
                   16665:                case 0x50: pcbios_int_17h_50h(); break;
                   16666:                case 0x51: pcbios_int_17h_51h(); break;
                   16667:                case 0x52: pcbios_int_17h_52h(); break;
                   16668:                case 0x84: pcbios_int_17h_84h(); break;
                   16669:                case 0x85: pcbios_int_17h_85h(); break;
                   16670:                default:
                   16671:                        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));
                   16672:                        break;
                   16673:                }
1.1       root     16674:                break;
                   16675:        case 0x1a:
                   16676:                // PC BIOS - Timer
1.1.1.3   root     16677:                m_CF = 0;
1.1       root     16678:                switch(REG8(AH)) {
                   16679:                case 0x00: pcbios_int_1ah_00h(); break;
                   16680:                case 0x01: break;
                   16681:                case 0x02: pcbios_int_1ah_02h(); break;
                   16682:                case 0x03: break;
                   16683:                case 0x04: pcbios_int_1ah_04h(); break;
                   16684:                case 0x05: break;
                   16685:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   16686:                case 0x0b: break;
1.1.1.14  root     16687:                case 0x35: break; // Word Perfect Third Party Interface?
                   16688:                case 0x36: break; // Word Perfect Third Party Interface
                   16689:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     16690:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     16691:                case 0xb1: break; // PCI BIOS v2.0c+
                   16692:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     16693:                default:
1.1.1.22  root     16694:                        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     16695:                        break;
                   16696:                }
                   16697:                break;
1.1.1.33  root     16698:        case 0x1b:
                   16699:                mem[0x471] = 0x00;
                   16700:                break;
1.1       root     16701:        case 0x20:
1.1.1.28  root     16702:                try {
                   16703:                        msdos_process_terminate(SREG(CS), retval, 1);
                   16704:                } catch(...) {
                   16705:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   16706:                }
1.1       root     16707:                break;
1.1.1.49  root     16708:        case 0x30:
1.1.1.46  root     16709:                // dummy interrupt for case map routine pointed in the country info
                   16710: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   16711: //                     REG8(AL) = 0x00;
                   16712: //                     break;
                   16713: //             }
1.1       root     16714:        case 0x21:
                   16715:                // MS-DOS System Call
1.1.1.3   root     16716:                m_CF = 0;
1.1.1.28  root     16717:                try {
1.1.1.46  root     16718:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     16719:                        case 0x00: msdos_int_21h_00h(); break;
                   16720:                        case 0x01: msdos_int_21h_01h(); break;
                   16721:                        case 0x02: msdos_int_21h_02h(); break;
                   16722:                        case 0x03: msdos_int_21h_03h(); break;
                   16723:                        case 0x04: msdos_int_21h_04h(); break;
                   16724:                        case 0x05: msdos_int_21h_05h(); break;
                   16725:                        case 0x06: msdos_int_21h_06h(); break;
                   16726:                        case 0x07: msdos_int_21h_07h(); break;
                   16727:                        case 0x08: msdos_int_21h_08h(); break;
                   16728:                        case 0x09: msdos_int_21h_09h(); break;
                   16729:                        case 0x0a: msdos_int_21h_0ah(); break;
                   16730:                        case 0x0b: msdos_int_21h_0bh(); break;
                   16731:                        case 0x0c: msdos_int_21h_0ch(); break;
                   16732:                        case 0x0d: msdos_int_21h_0dh(); break;
                   16733:                        case 0x0e: msdos_int_21h_0eh(); break;
                   16734:                        case 0x0f: msdos_int_21h_0fh(); break;
                   16735:                        case 0x10: msdos_int_21h_10h(); break;
                   16736:                        case 0x11: msdos_int_21h_11h(); break;
                   16737:                        case 0x12: msdos_int_21h_12h(); break;
                   16738:                        case 0x13: msdos_int_21h_13h(); break;
                   16739:                        case 0x14: msdos_int_21h_14h(); break;
                   16740:                        case 0x15: msdos_int_21h_15h(); break;
                   16741:                        case 0x16: msdos_int_21h_16h(); break;
                   16742:                        case 0x17: msdos_int_21h_17h(); break;
                   16743:                        case 0x18: msdos_int_21h_18h(); break;
                   16744:                        case 0x19: msdos_int_21h_19h(); break;
                   16745:                        case 0x1a: msdos_int_21h_1ah(); break;
                   16746:                        case 0x1b: msdos_int_21h_1bh(); break;
                   16747:                        case 0x1c: msdos_int_21h_1ch(); break;
                   16748:                        case 0x1d: msdos_int_21h_1dh(); break;
                   16749:                        case 0x1e: msdos_int_21h_1eh(); break;
                   16750:                        case 0x1f: msdos_int_21h_1fh(); break;
                   16751:                        case 0x20: msdos_int_21h_20h(); break;
                   16752:                        case 0x21: msdos_int_21h_21h(); break;
                   16753:                        case 0x22: msdos_int_21h_22h(); break;
                   16754:                        case 0x23: msdos_int_21h_23h(); break;
                   16755:                        case 0x24: msdos_int_21h_24h(); break;
                   16756:                        case 0x25: msdos_int_21h_25h(); break;
                   16757:                        case 0x26: msdos_int_21h_26h(); break;
                   16758:                        case 0x27: msdos_int_21h_27h(); break;
                   16759:                        case 0x28: msdos_int_21h_28h(); break;
                   16760:                        case 0x29: msdos_int_21h_29h(); break;
                   16761:                        case 0x2a: msdos_int_21h_2ah(); break;
                   16762:                        case 0x2b: msdos_int_21h_2bh(); break;
                   16763:                        case 0x2c: msdos_int_21h_2ch(); break;
                   16764:                        case 0x2d: msdos_int_21h_2dh(); break;
                   16765:                        case 0x2e: msdos_int_21h_2eh(); break;
                   16766:                        case 0x2f: msdos_int_21h_2fh(); break;
                   16767:                        case 0x30: msdos_int_21h_30h(); break;
                   16768:                        case 0x31: msdos_int_21h_31h(); break;
                   16769:                        case 0x32: msdos_int_21h_32h(); break;
                   16770:                        case 0x33: msdos_int_21h_33h(); break;
                   16771:                        case 0x34: msdos_int_21h_34h(); break;
                   16772:                        case 0x35: msdos_int_21h_35h(); break;
                   16773:                        case 0x36: msdos_int_21h_36h(); break;
                   16774:                        case 0x37: msdos_int_21h_37h(); break;
                   16775:                        case 0x38: msdos_int_21h_38h(); break;
                   16776:                        case 0x39: msdos_int_21h_39h(0); break;
                   16777:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   16778:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   16779:                        case 0x3c: msdos_int_21h_3ch(); break;
                   16780:                        case 0x3d: msdos_int_21h_3dh(); break;
                   16781:                        case 0x3e: msdos_int_21h_3eh(); break;
                   16782:                        case 0x3f: msdos_int_21h_3fh(); break;
                   16783:                        case 0x40: msdos_int_21h_40h(); break;
                   16784:                        case 0x41: msdos_int_21h_41h(0); break;
                   16785:                        case 0x42: msdos_int_21h_42h(); break;
                   16786:                        case 0x43: msdos_int_21h_43h(0); break;
                   16787:                        case 0x44: msdos_int_21h_44h(); break;
                   16788:                        case 0x45: msdos_int_21h_45h(); break;
                   16789:                        case 0x46: msdos_int_21h_46h(); break;
                   16790:                        case 0x47: msdos_int_21h_47h(0); break;
                   16791:                        case 0x48: msdos_int_21h_48h(); break;
                   16792:                        case 0x49: msdos_int_21h_49h(); break;
                   16793:                        case 0x4a: msdos_int_21h_4ah(); break;
                   16794:                        case 0x4b: msdos_int_21h_4bh(); break;
                   16795:                        case 0x4c: msdos_int_21h_4ch(); break;
                   16796:                        case 0x4d: msdos_int_21h_4dh(); break;
                   16797:                        case 0x4e: msdos_int_21h_4eh(); break;
                   16798:                        case 0x4f: msdos_int_21h_4fh(); break;
                   16799:                        case 0x50: msdos_int_21h_50h(); break;
                   16800:                        case 0x51: msdos_int_21h_51h(); break;
                   16801:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     16802:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     16803:                        case 0x54: msdos_int_21h_54h(); break;
                   16804:                        case 0x55: msdos_int_21h_55h(); break;
                   16805:                        case 0x56: msdos_int_21h_56h(0); break;
                   16806:                        case 0x57: msdos_int_21h_57h(); break;
                   16807:                        case 0x58: msdos_int_21h_58h(); break;
                   16808:                        case 0x59: msdos_int_21h_59h(); break;
                   16809:                        case 0x5a: msdos_int_21h_5ah(); break;
                   16810:                        case 0x5b: msdos_int_21h_5bh(); break;
                   16811:                        case 0x5c: msdos_int_21h_5ch(); break;
                   16812:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     16813:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     16814:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     16815:                        case 0x60: msdos_int_21h_60h(0); break;
                   16816:                        case 0x61: msdos_int_21h_61h(); break;
                   16817:                        case 0x62: msdos_int_21h_62h(); break;
                   16818:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     16819:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     16820:                        case 0x65: msdos_int_21h_65h(); break;
                   16821:                        case 0x66: msdos_int_21h_66h(); break;
                   16822:                        case 0x67: msdos_int_21h_67h(); break;
                   16823:                        case 0x68: msdos_int_21h_68h(); break;
                   16824:                        case 0x69: msdos_int_21h_69h(); break;
                   16825:                        case 0x6a: msdos_int_21h_6ah(); break;
                   16826:                        case 0x6b: msdos_int_21h_6bh(); break;
                   16827:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     16828:                        case 0x6d: // Find First ROM Program
                   16829:                        case 0x6e: // Find Next ROM Program
                   16830:                        case 0x6f: // Get/Set ROM Scan Start Address
                   16831:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   16832:                                break;
1.1.1.43  root     16833:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     16834:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     16835:                                switch(REG8(AL)) {
                   16836:                                case 0x0d: msdos_int_21h_710dh(); break;
                   16837:                                case 0x39: msdos_int_21h_39h(1); break;
                   16838:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   16839:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     16840:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     16841:                                case 0x43: msdos_int_21h_43h(1); break;
                   16842:                                case 0x47: msdos_int_21h_47h(1); break;
                   16843:                                case 0x4e: msdos_int_21h_714eh(); break;
                   16844:                                case 0x4f: msdos_int_21h_714fh(); break;
                   16845:                                case 0x56: msdos_int_21h_56h(1); break;
                   16846:                                case 0x60: msdos_int_21h_60h(1); break;
                   16847:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   16848:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   16849:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   16850:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   16851:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   16852:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     16853:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     16854:                                case 0xaa: msdos_int_21h_71aah(); break;
                   16855:                                default:
                   16856:                                        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));
                   16857:                                        REG16(AX) = 0x7100;
                   16858:                                        m_CF = 1;
                   16859:                                        break;
                   16860:                                }
                   16861:                                break;
1.1.1.48  root     16862:                        case 0x72: // Windows95 beta - LFN FindClose
                   16863: //                             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));
                   16864:                                REG16(AX) = 0x7200;
                   16865:                                m_CF = 1;
                   16866:                                break;
                   16867:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     16868:                                switch(REG8(AL)) {
                   16869:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     16870:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     16871:                                case 0x02: msdos_int_21h_7302h(); break;
                   16872:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     16873:                                // 0x04: Set DPB to Use for Formatting
                   16874:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     16875:                                default:
                   16876:                                        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));
                   16877:                                        REG16(AX) = 0x7300;
                   16878:                                        m_CF = 1;
                   16879:                                        break;
                   16880:                                }
1.1       root     16881:                                break;
1.1.1.30  root     16882:                        case 0xdb: msdos_int_21h_dbh(); break;
                   16883:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     16884:                        default:
1.1.1.22  root     16885:                                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     16886:                                REG16(AX) = 0x01;
1.1.1.3   root     16887:                                m_CF = 1;
1.1       root     16888:                                break;
                   16889:                        }
1.1.1.28  root     16890:                } catch(int error) {
                   16891:                        REG16(AX) = error;
                   16892:                        m_CF = 1;
                   16893:                } catch(...) {
                   16894:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     16895:                        m_CF = 1;
1.1       root     16896:                }
1.1.1.3   root     16897:                if(m_CF) {
1.1.1.23  root     16898:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     16899:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     16900:                        sda->extended_error_code = REG16(AX);
                   16901:                        switch(sda->extended_error_code) {
                   16902:                        case  4: // Too many open files
                   16903:                        case  8: // Insufficient memory
                   16904:                                sda->error_class = 1; // Out of resource
                   16905:                                break;
                   16906:                        case  5: // Access denied
                   16907:                                sda->error_class = 3; // Authorization
                   16908:                                break;
                   16909:                        case  7: // Memory control block destroyed
                   16910:                                sda->error_class = 4; // Internal
                   16911:                                break;
                   16912:                        case  2: // File not found
                   16913:                        case  3: // Path not found
                   16914:                        case 15: // Invaid drive specified
                   16915:                        case 18: // No more files
                   16916:                                sda->error_class = 8; // Not found
                   16917:                                break;
                   16918:                        case 32: // Sharing violation
                   16919:                        case 33: // Lock violation
                   16920:                                sda->error_class = 10; // Locked
                   16921:                                break;
                   16922: //                     case 16: // Removal of current directory attempted
                   16923:                        case 19: // Attempted write on protected disk
                   16924:                        case 21: // Drive not ready
                   16925: //                     case 29: // Write failure
                   16926: //                     case 30: // Read failure
                   16927: //                     case 82: // Cannot create subdirectory
                   16928:                                sda->error_class = 11; // Media
                   16929:                                break;
                   16930:                        case 80: // File already exists
                   16931:                                sda->error_class = 12; // Already exist
                   16932:                                break;
                   16933:                        default:
                   16934:                                sda->error_class = 13; // Unknown
                   16935:                                break;
                   16936:                        }
                   16937:                        sda->suggested_action = 1; // Retry
                   16938:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     16939:                }
1.1.1.33  root     16940:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     16941:                        // raise int 23h
                   16942: #if defined(HAS_I386)
                   16943:                        m_ext = 0; // not an external interrupt
                   16944:                        i386_trap(0x23, 1, 0);
                   16945:                        m_ext = 1;
                   16946: #else
                   16947:                        PREFIX86(_interrupt)(0x23);
                   16948: #endif
                   16949:                }
1.1       root     16950:                break;
                   16951:        case 0x22:
                   16952:                fatalerror("int 22h (terminate address)\n");
                   16953:        case 0x23:
1.1.1.28  root     16954:                try {
                   16955:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   16956:                } catch(...) {
                   16957:                        fatalerror("failed to terminate the current process by int 23h\n");
                   16958:                }
1.1       root     16959:                break;
                   16960:        case 0x24:
1.1.1.32  root     16961: /*
1.1.1.28  root     16962:                try {
                   16963:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16964:                } catch(...) {
                   16965:                        fatalerror("failed to terminate the current process by int 24h\n");
                   16966:                }
1.1.1.32  root     16967: */
                   16968:                msdos_int_24h();
1.1       root     16969:                break;
                   16970:        case 0x25:
                   16971:                msdos_int_25h();
                   16972:                break;
                   16973:        case 0x26:
                   16974:                msdos_int_26h();
                   16975:                break;
                   16976:        case 0x27:
1.1.1.28  root     16977:                try {
                   16978:                        msdos_int_27h();
                   16979:                } catch(...) {
                   16980:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   16981:                }
1.1       root     16982:                break;
                   16983:        case 0x28:
                   16984:                Sleep(10);
1.1.1.35  root     16985:                REQUEST_HARDWRE_UPDATE();
1.1       root     16986:                break;
                   16987:        case 0x29:
                   16988:                msdos_int_29h();
                   16989:                break;
                   16990:        case 0x2e:
                   16991:                msdos_int_2eh();
                   16992:                break;
                   16993:        case 0x2f:
                   16994:                // multiplex interrupt
                   16995:                switch(REG8(AH)) {
1.1.1.22  root     16996:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     16997:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     16998:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     16999:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17000:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17001:                case 0x14: msdos_int_2fh_14h(); break;
                   17002:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17003:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17004:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17005:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17006:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17007:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17008:                case 0x46: msdos_int_2fh_46h(); break;
                   17009:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17010:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17011:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17012:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17013:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17014:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17015:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17016:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17017:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17018:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17019:                default:
1.1.1.30  root     17020:                        switch(REG8(AL)) {
                   17021:                        case 0x00:
                   17022:                                // This is not installed
                   17023: //                             REG8(AL) = 0x00;
                   17024:                                break;
1.1.1.33  root     17025:                        case 0x01:
1.1.1.42  root     17026:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17027:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17028:                                        break;
                   17029:                                }
1.1.1.33  root     17030:                                // Banyan VINES v4+ is not installed
                   17031:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17032:                                        break;
                   17033:                                }
1.1.1.42  root     17034:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17035:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17036:                                        break;
                   17037:                                }
1.1.1.30  root     17038:                        default:
1.1.1.42  root     17039:                                // NORTON UTILITIES 5.0+
                   17040:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17041:                                        break;
                   17042:                                }
1.1.1.30  root     17043:                                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     17044:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17045:                                m_CF = 1;
                   17046:                                break;
                   17047:                        }
                   17048:                        break;
1.1       root     17049:                }
                   17050:                break;
1.1.1.24  root     17051:        case 0x33:
                   17052:                switch(REG8(AH)) {
                   17053:                case 0x00:
                   17054:                        // Mouse
1.1.1.49  root     17055:                        switch(REG16(AX)) {
                   17056:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17057:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17058:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17059:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17060:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17061:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17062:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17063:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17064:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17065:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17066:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17067:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17068:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17069:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17070:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17071:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17072:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17073:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17074:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17075:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17076:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17077:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17078:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17079:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17080:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17081:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17082:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17083:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17084:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17085:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17086:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17087:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17088:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17089:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17090:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17091:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17092:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17093:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17094:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17095:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17096:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17097:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17098:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17099:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17100:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17101:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17102:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17103:                        case 0x002f: break; // Mouse Hardware Reset
                   17104:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17105:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17106:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17107:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17108:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17109:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17110:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17111:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17112:                        default:
                   17113:                                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));
                   17114:                                break;
                   17115:                        }
                   17116:                        break;
                   17117:                default:
                   17118:                        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));
                   17119:                        break;
                   17120:                }
                   17121:                break;
1.1.1.50  root     17122: /*
                   17123:        case 0x67:
                   17124:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 68h
                   17125:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   17126:                break;
                   17127: */
1.1.1.19  root     17128:        case 0x68:
                   17129:                // dummy interrupt for EMS (int 67h)
                   17130:                switch(REG8(AH)) {
                   17131:                case 0x40: msdos_int_67h_40h(); break;
                   17132:                case 0x41: msdos_int_67h_41h(); break;
                   17133:                case 0x42: msdos_int_67h_42h(); break;
                   17134:                case 0x43: msdos_int_67h_43h(); break;
                   17135:                case 0x44: msdos_int_67h_44h(); break;
                   17136:                case 0x45: msdos_int_67h_45h(); break;
                   17137:                case 0x46: msdos_int_67h_46h(); break;
                   17138:                case 0x47: msdos_int_67h_47h(); break;
                   17139:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17140:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17141:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17142:                case 0x4b: msdos_int_67h_4bh(); break;
                   17143:                case 0x4c: msdos_int_67h_4ch(); break;
                   17144:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17145:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17146:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17147:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17148:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17149:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17150:                case 0x53: msdos_int_67h_53h(); break;
                   17151:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17152:                case 0x55: msdos_int_67h_55h(); break;
                   17153:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17154:                case 0x57: msdos_int_67h_57h(); break;
                   17155:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17156:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17157:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17158:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17159:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17160:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17161:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17162:                // 0xde: VCPI
1.1.1.30  root     17163:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17164:                default:
1.1.1.22  root     17165:                        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     17166:                        REG8(AH) = 0x84;
                   17167:                        break;
                   17168:                }
                   17169:                break;
                   17170: #ifdef SUPPORT_XMS
                   17171:        case 0x69:
                   17172:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17173:                try {
                   17174:                        switch(REG8(AH)) {
                   17175:                        case 0x00: msdos_call_xms_00h(); break;
                   17176:                        case 0x01: msdos_call_xms_01h(); break;
                   17177:                        case 0x02: msdos_call_xms_02h(); break;
                   17178:                        case 0x03: msdos_call_xms_03h(); break;
                   17179:                        case 0x04: msdos_call_xms_04h(); break;
                   17180:                        case 0x05: msdos_call_xms_05h(); break;
                   17181:                        case 0x06: msdos_call_xms_06h(); break;
                   17182:                        case 0x07: msdos_call_xms_07h(); break;
                   17183:                        case 0x08: msdos_call_xms_08h(); break;
                   17184:                        case 0x09: msdos_call_xms_09h(); break;
                   17185:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17186:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17187:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17188:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17189:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17190:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17191:                        case 0x10: msdos_call_xms_10h(); break;
                   17192:                        case 0x11: msdos_call_xms_11h(); break;
                   17193:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17194: #if defined(HAS_I386)
                   17195:                        case 0x88: msdos_call_xms_88h(); break;
                   17196:                        case 0x89: msdos_call_xms_89h(); break;
                   17197:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17198:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17199: #endif
1.1.1.28  root     17200:                        default:
                   17201:                                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));
                   17202:                                REG16(AX) = 0x0000;
                   17203:                                REG8(BL) = 0x80; // function not implemented
                   17204:                                break;
                   17205:                        }
                   17206:                } catch(...) {
1.1.1.19  root     17207:                        REG16(AX) = 0x0000;
1.1.1.28  root     17208:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17209:                }
                   17210:                break;
                   17211: #endif
                   17212:        case 0x6a:
1.1.1.24  root     17213:                // irq12 (mouse)
                   17214:                mouse_push_ax = REG16(AX);
                   17215:                mouse_push_bx = REG16(BX);
                   17216:                mouse_push_cx = REG16(CX);
                   17217:                mouse_push_dx = REG16(DX);
                   17218:                mouse_push_si = REG16(SI);
                   17219:                mouse_push_di = REG16(DI);
                   17220:                
1.1.1.43  root     17221:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17222:                        REG16(AX) = mouse.status_irq;
                   17223:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17224:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17225:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17226:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17227:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17228:                        
1.1.1.49  root     17229:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17230:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17231:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17232:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17233:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     17234:                        break;
1.1.1.24  root     17235:                }
1.1.1.43  root     17236:                for(int i = 0; i < 8; i++) {
                   17237:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17238:                                REG16(AX) = mouse.status_irq_alt;
                   17239:                                REG16(BX) = mouse.get_buttons();
                   17240:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17241:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17242:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17243:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17244:                                
1.1.1.49  root     17245:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17246:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17247:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17248:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17249:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.43  root     17250:                                break;
                   17251:                        }
                   17252:                }
1.1.1.54! root     17253:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw) {
        !          17254:                        UINT16 data_1st, data_2nd, data_3rd;
        !          17255:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
        !          17256:                        i386_push16(data_1st);
        !          17257:                        i386_push16(data_2nd);
        !          17258:                        i386_push16(data_3rd);
        !          17259:                        i386_push16(0x0000);
        !          17260:                        
        !          17261:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
        !          17262:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
        !          17263:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
        !          17264:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
        !          17265:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
        !          17266:                        break;
        !          17267:                }
1.1.1.43  root     17268:                // invalid call addr :-(
1.1.1.49  root     17269:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17270:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17271:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17272:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17273:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.24  root     17274:                break;
                   17275:        case 0x6b:
                   17276:                // end of irq12 (mouse)
                   17277:                REG16(AX) = mouse_push_ax;
                   17278:                REG16(BX) = mouse_push_bx;
                   17279:                REG16(CX) = mouse_push_cx;
                   17280:                REG16(DX) = mouse_push_dx;
                   17281:                REG16(SI) = mouse_push_si;
                   17282:                REG16(DI) = mouse_push_di;
                   17283:                
                   17284:                // EOI
                   17285:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17286:                        pic[0].isr &= ~(1 << 2); // master
                   17287:                }
                   17288:                pic_update();
                   17289:                break;
                   17290:        case 0x6c:
1.1.1.19  root     17291:                // dummy interrupt for case map routine pointed in the country info
                   17292:                if(REG8(AL) >= 0x80) {
                   17293:                        char tmp[2] = {0};
                   17294:                        tmp[0] = REG8(AL);
                   17295:                        my_strupr(tmp);
                   17296:                        REG8(AL) = tmp[0];
                   17297:                }
                   17298:                break;
1.1.1.27  root     17299:        case 0x6d:
                   17300:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17301:                REG8(AL) = 0x86; // not supported
                   17302:                m_CF = 1;
                   17303:                break;
1.1.1.32  root     17304:        case 0x6e:
                   17305:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17306:                {
                   17307:                        UINT16 code = REG16(AX);
                   17308:                        if(code & 0xf0) {
                   17309:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17310:                        }
                   17311:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17312:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17313:                                        const char *message = NULL;
                   17314:                                        if(active_code_page == 932) {
                   17315:                                                message = param_error_table[i].message_japanese;
                   17316:                                        }
                   17317:                                        if(message == NULL) {
                   17318:                                                message = param_error_table[i].message_english;
                   17319:                                        }
                   17320:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   17321:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   17322:                                        
                   17323:                                        SREG(ES) = WORK_TOP >> 4;
                   17324:                                        i386_load_segment_descriptor(ES);
                   17325:                                        REG16(DI) = 0x0000;
                   17326:                                        break;
                   17327:                                }
                   17328:                        }
                   17329:                }
                   17330:                break;
1.1.1.49  root     17331:        case 0x6f:
                   17332:                // dummy interrupt for end of alter page map and call
                   17333:                {
                   17334:                        UINT16 handles[4], pages[4];
                   17335:                        
                   17336:                        // pop old mapping data in new mapping
                   17337:                        for(int i = 0; i < 4; i++) {
                   17338:                                pages  [3 - i] = i386_pop16();
                   17339:                                handles[3 - i] = i386_pop16();
                   17340:                        }
                   17341:                        
                   17342:                        // restore old mapping
                   17343:                        for(int i = 0; i < 4; i++) {
                   17344:                                UINT16 handle = handles[i];
                   17345:                                UINT16 page   = pages  [i];
                   17346:                                
                   17347:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   17348:                                        ems_map_page(i, handle, page);
                   17349:                                } else {
                   17350:                                        ems_unmap_page(i);
                   17351:                                }
                   17352:                        }
                   17353:                        // do ret_far (pop cs/ip) in old mapping
                   17354:                }
                   17355:                break;
1.1.1.8   root     17356:        case 0x70:
                   17357:        case 0x71:
                   17358:        case 0x72:
                   17359:        case 0x73:
                   17360:        case 0x74:
                   17361:        case 0x75:
                   17362:        case 0x76:
                   17363:        case 0x77:
                   17364:                // EOI
                   17365:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   17366:                        pic[0].isr &= ~(1 << 2); // master
                   17367:                }
                   17368:                pic_update();
                   17369:                break;
1.1       root     17370:        default:
1.1.1.22  root     17371: //             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     17372:                break;
                   17373:        }
                   17374:        
                   17375:        // update cursor position
                   17376:        if(cursor_moved) {
1.1.1.36  root     17377:                pcbios_update_cursor_position();
1.1       root     17378:                cursor_moved = false;
                   17379:        }
                   17380: }
                   17381: 
                   17382: // init
                   17383: 
                   17384: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   17385: {
                   17386:        // init file handler
                   17387:        memset(file_handler, 0, sizeof(file_handler));
                   17388:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   17389:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   17390:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     17391: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17392:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     17393: #else
                   17394:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   17395: #endif
                   17396:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     17397:        }
1.1.1.21  root     17398:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     17399: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   17400:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     17401:        }
1.1       root     17402:        _dup2(0, DUP_STDIN);
                   17403:        _dup2(1, DUP_STDOUT);
                   17404:        _dup2(2, DUP_STDERR);
1.1.1.21  root     17405:        _dup2(3, DUP_STDAUX);
                   17406:        _dup2(4, DUP_STDPRN);
1.1       root     17407:        
1.1.1.24  root     17408:        // init mouse
                   17409:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     17410:        mouse.enabled = true;   // from DOSBox
                   17411:        mouse.hidden = 1;       // hidden in default ???
                   17412:        mouse.old_hidden = 1;   // from DOSBox
                   17413:        mouse.max_position.x = 8 * (scr_width  - 1);
                   17414:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     17415:        mouse.mickey.x = 8;
                   17416:        mouse.mickey.y = 16;
                   17417:        
1.1.1.26  root     17418: #ifdef SUPPORT_XMS
                   17419:        // init xms
                   17420:        msdos_xms_init();
                   17421: #endif
                   17422:        
1.1       root     17423:        // init process
                   17424:        memset(process, 0, sizeof(process));
                   17425:        
1.1.1.13  root     17426:        // init dtainfo
                   17427:        msdos_dta_info_init();
                   17428:        
1.1       root     17429:        // init memory
                   17430:        memset(mem, 0, sizeof(mem));
                   17431:        
                   17432:        // bios data area
1.1.1.23  root     17433:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     17434:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   17435:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     17436:        CONSOLE_FONT_INFO cfi;
                   17437:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   17438:        
                   17439:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   17440:        text_vram_top_address = TEXT_VRAM_TOP;
                   17441:        text_vram_end_address = text_vram_top_address + regen;
                   17442:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   17443:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     17444:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     17445:        
                   17446:        if(regen > 0x4000) {
                   17447:                regen = 0x8000;
                   17448:                vram_pages = 1;
                   17449:        } else if(regen > 0x2000) {
                   17450:                regen = 0x4000;
                   17451:                vram_pages = 2;
                   17452:        } else if(regen > 0x1000) {
                   17453:                regen = 0x2000;
                   17454:                vram_pages = 4;
                   17455:        } else {
                   17456:                regen = 0x1000;
                   17457:                vram_pages = 8;
                   17458:        }
1.1       root     17459:        
1.1.1.25  root     17460:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   17461:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     17462:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   17463:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     17464:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     17465:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   17466:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     17467: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17468:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     17469: #endif
1.1.1.26  root     17470:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     17471:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     17472:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   17473:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     17474:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     17475:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   17476:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     17477:        *(UINT16 *)(mem + 0x44e) = 0;
                   17478:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     17479:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     17480:        *(UINT8  *)(mem + 0x460) = 7;
                   17481:        *(UINT8  *)(mem + 0x461) = 7;
                   17482:        *(UINT8  *)(mem + 0x462) = 0;
                   17483:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     17484:        *(UINT8  *)(mem + 0x465) = 0x09;
                   17485:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     17486:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     17487:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   17488:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     17489:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   17490:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   17491:        *(UINT8  *)(mem + 0x487) = 0x60;
                   17492:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     17493: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17494:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     17495: #endif
1.1.1.14  root     17496:        
                   17497:        // initial screen
                   17498:        SMALL_RECT rect;
                   17499:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   17500:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   17501:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   17502:                for(int x = 0; x < scr_width; x++) {
                   17503:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   17504:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   17505:                }
                   17506:        }
1.1       root     17507:        
1.1.1.19  root     17508:        // init mcb
1.1       root     17509:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     17510:        
                   17511:        // iret table
                   17512:        // note: int 2eh vector should address the routine in command.com,
                   17513:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   17514:        // so move iret table into allocated memory block
                   17515:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     17516:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     17517:        IRET_TOP = seg << 4;
                   17518:        seg += IRET_SIZE >> 4;
1.1.1.25  root     17519:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     17520:        
                   17521:        // dummy xms/ems device
1.1.1.33  root     17522:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     17523:        XMS_TOP = seg << 4;
                   17524:        seg += XMS_SIZE >> 4;
                   17525:        
                   17526:        // environment
1.1.1.33  root     17527:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     17528:        int env_seg = seg;
                   17529:        int ofs = 0;
1.1.1.32  root     17530:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   17531:        char comspec_added = 0;
1.1.1.33  root     17532:        char lastdrive_added = 0;
1.1.1.32  root     17533:        char env_msdos_path[ENV_SIZE] = {0};
                   17534:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     17535:        char prompt_added = 0;
1.1.1.32  root     17536:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     17537:        char tz_added = 0;
1.1.1.45  root     17538:        const char *path, *short_path;
1.1.1.32  root     17539:        
                   17540:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   17541:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17542:                        strcpy(env_append, short_path);
                   17543:                }
                   17544:        }
                   17545:        if((path = getenv("APPEND")) != NULL) {
                   17546:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17547:                        if(env_append[0] != '\0') {
                   17548:                                strcat(env_append, ";");
                   17549:                        }
                   17550:                        strcat(env_append, short_path);
                   17551:                }
                   17552:        }
                   17553:        
                   17554:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   17555:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17556:                        strcpy(comspec_path, short_path);
                   17557:                }
                   17558:        }
                   17559:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   17560:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17561:                        strcpy(comspec_path, short_path);
                   17562:                }
                   17563:        }
1.1       root     17564:        
1.1.1.28  root     17565:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     17566:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17567:                        strcpy(env_msdos_path, short_path);
                   17568:                        strcpy(env_path, short_path);
1.1.1.14  root     17569:                }
                   17570:        }
1.1.1.28  root     17571:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     17572:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17573:                        if(env_path[0] != '\0') {
                   17574:                                strcat(env_path, ";");
                   17575:                        }
                   17576:                        strcat(env_path, short_path);
1.1.1.9   root     17577:                }
                   17578:        }
1.1.1.32  root     17579:        
                   17580:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   17581:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     17582:        }
1.1.1.32  root     17583:        for(int i = 0; i < 4; i++) {
                   17584:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   17585:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   17586:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17587:                                strcpy(env_temp, short_path);
                   17588:                                break;
                   17589:                        }
                   17590:                }
1.1.1.24  root     17591:        }
1.1.1.32  root     17592:        
1.1.1.9   root     17593:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     17594:                // lower to upper
1.1.1.28  root     17595:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     17596:                strcpy(tmp, *p);
                   17597:                for(int i = 0;; i++) {
                   17598:                        if(tmp[i] == '=') {
                   17599:                                tmp[i] = '\0';
                   17600:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     17601:                                my_strupr(name);
1.1       root     17602:                                tmp[i] = '=';
                   17603:                                break;
                   17604:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     17605:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     17606:                        }
                   17607:                }
1.1.1.33  root     17608:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   17609:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   17610:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     17611:                        // ignore non standard environments
                   17612:                } else {
1.1.1.33  root     17613:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     17614:                                if(env_append[0] != '\0') {
                   17615:                                        sprintf(tmp, "APPEND=%s", env_append);
                   17616:                                } else {
                   17617:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   17618:                                }
                   17619:                                append_added = 1;
                   17620:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     17621:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     17622:                                comspec_added = 1;
1.1.1.33  root     17623:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   17624:                                char *env = getenv("MSDOS_LASTDRIVE");
                   17625:                                if(env != NULL) {
                   17626:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   17627:                                }
                   17628:                                lastdrive_added = 1;
                   17629:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     17630:                                if(env_msdos_path[0] != '\0') {
                   17631:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   17632:                                } else {
                   17633:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   17634:                                }
1.1.1.33  root     17635:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     17636:                                if(env_path[0] != '\0') {
                   17637:                                        sprintf(tmp, "PATH=%s", env_path);
                   17638:                                } else {
                   17639:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   17640:                                }
1.1.1.32  root     17641:                                path_added = 1;
1.1.1.33  root     17642:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   17643:                                prompt_added = 1;
1.1.1.28  root     17644:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   17645:                                if(env_temp[0] != '\0') {
                   17646:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   17647:                                } else {
                   17648:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   17649:                                }
1.1.1.32  root     17650:                                temp_added = 1;
1.1.1.33  root     17651:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     17652:                                if(env_temp[0] != '\0') {
                   17653:                                        sprintf(tmp, "TMP=%s", env_temp);
                   17654:                                } else {
                   17655:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     17656:                                }
1.1.1.32  root     17657:                                tmp_added = 1;
1.1.1.33  root     17658:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   17659:                                char *env = getenv("MSDOS_TZ");
                   17660:                                if(env != NULL) {
                   17661:                                        sprintf(tmp, "TZ=%s", env);
                   17662:                                }
                   17663:                                tz_added = 1;
1.1       root     17664:                        }
                   17665:                        int len = strlen(tmp);
1.1.1.14  root     17666:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     17667:                                fatalerror("too many environments\n");
                   17668:                        }
                   17669:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   17670:                        ofs += len + 1;
                   17671:                }
                   17672:        }
1.1.1.32  root     17673:        if(!append_added && env_append[0] != '\0') {
                   17674:                #define SET_ENV(name, value) { \
                   17675:                        char tmp[ENV_SIZE]; \
                   17676:                        sprintf(tmp, "%s=%s", name, value); \
                   17677:                        int len = strlen(tmp); \
                   17678:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   17679:                                fatalerror("too many environments\n"); \
                   17680:                        } \
                   17681:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   17682:                        ofs += len + 1; \
                   17683:                }
                   17684:                SET_ENV("APPEND", env_append);
                   17685:        }
                   17686:        if(!comspec_added) {
                   17687:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   17688:        }
1.1.1.33  root     17689:        if(!lastdrive_added) {
                   17690:                SET_ENV("LASTDRIVE", "Z");
                   17691:        }
1.1.1.32  root     17692:        if(!path_added) {
                   17693:                SET_ENV("PATH", env_path);
                   17694:        }
1.1.1.33  root     17695:        if(!prompt_added) {
                   17696:                SET_ENV("PROMPT", "$P$G");
                   17697:        }
1.1.1.32  root     17698:        if(!temp_added) {
                   17699:                SET_ENV("TEMP", env_temp);
                   17700:        }
                   17701:        if(!tmp_added) {
                   17702:                SET_ENV("TMP", env_temp);
                   17703:        }
1.1.1.33  root     17704:        if(!tz_added) {
                   17705:                TIME_ZONE_INFORMATION tzi;
                   17706:                HKEY hKey, hSubKey;
                   17707:                char tzi_std_name[64];
                   17708:                char tz_std[8] = "GMT";
                   17709:                char tz_dlt[8] = "GST";
                   17710:                char tz_value[32];
                   17711:                
                   17712:                // timezone name from GetTimeZoneInformation may not be english
                   17713:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   17714:                setlocale(LC_CTYPE, "");
                   17715:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   17716:                
                   17717:                // get english timezone name from registry
                   17718:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   17719:                        for(DWORD i = 0; !tz_added; i++) {
                   17720:                                char reg_name[256], sub_key[1024], std_name[256];
                   17721:                                DWORD size;
                   17722:                                FILETIME ftTime;
                   17723:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   17724:                                
                   17725:                                if(result == ERROR_SUCCESS) {
                   17726:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   17727:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   17728:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   17729:                                                        // search english timezone name from table
1.1.1.37  root     17730:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     17731:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   17732:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   17733:                                                                                if(tz_table[j].std != NULL) {
                   17734:                                                                                        strcpy(tz_std, tz_table[j].std);
                   17735:                                                                                }
                   17736:                                                                                if(tz_table[j].dlt != NULL) {
                   17737:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   17738:                                                                                }
                   17739:                                                                                tz_added = 1;
                   17740:                                                                                break;
                   17741:                                                                        }
                   17742:                                                                }
                   17743:                                                        }
                   17744:                                                }
                   17745:                                                RegCloseKey(hSubKey);
                   17746:                                        }
                   17747:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   17748:                                        break;
                   17749:                                }
                   17750:                        }
                   17751:                        RegCloseKey(hKey);
                   17752:                }
                   17753:                if((tzi.Bias % 60) != 0) {
                   17754:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   17755:                } else {
                   17756:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   17757:                }
                   17758:                if(daylight) {
                   17759:                        strcat(tz_value, tz_dlt);
                   17760:                }
                   17761:                SET_ENV("TZ", tz_value);
                   17762:        }
1.1       root     17763:        seg += (ENV_SIZE >> 4);
                   17764:        
                   17765:        // psp
1.1.1.33  root     17766:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     17767:        current_psp = seg;
1.1.1.35  root     17768:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   17769:        psp->parent_psp = current_psp;
1.1       root     17770:        seg += (PSP_SIZE >> 4);
                   17771:        
1.1.1.19  root     17772:        // first free mcb in conventional memory
1.1.1.33  root     17773:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     17774:        first_mcb = seg;
                   17775:        
1.1.1.19  root     17776:        // dummy mcb to link to umb
1.1.1.33  root     17777: #if 0
1.1.1.39  root     17778:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     17779: #else
1.1.1.39  root     17780:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     17781: #endif
1.1.1.19  root     17782:        
                   17783:        // first free mcb in upper memory block
1.1.1.8   root     17784:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     17785:        
1.1.1.29  root     17786: #ifdef SUPPORT_HMA
                   17787:        // first free mcb in high memory area
                   17788:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   17789: #endif
                   17790:        
1.1.1.26  root     17791:        // interrupt vector
                   17792:        for(int i = 0; i < 0x80; i++) {
                   17793:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   17794:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   17795:        }
1.1.1.49  root     17796:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   17797:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17798:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   17799:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   17800:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   17801:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     17802:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   17803:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17804:        
1.1.1.29  root     17805:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     17806:        static const struct {
                   17807:                UINT16 attributes;
                   17808:                char *dev_name;
                   17809:        } dummy_devices[] = {
                   17810:                {0x8013, "CON     "},
                   17811:                {0x8000, "AUX     "},
                   17812:                {0xa0c0, "PRN     "},
                   17813:                {0x8008, "CLOCK$  "},
                   17814:                {0x8000, "COM1    "},
                   17815:                {0xa0c0, "LPT1    "},
                   17816:                {0xa0c0, "LPT2    "},
                   17817:                {0xa0c0, "LPT3    "},
                   17818:                {0x8000, "COM2    "},
                   17819:                {0x8000, "COM3    "},
                   17820:                {0x8000, "COM4    "},
1.1.1.30  root     17821: //             {0xc000, "CONFIG$ "},
                   17822:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     17823:        };
                   17824:        static const UINT8 dummy_device_routine[] = {
                   17825:                // from NUL device of Windows 98 SE
                   17826:                // or word ptr ES:[BX+03],0100
                   17827:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   17828:                // retf
                   17829:                0xcb,
                   17830:        };
1.1.1.29  root     17831:        device_t *last = NULL;
1.1.1.32  root     17832:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     17833:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     17834:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   17835:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17836:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     17837:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   17838:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     17839:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     17840:                last = device;
                   17841:        }
                   17842:        if(last != NULL) {
                   17843:                last->next_driver.w.l = 0;
                   17844:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     17845:        }
1.1.1.29  root     17846:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     17847:        
1.1.1.25  root     17848:        // dos info
                   17849:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   17850:        dos_info->magic_word = 1;
                   17851:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   17852:        dos_info->first_dpb.w.l = 0;
                   17853:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   17854:        dos_info->first_sft.w.l = 0;
                   17855:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     17856:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     17857:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17858:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     17859:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   17860:        dos_info->max_sector_len = 512;
                   17861:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   17862:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   17863:        dos_info->cds.w.l = 0;
                   17864:        dos_info->cds.w.h = CDS_TOP >> 4;
                   17865:        dos_info->fcb_table.w.l = 0;
                   17866:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   17867:        dos_info->last_drive = 'Z' - 'A' + 1;
                   17868:        dos_info->buffers_x = 20;
                   17869:        dos_info->buffers_y = 0;
                   17870:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     17871:        dos_info->nul_device.next_driver.w.l = 22;
                   17872:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     17873:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     17874:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   17875:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17876:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   17877:        dos_info->disk_buf_heads.w.l = 0;
                   17878:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     17879:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     17880:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   17881:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     17882:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     17883:        
                   17884:        char *env;
                   17885:        if((env = getenv("LASTDRIVE")) != NULL) {
                   17886:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17887:                        dos_info->last_drive = env[0] - 'A' + 1;
                   17888:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17889:                        dos_info->last_drive = env[0] - 'a' + 1;
                   17890:                }
                   17891:        }
                   17892:        if((env = getenv("windir")) != NULL) {
                   17893:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17894:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   17895:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17896:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   17897:                }
                   17898:        }
                   17899: #if defined(HAS_I386)
                   17900:        dos_info->i386_or_later = 1;
                   17901: #else
                   17902:        dos_info->i386_or_later = 0;
                   17903: #endif
                   17904:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   17905:        
1.1.1.27  root     17906:        // ems (int 67h) and xms
1.1.1.25  root     17907:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   17908:        xms_device->next_driver.w.l = 0xffff;
                   17909:        xms_device->next_driver.w.h = 0xffff;
                   17910:        xms_device->attributes = 0xc000;
1.1.1.29  root     17911:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   17912:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17913:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   17914:        
1.1.1.26  root     17915:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   17916:        mem[XMS_TOP + 0x13] = 0x68;
                   17917:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     17918: #ifdef SUPPORT_XMS
                   17919:        if(support_xms) {
1.1.1.26  root     17920:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   17921:                mem[XMS_TOP + 0x16] = 0x69;
                   17922:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     17923:        } else
                   17924: #endif
1.1.1.26  root     17925:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     17926:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     17927:        
1.1.1.26  root     17928:        // irq12 routine (mouse)
1.1.1.49  root     17929:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 6ah (dummy)
                   17930:        mem[DUMMY_TOP + 0x01] = 0x6a;
                   17931:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   17932:        mem[DUMMY_TOP + 0x03] = 0xff;
                   17933:        mem[DUMMY_TOP + 0x04] = 0xff;
                   17934:        mem[DUMMY_TOP + 0x05] = 0xff;
                   17935:        mem[DUMMY_TOP + 0x06] = 0xff;
                   17936:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17937:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   17938:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     17939:        
1.1.1.27  root     17940:        // case map routine
1.1.1.49  root     17941:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   17942:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   17943:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     17944:        
                   17945:        // font read routine
1.1.1.49  root     17946:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   17947:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   17948:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     17949:        
1.1.1.32  root     17950:        // error message read routine
1.1.1.49  root     17951:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   17952:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   17953:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     17954:        
1.1.1.35  root     17955:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     17956:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   17957:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   17958:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   17959:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   17960:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     17961:        
1.1.1.50  root     17962:        // irq0 routine (system timer)
1.1.1.49  root     17963:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   17964:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   17965:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   17966:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   17967:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   17968:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   17969:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   17970:        
                   17971:        // alter page map and call routine
                   17972:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   17973:        mem[DUMMY_TOP + 0x20] = 0xff;
                   17974:        mem[DUMMY_TOP + 0x21] = 0xff;
                   17975:        mem[DUMMY_TOP + 0x22] = 0xff;
                   17976:        mem[DUMMY_TOP + 0x23] = 0xff;
                   17977:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   17978:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   17979:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     17980:        
1.1.1.50  root     17981:        // call int 29h routine
                   17982:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   17983:        mem[DUMMY_TOP + 0x28] = 0x29;
                   17984:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   17985:        
1.1.1.26  root     17986:        // boot routine
1.1.1.49  root     17987:        mem[0xffff0 + 0x00] = 0xf4;     // halt
                   17988:        mem[0xffff0 + 0x05] = '0';      // rom date
                   17989:        mem[0xffff0 + 0x06] = '2';
                   17990:        mem[0xffff0 + 0x07] = '/';
                   17991:        mem[0xffff0 + 0x08] = '2';
                   17992:        mem[0xffff0 + 0x09] = '2';
                   17993:        mem[0xffff0 + 0x0a] = '/';
                   17994:        mem[0xffff0 + 0x0b] = '0';
                   17995:        mem[0xffff0 + 0x0c] = '6';
                   17996:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id
                   17997:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.24  root     17998:        
1.1       root     17999:        // param block
                   18000:        // + 0: param block (22bytes)
                   18001:        // +24: fcb1/2 (20bytes)
                   18002:        // +44: command tail (128bytes)
                   18003:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18004:        param->env_seg = 0;
                   18005:        param->cmd_line.w.l = 44;
                   18006:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18007:        param->fcb1.w.l = 24;
                   18008:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18009:        param->fcb2.w.l = 24;
                   18010:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18011:        
                   18012:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18013:        
                   18014:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18015:        if(argc > 1) {
                   18016:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18017:                for(int i = 2; i < argc; i++) {
                   18018:                        char tmp[128];
                   18019:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18020:                        strcpy(cmd_line->cmd, tmp);
                   18021:                }
                   18022:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18023:        } else {
                   18024:                cmd_line->len = 0;
                   18025:        }
                   18026:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18027:        
                   18028:        // system file table
1.1.1.21  root     18029:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18030:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18031:        
1.1.1.19  root     18032:        // disk buffer header (from DOSBox)
                   18033:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18034:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18035:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18036:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18037:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18038:        
1.1       root     18039:        // fcb table
                   18040:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18041:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18042:        
1.1.1.41  root     18043:        // drive parameter block
1.1.1.42  root     18044:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18045:                // may be a floppy drive
1.1.1.44  root     18046:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18047:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18048:                cds->drive_attrib = 0x4000;     // physical drive
                   18049:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18050:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18051:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18052:                cds->bs_offset = 2;
                   18053:                
1.1.1.41  root     18054:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18055:                dpb->drive_num = i;
                   18056:                dpb->unit_num = i;
1.1.1.43  root     18057:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18058:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18059:        }
                   18060:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18061:                msdos_cds_update(i);
1.1.1.42  root     18062:                UINT16 seg, ofs;
                   18063:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18064:        }
                   18065:        
1.1.1.17  root     18066:        // nls stuff
                   18067:        msdos_nls_tables_init();
1.1       root     18068:        
                   18069:        // execute command
1.1.1.28  root     18070:        try {
1.1.1.52  root     18071:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18072:                        fatalerror("'%s' not found\n", argv[0]);
                   18073:                }
                   18074:        } catch(...) {
                   18075:                // we should not reach here :-(
                   18076:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18077:        }
                   18078:        retval = 0;
                   18079:        return(0);
                   18080: }
                   18081: 
                   18082: #define remove_std_file(path) { \
                   18083:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18084:        if(fd != -1) { \
                   18085:                _lseek(fd, 0, SEEK_END); \
                   18086:                int size = _tell(fd); \
                   18087:                _close(fd); \
                   18088:                if(size == 0) { \
                   18089:                        remove(path); \
                   18090:                } \
                   18091:        } \
                   18092: }
                   18093: 
                   18094: void msdos_finish()
                   18095: {
                   18096:        for(int i = 0; i < MAX_FILES; i++) {
                   18097:                if(file_handler[i].valid) {
                   18098:                        _close(i);
                   18099:                }
                   18100:        }
1.1.1.21  root     18101: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18102:        remove_std_file("stdaux.txt");
1.1.1.21  root     18103: #endif
1.1.1.30  root     18104: #ifdef SUPPORT_XMS
                   18105:        msdos_xms_finish();
                   18106: #endif
1.1       root     18107:        msdos_dbcs_table_finish();
                   18108: }
                   18109: 
                   18110: /* ----------------------------------------------------------------------------
                   18111:        PC/AT hardware emulation
                   18112: ---------------------------------------------------------------------------- */
                   18113: 
                   18114: void hardware_init()
                   18115: {
1.1.1.3   root     18116:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18117:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18118:        m_IF = 1;
1.1.1.3   root     18119: #if defined(HAS_I386)
1.1       root     18120:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18121:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18122: #endif
                   18123:        i386_set_a20_line(0);
1.1.1.14  root     18124:        
1.1.1.19  root     18125:        ems_init();
1.1.1.25  root     18126:        dma_init();
1.1       root     18127:        pic_init();
1.1.1.25  root     18128:        pio_init();
1.1.1.8   root     18129: #ifdef PIT_ALWAYS_RUNNING
                   18130:        pit_init();
                   18131: #else
1.1       root     18132:        pit_active = 0;
                   18133: #endif
1.1.1.25  root     18134:        sio_init();
1.1.1.8   root     18135:        cmos_init();
                   18136:        kbd_init();
1.1       root     18137: }
                   18138: 
1.1.1.10  root     18139: void hardware_finish()
                   18140: {
                   18141: #if defined(HAS_I386)
                   18142:        vtlb_free(m_vtlb);
                   18143: #endif
1.1.1.19  root     18144:        ems_finish();
1.1.1.37  root     18145:        pio_finish();
1.1.1.25  root     18146:        sio_finish();
1.1.1.10  root     18147: }
                   18148: 
1.1.1.28  root     18149: void hardware_release()
                   18150: {
                   18151:        // release hardware resources when this program will be terminated abnormally
                   18152: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18153:        if(fp_debug_log != NULL) {
                   18154:                fclose(fp_debug_log);
                   18155:                fp_debug_log = NULL;
1.1.1.28  root     18156:        }
                   18157: #endif
                   18158: #if defined(HAS_I386)
                   18159:        vtlb_free(m_vtlb);
                   18160: #endif
                   18161:        ems_release();
1.1.1.37  root     18162:        pio_release();
1.1.1.28  root     18163:        sio_release();
                   18164: }
                   18165: 
1.1       root     18166: void hardware_run()
                   18167: {
1.1.1.22  root     18168: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18169:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18170:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18171: #endif
1.1.1.51  root     18172: #ifdef USE_DEBUGGER
                   18173:        m_int_num = -1;
                   18174: #endif
1.1.1.54! root     18175:        while(!m_exit) {
1.1.1.50  root     18176:                hardware_run_cpu();
1.1       root     18177:        }
1.1.1.22  root     18178: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18179:        if(fp_debug_log != NULL) {
                   18180:                fclose(fp_debug_log);
                   18181:                fp_debug_log = NULL;
1.1.1.28  root     18182:        }
1.1.1.22  root     18183: #endif
1.1       root     18184: }
                   18185: 
1.1.1.50  root     18186: inline void hardware_run_cpu()
                   18187: {
                   18188: #if defined(HAS_I386)
                   18189:        CPU_EXECUTE_CALL(i386);
                   18190:        if(m_eip != m_prev_eip) {
                   18191:                idle_ops++;
                   18192:        }
                   18193: #else
                   18194:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18195:        if(m_pc != m_prevpc) {
                   18196:                idle_ops++;
                   18197:        }
                   18198: #endif
                   18199: #ifdef USE_DEBUGGER
                   18200:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18201:        if(m_int_num >= 0) {
                   18202:                unsigned num = (unsigned)m_int_num;
                   18203:                m_int_num = -1;
                   18204:                msdos_syscall(num);
                   18205:        }
                   18206: #endif
                   18207:        if(++update_ops == UPDATE_OPS) {
                   18208:                update_ops = 0;
                   18209:                hardware_update();
                   18210:        }
                   18211: }
                   18212: 
1.1       root     18213: void hardware_update()
                   18214: {
1.1.1.8   root     18215:        static UINT32 prev_time = 0;
                   18216:        UINT32 cur_time = timeGetTime();
                   18217:        
                   18218:        if(prev_time != cur_time) {
                   18219:                // update pit and raise irq0
                   18220: #ifndef PIT_ALWAYS_RUNNING
                   18221:                if(pit_active)
                   18222: #endif
                   18223:                {
                   18224:                        if(pit_run(0, cur_time)) {
                   18225:                                pic_req(0, 0, 1);
                   18226:                        }
                   18227:                        pit_run(1, cur_time);
                   18228:                        pit_run(2, cur_time);
                   18229:                }
1.1.1.24  root     18230:                
1.1.1.25  root     18231:                // update sio and raise irq4/3
1.1.1.29  root     18232:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18233:                        sio_update(c);
                   18234:                }
                   18235:                
1.1.1.24  root     18236:                // update keyboard and mouse
1.1.1.14  root     18237:                static UINT32 prev_tick = 0;
                   18238:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     18239:                
1.1.1.14  root     18240:                if(prev_tick != cur_tick) {
                   18241:                        // update keyboard flags
                   18242:                        UINT8 state;
1.1.1.24  root     18243:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   18244:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   18245:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   18246:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   18247:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   18248:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   18249:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   18250:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     18251:                        mem[0x417] = state;
                   18252:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   18253:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   18254:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   18255:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     18256: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   18257: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     18258:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   18259:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   18260:                        mem[0x418] = state;
                   18261:                        
1.1.1.24  root     18262:                        // update console input if needed
1.1.1.34  root     18263:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     18264:                                update_console_input();
                   18265:                        }
                   18266:                        
                   18267:                        // raise irq1 if key is pressed/released
                   18268:                        if(key_changed) {
1.1.1.8   root     18269:                                pic_req(0, 1, 1);
1.1.1.24  root     18270:                                key_changed = false;
                   18271:                        }
                   18272:                        
                   18273:                        // raise irq12 if mouse status is changed
1.1.1.54! root     18274:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw) {
        !          18275:                                mouse.status_irq = 0; // ???
        !          18276:                                mouse.status_irq_alt = 0; // ???
        !          18277:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
        !          18278:                                mouse.status = 0;
        !          18279:                                pic_req(1, 4, 1);
        !          18280:                        } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     18281:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   18282:                                mouse.status_irq_alt = 0; // ???
1.1.1.54! root     18283:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     18284:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     18285:                                pic_req(1, 4, 1);
                   18286:                        } else {
                   18287:                                for(int i = 0; i < 8; i++) {
                   18288:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   18289:                                                mouse.status_irq = 0; // ???
                   18290:                                                mouse.status_irq_alt = 0;
1.1.1.54! root     18291:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     18292:                                                for(int j = 0; j < 8; j++) {
                   18293:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   18294:                                                                mouse.status_irq_alt |=  (1 << j);
                   18295:                                                                mouse.status_alt     &= ~(1 << j);
                   18296:                                                        }
                   18297:                                                }
                   18298:                                                pic_req(1, 4, 1);
                   18299:                                                break;
                   18300:                                        }
                   18301:                                }
1.1.1.8   root     18302:                        }
1.1.1.24  root     18303:                        
1.1.1.14  root     18304:                        prev_tick = cur_tick;
1.1.1.8   root     18305:                }
1.1.1.24  root     18306:                
1.1.1.19  root     18307:                // update daily timer counter
                   18308:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     18309:                
1.1.1.8   root     18310:                prev_time = cur_time;
1.1       root     18311:        }
                   18312: }
                   18313: 
1.1.1.19  root     18314: // ems
                   18315: 
                   18316: void ems_init()
                   18317: {
                   18318:        memset(ems_handles, 0, sizeof(ems_handles));
                   18319:        memset(ems_pages, 0, sizeof(ems_pages));
                   18320:        free_ems_pages = MAX_EMS_PAGES;
                   18321: }
                   18322: 
                   18323: void ems_finish()
                   18324: {
1.1.1.28  root     18325:        ems_release();
                   18326: }
                   18327: 
                   18328: void ems_release()
                   18329: {
1.1.1.31  root     18330:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     18331:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     18332:                        free(ems_handles[i].buffer);
                   18333:                        ems_handles[i].buffer = NULL;
                   18334:                }
                   18335:        }
                   18336: }
                   18337: 
                   18338: void ems_allocate_pages(int handle, int pages)
                   18339: {
                   18340:        if(pages > 0) {
                   18341:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18342:        } else {
                   18343:                ems_handles[handle].buffer = NULL;
                   18344:        }
                   18345:        ems_handles[handle].pages = pages;
                   18346:        ems_handles[handle].allocated = true;
                   18347:        free_ems_pages -= pages;
                   18348: }
                   18349: 
                   18350: void ems_reallocate_pages(int handle, int pages)
                   18351: {
                   18352:        if(ems_handles[handle].allocated) {
                   18353:                if(ems_handles[handle].pages != pages) {
                   18354:                        UINT8 *new_buffer = NULL;
                   18355:                        
                   18356:                        if(pages > 0) {
                   18357:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18358:                        }
1.1.1.32  root     18359:                        if(ems_handles[handle].buffer != NULL) {
                   18360:                                if(new_buffer != NULL) {
1.1.1.19  root     18361:                                        if(pages > ems_handles[handle].pages) {
                   18362:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   18363:                                        } else {
                   18364:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   18365:                                        }
                   18366:                                }
                   18367:                                free(ems_handles[handle].buffer);
                   18368:                                ems_handles[handle].buffer = NULL;
                   18369:                        }
                   18370:                        free_ems_pages += ems_handles[handle].pages;
                   18371:                        
                   18372:                        ems_handles[handle].buffer = new_buffer;
                   18373:                        ems_handles[handle].pages = pages;
                   18374:                        free_ems_pages -= pages;
                   18375:                }
                   18376:        } else {
                   18377:                ems_allocate_pages(handle, pages);
                   18378:        }
                   18379: }
                   18380: 
                   18381: void ems_release_pages(int handle)
                   18382: {
                   18383:        if(ems_handles[handle].allocated) {
1.1.1.32  root     18384:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     18385:                        free(ems_handles[handle].buffer);
                   18386:                        ems_handles[handle].buffer = NULL;
                   18387:                }
                   18388:                free_ems_pages += ems_handles[handle].pages;
                   18389:                ems_handles[handle].allocated = false;
                   18390:        }
                   18391: }
                   18392: 
                   18393: void ems_map_page(int physical, int handle, int logical)
                   18394: {
                   18395:        if(ems_pages[physical].mapped) {
                   18396:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   18397:                        return;
                   18398:                }
                   18399:                ems_unmap_page(physical);
                   18400:        }
1.1.1.32  root     18401:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18402:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   18403:        }
                   18404:        ems_pages[physical].handle = handle;
                   18405:        ems_pages[physical].page = logical;
                   18406:        ems_pages[physical].mapped = true;
                   18407: }
                   18408: 
                   18409: void ems_unmap_page(int physical)
                   18410: {
                   18411:        if(ems_pages[physical].mapped) {
                   18412:                int handle = ems_pages[physical].handle;
                   18413:                int logical = ems_pages[physical].page;
                   18414:                
1.1.1.32  root     18415:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18416:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   18417:                }
                   18418:                ems_pages[physical].mapped = false;
                   18419:        }
                   18420: }
                   18421: 
1.1.1.25  root     18422: // dma
1.1       root     18423: 
1.1.1.25  root     18424: void dma_init()
1.1       root     18425: {
1.1.1.26  root     18426:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     18427:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     18428: //             for(int ch = 0; ch < 4; ch++) {
                   18429: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   18430: //             }
1.1.1.25  root     18431:                dma_reset(c);
                   18432:        }
1.1       root     18433: }
                   18434: 
1.1.1.25  root     18435: void dma_reset(int c)
1.1       root     18436: {
1.1.1.25  root     18437:        dma[c].low_high = false;
                   18438:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   18439:        dma[c].mask = 0xff;
                   18440: }
                   18441: 
                   18442: void dma_write(int c, UINT32 addr, UINT8 data)
                   18443: {
                   18444:        int ch = (addr >> 1) & 3;
                   18445:        UINT8 bit = 1 << (data & 3);
                   18446:        
                   18447:        switch(addr & 0x0f) {
                   18448:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18449:                if(dma[c].low_high) {
                   18450:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     18451:                } else {
1.1.1.25  root     18452:                        dma[c].ch[ch].bareg.b.l = data;
                   18453:                }
                   18454:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18455:                dma[c].low_high = !dma[c].low_high;
                   18456:                break;
                   18457:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18458:                if(dma[c].low_high) {
                   18459:                        dma[c].ch[ch].bcreg.b.h = data;
                   18460:                } else {
                   18461:                        dma[c].ch[ch].bcreg.b.l = data;
                   18462:                }
                   18463:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18464:                dma[c].low_high = !dma[c].low_high;
                   18465:                break;
                   18466:        case 0x08:
                   18467:                // command register
                   18468:                dma[c].cmd = data;
                   18469:                break;
                   18470:        case 0x09:
                   18471:                // dma[c].request register
                   18472:                if(data & 4) {
                   18473:                        if(!(dma[c].req & bit)) {
                   18474:                                dma[c].req |= bit;
                   18475: //                             dma_run(c, ch);
                   18476:                        }
                   18477:                } else {
                   18478:                        dma[c].req &= ~bit;
                   18479:                }
                   18480:                break;
                   18481:        case 0x0a:
                   18482:                // single mask register
                   18483:                if(data & 4) {
                   18484:                        dma[c].mask |= bit;
                   18485:                } else {
                   18486:                        dma[c].mask &= ~bit;
                   18487:                }
                   18488:                break;
                   18489:        case 0x0b:
                   18490:                // mode register
                   18491:                dma[c].ch[data & 3].mode = data;
                   18492:                break;
                   18493:        case 0x0c:
                   18494:                dma[c].low_high = false;
                   18495:                break;
                   18496:        case 0x0d:
                   18497:                // clear master
                   18498:                dma_reset(c);
                   18499:                break;
                   18500:        case 0x0e:
                   18501:                // clear mask register
                   18502:                dma[c].mask = 0;
                   18503:                break;
                   18504:        case 0x0f:
                   18505:                // all mask register
                   18506:                dma[c].mask = data & 0x0f;
                   18507:                break;
                   18508:        }
                   18509: }
                   18510: 
                   18511: UINT8 dma_read(int c, UINT32 addr)
                   18512: {
                   18513:        int ch = (addr >> 1) & 3;
                   18514:        UINT8 val = 0xff;
                   18515:        
                   18516:        switch(addr & 0x0f) {
                   18517:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18518:                if(dma[c].low_high) {
                   18519:                        val = dma[c].ch[ch].areg.b.h;
                   18520:                } else {
                   18521:                        val = dma[c].ch[ch].areg.b.l;
                   18522:                }
                   18523:                dma[c].low_high = !dma[c].low_high;
                   18524:                return(val);
                   18525:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18526:                if(dma[c].low_high) {
                   18527:                        val = dma[c].ch[ch].creg.b.h;
                   18528:                } else {
                   18529:                        val = dma[c].ch[ch].creg.b.l;
                   18530:                }
                   18531:                dma[c].low_high = !dma[c].low_high;
                   18532:                return(val);
                   18533:        case 0x08:
                   18534:                // status register
                   18535:                val = (dma[c].req << 4) | dma[c].tc;
                   18536:                dma[c].tc = 0;
                   18537:                return(val);
                   18538:        case 0x0d:
1.1.1.26  root     18539:                // temporary register (intel 82374 does not support)
1.1.1.25  root     18540:                return(dma[c].tmp & 0xff);
1.1.1.26  root     18541:        case 0x0f:
                   18542:                // mask register (intel 82374 does support)
                   18543:                return(dma[c].mask);
1.1.1.25  root     18544:        }
                   18545:        return(0xff);
                   18546: }
                   18547: 
                   18548: void dma_page_write(int c, int ch, UINT8 data)
                   18549: {
                   18550:        dma[c].ch[ch].pagereg = data;
                   18551: }
                   18552: 
                   18553: UINT8 dma_page_read(int c, int ch)
                   18554: {
                   18555:        return(dma[c].ch[ch].pagereg);
                   18556: }
                   18557: 
                   18558: void dma_run(int c, int ch)
                   18559: {
                   18560:        UINT8 bit = 1 << ch;
                   18561:        
                   18562:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   18563:                // execute dma
                   18564:                while(dma[c].req & bit) {
                   18565:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   18566:                                // memory -> memory
                   18567:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   18568:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   18569:                                
                   18570:                                if(c == 0) {
                   18571:                                        dma[c].tmp = read_byte(saddr);
                   18572:                                        write_byte(daddr, dma[c].tmp);
                   18573:                                } else {
                   18574:                                        dma[c].tmp = read_word(saddr << 1);
                   18575:                                        write_word(daddr << 1, dma[c].tmp);
                   18576:                                }
                   18577:                                if(!(dma[c].cmd & 0x02)) {
                   18578:                                        if(dma[c].ch[0].mode & 0x20) {
                   18579:                                                dma[c].ch[0].areg.w--;
                   18580:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   18581:                                                        dma[c].ch[0].pagereg--;
                   18582:                                                }
                   18583:                                        } else {
                   18584:                                                dma[c].ch[0].areg.w++;
                   18585:                                                if(dma[c].ch[0].areg.w == 0) {
                   18586:                                                        dma[c].ch[0].pagereg++;
                   18587:                                                }
                   18588:                                        }
                   18589:                                }
                   18590:                                if(dma[c].ch[1].mode & 0x20) {
                   18591:                                        dma[c].ch[1].areg.w--;
                   18592:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   18593:                                                dma[c].ch[1].pagereg--;
                   18594:                                        }
                   18595:                                } else {
                   18596:                                        dma[c].ch[1].areg.w++;
                   18597:                                        if(dma[c].ch[1].areg.w == 0) {
                   18598:                                                dma[c].ch[1].pagereg++;
                   18599:                                        }
                   18600:                                }
                   18601:                                
                   18602:                                // check dma condition
                   18603:                                if(dma[c].ch[0].creg.w-- == 0) {
                   18604:                                        if(dma[c].ch[0].mode & 0x10) {
                   18605:                                                // self initialize
                   18606:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   18607:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   18608:                                        } else {
                   18609: //                                             dma[c].mask |= bit;
                   18610:                                        }
                   18611:                                }
                   18612:                                if(dma[c].ch[1].creg.w-- == 0) {
                   18613:                                        // terminal count
                   18614:                                        if(dma[c].ch[1].mode & 0x10) {
                   18615:                                                // self initialize
                   18616:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   18617:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   18618:                                        } else {
                   18619:                                                dma[c].mask |= bit;
                   18620:                                        }
                   18621:                                        dma[c].req &= ~bit;
                   18622:                                        dma[c].tc |= bit;
                   18623:                                }
                   18624:                        } else {
                   18625:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   18626:                                
                   18627:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   18628:                                        // verify
                   18629:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   18630:                                        // io -> memory
                   18631:                                        if(c == 0) {
                   18632:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   18633:                                                write_byte(addr, dma[c].tmp);
                   18634:                                        } else {
                   18635:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   18636:                                                write_word(addr << 1, dma[c].tmp);
                   18637:                                        }
                   18638:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   18639:                                        // memory -> io
                   18640:                                        if(c == 0) {
                   18641:                                                dma[c].tmp = read_byte(addr);
                   18642:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   18643:                                        } else {
                   18644:                                                dma[c].tmp = read_word(addr << 1);
                   18645:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   18646:                                        }
                   18647:                                }
                   18648:                                if(dma[c].ch[ch].mode & 0x20) {
                   18649:                                        dma[c].ch[ch].areg.w--;
                   18650:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   18651:                                                dma[c].ch[ch].pagereg--;
                   18652:                                        }
                   18653:                                } else {
                   18654:                                        dma[c].ch[ch].areg.w++;
                   18655:                                        if(dma[c].ch[ch].areg.w == 0) {
                   18656:                                                dma[c].ch[ch].pagereg++;
                   18657:                                        }
                   18658:                                }
                   18659:                                
                   18660:                                // check dma condition
                   18661:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   18662:                                        // terminal count
                   18663:                                        if(dma[c].ch[ch].mode & 0x10) {
                   18664:                                                // self initialize
                   18665:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18666:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18667:                                        } else {
                   18668:                                                dma[c].mask |= bit;
                   18669:                                        }
                   18670:                                        dma[c].req &= ~bit;
                   18671:                                        dma[c].tc |= bit;
                   18672:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   18673:                                        // single mode
                   18674:                                        break;
                   18675:                                }
                   18676:                        }
                   18677:                }
                   18678:        }
                   18679: }
                   18680: 
                   18681: // pic
                   18682: 
                   18683: void pic_init()
                   18684: {
                   18685:        memset(pic, 0, sizeof(pic));
                   18686:        pic[0].imr = pic[1].imr = 0xff;
                   18687:        
                   18688:        // from bochs bios
                   18689:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   18690:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   18691:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   18692:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   18693:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   18694:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   18695:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   18696:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   18697:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   18698: }
                   18699: 
                   18700: void pic_write(int c, UINT32 addr, UINT8 data)
                   18701: {
                   18702:        if(addr & 1) {
                   18703:                if(pic[c].icw2_r) {
                   18704:                        // icw2
                   18705:                        pic[c].icw2 = data;
                   18706:                        pic[c].icw2_r = 0;
                   18707:                } else if(pic[c].icw3_r) {
                   18708:                        // icw3
                   18709:                        pic[c].icw3 = data;
                   18710:                        pic[c].icw3_r = 0;
                   18711:                } else if(pic[c].icw4_r) {
                   18712:                        // icw4
                   18713:                        pic[c].icw4 = data;
                   18714:                        pic[c].icw4_r = 0;
                   18715:                } else {
                   18716:                        // ocw1
1.1       root     18717:                        pic[c].imr = data;
                   18718:                }
                   18719:        } else {
                   18720:                if(data & 0x10) {
                   18721:                        // icw1
                   18722:                        pic[c].icw1 = data;
                   18723:                        pic[c].icw2_r = 1;
                   18724:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   18725:                        pic[c].icw4_r = data & 1;
                   18726:                        pic[c].irr = 0;
                   18727:                        pic[c].isr = 0;
                   18728:                        pic[c].imr = 0;
                   18729:                        pic[c].prio = 0;
                   18730:                        if(!(pic[c].icw1 & 1)) {
                   18731:                                pic[c].icw4 = 0;
                   18732:                        }
                   18733:                        pic[c].ocw3 = 0;
                   18734:                } else if(data & 8) {
                   18735:                        // ocw3
                   18736:                        if(!(data & 2)) {
                   18737:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   18738:                        }
                   18739:                        if(!(data & 0x40)) {
                   18740:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   18741:                        }
                   18742:                        pic[c].ocw3 = data;
                   18743:                } else {
                   18744:                        // ocw2
                   18745:                        int level = 0;
                   18746:                        if(data & 0x40) {
                   18747:                                level = data & 7;
                   18748:                        } else {
                   18749:                                if(!pic[c].isr) {
                   18750:                                        return;
                   18751:                                }
                   18752:                                level = pic[c].prio;
                   18753:                                while(!(pic[c].isr & (1 << level))) {
                   18754:                                        level = (level + 1) & 7;
                   18755:                                }
                   18756:                        }
                   18757:                        if(data & 0x80) {
                   18758:                                pic[c].prio = (level + 1) & 7;
                   18759:                        }
                   18760:                        if(data & 0x20) {
                   18761:                                pic[c].isr &= ~(1 << level);
                   18762:                        }
                   18763:                }
                   18764:        }
                   18765:        pic_update();
                   18766: }
                   18767: 
                   18768: UINT8 pic_read(int c, UINT32 addr)
                   18769: {
                   18770:        if(addr & 1) {
                   18771:                return(pic[c].imr);
                   18772:        } else {
                   18773:                // polling mode is not supported...
                   18774:                //if(pic[c].ocw3 & 4) {
                   18775:                //      return ???;
                   18776:                //}
                   18777:                if(pic[c].ocw3 & 1) {
                   18778:                        return(pic[c].isr);
                   18779:                } else {
                   18780:                        return(pic[c].irr);
                   18781:                }
                   18782:        }
                   18783: }
                   18784: 
                   18785: void pic_req(int c, int level, int signal)
                   18786: {
                   18787:        if(signal) {
                   18788:                pic[c].irr |= (1 << level);
                   18789:        } else {
                   18790:                pic[c].irr &= ~(1 << level);
                   18791:        }
                   18792:        pic_update();
                   18793: }
                   18794: 
                   18795: int pic_ack()
                   18796: {
                   18797:        // ack (INTA=L)
                   18798:        pic[pic_req_chip].isr |= pic_req_bit;
                   18799:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   18800:        if(pic_req_chip > 0) {
                   18801:                // update isr and irr of master
                   18802:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   18803:                pic[pic_req_chip - 1].isr |= slave;
                   18804:                pic[pic_req_chip - 1].irr &= ~slave;
                   18805:        }
                   18806:        //if(pic[pic_req_chip].icw4 & 1) {
                   18807:                // 8086 mode
                   18808:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   18809:        //} else {
                   18810:        //      // 8080 mode
                   18811:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   18812:        //      if(pic[pic_req_chip].icw1 & 4) {
                   18813:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   18814:        //      } else {
                   18815:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   18816:        //      }
                   18817:        //      vector = 0xcd | (addr << 8);
                   18818:        //}
                   18819:        if(pic[pic_req_chip].icw4 & 2) {
                   18820:                // auto eoi
                   18821:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   18822:        }
                   18823:        return(vector);
                   18824: }
                   18825: 
                   18826: void pic_update()
                   18827: {
                   18828:        for(int c = 0; c < 2; c++) {
                   18829:                UINT8 irr = pic[c].irr;
                   18830:                if(c + 1 < 2) {
                   18831:                        // this is master
                   18832:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   18833:                                // request from slave
                   18834:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   18835:                        }
                   18836:                }
                   18837:                irr &= (~pic[c].imr);
                   18838:                if(!irr) {
                   18839:                        break;
                   18840:                }
                   18841:                if(!(pic[c].ocw3 & 0x20)) {
                   18842:                        irr |= pic[c].isr;
                   18843:                }
                   18844:                int level = pic[c].prio;
                   18845:                UINT8 bit = 1 << level;
                   18846:                while(!(irr & bit)) {
                   18847:                        level = (level + 1) & 7;
                   18848:                        bit = 1 << level;
                   18849:                }
                   18850:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   18851:                        // check slave
                   18852:                        continue;
                   18853:                }
                   18854:                if(pic[c].isr & bit) {
                   18855:                        break;
                   18856:                }
                   18857:                // interrupt request
                   18858:                pic_req_chip = c;
                   18859:                pic_req_level = level;
                   18860:                pic_req_bit = bit;
1.1.1.3   root     18861:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     18862:                return;
                   18863:        }
1.1.1.3   root     18864:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     18865: }
1.1       root     18866: 
1.1.1.25  root     18867: // pio
                   18868: 
                   18869: void pio_init()
                   18870: {
1.1.1.38  root     18871: //     bool conv_mode = (GetConsoleCP() == 932);
                   18872:        
1.1.1.26  root     18873:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     18874:        
1.1.1.25  root     18875:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     18876:                pio[c].stat = 0xdf;
1.1.1.25  root     18877:                pio[c].ctrl = 0x0c;
1.1.1.38  root     18878: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     18879:        }
                   18880: }
                   18881: 
1.1.1.37  root     18882: void pio_finish()
                   18883: {
                   18884:        pio_release();
                   18885: }
                   18886: 
                   18887: void pio_release()
                   18888: {
                   18889:        for(int c = 0; c < 2; c++) {
                   18890:                if(pio[c].fp != NULL) {
1.1.1.38  root     18891:                        if(pio[c].jis_mode) {
                   18892:                                fputc(0x1c, pio[c].fp);
                   18893:                                fputc(0x2e, pio[c].fp);
                   18894:                        }
1.1.1.37  root     18895:                        fclose(pio[c].fp);
                   18896:                        pio[c].fp = NULL;
                   18897:                }
                   18898:        }
                   18899: }
                   18900: 
1.1.1.25  root     18901: void pio_write(int c, UINT32 addr, UINT8 data)
                   18902: {
                   18903:        switch(addr & 3) {
                   18904:        case 0:
                   18905:                pio[c].data = data;
                   18906:                break;
                   18907:        case 2:
1.1.1.37  root     18908:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   18909:                        // strobe H -> L
                   18910:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   18911:                                // auto feed
                   18912:                                printer_out(c, 0x0d);
                   18913:                                printer_out(c, 0x0a);
                   18914:                        } else {
                   18915:                                printer_out(c, pio[c].data);
                   18916:                        }
                   18917:                        pio[c].stat &= ~0x40; // set ack
                   18918:                }
1.1.1.25  root     18919:                pio[c].ctrl = data;
                   18920:                break;
                   18921:        }
                   18922: }
                   18923: 
                   18924: UINT8 pio_read(int c, UINT32 addr)
                   18925: {
                   18926:        switch(addr & 3) {
                   18927:        case 0:
1.1.1.37  root     18928:                if(pio[c].ctrl & 0x20) {
                   18929:                        // input mode
                   18930:                        return(0xff);
                   18931:                }
1.1.1.25  root     18932:                return(pio[c].data);
                   18933:        case 1:
1.1.1.37  root     18934:                {
                   18935:                        UINT8 stat = pio[c].stat;
                   18936:                        pio[c].stat |= 0x40; // clear ack
                   18937:                        return(stat);
                   18938:                }
1.1.1.25  root     18939:        case 2:
                   18940:                return(pio[c].ctrl);
                   18941:        }
                   18942:        return(0xff);
                   18943: }
                   18944: 
1.1.1.37  root     18945: void printer_out(int c, UINT8 data)
                   18946: {
                   18947:        SYSTEMTIME time;
1.1.1.38  root     18948:        bool jis_mode = false;
1.1.1.37  root     18949:        
                   18950:        GetLocalTime(&time);
                   18951:        
                   18952:        if(pio[c].fp != NULL) {
                   18953:                // if at least 1000ms passed from last written, close the current file
                   18954:                FILETIME ftime1;
                   18955:                FILETIME ftime2;
                   18956:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   18957:                SystemTimeToFileTime(&time, &ftime2);
                   18958:                INT64 *time1 = (INT64 *)&ftime1;
                   18959:                INT64 *time2 = (INT64 *)&ftime2;
                   18960:                INT64 msec = (*time2 - *time1) / 10000;
                   18961:                
                   18962:                if(msec >= 1000) {
1.1.1.38  root     18963:                        if(pio[c].jis_mode) {
                   18964:                                fputc(0x1c, pio[c].fp);
                   18965:                                fputc(0x2e, pio[c].fp);
                   18966:                                jis_mode = true;
                   18967:                        }
1.1.1.37  root     18968:                        fclose(pio[c].fp);
                   18969:                        pio[c].fp = NULL;
                   18970:                }
                   18971:        }
                   18972:        if(pio[c].fp == NULL) {
                   18973:                // create a new file in the temp folder
                   18974:                char file_name[MAX_PATH];
                   18975:                
                   18976:                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);
                   18977:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   18978:                        strcat(pio[c].path, file_name);
                   18979:                } else {
                   18980:                        strcpy(pio[c].path, file_name);
                   18981:                }
1.1.1.38  root     18982:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     18983:        }
                   18984:        if(pio[c].fp != NULL) {
1.1.1.38  root     18985:                if(jis_mode) {
                   18986:                        fputc(0x1c, pio[c].fp);
                   18987:                        fputc(0x26, pio[c].fp);
                   18988:                }
1.1.1.37  root     18989:                fputc(data, pio[c].fp);
1.1.1.38  root     18990:                
                   18991:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   18992:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   18993:                        UINT8 buffer[4];
                   18994:                        fseek(pio[c].fp, 0, SEEK_SET);
                   18995:                        fread(buffer, 4, 1, pio[c].fp);
                   18996:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   18997:                                fclose(pio[c].fp);
                   18998:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   18999:                        }
                   19000:                }
1.1.1.37  root     19001:                pio[c].time = time;
                   19002:        }
                   19003: }
                   19004: 
1.1       root     19005: // pit
                   19006: 
1.1.1.22  root     19007: #define PIT_FREQ 1193182ULL
1.1       root     19008: #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)
                   19009: 
                   19010: void pit_init()
                   19011: {
1.1.1.8   root     19012:        memset(pit, 0, sizeof(pit));
1.1       root     19013:        for(int ch = 0; ch < 3; ch++) {
                   19014:                pit[ch].count = 0x10000;
                   19015:                pit[ch].ctrl_reg = 0x34;
                   19016:                pit[ch].mode = 3;
                   19017:        }
                   19018:        
                   19019:        // from bochs bios
                   19020:        pit_write(3, 0x34);
                   19021:        pit_write(0, 0x00);
                   19022:        pit_write(0, 0x00);
                   19023: }
                   19024: 
                   19025: void pit_write(int ch, UINT8 val)
                   19026: {
1.1.1.8   root     19027: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19028:        if(!pit_active) {
                   19029:                pit_active = 1;
                   19030:                pit_init();
                   19031:        }
1.1.1.8   root     19032: #endif
1.1       root     19033:        switch(ch) {
                   19034:        case 0:
                   19035:        case 1:
                   19036:        case 2:
                   19037:                // write count register
                   19038:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19039:                        if(pit[ch].ctrl_reg & 0x10) {
                   19040:                                pit[ch].low_write = 1;
                   19041:                        }
                   19042:                        if(pit[ch].ctrl_reg & 0x20) {
                   19043:                                pit[ch].high_write = 1;
                   19044:                        }
                   19045:                }
                   19046:                if(pit[ch].low_write) {
                   19047:                        pit[ch].count_reg = val;
                   19048:                        pit[ch].low_write = 0;
                   19049:                } else if(pit[ch].high_write) {
                   19050:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19051:                                pit[ch].count_reg = val << 8;
                   19052:                        } else {
                   19053:                                pit[ch].count_reg |= val << 8;
                   19054:                        }
                   19055:                        pit[ch].high_write = 0;
                   19056:                }
                   19057:                // start count
1.1.1.8   root     19058:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19059:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19060:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19061:                                pit[ch].prev_time = timeGetTime();
                   19062:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19063:                        }
                   19064:                }
                   19065:                break;
                   19066:        case 3: // ctrl reg
                   19067:                if((val & 0xc0) == 0xc0) {
                   19068:                        // i8254 read-back command
                   19069:                        for(ch = 0; ch < 3; ch++) {
                   19070:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19071:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19072:                                        pit[ch].status_latched = 1;
                   19073:                                }
                   19074:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19075:                                        pit_latch_count(ch);
                   19076:                                }
                   19077:                        }
                   19078:                        break;
                   19079:                }
                   19080:                ch = (val >> 6) & 3;
                   19081:                if(val & 0x30) {
1.1.1.35  root     19082:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19083:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19084:                        pit[ch].count_latched = 0;
                   19085:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19086:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19087:                        pit[ch].ctrl_reg = val;
                   19088:                        // stop count
1.1.1.8   root     19089:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19090:                        pit[ch].count_reg = 0;
                   19091:                } else if(!pit[ch].count_latched) {
                   19092:                        pit_latch_count(ch);
                   19093:                }
                   19094:                break;
                   19095:        }
                   19096: }
                   19097: 
                   19098: UINT8 pit_read(int ch)
                   19099: {
1.1.1.8   root     19100: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19101:        if(!pit_active) {
                   19102:                pit_active = 1;
                   19103:                pit_init();
                   19104:        }
1.1.1.8   root     19105: #endif
1.1       root     19106:        switch(ch) {
                   19107:        case 0:
                   19108:        case 1:
                   19109:        case 2:
                   19110:                if(pit[ch].status_latched) {
                   19111:                        pit[ch].status_latched = 0;
                   19112:                        return(pit[ch].status);
                   19113:                }
                   19114:                // if not latched, through current count
                   19115:                if(!pit[ch].count_latched) {
                   19116:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19117:                                pit_latch_count(ch);
                   19118:                        }
                   19119:                }
                   19120:                // return latched count
                   19121:                if(pit[ch].low_read) {
                   19122:                        pit[ch].low_read = 0;
                   19123:                        if(!pit[ch].high_read) {
                   19124:                                pit[ch].count_latched = 0;
                   19125:                        }
                   19126:                        return(pit[ch].latch & 0xff);
                   19127:                } else if(pit[ch].high_read) {
                   19128:                        pit[ch].high_read = 0;
                   19129:                        pit[ch].count_latched = 0;
                   19130:                        return((pit[ch].latch >> 8) & 0xff);
                   19131:                }
                   19132:        }
                   19133:        return(0xff);
                   19134: }
                   19135: 
1.1.1.8   root     19136: int pit_run(int ch, UINT32 cur_time)
1.1       root     19137: {
1.1.1.8   root     19138:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19139:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19140:                pit[ch].prev_time = pit[ch].expired_time;
                   19141:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19142:                if(cur_time >= pit[ch].expired_time) {
                   19143:                        pit[ch].prev_time = cur_time;
                   19144:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19145:                }
1.1.1.8   root     19146:                return(1);
1.1       root     19147:        }
1.1.1.8   root     19148:        return(0);
1.1       root     19149: }
                   19150: 
                   19151: void pit_latch_count(int ch)
                   19152: {
1.1.1.8   root     19153:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19154:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19155:                pit_run(ch, cur_time);
                   19156:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19157:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19158:                
                   19159:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19160:                        // decrement counter in 1msec period
                   19161:                        if(pit[ch].next_latch == 0) {
                   19162:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   19163:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19164:                        }
                   19165:                        if(pit[ch].latch > pit[ch].next_latch) {
                   19166:                                pit[ch].latch--;
                   19167:                        }
                   19168:                } else {
                   19169:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   19170:                        pit[ch].next_latch = 0;
                   19171:                }
1.1.1.8   root     19172:        } else {
                   19173:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     19174:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     19175:        }
                   19176:        pit[ch].count_latched = 1;
                   19177:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   19178:                // lower byte
                   19179:                pit[ch].low_read = 1;
                   19180:                pit[ch].high_read = 0;
                   19181:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19182:                // upper byte
                   19183:                pit[ch].low_read = 0;
                   19184:                pit[ch].high_read = 1;
                   19185:        } else {
                   19186:                // lower -> upper
1.1.1.14  root     19187:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     19188:        }
                   19189: }
                   19190: 
1.1.1.8   root     19191: int pit_get_expired_time(int ch)
1.1       root     19192: {
1.1.1.22  root     19193:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   19194:        UINT64 val = pit[ch].accum >> 10;
                   19195:        pit[ch].accum -= val << 10;
                   19196:        return((val != 0) ? val : 1);
1.1.1.8   root     19197: }
                   19198: 
1.1.1.25  root     19199: // sio
                   19200: 
                   19201: void sio_init()
                   19202: {
1.1.1.26  root     19203:        memset(sio, 0, sizeof(sio));
                   19204:        memset(sio_mt, 0, sizeof(sio_mt));
                   19205:        
1.1.1.29  root     19206:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19207:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19208:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19209:                
                   19210:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   19211:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     19212:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   19213:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     19214:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   19215:                sio[c].irq_identify = 0x01;     // no pending irq
                   19216:                
                   19217:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   19218:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   19219:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   19220:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   19221:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   19222:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   19223:                
1.1.1.26  root     19224:                if(sio_port_number[c] != 0) {
1.1.1.25  root     19225:                        sio[c].channel = c;
                   19226:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   19227:                }
                   19228:        }
                   19229: }
                   19230: 
                   19231: void sio_finish()
                   19232: {
1.1.1.29  root     19233:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19234:                if(sio_mt[c].hThread != NULL) {
                   19235:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   19236:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     19237:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     19238:                }
                   19239:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   19240:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   19241:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   19242:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   19243:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   19244:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     19245:        }
                   19246:        sio_release();
                   19247: }
                   19248: 
                   19249: void sio_release()
                   19250: {
1.1.1.29  root     19251:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     19252:                // sio_thread() may access the resources :-(
1.1.1.32  root     19253:                bool running = (sio_mt[c].hThread != NULL);
                   19254:                
                   19255:                if(running) {
                   19256:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   19257:                }
                   19258:                if(sio[c].send_buffer != NULL) {
                   19259:                        sio[c].send_buffer->release();
                   19260:                        delete sio[c].send_buffer;
                   19261:                        sio[c].send_buffer = NULL;
                   19262:                }
                   19263:                if(running) {
                   19264:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19265:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   19266:                }
                   19267:                if(sio[c].recv_buffer != NULL) {
                   19268:                        sio[c].recv_buffer->release();
                   19269:                        delete sio[c].recv_buffer;
                   19270:                        sio[c].recv_buffer = NULL;
                   19271:                }
                   19272:                if(running) {
                   19273:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     19274:                }
1.1.1.25  root     19275:        }
                   19276: }
                   19277: 
                   19278: void sio_write(int c, UINT32 addr, UINT8 data)
                   19279: {
                   19280:        switch(addr & 7) {
                   19281:        case 0:
                   19282:                if(sio[c].selector & 0x80) {
                   19283:                        if(sio[c].divisor.b.l != data) {
                   19284:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19285:                                sio[c].divisor.b.l = data;
                   19286:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19287:                        }
                   19288:                } else {
                   19289:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19290:                        if(sio[c].send_buffer != NULL) {
                   19291:                                sio[c].send_buffer->write(data);
                   19292:                        }
1.1.1.25  root     19293:                        // transmitter holding/shift registers are not empty
                   19294:                        sio[c].line_stat_buf &= ~0x60;
                   19295:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19296:                        
                   19297:                        if(sio[c].irq_enable & 0x02) {
                   19298:                                sio_update_irq(c);
                   19299:                        }
                   19300:                }
                   19301:                break;
                   19302:        case 1:
                   19303:                if(sio[c].selector & 0x80) {
                   19304:                        if(sio[c].divisor.b.h != data) {
                   19305:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19306:                                sio[c].divisor.b.h = data;
                   19307:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19308:                        }
                   19309:                } else {
                   19310:                        if(sio[c].irq_enable != data) {
                   19311:                                sio[c].irq_enable = data;
                   19312:                                sio_update_irq(c);
                   19313:                        }
                   19314:                }
                   19315:                break;
                   19316:        case 3:
                   19317:                {
                   19318:                        UINT8 line_ctrl = data & 0x3f;
                   19319:                        bool set_brk = ((data & 0x40) != 0);
                   19320:                        
                   19321:                        if(sio[c].line_ctrl != line_ctrl) {
                   19322:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19323:                                sio[c].line_ctrl = line_ctrl;
                   19324:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19325:                        }
                   19326:                        if(sio[c].set_brk != set_brk) {
                   19327:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   19328:                                sio[c].set_brk = set_brk;
                   19329:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19330:                        }
                   19331:                }
                   19332:                sio[c].selector = data;
                   19333:                break;
                   19334:        case 4:
                   19335:                {
                   19336:                        bool set_dtr = ((data & 0x01) != 0);
                   19337:                        bool set_rts = ((data & 0x02) != 0);
                   19338:                        
                   19339:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     19340: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     19341:                                sio[c].set_dtr = set_dtr;
                   19342:                                sio[c].set_rts = set_rts;
1.1.1.26  root     19343: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19344:                                
                   19345:                                bool state_changed = false;
                   19346:                                
                   19347:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19348:                                if(set_dtr) {
                   19349:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   19350:                                } else {
                   19351:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   19352:                                }
                   19353:                                if(set_rts) {
                   19354:                                        sio[c].modem_stat |= 0x10;      // cts on
                   19355:                                } else {
                   19356:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   19357:                                }
                   19358:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   19359:                                        if(!(sio[c].modem_stat & 0x02)) {
                   19360:                                                if(sio[c].irq_enable & 0x08) {
                   19361:                                                        state_changed = true;
                   19362:                                                }
                   19363:                                                sio[c].modem_stat |= 0x02;
                   19364:                                        }
                   19365:                                }
                   19366:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   19367:                                        if(!(sio[c].modem_stat & 0x01)) {
                   19368:                                                if(sio[c].irq_enable & 0x08) {
                   19369:                                                        state_changed = true;
                   19370:                                                }
                   19371:                                                sio[c].modem_stat |= 0x01;
                   19372:                                        }
                   19373:                                }
                   19374:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19375:                                
                   19376:                                if(state_changed) {
                   19377:                                        sio_update_irq(c);
                   19378:                                }
1.1.1.25  root     19379:                        }
                   19380:                }
                   19381:                sio[c].modem_ctrl = data;
                   19382:                break;
                   19383:        case 7:
                   19384:                sio[c].scratch = data;
                   19385:                break;
                   19386:        }
                   19387: }
                   19388: 
                   19389: UINT8 sio_read(int c, UINT32 addr)
                   19390: {
                   19391:        switch(addr & 7) {
                   19392:        case 0:
                   19393:                if(sio[c].selector & 0x80) {
                   19394:                        return(sio[c].divisor.b.l);
                   19395:                } else {
                   19396:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19397:                        UINT8 data = 0;
                   19398:                        if(sio[c].recv_buffer != NULL) {
                   19399:                                data = sio[c].recv_buffer->read();
                   19400:                        }
1.1.1.25  root     19401:                        // data is not ready
                   19402:                        sio[c].line_stat_buf &= ~0x01;
                   19403:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19404:                        
                   19405:                        if(sio[c].irq_enable & 0x01) {
                   19406:                                sio_update_irq(c);
                   19407:                        }
                   19408:                        return(data);
                   19409:                }
                   19410:        case 1:
                   19411:                if(sio[c].selector & 0x80) {
                   19412:                        return(sio[c].divisor.b.h);
                   19413:                } else {
                   19414:                        return(sio[c].irq_enable);
                   19415:                }
                   19416:        case 2:
                   19417:                return(sio[c].irq_identify);
                   19418:        case 3:
                   19419:                return(sio[c].selector);
                   19420:        case 4:
                   19421:                return(sio[c].modem_ctrl);
                   19422:        case 5:
                   19423:                {
                   19424:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   19425:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   19426:                        sio[c].line_stat_err = 0x00;
                   19427:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19428:                        
                   19429:                        bool state_changed = false;
                   19430:                        
                   19431:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19432:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19433:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19434:                                        // transmitter holding register will be empty first
                   19435:                                        if(sio[c].irq_enable & 0x02) {
                   19436:                                                state_changed = true;
                   19437:                                        }
                   19438:                                        sio[c].line_stat_buf |= 0x20;
                   19439:                                }
                   19440:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19441:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19442:                                // transmitter shift register will be empty later
                   19443:                                sio[c].line_stat_buf |= 0x40;
                   19444:                        }
                   19445:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   19446:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19447:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19448:                                        // data is ready
                   19449:                                        if(sio[c].irq_enable & 0x01) {
                   19450:                                                state_changed = true;
                   19451:                                        }
                   19452:                                        sio[c].line_stat_buf |= 0x01;
                   19453:                                }
                   19454:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19455:                        }
                   19456:                        if(state_changed) {
                   19457:                                sio_update_irq(c);
                   19458:                        }
                   19459:                        return(val);
                   19460:                }
                   19461:        case 6:
                   19462:                {
                   19463:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   19464:                        UINT8 val = sio[c].modem_stat;
                   19465:                        sio[c].modem_stat &= 0xf0;
                   19466:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   19467:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19468:                        
                   19469:                        if(sio[c].modem_ctrl & 0x10) {
                   19470:                                // loop-back
                   19471:                                val &= 0x0f;
                   19472:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   19473:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   19474:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   19475:                        }
                   19476:                        return(val);
                   19477:                }
                   19478:        case 7:
                   19479:                return(sio[c].scratch);
                   19480:        }
                   19481:        return(0xff);
                   19482: }
                   19483: 
                   19484: void sio_update(int c)
                   19485: {
                   19486:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19487:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19488:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19489:                        // transmitter holding/shift registers will be empty
                   19490:                        sio[c].line_stat_buf |= 0x60;
                   19491:                }
                   19492:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19493:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19494:                // transmitter shift register will be empty
                   19495:                sio[c].line_stat_buf |= 0x40;
                   19496:        }
                   19497:        if(!(sio[c].line_stat_buf & 0x01)) {
                   19498:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19499:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19500:                        // data is ready
                   19501:                        sio[c].line_stat_buf |= 0x01;
                   19502:                }
                   19503:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19504:        }
                   19505:        sio_update_irq(c);
                   19506: }
                   19507: 
                   19508: void sio_update_irq(int c)
                   19509: {
                   19510:        int level = -1;
                   19511:        
                   19512:        if(sio[c].irq_enable & 0x08) {
                   19513:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19514:                if((sio[c].modem_stat & 0x0f) != 0) {
                   19515:                        level = 0;
                   19516:                }
                   19517:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19518:        }
                   19519:        if(sio[c].irq_enable & 0x02) {
                   19520:                if(sio[c].line_stat_buf & 0x20) {
                   19521:                        level = 1;
                   19522:                }
                   19523:        }
                   19524:        if(sio[c].irq_enable & 0x01) {
                   19525:                if(sio[c].line_stat_buf & 0x01) {
                   19526:                        level = 2;
                   19527:                }
                   19528:        }
                   19529:        if(sio[c].irq_enable & 0x04) {
                   19530:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   19531:                if(sio[c].line_stat_err != 0) {
                   19532:                        level = 3;
                   19533:                }
                   19534:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19535:        }
1.1.1.29  root     19536:        
                   19537:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     19538:        if(level != -1) {
                   19539:                sio[c].irq_identify = level << 1;
1.1.1.29  root     19540:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     19541:        } else {
                   19542:                sio[c].irq_identify = 1;
1.1.1.29  root     19543:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     19544:        }
                   19545: }
                   19546: 
                   19547: DWORD WINAPI sio_thread(void *lpx)
                   19548: {
                   19549:        volatile sio_t *p = (sio_t *)lpx;
                   19550:        sio_mt_t *q = &sio_mt[p->channel];
                   19551:        
                   19552:        char name[] = "COM1";
1.1.1.26  root     19553:        name[3] = '0' + sio_port_number[p->channel];
                   19554:        HANDLE hComm = NULL;
                   19555:        COMMPROP commProp;
                   19556:        DCB dcb;
                   19557:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   19558:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   19559:        
                   19560:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   19561:                if(GetCommProperties(hComm, &commProp)) {
                   19562:                        dwSettableBaud = commProp.dwSettableBaud;
                   19563:                }
1.1.1.25  root     19564:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     19565: //             EscapeCommFunction(hComm, SETRTS);
                   19566: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     19567:                
1.1.1.54! root     19568:                while(!m_exit) {
1.1.1.25  root     19569:                        // setup comm port
                   19570:                        bool comm_state_changed = false;
                   19571:                        
                   19572:                        EnterCriticalSection(&q->csLineCtrl);
                   19573:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   19574:                                p->prev_divisor = p->divisor.w;
                   19575:                                p->prev_line_ctrl = p->line_ctrl;
                   19576:                                comm_state_changed = true;
                   19577:                        }
                   19578:                        LeaveCriticalSection(&q->csLineCtrl);
                   19579:                        
                   19580:                        if(comm_state_changed) {
1.1.1.26  root     19581:                                if(GetCommState(hComm, &dcb)) {
                   19582: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   19583:                                        DWORD baud = 115200 / p->prev_divisor;
                   19584:                                        dcb.BaudRate = 9600; // default
                   19585:                                        
                   19586:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   19587:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   19588:                                        // 134.5bps is not supported ???
                   19589: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   19590:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   19591:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   19592:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   19593:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   19594:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   19595:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   19596:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   19597:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   19598:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   19599: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   19600: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   19601: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   19602:                                        
                   19603:                                        switch(p->prev_line_ctrl & 0x03) {
                   19604:                                        case 0x00: dcb.ByteSize = 5; break;
                   19605:                                        case 0x01: dcb.ByteSize = 6; break;
                   19606:                                        case 0x02: dcb.ByteSize = 7; break;
                   19607:                                        case 0x03: dcb.ByteSize = 8; break;
                   19608:                                        }
                   19609:                                        switch(p->prev_line_ctrl & 0x04) {
                   19610:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   19611:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   19612:                                        }
                   19613:                                        switch(p->prev_line_ctrl & 0x38) {
                   19614:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   19615:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   19616:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   19617:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   19618:                                        default:   dcb.Parity = NOPARITY;    break;
                   19619:                                        }
                   19620:                                        dcb.fBinary = TRUE;
                   19621:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   19622:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   19623:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   19624:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   19625:                                        dcb.fTXContinueOnXoff = TRUE;
                   19626:                                        dcb.fOutX = dcb.fInX = FALSE;
                   19627:                                        dcb.fErrorChar = FALSE;
                   19628:                                        dcb.fNull = FALSE;
                   19629:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   19630:                                        dcb.fAbortOnError = FALSE;
                   19631:                                        
                   19632:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     19633:                                }
                   19634:                                
                   19635:                                // check again to apply all comm state changes
                   19636:                                Sleep(10);
                   19637:                                continue;
                   19638:                        }
                   19639:                        
                   19640:                        // set comm pins
                   19641:                        bool change_brk = false;
1.1.1.26  root     19642: //                     bool change_rts = false;
                   19643: //                     bool change_dtr = false;
1.1.1.25  root     19644:                        
                   19645:                        EnterCriticalSection(&q->csModemCtrl);
                   19646:                        if(p->prev_set_brk != p->set_brk) {
                   19647:                                p->prev_set_brk = p->set_brk;
                   19648:                                change_brk = true;
                   19649:                        }
1.1.1.26  root     19650: //                     if(p->prev_set_rts != p->set_rts) {
                   19651: //                             p->prev_set_rts = p->set_rts;
                   19652: //                             change_rts = true;
                   19653: //                     }
                   19654: //                     if(p->prev_set_dtr != p->set_dtr) {
                   19655: //                             p->prev_set_dtr = p->set_dtr;
                   19656: //                             change_dtr = true;
                   19657: //                     }
1.1.1.25  root     19658:                        LeaveCriticalSection(&q->csModemCtrl);
                   19659:                        
                   19660:                        if(change_brk) {
1.1.1.26  root     19661:                                static UINT32 clear_time = 0;
                   19662:                                if(p->prev_set_brk) {
                   19663:                                        EscapeCommFunction(hComm, SETBREAK);
                   19664:                                        clear_time = timeGetTime() + 200;
                   19665:                                } else {
                   19666:                                        // keep break for at least 200msec
                   19667:                                        UINT32 cur_time = timeGetTime();
                   19668:                                        if(clear_time > cur_time) {
                   19669:                                                Sleep(clear_time - cur_time);
                   19670:                                        }
                   19671:                                        EscapeCommFunction(hComm, CLRBREAK);
                   19672:                                }
1.1.1.25  root     19673:                        }
1.1.1.26  root     19674: //                     if(change_rts) {
                   19675: //                             if(p->prev_set_rts) {
                   19676: //                                     EscapeCommFunction(hComm, SETRTS);
                   19677: //                             } else {
                   19678: //                                     EscapeCommFunction(hComm, CLRRTS);
                   19679: //                             }
                   19680: //                     }
                   19681: //                     if(change_dtr) {
                   19682: //                             if(p->prev_set_dtr) {
                   19683: //                                     EscapeCommFunction(hComm, SETDTR);
                   19684: //                             } else {
                   19685: //                                     EscapeCommFunction(hComm, CLRDTR);
                   19686: //                             }
                   19687: //                     }
1.1.1.25  root     19688:                        
                   19689:                        // get comm pins
                   19690:                        DWORD dwModemStat = 0;
                   19691:                        
                   19692:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   19693:                                EnterCriticalSection(&q->csModemStat);
                   19694:                                if(dwModemStat & MS_RLSD_ON) {
                   19695:                                        p->modem_stat |= 0x80;
                   19696:                                } else {
                   19697:                                        p->modem_stat &= ~0x80;
                   19698:                                }
                   19699:                                if(dwModemStat & MS_RING_ON) {
                   19700:                                        p->modem_stat |= 0x40;
                   19701:                                } else {
                   19702:                                        p->modem_stat &= ~0x40;
                   19703:                                }
1.1.1.26  root     19704: //                             if(dwModemStat & MS_DSR_ON) {
                   19705: //                                     p->modem_stat |= 0x20;
                   19706: //                             } else {
                   19707: //                                     p->modem_stat &= ~0x20;
                   19708: //                             }
                   19709: //                             if(dwModemStat & MS_CTS_ON) {
                   19710: //                                     p->modem_stat |= 0x10;
                   19711: //                             } else {
                   19712: //                                     p->modem_stat &= ~0x10;
                   19713: //                             }
1.1.1.25  root     19714:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   19715:                                        p->modem_stat |= 0x08;
                   19716:                                }
                   19717:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   19718:                                        p->modem_stat |= 0x04;
                   19719:                                }
1.1.1.26  root     19720: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   19721: //                                     p->modem_stat |= 0x02;
                   19722: //                             }
                   19723: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   19724: //                                     p->modem_stat |= 0x01;
                   19725: //                             }
1.1.1.25  root     19726:                                LeaveCriticalSection(&q->csModemStat);
                   19727:                        }
                   19728:                        
                   19729:                        // send data
                   19730:                        DWORD dwSend = 0;
                   19731:                        
                   19732:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     19733:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     19734:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   19735:                        }
                   19736:                        LeaveCriticalSection(&q->csSendData);
                   19737:                        
                   19738:                        if(dwSend != 0) {
                   19739:                                DWORD dwWritten = 0;
                   19740:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   19741:                        }
                   19742:                        
                   19743:                        // get line status and recv data
                   19744:                        DWORD dwLineStat = 0;
                   19745:                        COMSTAT comStat;
                   19746:                        
                   19747:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   19748:                                EnterCriticalSection(&q->csLineStat);
                   19749:                                if(dwLineStat & CE_BREAK) {
                   19750:                                        p->line_stat_err |= 0x10;
                   19751:                                }
                   19752:                                if(dwLineStat & CE_FRAME) {
                   19753:                                        p->line_stat_err |= 0x08;
                   19754:                                }
                   19755:                                if(dwLineStat & CE_RXPARITY) {
                   19756:                                        p->line_stat_err |= 0x04;
                   19757:                                }
                   19758:                                if(dwLineStat & CE_OVERRUN) {
                   19759:                                        p->line_stat_err |= 0x02;
                   19760:                                }
                   19761:                                LeaveCriticalSection(&q->csLineStat);
                   19762:                                
                   19763:                                if(comStat.cbInQue != 0) {
                   19764:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19765:                                        DWORD dwRecv = 0;
                   19766:                                        if(p->recv_buffer != NULL) {
                   19767:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   19768:                                        }
1.1.1.25  root     19769:                                        LeaveCriticalSection(&q->csRecvData);
                   19770:                                        
                   19771:                                        if(dwRecv != 0) {
                   19772:                                                DWORD dwRead = 0;
                   19773:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   19774:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19775:                                                        if(p->recv_buffer != NULL) {
                   19776:                                                                for(int i = 0; i < dwRead; i++) {
                   19777:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   19778:                                                                }
1.1.1.25  root     19779:                                                        }
                   19780:                                                        LeaveCriticalSection(&q->csRecvData);
                   19781:                                                }
                   19782:                                        }
                   19783:                                }
                   19784:                        }
                   19785:                        Sleep(10);
                   19786:                }
                   19787:                CloseHandle(hComm);
                   19788:        }
                   19789:        return 0;
                   19790: }
                   19791: 
1.1.1.8   root     19792: // cmos
                   19793: 
                   19794: void cmos_init()
                   19795: {
                   19796:        memset(cmos, 0, sizeof(cmos));
                   19797:        cmos_addr = 0;
1.1       root     19798:        
1.1.1.8   root     19799:        // from DOSBox
                   19800:        cmos_write(0x0a, 0x26);
                   19801:        cmos_write(0x0b, 0x02);
                   19802:        cmos_write(0x0d, 0x80);
1.1       root     19803: }
                   19804: 
1.1.1.8   root     19805: void cmos_write(int addr, UINT8 val)
1.1       root     19806: {
1.1.1.8   root     19807:        cmos[addr & 0x7f] = val;
                   19808: }
                   19809: 
                   19810: #define CMOS_GET_TIME() { \
                   19811:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   19812:        if(prev_sec != cur_sec) { \
                   19813:                GetLocalTime(&time); \
                   19814:                prev_sec = cur_sec; \
                   19815:        } \
1.1       root     19816: }
1.1.1.8   root     19817: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     19818: 
1.1.1.8   root     19819: UINT8 cmos_read(int addr)
1.1       root     19820: {
1.1.1.8   root     19821:        static SYSTEMTIME time;
                   19822:        static UINT32 prev_sec = 0;
1.1       root     19823:        
1.1.1.8   root     19824:        switch(addr & 0x7f) {
                   19825:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   19826:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   19827:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   19828:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   19829:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   19830:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   19831:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   19832: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   19833:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   19834:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   19835:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   19836:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   19837:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   19838:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   19839:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   19840:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     19841:        }
1.1.1.8   root     19842:        return(cmos[addr & 0x7f]);
1.1       root     19843: }
                   19844: 
1.1.1.7   root     19845: // kbd (a20)
                   19846: 
                   19847: void kbd_init()
                   19848: {
1.1.1.8   root     19849:        kbd_data = kbd_command = 0;
1.1.1.7   root     19850:        kbd_status = 0x18;
                   19851: }
                   19852: 
                   19853: UINT8 kbd_read_data()
                   19854: {
1.1.1.8   root     19855:        kbd_status &= ~1;
1.1.1.7   root     19856:        return(kbd_data);
                   19857: }
                   19858: 
                   19859: void kbd_write_data(UINT8 val)
                   19860: {
                   19861:        switch(kbd_command) {
                   19862:        case 0xd1:
                   19863:                i386_set_a20_line((val >> 1) & 1);
                   19864:                break;
                   19865:        }
                   19866:        kbd_command = 0;
1.1.1.8   root     19867:        kbd_status &= ~8;
1.1.1.7   root     19868: }
                   19869: 
                   19870: UINT8 kbd_read_status()
                   19871: {
                   19872:        return(kbd_status);
                   19873: }
                   19874: 
                   19875: void kbd_write_command(UINT8 val)
                   19876: {
                   19877:        switch(val) {
                   19878:        case 0xd0:
                   19879:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     19880:                kbd_status |= 1;
1.1.1.7   root     19881:                break;
                   19882:        case 0xdd:
                   19883:                i386_set_a20_line(0);
                   19884:                break;
                   19885:        case 0xdf:
                   19886:                i386_set_a20_line(1);
                   19887:                break;
1.1.1.26  root     19888:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   19889:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     19890:                if(!(val & 1)) {
1.1.1.8   root     19891:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     19892:                                // reset pic
                   19893:                                pic_init();
                   19894:                                pic[0].irr = pic[1].irr = 0x00;
                   19895:                                pic[0].imr = pic[1].imr = 0xff;
                   19896:                        }
                   19897:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     19898:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   19899:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   19900:                        i386_jmp_far(selector, address);
1.1.1.7   root     19901:                }
                   19902:                i386_set_a20_line((val >> 1) & 1);
                   19903:                break;
                   19904:        }
                   19905:        kbd_command = val;
1.1.1.8   root     19906:        kbd_status |= 8;
1.1.1.7   root     19907: }
                   19908: 
1.1.1.9   root     19909: // vga
                   19910: 
                   19911: UINT8 vga_read_status()
                   19912: {
                   19913:        // 60hz
                   19914:        static const int period[3] = {16, 17, 17};
                   19915:        static int index = 0;
                   19916:        UINT32 time = timeGetTime() % period[index];
                   19917:        
                   19918:        index = (index + 1) % 3;
1.1.1.14  root     19919:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     19920: }
                   19921: 
1.1       root     19922: // i/o bus
                   19923: 
1.1.1.29  root     19924: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   19925: //#define SW1US_PATCH
                   19926: 
1.1.1.25  root     19927: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     19928: #ifdef USE_DEBUGGER
1.1.1.25  root     19929: {
1.1.1.33  root     19930:        if(now_debugging) {
                   19931:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19932:                        if(in_break_point.table[i].status == 1) {
                   19933:                                if(addr == in_break_point.table[i].addr) {
                   19934:                                        in_break_point.hit = i + 1;
                   19935:                                        now_suspended = true;
                   19936:                                        break;
                   19937:                                }
                   19938:                        }
                   19939:                }
1.1.1.25  root     19940:        }
1.1.1.33  root     19941:        return(debugger_read_io_byte(addr));
1.1.1.25  root     19942: }
1.1.1.33  root     19943: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     19944: #endif
1.1       root     19945: {
1.1.1.33  root     19946:        UINT8 val = 0xff;
                   19947:        
1.1       root     19948:        switch(addr) {
1.1.1.29  root     19949: #ifdef SW1US_PATCH
                   19950:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19951:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     19952:                val = sio_read(0, addr - 1);
                   19953:                break;
1.1.1.29  root     19954: #else
1.1.1.25  root     19955:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19956:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     19957:                val = dma_read(0, addr);
                   19958:                break;
1.1.1.29  root     19959: #endif
1.1.1.25  root     19960:        case 0x20: case 0x21:
1.1.1.33  root     19961:                val = pic_read(0, addr);
                   19962:                break;
1.1.1.25  root     19963:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     19964:                val = pit_read(addr & 0x03);
                   19965:                break;
1.1.1.7   root     19966:        case 0x60:
1.1.1.33  root     19967:                val = kbd_read_data();
                   19968:                break;
1.1.1.9   root     19969:        case 0x61:
1.1.1.33  root     19970:                val = system_port;
                   19971:                break;
1.1.1.7   root     19972:        case 0x64:
1.1.1.33  root     19973:                val = kbd_read_status();
                   19974:                break;
1.1       root     19975:        case 0x71:
1.1.1.33  root     19976:                val = cmos_read(cmos_addr);
                   19977:                break;
1.1.1.25  root     19978:        case 0x81:
1.1.1.33  root     19979:                val = dma_page_read(0, 2);
                   19980:                break;
1.1.1.25  root     19981:        case 0x82:
1.1.1.33  root     19982:                val = dma_page_read(0, 3);
                   19983:                break;
1.1.1.25  root     19984:        case 0x83:
1.1.1.33  root     19985:                val = dma_page_read(0, 1);
                   19986:                break;
1.1.1.25  root     19987:        case 0x87:
1.1.1.33  root     19988:                val = dma_page_read(0, 0);
                   19989:                break;
1.1.1.25  root     19990:        case 0x89:
1.1.1.33  root     19991:                val = dma_page_read(1, 2);
                   19992:                break;
1.1.1.25  root     19993:        case 0x8a:
1.1.1.33  root     19994:                val = dma_page_read(1, 3);
                   19995:                break;
1.1.1.25  root     19996:        case 0x8b:
1.1.1.33  root     19997:                val = dma_page_read(1, 1);
                   19998:                break;
1.1.1.25  root     19999:        case 0x8f:
1.1.1.33  root     20000:                val = dma_page_read(1, 0);
                   20001:                break;
1.1       root     20002:        case 0x92:
1.1.1.33  root     20003:                val = (m_a20_mask >> 19) & 2;
                   20004:                break;
1.1.1.25  root     20005:        case 0xa0: case 0xa1:
1.1.1.33  root     20006:                val = pic_read(1, addr);
                   20007:                break;
1.1.1.25  root     20008:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20009:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20010:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20011:                break;
1.1.1.37  root     20012:        case 0x278: case 0x279: case 0x27a:
                   20013:                val = pio_read(1, addr);
                   20014:                break;
1.1.1.29  root     20015:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20016:                val = sio_read(3, addr);
                   20017:                break;
1.1.1.25  root     20018:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20019:                val = sio_read(1, addr);
                   20020:                break;
1.1.1.25  root     20021:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20022:                val = pio_read(0, addr);
                   20023:                break;
1.1.1.25  root     20024:        case 0x3ba: case 0x3da:
1.1.1.33  root     20025:                val = vga_read_status();
                   20026:                break;
1.1.1.37  root     20027:        case 0x3bc: case 0x3bd: case 0x3be:
                   20028:                val = pio_read(2, addr);
                   20029:                break;
1.1.1.29  root     20030:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20031:                val = sio_read(2, addr);
                   20032:                break;
1.1.1.25  root     20033:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20034:                val = sio_read(0, addr);
                   20035:                break;
1.1       root     20036:        default:
1.1.1.33  root     20037: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20038:                break;
                   20039:        }
1.1.1.33  root     20040: #ifdef ENABLE_DEBUG_IOPORT
                   20041:        if(fp_debug_log != NULL) {
                   20042:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20043:        }
                   20044: #endif
                   20045:        return(val);
1.1       root     20046: }
                   20047: 
                   20048: UINT16 read_io_word(offs_t addr)
                   20049: {
                   20050:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20051: }
                   20052: 
1.1.1.33  root     20053: #ifdef USE_DEBUGGER
                   20054: UINT16 debugger_read_io_word(offs_t addr)
                   20055: {
                   20056:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20057: }
                   20058: #endif
                   20059: 
1.1       root     20060: UINT32 read_io_dword(offs_t addr)
                   20061: {
                   20062:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20063: }
                   20064: 
1.1.1.33  root     20065: #ifdef USE_DEBUGGER
                   20066: UINT32 debugger_read_io_dword(offs_t addr)
                   20067: {
                   20068:        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));
                   20069: }
                   20070: #endif
                   20071: 
1.1       root     20072: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20073: #ifdef USE_DEBUGGER
                   20074: {
                   20075:        if(now_debugging) {
                   20076:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20077:                        if(out_break_point.table[i].status == 1) {
                   20078:                                if(addr == out_break_point.table[i].addr) {
                   20079:                                        out_break_point.hit = i + 1;
                   20080:                                        now_suspended = true;
                   20081:                                        break;
                   20082:                                }
                   20083:                        }
                   20084:                }
                   20085:        }
                   20086:        debugger_write_io_byte(addr, val);
                   20087: }
                   20088: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20089: #endif
1.1       root     20090: {
1.1.1.25  root     20091: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20092:        if(fp_debug_log != NULL) {
1.1.1.43  root     20093: #ifdef USE_SERVICE_THREAD
                   20094:                if(addr != 0xf7)
                   20095: #endif
1.1.1.33  root     20096:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20097:        }
                   20098: #endif
1.1       root     20099:        switch(addr) {
1.1.1.29  root     20100: #ifdef SW1US_PATCH
                   20101:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20102:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20103:                sio_write(0, addr - 1, val);
                   20104:                break;
                   20105: #else
1.1.1.25  root     20106:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20107:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20108:                dma_write(0, addr, val);
                   20109:                break;
1.1.1.29  root     20110: #endif
1.1.1.25  root     20111:        case 0x20: case 0x21:
1.1       root     20112:                pic_write(0, addr, val);
                   20113:                break;
1.1.1.25  root     20114:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20115:                pit_write(addr & 0x03, val);
                   20116:                break;
1.1.1.7   root     20117:        case 0x60:
                   20118:                kbd_write_data(val);
                   20119:                break;
1.1.1.9   root     20120:        case 0x61:
                   20121:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20122:                        // beep on
                   20123: //                     MessageBeep(-1);
                   20124:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20125:                        // beep off
                   20126:                }
                   20127:                system_port = val;
                   20128:                break;
1.1       root     20129:        case 0x64:
1.1.1.7   root     20130:                kbd_write_command(val);
1.1       root     20131:                break;
                   20132:        case 0x70:
                   20133:                cmos_addr = val;
                   20134:                break;
                   20135:        case 0x71:
1.1.1.8   root     20136:                cmos_write(cmos_addr, val);
1.1       root     20137:                break;
1.1.1.25  root     20138:        case 0x81:
                   20139:                dma_page_write(0, 2, val);
                   20140:        case 0x82:
                   20141:                dma_page_write(0, 3, val);
                   20142:        case 0x83:
                   20143:                dma_page_write(0, 1, val);
                   20144:        case 0x87:
                   20145:                dma_page_write(0, 0, val);
                   20146:        case 0x89:
                   20147:                dma_page_write(1, 2, val);
                   20148:        case 0x8a:
                   20149:                dma_page_write(1, 3, val);
                   20150:        case 0x8b:
                   20151:                dma_page_write(1, 1, val);
                   20152:        case 0x8f:
                   20153:                dma_page_write(1, 0, val);
1.1       root     20154:        case 0x92:
1.1.1.7   root     20155:                i386_set_a20_line((val >> 1) & 1);
1.1       root     20156:                break;
1.1.1.25  root     20157:        case 0xa0: case 0xa1:
1.1       root     20158:                pic_write(1, addr, val);
                   20159:                break;
1.1.1.25  root     20160:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20161:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     20162:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     20163:                break;
1.1.1.35  root     20164: #ifdef USE_SERVICE_THREAD
                   20165:        case 0xf7:
                   20166:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     20167:                if(in_service && cursor_moved) {
                   20168:                        // update cursor position before service is done
                   20169:                        pcbios_update_cursor_position();
                   20170:                        cursor_moved = false;
                   20171:                }
1.1.1.35  root     20172:                finish_service_loop();
                   20173:                break;
                   20174: #endif
1.1.1.37  root     20175:        case 0x278: case 0x279: case 0x27a:
                   20176:                pio_write(1, addr, val);
                   20177:                break;
1.1.1.29  root     20178:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   20179:                sio_write(3, addr, val);
                   20180:                break;
1.1.1.25  root     20181:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   20182:                sio_write(1, addr, val);
                   20183:                break;
                   20184:        case 0x378: case 0x379: case 0x37a:
                   20185:                pio_write(0, addr, val);
                   20186:                break;
1.1.1.37  root     20187:        case 0x3bc: case 0x3bd: case 0x3be:
                   20188:                pio_write(2, addr, val);
                   20189:                break;
1.1.1.29  root     20190:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   20191:                sio_write(2, addr, val);
                   20192:                break;
1.1.1.25  root     20193:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   20194:                sio_write(0, addr, val);
                   20195:                break;
1.1       root     20196:        default:
1.1.1.33  root     20197: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     20198:                break;
                   20199:        }
                   20200: }
                   20201: 
                   20202: void write_io_word(offs_t addr, UINT16 val)
                   20203: {
                   20204:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20205:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20206: }
                   20207: 
1.1.1.33  root     20208: #ifdef USE_DEBUGGER
                   20209: void debugger_write_io_word(offs_t addr, UINT16 val)
                   20210: {
                   20211:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20212:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20213: }
                   20214: #endif
                   20215: 
1.1       root     20216: void write_io_dword(offs_t addr, UINT32 val)
                   20217: {
                   20218:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20219:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20220:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20221:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20222: }
1.1.1.33  root     20223: 
                   20224: #ifdef USE_DEBUGGER
                   20225: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   20226: {
                   20227:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20228:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20229:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20230:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20231: }
                   20232: #endif

unix.superglobalmegacorp.com

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