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

1.1       root        1: /*
                      2:        MS-DOS Player for Win32 console
                      3: 
                      4:        Author : Takeda.Toshiya
                      5:        Date   : 2009.11.09-
                      6: */
                      7: 
                      8: #include "msdos.h"
                      9: 
1.1.1.28  root       10: void exit_handler();
                     11: 
1.1       root       12: #define fatalerror(...) { \
                     13:        fprintf(stderr, __VA_ARGS__); \
1.1.1.28  root       14:        exit_handler(); \
1.1       root       15:        exit(1); \
                     16: }
                     17: #define error(...) fprintf(stderr, "error: " __VA_ARGS__)
1.1.1.22  root       18: #define nolog(...)
                     19: 
1.1.1.33  root       20: //#define ENABLE_DEBUG_LOG
                     21: #ifdef ENABLE_DEBUG_LOG
1.1.1.22  root       22:        #define EXPORT_DEBUG_TO_FILE
                     23:        #define ENABLE_DEBUG_SYSCALL
                     24:        #define ENABLE_DEBUG_UNIMPLEMENTED
1.1.1.25  root       25:        #define ENABLE_DEBUG_IOPORT
1.1.1.22  root       26:        
                     27:        #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root       28:                FILE* fp_debug_log = NULL;
1.1.1.22  root       29:        #else
1.1.1.33  root       30:                #define fp_debug_log stderr
1.1.1.22  root       31:        #endif
                     32:        #ifdef ENABLE_DEBUG_UNIMPLEMENTED
                     33:                #define unimplemented_10h fatalerror
1.1.1.42  root       34:                #define unimplemented_13h fatalerror
1.1.1.25  root       35:                #define unimplemented_14h fatalerror
1.1.1.22  root       36:                #define unimplemented_15h fatalerror
                     37:                #define unimplemented_16h fatalerror
1.1.1.37  root       38:                #define unimplemented_17h fatalerror
1.1.1.22  root       39:                #define unimplemented_1ah fatalerror
                     40:                #define unimplemented_21h fatalerror
                     41:                #define unimplemented_2fh fatalerror
1.1.1.24  root       42:                #define unimplemented_33h fatalerror
1.1.1.22  root       43:                #define unimplemented_67h fatalerror
                     44:                #define unimplemented_xms fatalerror
                     45:        #endif
                     46: #endif
                     47: #ifndef unimplemented_10h
                     48:        #define unimplemented_10h nolog
                     49: #endif
1.1.1.42  root       50: #ifndef unimplemented_13h
                     51:        #define unimplemented_13h nolog
                     52: #endif
1.1.1.25  root       53: #ifndef unimplemented_14h
                     54:        #define unimplemented_14h nolog
                     55: #endif
1.1.1.22  root       56: #ifndef unimplemented_15h
                     57:        #define unimplemented_15h nolog
                     58: #endif
                     59: #ifndef unimplemented_16h
                     60:        #define unimplemented_16h nolog
                     61: #endif
1.1.1.37  root       62: #ifndef unimplemented_17h
                     63:        #define unimplemented_17h nolog
                     64: #endif
1.1.1.22  root       65: #ifndef unimplemented_1ah
                     66:        #define unimplemented_1ah nolog
                     67: #endif
                     68: #ifndef unimplemented_21h
                     69:        #define unimplemented_21h nolog
                     70: #endif
                     71: #ifndef unimplemented_2fh
                     72:        #define unimplemented_2fh nolog
                     73: #endif
1.1.1.24  root       74: #ifndef unimplemented_33h
                     75:        #define unimplemented_33h nolog
                     76: #endif
1.1.1.22  root       77: #ifndef unimplemented_67h
                     78:        #define unimplemented_67h nolog
                     79: #endif
                     80: #ifndef unimplemented_xms
                     81:        #define unimplemented_xms nolog
                     82: #endif
                     83: 
1.1.1.32  root       84: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1.1.22  root       85: #define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
                     86: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
                     87: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
1.1       root       88: 
1.1.1.12  root       89: #if defined(__MINGW32__)
                     90: extern "C" int _CRT_glob = 0;
                     91: #endif
                     92: 
                     93: /*
                     94:        kludge for "more-standardized" C++
                     95: */
                     96: #if !defined(_MSC_VER)
                     97: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
                     98: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
                     99: #define min(a,b) kludge_min(a,b)
                    100: #define max(a,b) kludge_max(a,b)
1.1.1.14  root      101: #elif _MSC_VER >= 1400
                    102: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
                    103: {
                    104: }
                    105: #endif
                    106: 
1.1.1.35  root      107: #define USE_VRAM_THREAD
1.1.1.14  root      108: 
1.1.1.35  root      109: #ifdef USE_VRAM_THREAD
1.1.1.14  root      110: static CRITICAL_SECTION vram_crit_sect;
                    111: #else
                    112: #define vram_flush()
1.1.1.12  root      113: #endif
                    114: 
1.1.1.14  root      115: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
                    116: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
                    117: 
                    118: void change_console_size(int width, int height);
                    119: void clear_scr_buffer(WORD attr);
                    120: 
                    121: static UINT32 vram_length_char = 0, vram_length_attr = 0;
                    122: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
                    123: static COORD vram_coord_char, vram_coord_attr;
                    124: 
1.1.1.28  root      125: char temp_file_path[MAX_PATH];
                    126: bool temp_file_created = false;
                    127: 
1.1.1.14  root      128: bool ignore_illegal_insn = false;
                    129: bool limit_max_memory = false;
                    130: bool no_windows = false;
                    131: bool stay_busy = false;
1.1.1.19  root      132: bool support_ems = false;
                    133: #ifdef SUPPORT_XMS
                    134: bool support_xms = false;
                    135: #endif
1.1.1.29  root      136: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14  root      137: 
1.1.1.54  root      138: BOOL is_xp_64_or_later;
1.1.1.14  root      139: BOOL is_vista_or_later;
                    140: 
1.1.1.35  root      141: #define UPDATE_OPS 16384
                    142: #define REQUEST_HARDWRE_UPDATE() { \
                    143:        update_ops = UPDATE_OPS - 1; \
                    144: }
                    145: UINT32 update_ops = 0;
                    146: UINT32 idle_ops = 0;
                    147: 
1.1.1.54  root      148: inline BOOL is_sse2_ready()
                    149: {
                    150:        static int result = -1;
                    151:        int cpu_info[4];
                    152:        
                    153:        if(result == -1) {
                    154:                result = 0;
                    155:                __cpuid(cpu_info, 0);
                    156:                if(cpu_info[0] >= 1){
                    157:                        __cpuid(cpu_info, 1);
                    158:                        if(cpu_info[3] & (1 << 26)) {
                    159:                                result = 1;
                    160:                        }
                    161:                }
                    162:        }
                    163:        return(result == 1);
                    164: }
                    165: 
1.1.1.14  root      166: inline void maybe_idle()
                    167: {
                    168:        // if it appears to be in a tight loop, assume waiting for input
                    169:        // allow for one updated video character, for a spinning cursor
1.1.1.35  root      170:        if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.54  root      171:                if(is_sse2_ready()) {
                    172:                        _mm_pause();    // SSE2 pause
                    173:                } else if(is_xp_64_or_later) {
                    174:                        Sleep(0);       // switch to other thread that is ready to run, without checking priority
                    175:                } else {
                    176:                        Sleep(1);
                    177:                        REQUEST_HARDWRE_UPDATE();
                    178:                }
1.1.1.14  root      179:        }
1.1.1.35  root      180:        idle_ops = 0;
1.1.1.14  root      181: }
1.1.1.12  root      182: 
1.1       root      183: /* ----------------------------------------------------------------------------
1.1.1.3   root      184:        MAME i86/i386
1.1       root      185: ---------------------------------------------------------------------------- */
                    186: 
1.1.1.10  root      187: #ifndef __BIG_ENDIAN__
1.1       root      188: #define LSB_FIRST
1.1.1.10  root      189: #endif
1.1       root      190: 
                    191: #ifndef INLINE
                    192: #define INLINE inline
                    193: #endif
                    194: #define U64(v) UINT64(v)
                    195: 
                    196: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    197: #define logerror(...)
                    198: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    199: #define popmessage(...)
                    200: 
                    201: /*****************************************************************************/
1.1.1.10  root      202: /* src/emu/devcpu.h */
                    203: 
                    204: // CPU interface functions
                    205: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    206: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    207: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    208: 
                    209: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    210: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    211: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    212: 
                    213: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    214: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    215: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    216: 
                    217: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    218: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    219: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    220: 
                    221: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    222: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    223: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    224: 
1.1.1.14  root      225: #define CPU_MODEL_STR(name)                    #name
                    226: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    227: 
1.1.1.10  root      228: /*****************************************************************************/
                    229: /* src/emu/didisasm.h */
                    230: 
                    231: // Disassembler constants
                    232: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    233: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    234: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    235: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    236: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    237: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    238: 
                    239: /*****************************************************************************/
1.1       root      240: /* src/emu/diexec.h */
                    241: 
                    242: // I/O line states
                    243: enum line_state
                    244: {
                    245:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    246:        ASSERT_LINE,                            // assert an interrupt immediately
                    247:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    248:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    249: };
                    250: 
                    251: // I/O line definitions
                    252: enum
                    253: {
                    254:        INPUT_LINE_IRQ = 0,
                    255:        INPUT_LINE_NMI
                    256: };
                    257: 
                    258: /*****************************************************************************/
1.1.1.10  root      259: /* src/emu/dimemory.h */
1.1       root      260: 
1.1.1.10  root      261: // Translation intentions
                    262: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    263: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    264: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    265: 
                    266: const int TRANSLATE_READ            = 0;        // translate for read
                    267: const int TRANSLATE_WRITE           = 1;        // translate for write
                    268: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    269: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    270: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    271: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    272: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    273: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    274: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      275: 
1.1.1.10  root      276: /*****************************************************************************/
                    277: /* src/emu/emucore.h */
1.1       root      278: 
1.1.1.10  root      279: // constants for expression endianness
                    280: enum endianness_t
                    281: {
                    282:        ENDIANNESS_LITTLE,
                    283:        ENDIANNESS_BIG
                    284: };
1.1       root      285: 
1.1.1.10  root      286: // declare native endianness to be one or the other
                    287: #ifdef LSB_FIRST
                    288: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    289: #else
                    290: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    291: #endif
                    292: 
                    293: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    294: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    295: 
                    296: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    297: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    298: 
                    299: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    300: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      301: 
                    302: /*****************************************************************************/
                    303: /* src/emu/memory.h */
                    304: 
1.1.1.10  root      305: // address spaces
                    306: enum address_spacenum
                    307: {
                    308:        AS_0,                           // first address space
                    309:        AS_1,                           // second address space
                    310:        AS_2,                           // third address space
                    311:        AS_3,                           // fourth address space
                    312:        ADDRESS_SPACES,                 // maximum number of address spaces
                    313: 
                    314:        // alternate address space names for common use
                    315:        AS_PROGRAM = AS_0,              // program address space
                    316:        AS_DATA = AS_1,                 // data address space
                    317:        AS_IO = AS_2                    // I/O address space
                    318: };
                    319: 
1.1       root      320: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      321: //typedef UINT32       offs_t;
1.1       root      322: 
                    323: // read accessors
                    324: UINT8 read_byte(offs_t byteaddress)
1.1.1.33  root      325: #ifdef USE_DEBUGGER
                    326: {
                    327:        if(now_debugging) {
                    328:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    329:                        if(rd_break_point.table[i].status == 1) {
                    330:                                if(byteaddress == rd_break_point.table[i].addr) {
                    331:                                        rd_break_point.hit = i + 1;
                    332:                                        now_suspended = true;
                    333:                                        break;
                    334:                                }
                    335:                        }
                    336:                }
                    337:        }
                    338:        return(debugger_read_byte(byteaddress));
                    339: }
                    340: UINT8 debugger_read_byte(offs_t byteaddress)
                    341: #endif
1.1       root      342: {
1.1.1.4   root      343: #if defined(HAS_I386)
1.1       root      344:        if(byteaddress < MAX_MEM) {
                    345:                return mem[byteaddress];
1.1.1.3   root      346: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    347: //             return read_byte(byteaddress & 0xfffff);
1.1       root      348:        }
                    349:        return 0;
1.1.1.4   root      350: #else
                    351:        return mem[byteaddress];
                    352: #endif
1.1       root      353: }
                    354: 
                    355: UINT16 read_word(offs_t byteaddress)
1.1.1.33  root      356: #ifdef USE_DEBUGGER
                    357: {
                    358:        if(now_debugging) {
                    359:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    360:                        if(rd_break_point.table[i].status == 1) {
                    361:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
                    362:                                        rd_break_point.hit = i + 1;
                    363:                                        now_suspended = true;
                    364:                                        break;
                    365:                                }
                    366:                        }
                    367:                }
                    368:        }
                    369:        return(debugger_read_word(byteaddress));
                    370: }
                    371: UINT16 debugger_read_word(offs_t byteaddress)
                    372: #endif
1.1       root      373: {
1.1.1.14  root      374:        if(byteaddress == 0x41c) {
                    375:                // pointer to first free slot in keyboard buffer
1.1.1.35  root      376:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                    377: #ifdef USE_SERVICE_THREAD
                    378:                        EnterCriticalSection(&key_buf_crit_sect);
                    379: #endif
1.1.1.55  root      380:                        bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root      381: #ifdef USE_SERVICE_THREAD
                    382:                        LeaveCriticalSection(&key_buf_crit_sect);
                    383: #endif
1.1.1.51  root      384:                        if(empty) maybe_idle();
1.1.1.14  root      385:                }
                    386:        }
1.1.1.4   root      387: #if defined(HAS_I386)
1.1       root      388:        if(byteaddress < MAX_MEM - 1) {
                    389:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      390: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    391: //             return read_word(byteaddress & 0xfffff);
1.1       root      392:        }
                    393:        return 0;
1.1.1.4   root      394: #else
                    395:        return *(UINT16 *)(mem + byteaddress);
                    396: #endif
1.1       root      397: }
                    398: 
                    399: UINT32 read_dword(offs_t byteaddress)
1.1.1.33  root      400: #ifdef USE_DEBUGGER
                    401: {
                    402:        if(now_debugging) {
                    403:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    404:                        if(rd_break_point.table[i].status == 1) {
                    405:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
                    406:                                        rd_break_point.hit = i + 1;
                    407:                                        now_suspended = true;
                    408:                                        break;
                    409:                                }
                    410:                        }
                    411:                }
                    412:        }
                    413:        return(debugger_read_dword(byteaddress));
                    414: }
                    415: UINT32 debugger_read_dword(offs_t byteaddress)
                    416: #endif
1.1       root      417: {
1.1.1.4   root      418: #if defined(HAS_I386)
1.1       root      419:        if(byteaddress < MAX_MEM - 3) {
                    420:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      421: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    422: //             return read_dword(byteaddress & 0xfffff);
1.1       root      423:        }
                    424:        return 0;
1.1.1.4   root      425: #else
                    426:        return *(UINT32 *)(mem + byteaddress);
                    427: #endif
1.1       root      428: }
                    429: 
                    430: // write accessors
1.1.1.35  root      431: #ifdef USE_VRAM_THREAD
1.1.1.14  root      432: void vram_flush_char()
                    433: {
                    434:        if(vram_length_char != 0) {
                    435:                DWORD num;
1.1.1.23  root      436:                WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      437:                vram_length_char = vram_last_length_char = 0;
                    438:        }
                    439: }
                    440: 
                    441: void vram_flush_attr()
                    442: {
                    443:        if(vram_length_attr != 0) {
                    444:                DWORD num;
1.1.1.23  root      445:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      446:                vram_length_attr = vram_last_length_attr = 0;
                    447:        }
                    448: }
                    449: 
                    450: void vram_flush()
                    451: {
                    452:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    453:                EnterCriticalSection(&vram_crit_sect);
                    454:                vram_flush_char();
                    455:                vram_flush_attr();
                    456:                LeaveCriticalSection(&vram_crit_sect);
                    457:        }
                    458: }
                    459: #endif
                    460: 
                    461: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      462: {
1.1.1.35  root      463: #ifdef USE_VRAM_THREAD
1.1.1.14  root      464:        static offs_t first_offset_char, last_offset_char;
                    465:        
                    466:        if(vram_length_char != 0) {
                    467:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    468:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    469:                        return;
                    470:                }
                    471:                if(offset != last_offset_char + 2) {
                    472:                        vram_flush_char();
                    473:                }
                    474:        }
                    475:        if(vram_length_char == 0) {
                    476:                first_offset_char = offset;
                    477:                vram_coord_char.X = (offset >> 1) % scr_width;
                    478:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    479:        }
                    480:        scr_char[vram_length_char++] = data;
                    481:        last_offset_char = offset;
                    482: #else
1.1.1.8   root      483:        COORD co;
                    484:        DWORD num;
                    485:        
1.1.1.14  root      486:        co.X = (offset >> 1) % scr_width;
                    487:        co.Y = (offset >> 1) / scr_width;
                    488:        scr_char[0] = data;
1.1.1.23  root      489:        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      490: #endif
                    491: }
                    492: 
                    493: void write_text_vram_attr(offs_t offset, UINT8 data)
                    494: {
1.1.1.35  root      495: #ifdef USE_VRAM_THREAD
1.1.1.14  root      496:        static offs_t first_offset_attr, last_offset_attr;
                    497:        
                    498:        if(vram_length_attr != 0) {
                    499:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    500:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    501:                        return;
                    502:                }
                    503:                if(offset != last_offset_attr + 2) {
                    504:                        vram_flush_attr();
                    505:                }
                    506:        }
                    507:        if(vram_length_attr == 0) {
                    508:                first_offset_attr = offset;
                    509:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    510:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    511:        }
                    512:        scr_attr[vram_length_attr++] = data;
                    513:        last_offset_attr = offset;
                    514: #else
                    515:        COORD co;
                    516:        DWORD num;
1.1.1.8   root      517:        
1.1.1.14  root      518:        co.X = (offset >> 1) % scr_width;
                    519:        co.Y = (offset >> 1) / scr_width;
                    520:        scr_attr[0] = data;
1.1.1.23  root      521:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      522: #endif
                    523: }
                    524: 
                    525: void write_text_vram_byte(offs_t offset, UINT8 data)
                    526: {
1.1.1.35  root      527: #ifdef USE_VRAM_THREAD
1.1.1.14  root      528:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      529: #endif
1.1.1.8   root      530:        if(offset & 1) {
1.1.1.14  root      531:                write_text_vram_attr(offset, data);
1.1.1.8   root      532:        } else {
1.1.1.14  root      533:                write_text_vram_char(offset, data);
1.1.1.8   root      534:        }
1.1.1.35  root      535: #ifdef USE_VRAM_THREAD
1.1.1.14  root      536:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      537: #endif
1.1.1.8   root      538: }
                    539: 
                    540: void write_text_vram_word(offs_t offset, UINT16 data)
                    541: {
1.1.1.35  root      542: #ifdef USE_VRAM_THREAD
1.1.1.14  root      543:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      544: #endif
1.1.1.8   root      545:        if(offset & 1) {
1.1.1.14  root      546:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    547:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      548:        } else {
1.1.1.14  root      549:                write_text_vram_char(offset    , (data     ) & 0xff);
                    550:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      551:        }
1.1.1.35  root      552: #ifdef USE_VRAM_THREAD
1.1.1.14  root      553:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      554: #endif
1.1.1.8   root      555: }
                    556: 
                    557: void write_text_vram_dword(offs_t offset, UINT32 data)
                    558: {
1.1.1.35  root      559: #ifdef USE_VRAM_THREAD
1.1.1.14  root      560:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      561: #endif
1.1.1.8   root      562:        if(offset & 1) {
1.1.1.14  root      563:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    564:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    565:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    566:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    567:        } else {
                    568:                write_text_vram_char(offset    , (data      ) & 0xff);
                    569:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    570:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    571:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      572:        }
1.1.1.35  root      573: #ifdef USE_VRAM_THREAD
1.1.1.14  root      574:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      575: #endif
1.1.1.8   root      576: }
                    577: 
1.1       root      578: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      579: #ifdef USE_DEBUGGER
                    580: {
                    581:        if(now_debugging) {
                    582:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    583:                        if(wr_break_point.table[i].status == 1) {
                    584:                                if(byteaddress == wr_break_point.table[i].addr) {
                    585:                                        wr_break_point.hit = i + 1;
                    586:                                        now_suspended = true;
                    587:                                        break;
                    588:                                }
                    589:                        }
                    590:                }
                    591:        }
                    592:        debugger_write_byte(byteaddress, data);
                    593: }
                    594: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    595: #endif
1.1       root      596: {
1.1.1.8   root      597:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      598:                mem[byteaddress] = data;
1.1.1.8   root      599:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      600:                if(!restore_console_on_exit) {
                    601:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      602:                }
1.1.1.8   root      603:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    604:                mem[byteaddress] = data;
                    605:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    606:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    607:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      608:                }
                    609:                mem[byteaddress] = data;
1.1.1.4   root      610: #if defined(HAS_I386)
1.1.1.3   root      611:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      612: #else
                    613:        } else {
                    614: #endif
1.1.1.3   root      615:                mem[byteaddress] = data;
1.1       root      616:        }
                    617: }
                    618: 
                    619: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      620: #ifdef USE_DEBUGGER
                    621: {
                    622:        if(now_debugging) {
                    623:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    624:                        if(wr_break_point.table[i].status == 1) {
                    625:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    626:                                        wr_break_point.hit = i + 1;
                    627:                                        now_suspended = true;
                    628:                                        break;
                    629:                                }
                    630:                        }
                    631:                }
                    632:        }
                    633:        debugger_write_word(byteaddress, data);
                    634: }
                    635: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    636: #endif
1.1       root      637: {
1.1.1.8   root      638:        if(byteaddress < MEMORY_END) {
1.1.1.51  root      639:                if(byteaddress == cursor_position_address) {
                    640:                        if(*(UINT16 *)(mem + byteaddress) != data) {
                    641:                                COORD co;
                    642:                                co.X = data & 0xff;
                    643:                                co.Y = (data >> 8) + scr_top;
                    644:                                SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.59! root      645:                                cursor_moved = false;
        !           646:                                cursor_moved_by_crtc = false;
1.1.1.51  root      647:                        }
1.1.1.14  root      648:                }
1.1.1.3   root      649:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      650:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      651:                if(!restore_console_on_exit) {
                    652:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      653:                }
1.1.1.8   root      654:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    655:                *(UINT16 *)(mem + byteaddress) = data;
                    656:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    657:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    658:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      659:                }
                    660:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      661: #if defined(HAS_I386)
1.1.1.3   root      662:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      663: #else
                    664:        } else {
                    665: #endif
1.1.1.3   root      666:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      667:        }
                    668: }
                    669: 
                    670: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      671: #ifdef USE_DEBUGGER
                    672: {
                    673:        if(now_debugging) {
                    674:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    675:                        if(wr_break_point.table[i].status == 1) {
                    676:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    677:                                        wr_break_point.hit = i + 1;
                    678:                                        now_suspended = true;
                    679:                                        break;
                    680:                                }
                    681:                        }
                    682:                }
                    683:        }
                    684:        debugger_write_dword(byteaddress, data);
                    685: }
                    686: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    687: #endif
1.1       root      688: {
1.1.1.8   root      689:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      690:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      691:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      692:                if(!restore_console_on_exit) {
                    693:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      694:                }
1.1.1.8   root      695:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    696:                *(UINT32 *)(mem + byteaddress) = data;
                    697:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    698:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    699:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      700:                }
                    701:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      702: #if defined(HAS_I386)
1.1.1.3   root      703:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      704: #else
                    705:        } else {
                    706: #endif
1.1.1.3   root      707:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      708:        }
                    709: }
                    710: 
                    711: #define read_decrypted_byte read_byte
                    712: #define read_decrypted_word read_word
                    713: #define read_decrypted_dword read_dword
                    714: 
1.1.1.3   root      715: #define read_raw_byte read_byte
                    716: #define write_raw_byte write_byte
                    717: 
                    718: #define read_word_unaligned read_word
                    719: #define write_word_unaligned write_word
                    720: 
                    721: #define read_io_word_unaligned read_io_word
                    722: #define write_io_word_unaligned write_io_word
                    723: 
1.1       root      724: UINT8 read_io_byte(offs_t byteaddress);
                    725: UINT16 read_io_word(offs_t byteaddress);
                    726: UINT32 read_io_dword(offs_t byteaddress);
                    727: 
                    728: void write_io_byte(offs_t byteaddress, UINT8 data);
                    729: void write_io_word(offs_t byteaddress, UINT16 data);
                    730: void write_io_dword(offs_t byteaddress, UINT32 data);
                    731: 
                    732: /*****************************************************************************/
                    733: /* src/osd/osdcomm.h */
                    734: 
                    735: /* Highly useful macro for compile-time knowledge of an array size */
                    736: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    737: 
1.1.1.54  root      738: // flag to exit MS-DOS Player
                    739: // this is set when the first process is terminated and jump to FFFF:0000 HALT
                    740: int m_exit = 0;
                    741: 
1.1.1.3   root      742: #if defined(HAS_I386)
1.1.1.10  root      743:        static CPU_TRANSLATE(i386);
                    744:        #include "mame/lib/softfloat/softfloat.c"
                    745:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      746:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      747: #elif defined(HAS_I286)
1.1.1.10  root      748:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      749: #else
1.1.1.10  root      750:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      751: #endif
1.1.1.33  root      752: #ifdef USE_DEBUGGER
1.1.1.10  root      753:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      754: #endif
                    755: 
1.1.1.3   root      756: #if defined(HAS_I386)
                    757:        #define SREG(x)                         m_sreg[x].selector
                    758:        #define SREG_BASE(x)                    m_sreg[x].base
                    759:        int cpu_type, cpu_step;
                    760: #else
                    761:        #define REG8(x)                         m_regs.b[x]
                    762:        #define REG16(x)                        m_regs.w[x]
                    763:        #define SREG(x)                         m_sregs[x]
                    764:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      765:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      766:        #define m_CF                            m_CarryVal
                    767:        #define m_a20_mask                      AMASK
                    768:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    769:        #if defined(HAS_I286)
                    770:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    771:        #else
                    772:                #define i386_set_a20_line(x)
                    773:        #endif
                    774:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    775: #endif
1.1       root      776: 
                    777: void i386_jmp_far(UINT16 selector, UINT32 address)
                    778: {
1.1.1.3   root      779: #if defined(HAS_I386)
1.1       root      780:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      781:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      782:        } else {
1.1.1.3   root      783:                SREG(CS) = selector;
                    784:                m_performed_intersegment_jump = 1;
                    785:                i386_load_segment_descriptor(CS);
                    786:                m_eip = address;
                    787:                CHANGE_PC(m_eip);
1.1       root      788:        }
1.1.1.3   root      789: #elif defined(HAS_I286)
                    790:        i80286_code_descriptor(selector, address, 1);
                    791: #else
                    792:        SREG(CS) = selector;
                    793:        i386_load_segment_descriptor(CS);
                    794:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    795: #endif
1.1       root      796: }
                    797: 
1.1.1.24  root      798: void i386_call_far(UINT16 selector, UINT32 address)
                    799: {
                    800: #if defined(HAS_I386)
                    801:        if(PROTECTED_MODE && !V8086_MODE) {
                    802:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    803:        } else {
                    804:                PUSH16(SREG(CS));
                    805:                PUSH16(m_eip);
                    806:                SREG(CS) = selector;
                    807:                m_performed_intersegment_jump = 1;
                    808:                i386_load_segment_descriptor(CS);
                    809:                m_eip = address;
                    810:                CHANGE_PC(m_eip);
                    811:        }
                    812: #else
                    813:        UINT16 ip = m_pc - SREG_BASE(CS);
                    814:        UINT16 cs = SREG(CS);
                    815: #if defined(HAS_I286)
                    816:        i80286_code_descriptor(selector, address, 2);
                    817: #else
                    818:        SREG(CS) = selector;
                    819:        i386_load_segment_descriptor(CS);
                    820:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    821: #endif
                    822:        PUSH(cs);
                    823:        PUSH(ip);
                    824:        CHANGE_PC(m_pc);
                    825: #endif
                    826: }
1.1.1.49  root      827: 
                    828: void i386_push16(UINT16 value)
                    829: {
                    830: #if defined(HAS_I386)
                    831:        PUSH16(value);
                    832: #else
                    833:        PUSH(value);
1.1.1.35  root      834: #endif
1.1.1.49  root      835: }
                    836: 
                    837: UINT16 i386_pop16()
                    838: {
                    839: #if defined(HAS_I386)
                    840:        return POP16();
                    841: #else
                    842:        UINT16 value;
                    843:        POP(value);
                    844:        return value;
                    845: #endif
                    846: }
1.1.1.24  root      847: 
1.1.1.29  root      848: UINT16 i386_read_stack()
                    849: {
                    850: #if defined(HAS_I386)
                    851:        UINT32 ea, new_esp;
                    852:        if( STACK_32BIT ) {
                    853:                new_esp = REG32(ESP) + 2;
                    854:                ea = i386_translate(SS, new_esp - 2, 0);
                    855:        } else {
                    856:                new_esp = REG16(SP) + 2;
                    857:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    858:        }
                    859:        return READ16(ea);
                    860: #else
                    861:        UINT16 sp = m_regs.w[SP] + 2;
                    862:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    863: #endif
                    864: }
                    865: 
1.1.1.53  root      866: void i386_write_stack(UINT16 value)
                    867: {
                    868: #if defined(HAS_I386)
                    869:        UINT32 ea, new_esp;
                    870:        if( STACK_32BIT ) {
                    871:                new_esp = REG32(ESP) + 2;
                    872:                ea = i386_translate(SS, new_esp - 2, 0);
                    873:        } else {
                    874:                new_esp = REG16(SP) + 2;
                    875:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    876:        }
                    877:        WRITE16(ea, value);
                    878: #else
                    879:        UINT16 sp = m_regs.w[SP] + 2;
                    880:        WriteWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK), value);
                    881: #endif
                    882: }
                    883: 
1.1       root      884: /* ----------------------------------------------------------------------------
1.1.1.33  root      885:        debugger
                    886: ---------------------------------------------------------------------------- */
                    887: 
                    888: #ifdef USE_DEBUGGER
                    889: #define TELNET_BLUE      0x0004 // text color contains blue.
                    890: #define TELNET_GREEN     0x0002 // text color contains green.
                    891: #define TELNET_RED       0x0001 // text color contains red.
                    892: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    893: 
                    894: int svr_socket = 0;
                    895: int cli_socket = 0;
                    896: 
1.1.1.55  root      897: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
1.1.1.33  root      898: 
                    899: void debugger_init()
                    900: {
                    901:        now_debugging = false;
                    902:        now_going = false;
                    903:        now_suspended = false;
                    904:        force_suspend = false;
                    905:        
                    906:        memset(&break_point, 0, sizeof(break_point_t));
                    907:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    908:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    909:        memset(&in_break_point, 0, sizeof(break_point_t));
                    910:        memset(&out_break_point, 0, sizeof(break_point_t));
                    911:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    912: }
                    913: 
1.1.1.45  root      914: void telnet_send(const char *string)
1.1.1.33  root      915: {
                    916:        char buffer[8192], *ptr;
                    917:        strcpy(buffer, string);
                    918:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    919:                char tmp[8192];
                    920:                *ptr = '\0';
                    921:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    922:                strcpy(buffer, tmp);
                    923:        }
                    924:        
                    925:        int len = strlen(buffer), res;
                    926:        ptr = buffer;
                    927:        while(len > 0) {
                    928:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    929:                        len -= res;
                    930:                        ptr += res;
                    931:                }
                    932:        }
                    933: }
                    934: 
                    935: void telnet_command(const char *format, ...)
                    936: {
                    937:        char buffer[1024];
                    938:        va_list ap;
                    939:        va_start(ap, format);
                    940:        vsprintf(buffer, format, ap);
                    941:        va_end(ap);
                    942:        
                    943:        telnet_send(buffer);
                    944: }
                    945: 
                    946: void telnet_printf(const char *format, ...)
                    947: {
                    948:        char buffer[1024];
                    949:        va_list ap;
                    950:        va_start(ap, format);
                    951:        vsprintf(buffer, format, ap);
                    952:        va_end(ap);
                    953:        
                    954:        if(fp_debugger != NULL) {
                    955:                fprintf(fp_debugger, "%s", buffer);
                    956:        }
                    957:        telnet_send(buffer);
                    958: }
                    959: 
                    960: bool telnet_gets(char *str, int n)
                    961: {
                    962:        char buffer[1024];
                    963:        int ptr = 0;
                    964:        
                    965:        telnet_command("\033[12l"); // local echo on
                    966:        telnet_command("\033[2l");  // key unlock
                    967:        
1.1.1.54  root      968:        while(!m_exit) {
1.1.1.33  root      969:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    970:                
                    971:                if(len > 0 && buffer[0] != 0xff) {
                    972:                        for(int i = 0; i < len; i++) {
                    973:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    974:                                        str[ptr] = 0;
                    975:                                        telnet_command("\033[2h");  // key lock
                    976:                                        telnet_command("\033[12h"); // local echo off
1.1.1.54  root      977:                                        return(!m_exit);
1.1.1.33  root      978:                                } else if(buffer[i] == 0x08) {
                    979:                                        if(ptr > 0) {
                    980:                                                telnet_command("\033[0K"); // erase from cursor position
                    981:                                                ptr--;
                    982:                                        } else {
                    983:                                                telnet_command("\033[1C"); // move cursor forward
                    984:                                        }
                    985:                                } else if(ptr < n - 1) {
1.1.1.37  root      986:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root      987:                                                str[ptr++] = buffer[i];
                    988:                                        }
                    989:                                } else {
                    990:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                    991:                                }
                    992:                        }
                    993:                } else if(len == -1) {
                    994:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    995:                                return(false);
                    996:                        }
                    997:                } else if(len == 0) {
                    998:                        return(false);
                    999:                }
                   1000:                Sleep(10);
                   1001:        }
1.1.1.54  root     1002:        return(!m_exit);
1.1.1.33  root     1003: }
                   1004: 
                   1005: bool telnet_kbhit()
                   1006: {
                   1007:        char buffer[1024];
                   1008:        
1.1.1.54  root     1009:        if(!m_exit) {
1.1.1.33  root     1010:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1011:                
                   1012:                if(len > 0) {
                   1013:                        for(int i = 0; i < len; i++) {
                   1014:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1015:                                        return(true);
                   1016:                                }
                   1017:                        }
                   1018:                } else if(len == 0) {
                   1019:                        return(true); // disconnected
                   1020:                }
                   1021:        }
                   1022:        return(false);
                   1023: }
                   1024: 
                   1025: bool telnet_disconnected()
                   1026: {
                   1027:        char buffer[1024];
                   1028:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1029:        
                   1030:        if(len == 0) {
                   1031:                return(true);
                   1032:        } else if(len == -1) {
                   1033:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1034:                        return(true);
                   1035:                }
                   1036:        }
                   1037:        return(false);
                   1038: }
                   1039: 
                   1040: void telnet_set_color(int color)
                   1041: {
                   1042:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                   1043: }
                   1044: 
                   1045: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                   1046: {
                   1047: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                   1048:        UINT8 ops[16];
                   1049:        for(int i = 0; i < 16; i++) {
                   1050:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                   1051:        }
                   1052:        UINT8 *oprom = ops;
                   1053:        
                   1054: #if defined(HAS_I386)
                   1055:        if(m_operand_size) {
                   1056:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                   1057:        } else
                   1058: #endif
                   1059:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1060: }
                   1061: 
                   1062: void debugger_regs_info(char *buffer)
                   1063: {
                   1064: #if defined(HAS_I386)
                   1065:        UINT32 flags = get_flags();
                   1066: #else
                   1067:        UINT32 flags = CompressFlags();
                   1068: #endif
                   1069: #if defined(HAS_I386)
                   1070:        if(m_operand_size) {
                   1071:                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",
                   1072:                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),
                   1073:                PROTECTED_MODE ? "PE" : "--",
                   1074:                (flags & 0x40000) ? 'A' : '-',
                   1075:                (flags & 0x20000) ? 'V' : '-',
                   1076:                (flags & 0x10000) ? 'R' : '-',
                   1077:                (flags & 0x04000) ? 'N' : '-',
                   1078:                (flags & 0x02000) ? '1' : '0',
                   1079:                (flags & 0x01000) ? '1' : '0',
                   1080:                (flags & 0x00800) ? 'O' : '-',
                   1081:                (flags & 0x00400) ? 'D' : '-',
                   1082:                (flags & 0x00200) ? 'I' : '-',
                   1083:                (flags & 0x00100) ? 'T' : '-',
                   1084:                (flags & 0x00080) ? 'S' : '-',
                   1085:                (flags & 0x00040) ? 'Z' : '-',
                   1086:                (flags & 0x00010) ? 'A' : '-',
                   1087:                (flags & 0x00004) ? 'P' : '-',
                   1088:                (flags & 0x00001) ? 'C' : '-');
                   1089:        } else {
                   1090: #endif
                   1091:                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",
                   1092:                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),
                   1093: #if defined(HAS_I386)
                   1094:                PROTECTED_MODE ? "PE" : "--",
                   1095: #else
                   1096:                "--",
                   1097: #endif
                   1098:                (flags & 0x40000) ? 'A' : '-',
                   1099:                (flags & 0x20000) ? 'V' : '-',
                   1100:                (flags & 0x10000) ? 'R' : '-',
                   1101:                (flags & 0x04000) ? 'N' : '-',
                   1102:                (flags & 0x02000) ? '1' : '0',
                   1103:                (flags & 0x01000) ? '1' : '0',
                   1104:                (flags & 0x00800) ? 'O' : '-',
                   1105:                (flags & 0x00400) ? 'D' : '-',
                   1106:                (flags & 0x00200) ? 'I' : '-',
                   1107:                (flags & 0x00100) ? 'T' : '-',
                   1108:                (flags & 0x00080) ? 'S' : '-',
                   1109:                (flags & 0x00040) ? 'Z' : '-',
                   1110:                (flags & 0x00010) ? 'A' : '-',
                   1111:                (flags & 0x00004) ? 'P' : '-',
                   1112:                (flags & 0x00001) ? 'C' : '-');
                   1113: #if defined(HAS_I386)
                   1114:        }
                   1115: #endif
                   1116: }
                   1117: 
                   1118: void debugger_process_info(char *buffer)
                   1119: {
                   1120:        UINT16 psp_seg = current_psp;
                   1121:        process_t *process;
                   1122:        bool check[0x10000] = {0};
                   1123:        
                   1124:        buffer[0] = '\0';
                   1125:        
                   1126:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1127:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1128:                char *file = process->module_path, *s;
                   1129:                char tmp[8192];
                   1130:                
                   1131:                while((s = strstr(file, "\\")) != NULL) {
                   1132:                        file = s + 1;
                   1133:                }
                   1134:                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));
                   1135:                strcat(tmp, buffer);
                   1136:                strcpy(buffer, tmp);
                   1137:                
                   1138:                check[psp_seg] = true;
                   1139:                psp_seg = psp->parent_psp;
                   1140:        }
                   1141: }
                   1142: 
                   1143: UINT32 debugger_get_val(const char *str)
                   1144: {
                   1145:        char tmp[1024];
                   1146:        
                   1147:        if(str == NULL || strlen(str) == 0) {
                   1148:                return(0);
                   1149:        }
                   1150:        strcpy(tmp, str);
                   1151:        
                   1152:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1153:                // ank
                   1154:                return(tmp[1] & 0xff);
                   1155:        } else if(tmp[0] == '%') {
                   1156:                // decimal
                   1157:                return(strtoul(tmp + 1, NULL, 10));
                   1158:        }
                   1159:        return(strtoul(tmp, NULL, 16));
                   1160: }
                   1161: 
                   1162: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1163: {
                   1164:        char tmp[1024], *s;
                   1165:        
                   1166:        if(str == NULL || strlen(str) == 0) {
                   1167:                return(val);
                   1168:        }
                   1169:        strcpy(tmp, str);
                   1170:        
                   1171:        if((s = strstr(tmp, ":")) != NULL) {
                   1172:                // 0000:0000
                   1173:                *s = '\0';
                   1174:                return(debugger_get_val(tmp));
                   1175:        }
                   1176:        return(val);
                   1177: }
                   1178: 
                   1179: UINT32 debugger_get_ofs(const char *str)
                   1180: {
                   1181:        char tmp[1024], *s;
                   1182:        
                   1183:        if(str == NULL || strlen(str) == 0) {
                   1184:                return(0);
                   1185:        }
                   1186:        strcpy(tmp, str);
                   1187:        
                   1188:        if((s = strstr(tmp, ":")) != NULL) {
                   1189:                // 0000:0000
                   1190:                return(debugger_get_val(s + 1));
                   1191:        }
                   1192:        return(debugger_get_val(tmp));
                   1193: }
                   1194: 
                   1195: void debugger_main()
                   1196: {
                   1197:        telnet_command("\033[20h"); // cr-lf
                   1198:        
                   1199:        force_suspend = true;
                   1200:        now_going = false;
                   1201:        now_debugging = true;
                   1202:        Sleep(100);
                   1203:        
1.1.1.54  root     1204:        if(!m_exit && !now_suspended) {
1.1.1.33  root     1205:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1206:                telnet_printf("waiting until cpu is suspended...\n");
                   1207:        }
1.1.1.54  root     1208:        while(!m_exit && !now_suspended) {
1.1.1.33  root     1209:                if(telnet_disconnected()) {
                   1210:                        break;
                   1211:                }
                   1212:                Sleep(10);
                   1213:        }
                   1214:        
                   1215:        char buffer[8192];
                   1216:        
                   1217:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1218:        debugger_process_info(buffer);
                   1219:        telnet_printf("%s", buffer);
                   1220:        debugger_regs_info(buffer);
                   1221:        telnet_printf("%s", buffer);
                   1222:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1223:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1224:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1225:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1226:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1227:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1228:        
                   1229:        #define MAX_COMMAND_LEN 64
                   1230:        
                   1231:        char command[MAX_COMMAND_LEN + 1];
                   1232:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1233:        
                   1234:        UINT32 data_seg = SREG(DS);
                   1235:        UINT32 data_ofs = 0;
                   1236:        UINT32 dasm_seg = SREG(CS);
                   1237:        UINT32 dasm_ofs = m_eip;
                   1238:        
1.1.1.54  root     1239:        while(!m_exit) {
1.1.1.33  root     1240:                telnet_printf("- ");
                   1241:                command[0] = '\0';
                   1242:                
                   1243:                if(fi_debugger != NULL) {
                   1244:                        while(command[0] == '\0') {
                   1245:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1246:                                        break;
                   1247:                                }
                   1248:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1249:                                        command[strlen(command) - 1] = '\0';
                   1250:                                }
                   1251:                        }
                   1252:                        if(command[0] != '\0') {
                   1253:                                telnet_command("%s\n", command);
                   1254:                        }
                   1255:                }
                   1256:                if(command[0] == '\0') {
                   1257:                        if(!telnet_gets(command, sizeof(command))) {
                   1258:                                break;
                   1259:                        }
                   1260:                }
                   1261:                if(command[0] == '\0') {
                   1262:                        strcpy(command, prev_command);
                   1263:                } else {
                   1264:                        strcpy(prev_command, command);
                   1265:                }
                   1266:                if(fp_debugger != NULL) {
                   1267:                        fprintf(fp_debugger, "%s\n", command);
                   1268:                }
                   1269:                
1.1.1.54  root     1270:                if(!m_exit && command[0] != 0) {
1.1.1.33  root     1271:                        char *params[32], *token = NULL;
                   1272:                        int num = 0;
                   1273:                        
                   1274:                        if((token = strtok(command, " ")) != NULL) {
                   1275:                                params[num++] = token;
                   1276:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1277:                                        params[num++] = token;
                   1278:                                }
                   1279:                        }
                   1280:                        if(stricmp(params[0], "D") == 0) {
                   1281:                                if(num <= 3) {
                   1282:                                        if(num >= 2) {
                   1283:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1284:                                                data_ofs = debugger_get_ofs(params[1]);
                   1285:                                        }
                   1286:                                        UINT32 end_seg = data_seg;
                   1287:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1288:                                        if(num == 3) {
                   1289:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1290:                                                end_ofs = debugger_get_ofs(params[2]);
                   1291:                                        }
                   1292:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1293:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1294: //                                     bool is_sjis = false;
1.1.1.33  root     1295:                                        
                   1296:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1297:                                                if((addr & 0x0f) == 0) {
                   1298:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1299:                                                                data_seg += 0x1000;
                   1300:                                                                data_ofs -= 0x10000;
                   1301:                                                        }
                   1302:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1303:                                                        memset(buffer, 0, sizeof(buffer));
                   1304:                                                }
                   1305:                                                if(addr < start_addr || addr > end_addr) {
                   1306:                                                        telnet_printf("   ");
                   1307:                                                        buffer[addr & 0x0f] = ' ';
                   1308:                                                } else {
                   1309:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1310:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1311: //                                                     if(is_sjis) {
1.1.1.33  root     1312: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1313: //                                                             is_sjis = false;
1.1.1.33  root     1314: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1315: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1316: //                                                             is_sjis = true;
1.1.1.33  root     1317: //                                                     } else
                   1318:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1319:                                                                buffer[addr & 0x0f] = data;
                   1320:                                                        } else {
                   1321:                                                                buffer[addr & 0x0f] = '.';
                   1322:                                                        }
                   1323:                                                }
                   1324:                                                if((addr & 0x0f) == 0x0f) {
                   1325:                                                        telnet_printf("  %s\n", buffer);
                   1326:                                                }
                   1327:                                        }
                   1328:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1329:                                                data_seg += 0x1000;
                   1330:                                                data_ofs -= 0x10000;
                   1331:                                        }
                   1332:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1333:                                } else {
                   1334:                                        telnet_printf("invalid parameter number\n");
                   1335:                                }
                   1336:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1337:                                if(num >= 3) {
                   1338:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1339:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1340:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1341:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1342:                                        }
                   1343:                                } else {
                   1344:                                        telnet_printf("invalid parameter number\n");
                   1345:                                }
                   1346:                        } else if(stricmp(params[0], "EW") == 0) {
                   1347:                                if(num >= 3) {
                   1348:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1349:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1350:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1351:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1352:                                        }
                   1353:                                } else {
                   1354:                                        telnet_printf("invalid parameter number\n");
                   1355:                                }
                   1356:                        } else if(stricmp(params[0], "ED") == 0) {
                   1357:                                if(num >= 3) {
                   1358:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1359:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1360:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1361:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1362:                                        }
                   1363:                                } else {
                   1364:                                        telnet_printf("invalid parameter number\n");
                   1365:                                }
                   1366:                        } else if(stricmp(params[0], "EA") == 0) {
                   1367:                                if(num >= 3) {
                   1368:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1369:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1370:                                        strcpy(buffer, prev_command);
                   1371:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1372:                                                int len = strlen(token);
                   1373:                                                for(int i = 0; i < len; i++) {
                   1374:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1375:                                                }
                   1376:                                        } else {
                   1377:                                                telnet_printf("invalid parameter\n");
                   1378:                                        }
                   1379:                                } else {
                   1380:                                        telnet_printf("invalid parameter number\n");
                   1381:                                }
                   1382:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1383:                                if(num == 2) {
                   1384:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1385:                                } else {
                   1386:                                        telnet_printf("invalid parameter number\n");
                   1387:                                }
                   1388:                        } else if(stricmp(params[0], "IW") == 0) {
                   1389:                                if(num == 2) {
                   1390:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1391:                                } else {
                   1392:                                        telnet_printf("invalid parameter number\n");
                   1393:                                }
                   1394:                        } else if(stricmp(params[0], "ID") == 0) {
                   1395:                                if(num == 2) {
                   1396:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1397:                                } else {
                   1398:                                        telnet_printf("invalid parameter number\n");
                   1399:                                }
                   1400:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1401:                                if(num == 3) {
                   1402:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1403:                                } else {
                   1404:                                        telnet_printf("invalid parameter number\n");
                   1405:                                }
                   1406:                        } else if(stricmp(params[0], "OW") == 0) {
                   1407:                                if(num == 3) {
                   1408:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1409:                                } else {
                   1410:                                        telnet_printf("invalid parameter number\n");
                   1411:                                }
                   1412:                        } else if(stricmp(params[0], "OD") == 0) {
                   1413:                                if(num == 3) {
                   1414:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1415:                                } else {
                   1416:                                        telnet_printf("invalid parameter number\n");
                   1417:                                }
                   1418:                        } else if(stricmp(params[0], "R") == 0) {
                   1419:                                if(num == 1) {
                   1420:                                        debugger_regs_info(buffer);
                   1421:                                        telnet_printf("%s", buffer);
                   1422:                                } else if(num == 3) {
                   1423: #if defined(HAS_I386)
                   1424:                                        if(stricmp(params[1], "EAX") == 0) {
                   1425:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1426:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1427:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1428:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1429:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1430:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1431:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1432:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1433:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1434:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1435:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1436:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1437:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1438:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1439:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1440:                                        } else
                   1441: #endif
                   1442:                                        if(stricmp(params[1], "AX") == 0) {
                   1443:                                                REG16(AX) = debugger_get_val(params[2]);
                   1444:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1445:                                                REG16(BX) = debugger_get_val(params[2]);
                   1446:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1447:                                                REG16(CX) = debugger_get_val(params[2]);
                   1448:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1449:                                                REG16(DX) = debugger_get_val(params[2]);
                   1450:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1451:                                                REG16(SP) = debugger_get_val(params[2]);
                   1452:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1453:                                                REG16(BP) = debugger_get_val(params[2]);
                   1454:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1455:                                                REG16(SI) = debugger_get_val(params[2]);
                   1456:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1457:                                                REG16(DI) = debugger_get_val(params[2]);
                   1458:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1459: #if defined(HAS_I386)
                   1460:                                                if(m_operand_size) {
                   1461:                                                        m_eip = debugger_get_val(params[2]);
                   1462:                                                } else {
                   1463:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1464:                                                }
                   1465:                                                CHANGE_PC(m_eip);
                   1466: #else
                   1467:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1468:                                                CHANGE_PC(m_pc);
                   1469: #endif
                   1470:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1471:                                                REG8(AL) = debugger_get_val(params[2]);
                   1472:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1473:                                                REG8(AH) = debugger_get_val(params[2]);
                   1474:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1475:                                                REG8(BL) = debugger_get_val(params[2]);
                   1476:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1477:                                                REG8(BH) = debugger_get_val(params[2]);
                   1478:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1479:                                                REG8(CL) = debugger_get_val(params[2]);
                   1480:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1481:                                                REG8(CH) = debugger_get_val(params[2]);
                   1482:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1483:                                                REG8(DL) = debugger_get_val(params[2]);
                   1484:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1485:                                                REG8(DH) = debugger_get_val(params[2]);
                   1486:                                        } else {
                   1487:                                                telnet_printf("unknown register %s\n", params[1]);
                   1488:                                        }
                   1489:                                } else {
                   1490:                                        telnet_printf("invalid parameter number\n");
                   1491:                                }
                   1492:                        } else if(_tcsicmp(params[0], "S") == 0) {
                   1493:                                if(num >= 4) {
                   1494:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1495:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1496:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1497:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1498:                                        UINT8 list[32];
                   1499:                                        
                   1500:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1501:                                                list[j] = debugger_get_val(params[i]);
                   1502:                                        }
                   1503:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1504:                                                bool found = true;
                   1505:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1506:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1507:                                                                found = false;
                   1508:                                                                break;
                   1509:                                                        }
                   1510:                                                }
                   1511:                                                if(found) {
                   1512:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1513:                                                }
                   1514:                                                if((cur_ofs += 1) > 0xffff) {
                   1515:                                                        cur_seg += 0x1000;
                   1516:                                                        cur_ofs -= 0x10000;
                   1517:                                                }
                   1518:                                        }
                   1519:                                } else {
                   1520:                                        telnet_printf("invalid parameter number\n");
                   1521:                                }
                   1522:                        } else if(stricmp(params[0], "U") == 0) {
                   1523:                                if(num <= 3) {
                   1524:                                        if(num >= 2) {
                   1525:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1526:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1527:                                        }
                   1528:                                        if(num == 3) {
                   1529:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1530:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1531:                                                
                   1532:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1533:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1534:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1535:                                                        for(int i = 0; i < len; i++) {
                   1536:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1537:                                                        }
                   1538:                                                        for(int i = len; i < 8; i++) {
                   1539:                                                                telnet_printf("  ");
                   1540:                                                        }
                   1541:                                                        telnet_printf("  %s\n", buffer);
                   1542:                                                        if((dasm_ofs += len) > 0xffff) {
                   1543:                                                                dasm_seg += 0x1000;
                   1544:                                                                dasm_ofs -= 0x10000;
                   1545:                                                        }
                   1546:                                                }
                   1547:                                        } else {
                   1548:                                                for(int i = 0; i < 16; i++) {
                   1549:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1550:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1551:                                                        for(int i = 0; i < len; i++) {
                   1552:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1553:                                                        }
                   1554:                                                        for(int i = len; i < 8; i++) {
                   1555:                                                                telnet_printf("  ");
                   1556:                                                        }
                   1557:                                                        telnet_printf("  %s\n", buffer);
                   1558:                                                        if((dasm_ofs += len) > 0xffff) {
                   1559:                                                                dasm_seg += 0x1000;
                   1560:                                                                dasm_ofs -= 0x10000;
                   1561:                                                        }
                   1562:                                                }
                   1563:                                        }
                   1564:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1565:                                } else {
                   1566:                                        telnet_printf("invalid parameter number\n");
                   1567:                                }
                   1568:                        } else if(stricmp(params[0], "H") == 0) {
                   1569:                                if(num == 3) {
                   1570:                                        UINT32 l = debugger_get_val(params[1]);
                   1571:                                        UINT32 r = debugger_get_val(params[2]);
                   1572:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1573:                                } else {
                   1574:                                        telnet_printf("invalid parameter number\n");
                   1575:                                }
                   1576:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1577:                                break_point_t *break_point_ptr;
                   1578:                                #define GET_BREAK_POINT_PTR() { \
1.1.1.58  root     1579:                                        if(params[0][0] == 'R' || params[0][0] == 'r') { \
1.1.1.33  root     1580:                                                break_point_ptr = &rd_break_point; \
1.1.1.58  root     1581:                                        } else if(params[0][0] == 'W' || params[0][0] == 'w') { \
1.1.1.33  root     1582:                                                break_point_ptr = &wr_break_point; \
1.1.1.58  root     1583:                                        } else if(params[0][0] == 'I' || params[0][0] == 'i') { \
1.1.1.33  root     1584:                                                break_point_ptr = &in_break_point; \
1.1.1.58  root     1585:                                        } else if(params[0][0] == 'O' || params[0][0] == 'o') { \
1.1.1.33  root     1586:                                                break_point_ptr = &out_break_point; \
                   1587:                                        } else { \
                   1588:                                                break_point_ptr = &break_point; \
                   1589:                                        } \
                   1590:                                }
                   1591:                                GET_BREAK_POINT_PTR();
                   1592:                                if(num == 2) {
1.1.1.58  root     1593:                                        UINT32 seg = 0;
                   1594:                                        if(params[0][0] == 'R' || params[0][0] == 'r' || params[0][0] == 'W' || params[0][0] == 'w') {
                   1595:                                                seg = debugger_get_seg(params[1], data_seg);
                   1596:                                        } else {
                   1597:                                                seg = debugger_get_seg(params[1], SREG(CS));
                   1598:                                        }
1.1.1.33  root     1599:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1600:                                        bool found = false;
                   1601:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1602:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1603:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1604:                                                        break_point_ptr->table[i].seg = seg;
                   1605:                                                        break_point_ptr->table[i].ofs = ofs;
                   1606:                                                        break_point_ptr->table[i].status = 1;
                   1607:                                                        found = true;
                   1608:                                                }
                   1609:                                        }
                   1610:                                        if(!found) {
                   1611:                                                telnet_printf("too many break points\n");
                   1612:                                        }
                   1613:                                } else {
                   1614:                                        telnet_printf("invalid parameter number\n");
                   1615:                                }
                   1616:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1617:                                break_point_t *break_point_ptr;
                   1618:                                GET_BREAK_POINT_PTR();
                   1619:                                if(num == 2) {
                   1620:                                        UINT32 addr = debugger_get_val(params[1]);
                   1621:                                        bool found = false;
                   1622:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1623:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1624:                                                        break_point_ptr->table[i].addr = addr;
                   1625:                                                        break_point_ptr->table[i].status = 1;
                   1626:                                                        found = true;
                   1627:                                                }
                   1628:                                        }
                   1629:                                        if(!found) {
                   1630:                                                telnet_printf("too many break points\n");
                   1631:                                        }
                   1632:                                } else {
                   1633:                                        telnet_printf("invalid parameter number\n");
                   1634:                                }
                   1635:                        } 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) {
                   1636:                                break_point_t *break_point_ptr;
                   1637:                                GET_BREAK_POINT_PTR();
                   1638:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1639:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1640:                                } else if(num >= 2) {
                   1641:                                        for(int i = 1; i < num; i++) {
                   1642:                                                int index = debugger_get_val(params[i]);
                   1643:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1644:                                                        telnet_printf("invalid index %x\n", index);
                   1645:                                                } else {
                   1646:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1647:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1648:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1649:                                                        break_point_ptr->table[index - 1].status = 0;
                   1650:                                                }
                   1651:                                        }
                   1652:                                } else {
                   1653:                                        telnet_printf("invalid parameter number\n");
                   1654:                                }
                   1655:                        } 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 ||
                   1656:                                  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) {
                   1657:                                break_point_t *break_point_ptr;
                   1658:                                GET_BREAK_POINT_PTR();
                   1659:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1660:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1661:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1662:                                                if(break_point_ptr->table[i].status != 0) {
                   1663:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1664:                                                }
                   1665:                                        }
                   1666:                                } else if(num >= 2) {
                   1667:                                        for(int i = 1; i < num; i++) {
                   1668:                                                int index = debugger_get_val(params[i]);
                   1669:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1670:                                                        telnet_printf("invalid index %x\n", index);
                   1671:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1672:                                                        telnet_printf("break point %x is null\n", index);
                   1673:                                                } else {
                   1674:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1675:                                                }
                   1676:                                        }
                   1677:                                } else {
                   1678:                                        telnet_printf("invalid parameter number\n");
                   1679:                                }
                   1680:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1681:                                break_point_t *break_point_ptr;
                   1682:                                GET_BREAK_POINT_PTR();
                   1683:                                if(num == 1) {
                   1684:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1685:                                                if(break_point_ptr->table[i].status) {
                   1686:                                                        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);
                   1687:                                                }
                   1688:                                        }
                   1689:                                } else {
                   1690:                                        telnet_printf("invalid parameter number\n");
                   1691:                                }
                   1692:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1693:                                break_point_t *break_point_ptr;
                   1694:                                GET_BREAK_POINT_PTR();
                   1695:                                if(num == 1) {
                   1696:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1697:                                                if(break_point_ptr->table[i].status) {
                   1698:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1699:                                                }
                   1700:                                        }
                   1701:                                } else {
                   1702:                                        telnet_printf("invalid parameter number\n");
                   1703:                                }
                   1704:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1705:                                if(num >= 2 && num <= 4) {
                   1706:                                        int int_num = debugger_get_val(params[1]);
                   1707:                                        UINT8 ah = 0, ah_registered = 0;
                   1708:                                        UINT8 al = 0, al_registered = 0;
                   1709:                                        if(num >= 3) {
                   1710:                                                ah = debugger_get_val(params[2]);
                   1711:                                                ah_registered = 1;
                   1712:                                        }
                   1713:                                        if(num == 4) {
                   1714:                                                al = debugger_get_val(params[3]);
                   1715:                                                al_registered = 1;
                   1716:                                        }
                   1717:                                        bool found = false;
                   1718:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1719:                                                if(int_break_point.table[i].status == 0 || (
                   1720:                                                   int_break_point.table[i].int_num == int_num &&
                   1721:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1722:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1723:                                                        int_break_point.table[i].int_num = int_num;
                   1724:                                                        int_break_point.table[i].ah = ah;
                   1725:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1726:                                                        int_break_point.table[i].al = al;
                   1727:                                                        int_break_point.table[i].al_registered = al_registered;
                   1728:                                                        int_break_point.table[i].status = 1;
                   1729:                                                        found = true;
                   1730:                                                }
                   1731:                                        }
                   1732:                                        if(!found) {
                   1733:                                                telnet_printf("too many break points\n");
                   1734:                                        }
                   1735:                                } else {
                   1736:                                        telnet_printf("invalid parameter number\n");
                   1737:                                }
                   1738:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1739:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1740:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1741:                                } else if(num >= 2) {
                   1742:                                        for(int i = 1; i < num; i++) {
                   1743:                                                int index = debugger_get_val(params[i]);
                   1744:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1745:                                                        telnet_printf("invalid index %x\n", index);
                   1746:                                                } else {
                   1747:                                                        int_break_point.table[index - 1].int_num = 0;
                   1748:                                                        int_break_point.table[index - 1].ah = 0;
                   1749:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1750:                                                        int_break_point.table[index - 1].al = 0;
                   1751:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1752:                                                        int_break_point.table[index - 1].status = 0;
                   1753:                                                }
                   1754:                                        }
                   1755:                                } else {
                   1756:                                        telnet_printf("invalid parameter number\n");
                   1757:                                }
                   1758:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1759:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1760:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1761:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1762:                                                if(int_break_point.table[i].status != 0) {
                   1763:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1764:                                                }
                   1765:                                        }
                   1766:                                } else if(num >= 2) {
                   1767:                                        for(int i = 1; i < num; i++) {
                   1768:                                                int index = debugger_get_val(params[i]);
                   1769:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1770:                                                        telnet_printf("invalid index %x\n", index);
                   1771:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1772:                                                        telnet_printf("break point %x is null\n", index);
                   1773:                                                } else {
                   1774:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1775:                                                }
                   1776:                                        }
                   1777:                                } else {
                   1778:                                        telnet_printf("invalid parameter number\n");
                   1779:                                }
                   1780:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1781:                                if(num == 1) {
                   1782:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1783:                                                if(int_break_point.table[i].status) {
                   1784:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1785:                                                        if(int_break_point.table[i].ah_registered) {
                   1786:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1787:                                                        }
                   1788:                                                        if(int_break_point.table[i].al_registered) {
                   1789:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1790:                                                        }
                   1791:                                                        telnet_printf("\n");
                   1792:                                                }
                   1793:                                        }
                   1794:                                } else {
                   1795:                                        telnet_printf("invalid parameter number\n");
                   1796:                                }
                   1797:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1798:                                if(num == 1 || num == 2) {
                   1799:                                        break_point_t break_point_stored;
                   1800:                                        bool break_points_stored = false;
                   1801:                                        
                   1802:                                        if(stricmp(params[0], "P") == 0) {
                   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:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1808:                                                break_point.table[0].status = 1;
                   1809:                                        } else if(num >= 2) {
                   1810:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1811:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1812:                                                break_points_stored = true;
                   1813:                                                
                   1814:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1815:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1816:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1817:                                                break_point.table[0].seg = seg;
                   1818:                                                break_point.table[0].ofs = ofs;
                   1819:                                                break_point.table[0].status = 1;
                   1820:                                        }
                   1821:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1822:                                        now_going = true;
                   1823:                                        now_suspended = false;
                   1824:                                        
                   1825:                                        telnet_command("\033[2l"); // key unlock
1.1.1.54  root     1826:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1827:                                                if(telnet_kbhit()) {
                   1828:                                                        break;
                   1829:                                                }
                   1830:                                                Sleep(10);
                   1831:                                        }
                   1832:                                        now_going = false;
                   1833:                                        telnet_command("\033[2h"); // key lock
                   1834:                                        
                   1835:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1836:                                                Sleep(100);
1.1.1.54  root     1837:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1838:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1839:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1840:                                                }
                   1841:                                        }
1.1.1.54  root     1842:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1843:                                                if(telnet_disconnected()) {
                   1844:                                                        break;
                   1845:                                                }
                   1846:                                                Sleep(10);
                   1847:                                        }
                   1848:                                        dasm_seg = SREG(CS);
                   1849:                                        dasm_ofs = m_eip;
                   1850:                                        
                   1851:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1852:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1853:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1854:                                        
                   1855:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1856:                                        debugger_regs_info(buffer);
                   1857:                                        telnet_printf("%s", buffer);
                   1858:                                        
                   1859:                                        if(break_point.hit) {
                   1860:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1861:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1862:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1863:                                                }
                   1864:                                        } else if(rd_break_point.hit) {
                   1865:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1866:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1867:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1868:                                                m_prev_cs, m_prev_eip);
                   1869:                                        } else if(wr_break_point.hit) {
                   1870:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1871:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1872:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1873:                                                m_prev_cs, m_prev_eip);
                   1874:                                        } else if(in_break_point.hit) {
                   1875:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1876:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1877:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1878:                                                m_prev_cs, m_prev_eip);
                   1879:                                        } else if(out_break_point.hit) {
                   1880:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1881:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1882:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1883:                                                m_prev_cs, m_prev_eip);
                   1884:                                        } else if(int_break_point.hit) {
                   1885:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1886:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1887:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1888:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1889:                                                }
                   1890:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1891:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1892:                                                }
                   1893:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1894:                                        } else {
                   1895:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1896:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1897:                                        }
                   1898:                                        if(break_points_stored) {
                   1899:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1900:                                        }
                   1901:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1902:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1903:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1904:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1905:                                } else {
                   1906:                                        telnet_printf("invalid parameter number\n");
                   1907:                                }
                   1908:                        } else if(stricmp(params[0], "T") == 0) {
                   1909:                                if(num == 1 || num == 2) {
                   1910:                                        int steps = 1;
                   1911:                                        if(num >= 2) {
                   1912:                                                steps = debugger_get_val(params[1]);
                   1913:                                        }
                   1914:                                        
                   1915:                                        telnet_command("\033[2l"); // key unlock
                   1916:                                        while(steps-- > 0) {
                   1917:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1918:                                                now_going = false;
                   1919:                                                now_suspended = false;
                   1920:                                                
1.1.1.54  root     1921:                                                while(!m_exit && !now_suspended) {
1.1.1.33  root     1922:                                                        if(telnet_disconnected()) {
                   1923:                                                                break;
                   1924:                                                        }
                   1925:                                                        Sleep(10);
                   1926:                                                }
                   1927:                                                dasm_seg = SREG(CS);
                   1928:                                                dasm_ofs = m_eip;
                   1929:                                                
                   1930:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1931:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1932:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1933:                                                
                   1934:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1935:                                                debugger_regs_info(buffer);
                   1936:                                                telnet_printf("%s", buffer);
                   1937:                                                
                   1938:                                                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()) {
                   1939:                                                        break;
                   1940:                                                }
                   1941:                                        }
                   1942:                                        telnet_command("\033[2h"); // key lock
                   1943:                                        
                   1944:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1945:                                                Sleep(100);
1.1.1.54  root     1946:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1947:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1948:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1949:                                                }
                   1950:                                        }
1.1.1.54  root     1951:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1952:                                                if(telnet_disconnected()) {
                   1953:                                                        break;
                   1954:                                                }
                   1955:                                                Sleep(10);
                   1956:                                        }
                   1957:                                        if(break_point.hit) {
                   1958:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1959:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1960:                                        } else if(rd_break_point.hit) {
                   1961:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1962:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1963:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1964:                                                m_prev_cs, m_prev_eip);
                   1965:                                        } else if(wr_break_point.hit) {
                   1966:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1967:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1968:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1969:                                                m_prev_cs, m_prev_eip);
                   1970:                                        } else if(in_break_point.hit) {
                   1971:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1972:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1973:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1974:                                                m_prev_cs, m_prev_eip);
                   1975:                                        } else if(out_break_point.hit) {
                   1976:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1977:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1978:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1979:                                                m_prev_cs, m_prev_eip);
                   1980:                                        } else if(int_break_point.hit) {
                   1981:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1982:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1983:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1984:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1985:                                                }
                   1986:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1987:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1988:                                                }
                   1989:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1990:                                        } else if(steps > 0) {
                   1991:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1992:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1993:                                        }
                   1994:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1995:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1996:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1997:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1998:                                } else {
                   1999:                                        telnet_printf("invalid parameter number\n");
                   2000:                                }
                   2001:                        } else if(stricmp(params[0], "Q") == 0) {
                   2002:                                break;
                   2003:                        } else if(stricmp(params[0], "X") == 0) {
                   2004:                                debugger_process_info(buffer);
                   2005:                                telnet_printf("%s", buffer);
                   2006:                        } else if(stricmp(params[0], ">") == 0) {
                   2007:                                if(num == 2) {
                   2008:                                        if(fp_debugger != NULL) {
                   2009:                                                fclose(fp_debugger);
                   2010:                                                fp_debugger = NULL;
                   2011:                                        }
                   2012:                                        fp_debugger = fopen(params[1], "w");
                   2013:                                } else {
                   2014:                                        telnet_printf("invalid parameter number\n");
                   2015:                                }
                   2016:                        } else if(stricmp(params[0], "<") == 0) {
                   2017:                                if(num == 2) {
                   2018:                                        if(fi_debugger != NULL) {
                   2019:                                                fclose(fi_debugger);
                   2020:                                                fi_debugger = NULL;
                   2021:                                        }
                   2022:                                        fi_debugger = fopen(params[1], "r");
                   2023:                                } else {
                   2024:                                        telnet_printf("invalid parameter number\n");
                   2025:                                }
                   2026:                        } else if(stricmp(params[0], "?") == 0) {
                   2027:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   2028:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   2029:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   2030:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   2031:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   2032:                                
                   2033:                                telnet_printf("R - show registers\n");
                   2034:                                telnet_printf("R <reg> <value> - edit register\n");
                   2035:                                telnet_printf("S <start> <end> <list> - search\n");
                   2036:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   2037:                                
                   2038:                                telnet_printf("H <value> <value> - hexadd\n");
                   2039:                                
                   2040:                                telnet_printf("BP <address> - set breakpoint\n");
                   2041:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   2042:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   2043:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   2044:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   2045:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   2046:                                
                   2047:                                telnet_printf("G - go (press enter key to break)\n");
                   2048:                                telnet_printf("G <address> - go and break at address\n");
                   2049:                                telnet_printf("P - trace one opcode (step over)\n");
                   2050:                                telnet_printf("T [<count>] - trace (step in)\n");
                   2051:                                telnet_printf("Q - quit\n");
                   2052:                                telnet_printf("X - show dos process info\n");
                   2053:                                
                   2054:                                telnet_printf("> <filename> - output logfile\n");
                   2055:                                telnet_printf("< <filename> - input commands from file\n");
                   2056:                                
                   2057:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   2058:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   2059:                        } else {
                   2060:                                telnet_printf("unknown command %s\n", params[0]);
                   2061:                        }
                   2062:                }
                   2063:        }
                   2064:        if(fp_debugger != NULL) {
                   2065:                fclose(fp_debugger);
                   2066:                fp_debugger = NULL;
                   2067:        }
                   2068:        if(fi_debugger != NULL) {
                   2069:                fclose(fi_debugger);
                   2070:                fi_debugger = NULL;
                   2071:        }
                   2072:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2073:        closesocket(cli_socket);
                   2074: }
                   2075: 
                   2076: const char *debugger_get_ttermpro_path()
                   2077: {
                   2078:        static char path[MAX_PATH] = {0};
                   2079:        
                   2080:        if(getenv("ProgramFiles")) {
                   2081:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2082:        }
                   2083:        return(path);
                   2084: }
                   2085: 
                   2086: const char *debugger_get_ttermpro_x86_path()
                   2087: {
                   2088:        static char path[MAX_PATH] = {0};
                   2089:        
                   2090:        if(getenv("ProgramFiles(x86)")) {
                   2091:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2092:        }
                   2093:        return(path);
                   2094: }
                   2095: 
                   2096: const char *debugger_get_putty_path()
                   2097: {
                   2098:        static char path[MAX_PATH] = {0};
                   2099:        
                   2100:        if(getenv("ProgramFiles")) {
                   2101:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2102:        }
                   2103:        return(path);
                   2104: }
                   2105: 
                   2106: const char *debugger_get_putty_x86_path()
                   2107: {
                   2108:        static char path[MAX_PATH] = {0};
                   2109:        
                   2110:        if(getenv("ProgramFiles(x86)")) {
                   2111:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2112:        }
                   2113:        return(path);
                   2114: }
                   2115: 
                   2116: const char *debugger_get_telnet_path()
                   2117: {
                   2118:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2119:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2120:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2121:        // and 64bit version of telnet.exe will be installed in System32.
                   2122:        static char path[MAX_PATH] = {0};
                   2123:        
                   2124:        if(getenv("windir") != NULL) {
                   2125:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2126:        }
                   2127:        return(path);
                   2128: }
                   2129: 
                   2130: DWORD WINAPI debugger_thread(LPVOID)
                   2131: {
                   2132:        WSADATA was_data;
                   2133:        struct sockaddr_in svr_addr;
                   2134:        struct sockaddr_in cli_addr;
                   2135:        int cli_addr_len = sizeof(cli_addr);
                   2136:        int port = 23;
                   2137:        int bind_stat = SOCKET_ERROR;
                   2138:        struct timeval timeout;
                   2139:        
                   2140:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2141:        
                   2142:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2143:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2144:                svr_addr.sin_family = AF_INET;
                   2145:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2146:                
1.1.1.54  root     2147:                while(!m_exit && port < 10000) {
1.1.1.33  root     2148:                        svr_addr.sin_port = htons(port);
                   2149:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2150:                                break;
                   2151:                        } else {
                   2152:                                port = (port == 23) ? 9000 : (port + 1);
                   2153:                        }
                   2154:                }
                   2155:                if(bind_stat == 0) {
                   2156:                        timeout.tv_sec = 1;
                   2157:                        timeout.tv_usec = 0;
1.1.1.45  root     2158:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2159:                        
                   2160:                        listen(svr_socket, 1);
                   2161:                        
                   2162:                        char command[MAX_PATH] = {0};
                   2163:                        STARTUPINFO si;
                   2164:                        PROCESS_INFORMATION pi;
                   2165:                        
                   2166:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2167:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2168:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2169:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2170:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2171:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2172:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2173:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2174:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2175:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2176:                        }
                   2177:                        if(command[0] != '\0') {
                   2178:                                memset(&si, 0, sizeof(STARTUPINFO));
                   2179:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
                   2180:                                CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
                   2181:                        }
                   2182:                        
1.1.1.54  root     2183:                        while(!m_exit) {
1.1.1.33  root     2184:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2185:                                        u_long val = 1;
                   2186:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2187:                                        debugger_main();
                   2188:                                }
                   2189:                        }
                   2190:                }
                   2191:        }
                   2192:        WSACleanup();
                   2193:        return(0);
                   2194: }
                   2195: #endif
                   2196: 
                   2197: /* ----------------------------------------------------------------------------
1.1       root     2198:        main
                   2199: ---------------------------------------------------------------------------- */
                   2200: 
1.1.1.28  root     2201: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2202: {
                   2203:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2204:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2205: #ifdef USE_SERVICE_THREAD
                   2206:                        EnterCriticalSection(&key_buf_crit_sect);
                   2207: #endif
1.1.1.51  root     2208:                        pcbios_clear_key_buffer();
1.1.1.35  root     2209: #ifdef USE_SERVICE_THREAD
                   2210:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2211: #endif
1.1.1.33  root     2212:                }
                   2213: //             key_code = key_recv = 0;
1.1.1.28  root     2214:                return TRUE;
                   2215:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2216:                return TRUE;
                   2217:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2218:                // this program will be terminated abnormally, do minimum end process
                   2219:                exit_handler();
                   2220:                exit(1);
                   2221:        }
                   2222:        return FALSE;
                   2223: }
                   2224: 
                   2225: void exit_handler()
                   2226: {
                   2227:        if(temp_file_created) {
                   2228:                DeleteFile(temp_file_path);
                   2229:                temp_file_created = false;
                   2230:        }
                   2231:        if(key_buf_char != NULL) {
                   2232:                key_buf_char->release();
                   2233:                delete key_buf_char;
                   2234:                key_buf_char = NULL;
                   2235:        }
                   2236:        if(key_buf_scan != NULL) {
                   2237:                key_buf_scan->release();
                   2238:                delete key_buf_scan;
                   2239:                key_buf_scan = NULL;
                   2240:        }
1.1.1.57  root     2241:        if(key_buf_data != NULL) {
                   2242:                key_buf_data->release();
                   2243:                delete key_buf_data;
                   2244:                key_buf_data = NULL;
                   2245:        }
1.1.1.32  root     2246: #ifdef SUPPORT_XMS
                   2247:        msdos_xms_release();
                   2248: #endif
1.1.1.28  root     2249:        hardware_release();
                   2250: }
                   2251: 
1.1.1.35  root     2252: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2253: DWORD WINAPI vram_thread(LPVOID)
                   2254: {
1.1.1.54  root     2255:        while(!m_exit) {
1.1.1.28  root     2256:                EnterCriticalSection(&vram_crit_sect);
                   2257:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2258:                        vram_flush_char();
                   2259:                }
                   2260:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2261:                        vram_flush_attr();
                   2262:                }
                   2263:                vram_last_length_char = vram_length_char;
                   2264:                vram_last_length_attr = vram_length_attr;
                   2265:                LeaveCriticalSection(&vram_crit_sect);
                   2266:                // this is about half the maximum keyboard repeat rate - any
                   2267:                // lower tends to be jerky, any higher misses updates
                   2268:                Sleep(15);
                   2269:        }
                   2270:        return 0;
                   2271: }
                   2272: #endif
                   2273: 
1.1.1.45  root     2274: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2275: {
                   2276:        UINT8 header[0x400];
                   2277:        
                   2278:        long position = ftell(fp);
                   2279:        fseek(fp, 0, SEEK_SET);
                   2280:        fread(header, sizeof(header), 1, fp);
                   2281:        fseek(fp, position, SEEK_SET);
                   2282:        
                   2283:        try {
                   2284:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2285:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2286:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2287:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2288:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2289:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2290:                
                   2291:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2292:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2293:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2294:                                return(sectionHeader->PointerToRawData);
                   2295:                        }
                   2296:                }
                   2297:        } catch(...) {
                   2298:        }
                   2299:        return(0);
                   2300: }
                   2301: 
1.1.1.10  root     2302: bool is_started_from_command_prompt()
                   2303: {
1.1.1.58  root     2304:        bool result = false;
1.1.1.18  root     2305:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
1.1.1.58  root     2306:        
1.1.1.18  root     2307:        if(hLibrary) {
                   2308:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2309:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2310:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
1.1.1.58  root     2311:                if(lpfnGetConsoleProcessList) { // Windows XP or later
1.1.1.18  root     2312:                        DWORD pl;
1.1.1.58  root     2313:                        result = (lpfnGetConsoleProcessList(&pl, 1) > 1);
1.1.1.18  root     2314:                        FreeLibrary(hLibrary);
1.1.1.58  root     2315:                        return(result);
1.1.1.18  root     2316:                }
                   2317:                FreeLibrary(hLibrary);
                   2318:        }
                   2319:        
                   2320:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2321:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2322:                DWORD dwParentProcessID = 0;
                   2323:                PROCESSENTRY32 pe32;
                   2324:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2325:                if(Process32First(hSnapshot, &pe32)) {
                   2326:                        do {
                   2327:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2328:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2329:                                        break;
                   2330:                                }
                   2331:                        } while(Process32Next(hSnapshot, &pe32));
                   2332:                }
                   2333:                CloseHandle(hSnapshot);
                   2334:                if(dwParentProcessID != 0) {
                   2335:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2336:                        if(hProcess != NULL) {
                   2337:                                HMODULE hMod;
                   2338:                                DWORD cbNeeded;
                   2339:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2340:                                        char module_name[MAX_PATH];
                   2341:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
1.1.1.58  root     2342:                                                result = (_strnicmp(module_name, "cmd.exe", 7) == 0);
1.1.1.18  root     2343:                                        }
                   2344:                                }
                   2345:                                CloseHandle(hProcess);
                   2346:                        }
                   2347:                }
                   2348:        }
1.1.1.58  root     2349:        return(result);
1.1.1.14  root     2350: }
                   2351: 
                   2352: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2353: {
1.1.1.24  root     2354:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2355:        OSVERSIONINFOEX osvi;
                   2356:        DWORDLONG dwlConditionMask = 0;
                   2357:        int op = VER_GREATER_EQUAL;
                   2358:        
                   2359:        // Initialize the OSVERSIONINFOEX structure.
                   2360:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2361:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2362:        osvi.dwMajorVersion = dwMajorVersion;
                   2363:        osvi.dwMinorVersion = dwMinorVersion;
                   2364:        osvi.wServicePackMajor = wServicePackMajor;
                   2365:        osvi.wServicePackMinor = wServicePackMinor;
                   2366:        
                   2367:         // Initialize the condition mask.
                   2368:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2369:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2370:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2371:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2372:        
                   2373:        // Perform the test.
                   2374:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2375: }
                   2376: 
1.1.1.58  root     2377: bool get_console_font_size(HANDLE hStdout, int *width, int *height)
                   2378: {
                   2379:        bool result = false;
                   2380:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2381:        
                   2382:        if(hLibrary) {
                   2383:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
                   2384:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
                   2385:                if(lpfnGetCurrentConsoleFont) { // Windows XP or later
                   2386:                        CONSOLE_FONT_INFO fi;
                   2387:                        if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi) != 0) {
                   2388:                                *width  = fi.dwFontSize.X;
                   2389:                                *height = fi.dwFontSize.Y;
                   2390:                                result = true;
                   2391:                        }
                   2392:                }
                   2393:                FreeLibrary(hLibrary);
                   2394:        }
                   2395:        return(result);
                   2396: }
                   2397: 
1.1.1.56  root     2398: bool set_console_font_size(HANDLE hStdout, int width, int height)
                   2399: {
                   2400:        // http://d.hatena.ne.jp/aharisu/20090427/1240852598
                   2401:        bool result = false;
                   2402:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2403:        
                   2404:        if(hLibrary) {
                   2405:                typedef BOOL (WINAPI* GetConsoleFontInfoFunction)(HANDLE, BOOL, DWORD, PCONSOLE_FONT_INFO);
                   2406:                typedef DWORD (WINAPI* GetNumberOfConsoleFontsFunction)(VOID);
                   2407:                typedef BOOL (WINAPI* SetConsoleFontFunction)(HANDLE, DWORD);
                   2408:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
                   2409:                
                   2410:                GetConsoleFontInfoFunction lpfnGetConsoleFontInfo = reinterpret_cast<GetConsoleFontInfoFunction>(::GetProcAddress(hLibrary, "GetConsoleFontInfo"));
                   2411:                GetNumberOfConsoleFontsFunction lpfnGetNumberOfConsoleFonts = reinterpret_cast<GetNumberOfConsoleFontsFunction>(::GetProcAddress(hLibrary, "GetNumberOfConsoleFonts"));
                   2412:                SetConsoleFontFunction lpfnSetConsoleFont = reinterpret_cast<SetConsoleFontFunction>(::GetProcAddress(hLibrary, "SetConsoleFont"));
                   2413:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
                   2414:                
                   2415:                if(lpfnGetConsoleFontInfo && lpfnGetNumberOfConsoleFonts && lpfnSetConsoleFont) {
1.1.1.58  root     2416:                        static const int offsets[] = {0, +1, -1, +2, -2, +3, -3};
1.1.1.56  root     2417:                        DWORD dwFontNum = lpfnGetNumberOfConsoleFonts();
                   2418:                        CONSOLE_FONT_INFO* fonts = (CONSOLE_FONT_INFO*)malloc(sizeof(CONSOLE_FONT_INFO) * dwFontNum);
                   2419:                        lpfnGetConsoleFontInfo(hStdout, FALSE, dwFontNum, fonts);
                   2420:                        for(int i = 0; i < dwFontNum; i++) {
                   2421:                                fonts[i].dwFontSize = GetConsoleFontSize(hStdout, fonts[i].nFont);
                   2422:                        }
1.1.1.58  root     2423:                        for(int h = 0; h < 5; h++) { // 0, +1, -1, +2, -2
                   2424:                                int height_tmp = height + offsets[h];
                   2425:                                for(int w = 0; w < 7; w++) { // 0, +1, -1, +2, -2, +3, -3
                   2426:                                        int width_tmp = width + offsets[w];
                   2427:                                        for(int i = 0; i < dwFontNum; i++) {
                   2428:                                                if(fonts[i].dwFontSize.X == width_tmp && fonts[i].dwFontSize.Y == height_tmp) {
                   2429:                                                        lpfnSetConsoleFont(hStdout, fonts[i].nFont);
                   2430:                                                        if(lpfnGetCurrentConsoleFont) { // Windows XP or later
                   2431:                                                                CONSOLE_FONT_INFO fi;
                   2432:                                                                if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi)) {
                   2433:                                                                        if(fi.dwFontSize.X == width_tmp && fi.dwFontSize.Y == height_tmp) {
                   2434:                                                                                result = true;
                   2435:                                                                        }
                   2436:                                                                }
                   2437:                                                        } else {
                   2438:                                                                result = true;
                   2439:                                                        }
                   2440:                                                }
                   2441:                                                if(result) {
                   2442:                                                        *(UINT16 *)(mem + 0x485) = height_tmp;
                   2443:                                                        goto exit_loop;
                   2444:                                                }
                   2445:                                        }
1.1.1.57  root     2446:                                }
1.1.1.56  root     2447:                        }
1.1.1.58  root     2448: exit_loop:
                   2449:                        free(fonts);
1.1.1.56  root     2450:                }
                   2451:                FreeLibrary(hLibrary);
                   2452:        }
                   2453:        return(result);
                   2454: }
                   2455: 
1.1.1.59! root     2456: bool is_cursor_blink_off()
        !          2457: {
        !          2458:        static int result = -1;
        !          2459:        HKEY hKey;
        !          2460:        char chData[64];
        !          2461:        DWORD dwSize = sizeof(chData);
        !          2462:        
        !          2463:        if(result == -1) {
        !          2464:                result = 0;
        !          2465:                if(RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
        !          2466:                        if(RegQueryValueEx(hKey, "CursorBlinkRate", NULL, NULL, (LPBYTE)chData, &dwSize) == ERROR_SUCCESS) {
        !          2467:                                if(strncmp(chData, "-1", 2) == 0) {
        !          2468:                                        result = 1;
        !          2469:                                }
        !          2470:                        }
        !          2471:                        RegCloseKey(hKey);
        !          2472:                }
        !          2473:        }
        !          2474:        return(result != 0);
        !          2475: }
        !          2476: 
1.1.1.27  root     2477: void get_sio_port_numbers()
                   2478: {
                   2479:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2480:        HDEVINFO hDevInfo = 0;
                   2481:        HKEY hKey = 0;
                   2482:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2483:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2484:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2485:                                char chData[256];
                   2486:                                DWORD dwType = 0;
                   2487:                                DWORD dwSize = sizeof(chData);
                   2488:                                int port_number = 0;
                   2489:                                
                   2490:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2491:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2492:                                                port_number = atoi(chData + 3);
                   2493:                                        }
                   2494:                                }
                   2495:                                RegCloseKey(hKey);
                   2496:                                
1.1.1.29  root     2497:                                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     2498:                                        continue;
                   2499:                                }
                   2500:                                if(sio_port_number[0] == 0) {
                   2501:                                        sio_port_number[0] = port_number;
                   2502:                                } else if(sio_port_number[1] == 0) {
                   2503:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2504:                                } else if(sio_port_number[2] == 0) {
                   2505:                                        sio_port_number[2] = port_number;
                   2506:                                } else if(sio_port_number[3] == 0) {
                   2507:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2508:                                }
1.1.1.29  root     2509:                                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     2510:                                        break;
                   2511:                                }
                   2512:                        }
                   2513:                }
                   2514:        }
                   2515: }
                   2516: 
1.1.1.28  root     2517: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2518: 
1.1       root     2519: int main(int argc, char *argv[], char *envp[])
                   2520: {
1.1.1.9   root     2521:        int arg_offset = 0;
                   2522:        int standard_env = 0;
1.1.1.14  root     2523:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2524:        bool get_console_info_success = false;
1.1.1.56  root     2525:        bool get_console_font_success = false;
1.1.1.28  root     2526:        bool screen_size_changed = false;
                   2527:        
                   2528:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2529:        GetModuleFileName(NULL, path, MAX_PATH);
                   2530:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2531:        
1.1.1.27  root     2532:        char dummy_argv_0[] = "msdos.exe";
                   2533:        char dummy_argv_1[MAX_PATH];
                   2534:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2535:        char new_exec_file[MAX_PATH];
                   2536:        bool convert_cmd_file = false;
1.1.1.28  root     2537:        unsigned int code_page = 0;
1.1.1.27  root     2538:        
                   2539:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2540:                // check if command file is embedded to this execution file
                   2541:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2542:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2543:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2544:                if(offset != 0) {
1.1.1.30  root     2545:                        UINT8 buffer[16];
1.1.1.28  root     2546:                        fseek(fp, offset, SEEK_SET);
                   2547:                        fread(buffer, sizeof(buffer), 1, fp);
                   2548:                        
                   2549:                        // restore flags
                   2550:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2551:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2552:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2553:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2554:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2555:                        if((buffer[0] & 0x20) != 0) {
                   2556:                                get_sio_port_numbers();
                   2557:                        }
                   2558:                        if((buffer[0] & 0x40) != 0) {
                   2559:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2560:                                support_ems = true;
1.1.1.30  root     2561:                        }
1.1.1.27  root     2562: #ifdef SUPPORT_XMS
1.1.1.30  root     2563:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2564:                                support_xms = true;
                   2565:                        }
1.1.1.30  root     2566: #endif
1.1.1.28  root     2567:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2568:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2569:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2570:                        }
                   2571:                        if(buffer[5] != 0) {
1.1.1.30  root     2572:                                dos_major_version = buffer[5];
                   2573:                                dos_minor_version = buffer[6];
                   2574:                        }
                   2575:                        if(buffer[7] != 0) {
                   2576:                                win_major_version = buffer[7];
                   2577:                                win_minor_version = buffer[8];
1.1.1.28  root     2578:                        }
1.1.1.30  root     2579:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2580:                                SetConsoleCP(code_page);
                   2581:                                SetConsoleOutputCP(code_page);
                   2582:                        }
1.1.1.30  root     2583:                        int name_len = buffer[11];
                   2584:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2585:                        
                   2586:                        // restore command file name
                   2587:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2588:                        fread(dummy_argv_1, name_len, 1, fp);
                   2589:                        
                   2590:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2591:                                // if original command file exists, create a temporary file name
                   2592:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2593:                                        // create a temporary command file in the current director
                   2594:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2595:                                } else {
1.1.1.28  root     2596:                                        // create a temporary command file in the temporary folder
                   2597:                                        GetTempPath(MAX_PATH, path);
                   2598:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2599:                                                DeleteFile(dummy_argv_1);
                   2600:                                        } else {
                   2601:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2602:                                        }
1.1.1.27  root     2603:                                }
1.1.1.28  root     2604:                                // check the command file type
                   2605:                                fread(buffer, 2, 1, fp);
                   2606:                                fseek(fp, -2, SEEK_CUR);
                   2607:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2608:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2609:                                } else {
                   2610:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2611:                                }
                   2612:                        }
1.1.1.28  root     2613:                        
                   2614:                        // restore command file
                   2615:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2616:                        for(int i = 0; i < file_len; i++) {
                   2617:                                fputc(fgetc(fp), fo);
                   2618:                        }
                   2619:                        fclose(fo);
                   2620:                        
                   2621:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2622:                        temp_file_created = true;
                   2623:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2624:                        
                   2625:                        // adjust argc/argv
                   2626:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2627:                                dummy_argv[i + 1] = argv[i];
                   2628:                        }
                   2629:                        argc++;
                   2630:                        argv = dummy_argv;
1.1.1.27  root     2631:                }
                   2632:                fclose(fp);
                   2633:        }
1.1.1.9   root     2634:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2635:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2636:                        stay_busy = true;
                   2637:                        arg_offset++;
1.1.1.27  root     2638:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2639:                        if(argv[i][2] != '\0') {
                   2640:                                strcpy(new_exec_file, &argv[i][2]);
                   2641:                        } else {
                   2642:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2643:                        }
                   2644:                        convert_cmd_file = true;
                   2645:                        arg_offset++;
1.1.1.28  root     2646:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2647:                        if(IS_NUMERIC(argv[i][2])) {
                   2648:                                code_page = atoi(&argv[i][2]);
                   2649:                        } else {
                   2650:                                code_page = GetConsoleCP();
                   2651:                        }
                   2652:                        arg_offset++;
1.1.1.25  root     2653:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2654:                        no_windows = true;
                   2655:                        arg_offset++;
                   2656:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2657:                        standard_env = 1;
                   2658:                        arg_offset++;
1.1.1.14  root     2659:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2660:                        ignore_illegal_insn = true;
                   2661:                        arg_offset++;
                   2662:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2663:                        limit_max_memory = true;
                   2664:                        arg_offset++;
                   2665:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51  root     2666:                        int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
                   2667:                        if(result == 1) {
                   2668:                                buf_width = 0;
                   2669:                        } else if(result != 2) {
1.1.1.17  root     2670:                                buf_width = buf_height = 0;
                   2671:                        }
                   2672:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2673:                                buf_width = 80;
                   2674:                        }
                   2675:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2676:                                buf_height = 25;
                   2677:                        }
1.1.1.14  root     2678:                        arg_offset++;
1.1.1.25  root     2679:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2680:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2681:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2682:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2683:                                        sio_port_number[1] = atoi(p1 + 1);
                   2684:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2685:                                                sio_port_number[2] = atoi(p2 + 1);
                   2686:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2687:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2688:                                                }
                   2689:                                        }
1.1.1.25  root     2690:                                }
1.1.1.29  root     2691:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2692:                        }
1.1.1.29  root     2693:                        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     2694:                                get_sio_port_numbers();
1.1.1.25  root     2695:                        }
                   2696:                        arg_offset++;
1.1.1.9   root     2697:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2698:                        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     2699:                                dos_major_version = argv[i][2] - '0';
                   2700:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2701:                        }
                   2702:                        arg_offset++;
                   2703:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2704:                        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]))) {
                   2705:                                win_major_version = argv[i][2] - '0';
                   2706:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2707:                        }
                   2708:                        arg_offset++;
1.1.1.25  root     2709:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2710:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2711:                        support_ems = true;
                   2712: #ifdef SUPPORT_XMS
                   2713:                        support_xms = true;
                   2714: #endif
                   2715:                        arg_offset++;
1.1.1.9   root     2716:                } else {
                   2717:                        break;
                   2718:                }
                   2719:        }
                   2720:        if(argc < 2 + arg_offset) {
1.1       root     2721: #ifdef _WIN64
1.1.1.14  root     2722:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2723: #else
1.1.1.14  root     2724:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2725: #endif
1.1.1.25  root     2726:                fprintf(stderr,
1.1.1.28  root     2727:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2728:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2729:                        "\n"
                   2730:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2731: #ifdef _WIN64
1.1.1.27  root     2732:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2733: #else
1.1.1.27  root     2734:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2735: #endif
1.1.1.28  root     2736:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2737:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2738:                        "\t-e\tuse a reduced environment block\n"
                   2739:                        "\t-i\tignore invalid instructions\n"
                   2740:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2741:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2742:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2743:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2744:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2745: #ifdef SUPPORT_XMS
1.1.1.28  root     2746:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2747: #else
1.1.1.28  root     2748:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2749: #endif
                   2750:                );
1.1.1.10  root     2751:                
                   2752:                if(!is_started_from_command_prompt()) {
                   2753:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2754:                        while(!_kbhit()) {
                   2755:                                Sleep(10);
                   2756:                        }
                   2757:                }
1.1.1.20  root     2758: #ifdef _DEBUG
                   2759:                _CrtDumpMemoryLeaks();
                   2760: #endif
1.1       root     2761:                return(EXIT_FAILURE);
                   2762:        }
1.1.1.27  root     2763:        if(convert_cmd_file) {
                   2764:                retval = EXIT_FAILURE;
1.1.1.28  root     2765:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2766:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2767:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2768:                        
1.1.1.28  root     2769:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2770:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2771:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2772:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2773:                        } else {
1.1.1.28  root     2774:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2775:                                if(offset != 0) {
                   2776:                                        UINT8 buffer[14];
                   2777:                                        fseek(fp, offset, SEEK_SET);
                   2778:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2779:                                        memset(path, 0, sizeof(path));
                   2780:                                        fread(path, buffer[9], 1, fp);
                   2781:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2782:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2783:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2784:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2785:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2786:                                } else {
                   2787:                                        // read pe header of msdos.exe
                   2788:                                        UINT8 header[0x400];
                   2789:                                        fseek(fp, 0, SEEK_SET);
                   2790:                                        fread(header, sizeof(header), 1, fp);
                   2791:                                        
                   2792:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2793:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2794:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2795:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2796:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2797:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2798:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2799:                                        
                   2800:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2801:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2802:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2803:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2804:                                        if(dwExtraLastSectionBytes != 0) {
                   2805:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2806:                                                dwLastSectionSize += dwRemain;
                   2807:                                        }
                   2808:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2809:                                        
                   2810:                                        // store msdos.exe
                   2811:                                        fseek(fp, 0, SEEK_SET);
                   2812:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2813:                                                if((data = fgetc(fp)) != EOF) {
                   2814:                                                        fputc(data, fo);
                   2815:                                                } else {
                   2816:                                                        // we should not reach here :-(
                   2817:                                                        fputc(0, fo);
                   2818:                                                }
                   2819:                                        }
                   2820:                                        
                   2821:                                        // store options
                   2822:                                        UINT8 flags = 0;
                   2823:                                        if(stay_busy) {
                   2824:                                                flags |= 0x01;
                   2825:                                        }
                   2826:                                        if(no_windows) {
                   2827:                                                flags |= 0x02;
                   2828:                                        }
                   2829:                                        if(standard_env) {
                   2830:                                                flags |= 0x04;
                   2831:                                        }
                   2832:                                        if(ignore_illegal_insn) {
                   2833:                                                flags |= 0x08;
                   2834:                                        }
                   2835:                                        if(limit_max_memory) {
                   2836:                                                flags |= 0x10;
                   2837:                                        }
1.1.1.29  root     2838:                                        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     2839:                                                flags |= 0x20;
                   2840:                                        }
                   2841:                                        if(support_ems) {
                   2842:                                                flags |= 0x40;
                   2843:                                        }
1.1.1.30  root     2844: #ifdef SUPPORT_XMS
                   2845:                                        if(support_xms) {
                   2846:                                                flags |= 0x80;
                   2847:                                        }
                   2848: #endif
1.1.1.28  root     2849:                                        
                   2850:                                        fputc(flags, fo);
                   2851:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2852:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2853:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2854:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2855:                                        fputc(dos_major_version, fo);
                   2856:                                        fputc(dos_minor_version, fo);
                   2857:                                        fputc(win_major_version, fo);
                   2858:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2859:                                        fputc((code_page >> 0) & 0xff, fo);
                   2860:                                        fputc((code_page >> 8) & 0xff, fo);
                   2861:                                        
                   2862:                                        // store command file info
                   2863:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2864:                                        int name_len = strlen(name);
                   2865:                                        fseek(fs, 0, SEEK_END);
                   2866:                                        long file_size = ftell(fs);
                   2867:                                        
                   2868:                                        fputc(name_len, fo);
                   2869:                                        fputc((file_size >>  0) & 0xff, fo);
                   2870:                                        fputc((file_size >>  8) & 0xff, fo);
                   2871:                                        fputc((file_size >> 16) & 0xff, fo);
                   2872:                                        fputc((file_size >> 24) & 0xff, fo);
                   2873:                                        fwrite(name, name_len, 1, fo);
                   2874:                                        
                   2875:                                        // store command file
                   2876:                                        fseek(fs, 0, SEEK_SET);
                   2877:                                        for(int i = 0; i < file_size; i++) {
                   2878:                                                if((data = fgetc(fs)) != EOF) {
                   2879:                                                        fputc(data, fo);
                   2880:                                                } else {
                   2881:                                                        // we should not reach here :-(
                   2882:                                                        fputc(0, fo);
                   2883:                                                }
                   2884:                                        }
                   2885:                                        
                   2886:                                        // store padding data and update pe header
1.1.1.29  root     2887:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2888:                                        coffHeader->NumberOfSections++;
                   2889:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2890:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2891:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2892:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2893:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2894:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2895:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2896:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2897:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2898:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2899:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2900:                                                        if(i < 2) {
                   2901:                                                                fputc(padding[i & 15], fo);
                   2902:                                                        } else {
                   2903:                                                                fputc(padding[(i - 2) & 15], fo);
                   2904:                                                        }
1.1.1.28  root     2905:                                                }
                   2906:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2907:                                        }
                   2908:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2909:                                        
                   2910:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2911:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2912:                                        if(dwExtraNewSectionBytes != 0) {
                   2913:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2914:                                                dwNewSectionSize += dwRemain;
                   2915:                                        }
                   2916:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2917:                                        
                   2918:                                        fseek(fo, 0, SEEK_SET);
                   2919:                                        fwrite(header, sizeof(header), 1, fo);
                   2920:                                        
                   2921:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2922:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2923:                                }
                   2924:                        }
                   2925:                        if(fp != NULL) {
                   2926:                                fclose(fp);
                   2927:                        }
                   2928:                        if(fs != NULL) {
                   2929:                                fclose(fs);
                   2930:                        }
                   2931:                        if(fo != NULL) {
                   2932:                                fclose(fo);
                   2933:                        }
                   2934:                }
                   2935: #ifdef _DEBUG
                   2936:                _CrtDumpMemoryLeaks();
                   2937: #endif
                   2938:                return(retval);
                   2939:        }
1.1       root     2940:        
1.1.1.54  root     2941:        is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14  root     2942:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2943:        
1.1.1.23  root     2944:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2945:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2946:        CONSOLE_CURSOR_INFO ci;
1.1.1.58  root     2947:        int font_width = 10, font_height = 18; // default in english mode
1.1.1.23  root     2948:        
1.1.1.28  root     2949:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2950:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.59! root     2951:        ci_old = ci_new = ci;
1.1.1.24  root     2952:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1.1.58  root     2953:        get_console_font_success = get_console_font_size(hStdout, &font_width, &font_height);
1.1       root     2954:        
1.1.1.14  root     2955:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2956:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2957:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2958:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2959:                }
                   2960:        }
1.1.1.28  root     2961:        if(get_console_info_success) {
1.1.1.12  root     2962:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2963:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2964:                
1.1.1.28  root     2965:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2966:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2967:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2968:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2969:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2970:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2971:                                scr_width = 80;
                   2972:                                scr_height = 25;
                   2973:                        }
1.1.1.28  root     2974:                        screen_size_changed = true;
1.1.1.14  root     2975:                }
1.1.1.12  root     2976:        } else {
                   2977:                // for a proof (not a console)
                   2978:                scr_width = 80;
                   2979:                scr_height = 25;
                   2980:        }
1.1.1.14  root     2981:        scr_buf_size.X = scr_width;
                   2982:        scr_buf_size.Y = scr_height;
                   2983:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2984:        scr_top = csbi.srWindow.Top;
1.1       root     2985:        cursor_moved = false;
1.1.1.59! root     2986:        cursor_moved_by_crtc = false;
1.1       root     2987:        
1.1.1.54  root     2988:        SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
                   2989:        
1.1.1.35  root     2990: #ifdef USE_SERVICE_THREAD
                   2991:        InitializeCriticalSection(&input_crit_sect);
                   2992:        InitializeCriticalSection(&key_buf_crit_sect);
                   2993:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     2994:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     2995: #endif
1.1.1.50  root     2996:        
1.1.1.25  root     2997:        key_buf_char = new FIFO(256);
                   2998:        key_buf_scan = new FIFO(256);
1.1.1.57  root     2999:        key_buf_data = new FIFO(256);
1.1       root     3000:        
                   3001:        hardware_init();
                   3002:        
1.1.1.33  root     3003: #ifdef USE_DEBUGGER
                   3004:        debugger_init();
                   3005: #endif
                   3006:        
1.1.1.9   root     3007:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     3008:                retval = EXIT_FAILURE;
                   3009:        } else {
1.1.1.27  root     3010: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     3011:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   3012: #endif
                   3013:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   3014:                
1.1.1.28  root     3015:                if(screen_size_changed) {
1.1.1.24  root     3016:                        change_console_size(scr_width, scr_height);
                   3017:                }
1.1.1.8   root     3018:                TIMECAPS caps;
                   3019:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   3020:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     3021: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3022:                InitializeCriticalSection(&vram_crit_sect);
                   3023:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   3024: #endif
1.1.1.33  root     3025: #ifdef USE_DEBUGGER
                   3026:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   3027:                // wait until telnet client starts and connects to me
                   3028:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   3029:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   3030:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   3031:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   3032:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   3033:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   3034:                                Sleep(100);
                   3035:                        }
                   3036:                }
                   3037: #endif
1.1       root     3038:                hardware_run();
1.1.1.35  root     3039: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3040:                vram_flush();
                   3041:                DeleteCriticalSection(&vram_crit_sect);
                   3042: #endif
1.1.1.24  root     3043:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     3044:                
1.1.1.24  root     3045:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.56  root     3046:                if(get_console_font_success) {
                   3047:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.58  root     3048:                        set_console_font_size(hStdout, font_width, font_height);
1.1.1.56  root     3049:                }
1.1.1.28  root     3050:                if(get_console_info_success) {
1.1.1.23  root     3051:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3052:                        if(restore_console_on_exit) {
1.1.1.14  root     3053:                                // window can't be bigger than buffer,
                   3054:                                // buffer can't be smaller than window,
                   3055:                                // so make a tiny window,
                   3056:                                // set the required buffer,
                   3057:                                // then set the required window
                   3058:                                SMALL_RECT rect;
                   3059:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   3060:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     3061:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     3062:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     3063:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3064:                        }
1.1.1.14  root     3065:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   3066:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     3067:                }
1.1.1.24  root     3068:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   3069:                
1.1       root     3070:                msdos_finish();
1.1.1.14  root     3071:                
                   3072:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     3073:        }
1.1.1.35  root     3074:        if(temp_file_created) {
                   3075:                DeleteFile(temp_file_path);
                   3076:                temp_file_created = false;
                   3077:        }
1.1.1.10  root     3078:        hardware_finish();
                   3079:        
1.1.1.28  root     3080:        if(key_buf_char != NULL) {
                   3081:                key_buf_char->release();
                   3082:                delete key_buf_char;
                   3083:                key_buf_char = NULL;
                   3084:        }
                   3085:        if(key_buf_scan != NULL) {
                   3086:                key_buf_scan->release();
                   3087:                delete key_buf_scan;
                   3088:                key_buf_scan = NULL;
                   3089:        }
1.1.1.57  root     3090:        if(key_buf_data != NULL) {
                   3091:                key_buf_data->release();
                   3092:                delete key_buf_data;
                   3093:                key_buf_data = NULL;
                   3094:        }
1.1.1.35  root     3095: #ifdef USE_SERVICE_THREAD
                   3096:        DeleteCriticalSection(&input_crit_sect);
                   3097:        DeleteCriticalSection(&key_buf_crit_sect);
                   3098:        DeleteCriticalSection(&putch_crit_sect);
                   3099: #endif
1.1.1.20  root     3100: #ifdef _DEBUG
                   3101:        _CrtDumpMemoryLeaks();
                   3102: #endif
1.1       root     3103:        return(retval);
                   3104: }
                   3105: 
1.1.1.20  root     3106: /* ----------------------------------------------------------------------------
                   3107:        console
                   3108: ---------------------------------------------------------------------------- */
                   3109: 
1.1.1.14  root     3110: void change_console_size(int width, int height)
1.1.1.12  root     3111: {
1.1.1.23  root     3112:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3113:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3114:        SMALL_RECT rect;
                   3115:        COORD co;
                   3116:        
                   3117:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     3118:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   3119:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   3120:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   3121:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3122:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3123:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   3124:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   3125:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   3126:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3127:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     3128:                }
                   3129:        }
1.1.1.14  root     3130:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     3131:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     3132:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     3133:                SetConsoleCursorPosition(hStdout, co);
                   3134:                cursor_moved = true;
1.1.1.59! root     3135:                cursor_moved_by_crtc = false;
1.1.1.12  root     3136:        }
1.1.1.14  root     3137:        
                   3138:        // window can't be bigger than buffer,
                   3139:        // buffer can't be smaller than window,
                   3140:        // so make a tiny window,
                   3141:        // set the required buffer,
                   3142:        // then set the required window
                   3143:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     3144:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     3145:        co.X = width;
                   3146:        co.Y = height;
1.1.1.12  root     3147:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     3148:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   3149:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3150:        
                   3151:        scr_width = scr_buf_size.X = width;
                   3152:        scr_height = scr_buf_size.Y = height;
                   3153:        scr_top = 0;
                   3154:        
                   3155:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3156:        
                   3157:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     3158:        text_vram_end_address = text_vram_top_address + regen;
                   3159:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   3160:        
1.1.1.14  root     3161:        if(regen > 0x4000) {
                   3162:                regen = 0x8000;
                   3163:                vram_pages = 1;
                   3164:        } else if(regen > 0x2000) {
                   3165:                regen = 0x4000;
                   3166:                vram_pages = 2;
                   3167:        } else if(regen > 0x1000) {
                   3168:                regen = 0x2000;
                   3169:                vram_pages = 4;
                   3170:        } else {
                   3171:                regen = 0x1000;
                   3172:                vram_pages = 8;
                   3173:        }
1.1.1.15  root     3174:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3175:        *(UINT16 *)(mem + 0x44c) = regen;
                   3176:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3177:        
1.1.1.24  root     3178:        mouse.min_position.x = 0;
                   3179:        mouse.min_position.y = 0;
1.1.1.34  root     3180:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3181:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3182:        
1.1.1.15  root     3183:        restore_console_on_exit = true;
1.1.1.14  root     3184: }
                   3185: 
                   3186: void clear_scr_buffer(WORD attr)
                   3187: {
                   3188:        for(int y = 0; y < scr_height; y++) {
                   3189:                for(int x = 0; x < scr_width; x++) {
                   3190:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3191:                        SCR_BUF(y,x).Attributes = attr;
                   3192:                }
                   3193:        }
1.1.1.12  root     3194: }
                   3195: 
1.1.1.24  root     3196: bool update_console_input()
1.1       root     3197: {
1.1.1.35  root     3198: #ifdef USE_SERVICE_THREAD
                   3199:        EnterCriticalSection(&input_crit_sect);
                   3200: #endif
1.1.1.23  root     3201:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3202:        DWORD dwNumberOfEvents = 0;
1.1       root     3203:        DWORD dwRead;
                   3204:        INPUT_RECORD ir[16];
1.1.1.24  root     3205:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3206:        bool result = false;
1.1       root     3207:        
1.1.1.8   root     3208:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3209:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3210:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3211:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.59! root     3212:                                        if(ir[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
        !          3213:                                                if(mouse.hidden == 0 || (mouse.call_addr_ps2.dw && mouse.enabled_ps2)) {
        !          3214:                                                        // NOTE: if restore_console_on_exit, console is not scrolled
        !          3215:                                                        if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
        !          3216:                                                                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
        !          3217:                                                        }
        !          3218:                                                        // FIXME: character size is always 8x8 ???
        !          3219:                                                        int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
        !          3220:                                                        int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
        !          3221:                                                        
        !          3222:                                                        if(mouse.position.x != x || mouse.position.y != y) {
        !          3223:                                                                mouse.position.x = x;
        !          3224:                                                                mouse.position.y = y;
        !          3225:                                                                mouse.status |= 1;
        !          3226:                                                                mouse.status_alt |= 1;
        !          3227:                                                        }
1.1.1.34  root     3228:                                                }
1.1.1.59! root     3229:                                        } else if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
        !          3230:                                                for(int j = 0; j < MAX_MOUSE_BUTTONS; j++) {
1.1.1.34  root     3231:                                                        static const DWORD bits[] = {
                   3232:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3233:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3234:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3235:                                                        };
1.1.1.59! root     3236:                                                        bool prev_status = mouse.buttons[j].status;
        !          3237:                                                        mouse.buttons[j].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[j]) != 0);
1.1.1.34  root     3238:                                                        
1.1.1.59! root     3239:                                                        if(!prev_status && mouse.buttons[j].status) {
        !          3240:                                                                mouse.buttons[j].pressed_times++;
        !          3241:                                                                mouse.buttons[j].pressed_position.x = mouse.position.x;
        !          3242:                                                                mouse.buttons[j].pressed_position.y = mouse.position.x;
        !          3243:                                                                if(j < 2) {
        !          3244:                                                                        mouse.status_alt |= 2 << (j * 2);
1.1.1.43  root     3245:                                                                }
1.1.1.59! root     3246:                                                                mouse.status |= 2 << (j * 2);
        !          3247:                                                        } else if(prev_status && !mouse.buttons[j].status) {
        !          3248:                                                                mouse.buttons[j].released_times++;
        !          3249:                                                                mouse.buttons[j].released_position.x = mouse.position.x;
        !          3250:                                                                mouse.buttons[j].released_position.y = mouse.position.x;
        !          3251:                                                                if(j < 2) {
        !          3252:                                                                        mouse.status_alt |= 4 << (j * 2);
1.1.1.43  root     3253:                                                                }
1.1.1.59! root     3254:                                                                mouse.status |= 4 << (j * 2);
1.1.1.14  root     3255:                                                        }
                   3256:                                                }
                   3257:                                        }
1.1.1.24  root     3258:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3259:                                        // update keyboard flags in bios data area
1.1.1.35  root     3260:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3261:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3262:                                        } else {
1.1.1.35  root     3263:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3264:                                        }
1.1.1.35  root     3265:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3266:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3267:                                        } else {
1.1.1.35  root     3268:                                                mem[0x417] &= ~0x20;
                   3269:                                        }
                   3270:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3271:                                                mem[0x417] |= 0x10;
                   3272:                                        } else {
                   3273:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3274:                                        }
                   3275:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3276:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3277:                                                        mouse.status_alt |= 0x80;
                   3278:                                                }
1.1.1.33  root     3279:                                                mem[0x417] |= 0x08;
                   3280:                                        } else {
                   3281:                                                mem[0x417] &= ~0x08;
                   3282:                                        }
1.1.1.35  root     3283:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3284:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3285:                                                        mouse.status_alt |= 0x40;
                   3286:                                                }
1.1.1.35  root     3287:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3288:                                        } else {
1.1.1.35  root     3289:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3290:                                        }
                   3291:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3292:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3293:                                                        mouse.status_alt |= 0x20;
                   3294:                                                }
1.1.1.33  root     3295:                                                if(!(mem[0x417] & 0x03)) {
                   3296:                                                        mem[0x417] |= 0x02; // left shift
                   3297:                                                }
                   3298:                                        } else {
                   3299:                                                mem[0x417] &= ~0x03;
                   3300:                                        }
1.1.1.35  root     3301:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3302:                                                mem[0x418] |= 0x02;
                   3303:                                        } else {
                   3304:                                                mem[0x418] &= ~0x02;
                   3305:                                        }
                   3306:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3307:                                                mem[0x418] |= 0x01;
                   3308:                                        } else {
                   3309:                                                mem[0x418] &= ~0x01;
                   3310:                                        }
1.1.1.33  root     3311:                                        
1.1.1.28  root     3312:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.57  root     3313: //                                     kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
                   3314: //                                     kbd_status |= 1;
                   3315:                                        UINT8 tmp_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3316:                                        
                   3317:                                        // update dos key buffer
                   3318:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3319:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3320:                                        UINT8 scn_old = scn;
1.1.1.33  root     3321:                                        
                   3322:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3323:                                                // make
1.1.1.57  root     3324:                                                tmp_data &= 0x7f;
1.1.1.24  root     3325:                                                
1.1.1.33  root     3326:                                                if(chr == 0x00) {
1.1.1.24  root     3327:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3328:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3329:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3330:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3331:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3332:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3333:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3334:                                                                } else if(scn == 0x35) {
                   3335:                                                                        scn = 0xa4;             // keypad /
                   3336:                                                                }
                   3337:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3338:                                                                if(scn == 0x07) {
                   3339:                                                                        chr = 0x1e;     // Ctrl+^
                   3340:                                                                } else if(scn == 0x0c) {
                   3341:                                                                        chr = 0x1f;     // Ctrl+_
                   3342:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3343:                                                                        static const UINT8 ctrl_map[] = {
                   3344:                                                                                0x95,   // keypad /
                   3345:                                                                                0,
                   3346:                                                                                0x96,   // keypad *
                   3347:                                                                                0, 0, 0,
                   3348:                                                                                0x5e,   // F1
                   3349:                                                                                0x5f,   // F2
                   3350:                                                                                0x60,   // F3
                   3351:                                                                                0x61,   // F4
                   3352:                                                                                0x62,   // F5
                   3353:                                                                                0x63,   // F6
                   3354:                                                                                0x64,   // F7
                   3355:                                                                                0x65,   // F8
                   3356:                                                                                0x66,   // F9
                   3357:                                                                                0x67,   // F10
                   3358:                                                                                0,
                   3359:                                                                                0,
                   3360:                                                                                0x77,   // Home
                   3361:                                                                                0x8d,   // Up
                   3362:                                                                                0x84,   // PgUp
                   3363:                                                                                0x8e,   // keypad -
                   3364:                                                                                0x73,   // Left
                   3365:                                                                                0x8f,   // keypad center
                   3366:                                                                                0x74,   // Right
                   3367:                                                                                0x90,   // keyapd +
                   3368:                                                                                0x75,   // End
                   3369:                                                                                0x91,   // Down
                   3370:                                                                                0x76,   // PgDn
                   3371:                                                                                0x92,   // Insert
                   3372:                                                                                0x93,   // Delete
                   3373:                                                                                0, 0, 0,
                   3374:                                                                                0x89,   // F11
                   3375:                                                                                0x8a,   // F12
                   3376:                                                                        };
                   3377:                                                                        scn = ctrl_map[scn - 0x35];
                   3378:                                                                }
                   3379:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3380:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3381:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3382:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3383:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3384:                                                                }
                   3385:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3386:                                                                scn += 0x85 - 0x57;
                   3387:                                                        }
                   3388:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3389:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3390:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3391: #ifdef USE_SERVICE_THREAD
                   3392:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3393: #endif
1.1.1.32  root     3394:                                                                        if(chr == 0) {
1.1.1.51  root     3395:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3396:                                                                        }
1.1.1.51  root     3397:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3398: #ifdef USE_SERVICE_THREAD
                   3399:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3400: #endif
1.1.1.24  root     3401:                                                                }
                   3402:                                                        }
                   3403:                                                } else {
                   3404:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3405:                                                                chr = 0;
                   3406:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3407:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3408:                                                                }
                   3409:                                                        }
1.1.1.32  root     3410:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3411: #ifdef USE_SERVICE_THREAD
                   3412:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3413: #endif
1.1.1.51  root     3414:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3415: #ifdef USE_SERVICE_THREAD
                   3416:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3417: #endif
1.1.1.32  root     3418:                                                        }
1.1.1.24  root     3419:                                                }
1.1.1.57  root     3420:                                        } else {
                   3421:                                                if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3422:                                                        // ctrl-break, ctrl-c
                   3423:                                                        if(scn == 0x46) {
                   3424:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3425: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3426:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3427: #endif
1.1.1.57  root     3428:                                                                        pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3429: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3430:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3431: #endif
1.1.1.57  root     3432:                                                                }
                   3433:                                                                ctrl_break_pressed = true;
                   3434:                                                                mem[0x471] = 0x80;
                   3435:                                                                raise_int_1bh = true;
                   3436:                                                        } else {
                   3437:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3438: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3439:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3440: #endif
1.1.1.57  root     3441:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3442: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3443:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3444: #endif
1.1.1.57  root     3445:                                                                }
                   3446:                                                                ctrl_c_pressed = (scn == 0x2e);
1.1.1.33  root     3447:                                                        }
                   3448:                                                }
                   3449:                                                // break
1.1.1.57  root     3450:                                                tmp_data |= 0x80;
                   3451:                                        }
                   3452:                                        if(!(kbd_status & 1)) {
                   3453:                                                kbd_data = tmp_data;
                   3454:                                                kbd_status |= 1;
                   3455:                                        } else {
                   3456:                                                if(key_buf_data != NULL) {
                   3457: #ifdef USE_SERVICE_THREAD
                   3458:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3459: #endif
                   3460:                                                        key_buf_data->write(tmp_data);
                   3461: #ifdef USE_SERVICE_THREAD
                   3462:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3463: #endif
                   3464:                                                }
1.1       root     3465:                                        }
1.1.1.24  root     3466:                                        result = key_changed = true;
1.1.1.36  root     3467:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3468:                                        cursor_moved = true;
1.1       root     3469:                                }
                   3470:                        }
                   3471:                }
                   3472:        }
1.1.1.35  root     3473: #ifdef USE_SERVICE_THREAD
                   3474:        LeaveCriticalSection(&input_crit_sect);
                   3475: #endif
1.1.1.24  root     3476:        return(result);
1.1.1.8   root     3477: }
                   3478: 
1.1.1.14  root     3479: bool update_key_buffer()
1.1.1.8   root     3480: {
1.1.1.35  root     3481:        if(update_console_input()) {
                   3482:                return(true);
                   3483:        }
                   3484:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3485: #ifdef USE_SERVICE_THREAD
                   3486:                EnterCriticalSection(&key_buf_crit_sect);
                   3487: #endif
1.1.1.55  root     3488:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     3489: #ifdef USE_SERVICE_THREAD
                   3490:                LeaveCriticalSection(&key_buf_crit_sect);
                   3491: #endif
                   3492:                if(!empty) return(true);
                   3493:        }
                   3494:        return(false);
1.1.1.8   root     3495: }
                   3496: 
1.1.1.20  root     3497: /* ----------------------------------------------------------------------------
                   3498:        MS-DOS virtual machine
                   3499: ---------------------------------------------------------------------------- */
                   3500: 
1.1.1.32  root     3501: static const struct {
1.1.1.33  root     3502:        char *name;
                   3503:        DWORD lcid;
                   3504:        char *std;
                   3505:        char *dlt;
                   3506: } tz_table[] = {
                   3507:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3508: //     0       GMT             Greenwich Mean Time             GMT0
                   3509:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3510:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3511:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3512:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3513: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3514:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3515:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3516: //     3       BST             Brazil Standard Time            BST3
                   3517:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3518:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3519:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3520: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3521:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3522: //     3       GST             Greenland Standard Time         GST3
                   3523:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3524: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3525:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3526: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3527:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3528: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3529:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3530:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3531: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3532:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3533:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3534:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3535: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3536:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3537: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3538:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3539: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3540:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3541: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3542:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3543:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3544:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3545: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3546:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3547: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3548:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3549:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3550:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3551: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3552:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3553:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3554: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3555: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3556:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3557: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3558:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3559:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3560: //     11      SST             Samoa Standard Time             SST11
                   3561:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3562: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3563:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3564: //     -10     GST             Guam Standard Time              GST-10
                   3565:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3566: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3567:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3568:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3569:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3570: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3571:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3572:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3573: //     -9      JST             Japan Standard Time             JST-9
                   3574:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3575: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3576:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3577:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3578: //     -8      HKT             Hong Kong Time                  HKT-8
                   3579:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3580: //     -8      CCT             China Coast Time                CCT-8
                   3581:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3582:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3583: //     -8      SST             Singapore Standard Time         SST-8
                   3584:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3585: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3586:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3587:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3588: //     -7:30   JT              Java Standard Time              JST-7:30
                   3589: //     -7      NST             North Sumatra Time              NST-7
                   3590:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3591: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3592:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3593: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3594:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3595: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3596:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3597:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3598: //     -2      EET             Eastern Europe Time             EET-2
                   3599:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3600:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3601:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3602:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3603: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3604:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3605: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3606: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3607: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3608: //     -1      CET     CES     Central European Time           CET-1CES
                   3609:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3610:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3611:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3612:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3613: //     -1      WAT             West African Time               WAT-1
                   3614:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3615:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3616: //     0       UTC             Universal Coordinated Time      UTC0
                   3617:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3618:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3619:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3620:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3621:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3622:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3623: };
                   3624: 
1.1.1.53  root     3625: // FIXME: consider to build on non-Japanese environment :-(
                   3626: // message_japanese string must be in shift-jis
                   3627: 
1.1.1.33  root     3628: static const struct {
1.1.1.32  root     3629:        UINT16 code;
                   3630:        char *message_english;
                   3631:        char *message_japanese;
                   3632: } standard_error_table[] = {
                   3633:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3634:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3635:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3636:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3637:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3638:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3639:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3640:        {0x08,  "Insufficient memory", "������������܂���."},
                   3641:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3642:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3643:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3644:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3645:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3646:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3647:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3648:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3649:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3650:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3651:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3652:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3653:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3654:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3655:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3656:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3657:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3658:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3659:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3660:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3661:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3662:        {0x1F,  "General failure", "�G���[�ł�."},
                   3663:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3664:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3665:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3666:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3667:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3668:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3669:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3670:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3671: /*
                   3672:        {0x32,  "Network request not supported", NULL},
                   3673:        {0x33,  "Remote computer not listening", NULL},
                   3674:        {0x34,  "Duplicate name on network", NULL},
                   3675:        {0x35,  "Network name not found", NULL},
                   3676:        {0x36,  "Network busy", NULL},
                   3677:        {0x37,  "Network device no longer exists", NULL},
                   3678:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3679:        {0x39,  "Network adapter hardware error", NULL},
                   3680:        {0x3A,  "Incorrect response from network", NULL},
                   3681:        {0x3B,  "Unexpected network error", NULL},
                   3682:        {0x3C,  "Incompatible remote adapter", NULL},
                   3683:        {0x3D,  "Print queue full", NULL},
                   3684:        {0x3E,  "Queue not full", NULL},
                   3685:        {0x3F,  "Not enough space to print file", NULL},
                   3686:        {0x40,  "Network name was deleted", NULL},
                   3687:        {0x41,  "Network: Access denied", NULL},
                   3688:        {0x42,  "Network device type incorrect", NULL},
                   3689:        {0x43,  "Network name not found", NULL},
                   3690:        {0x44,  "Network name limit exceeded", NULL},
                   3691:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3692:        {0x46,  "Temporarily paused", NULL},
                   3693:        {0x47,  "Network request not accepted", NULL},
                   3694:        {0x48,  "Network print/disk redirection paused", NULL},
                   3695:        {0x49,  "Network software not installed", NULL},
                   3696:        {0x4A,  "Unexpected adapter close", NULL},
                   3697: */
                   3698:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3699:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3700:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3701:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3702:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3703:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3704:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3705:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3706:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3707:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53  root     3708: #ifdef SUPPORT_MSCDEX
1.1.1.32  root     3709:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3710:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3711:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3712:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3713:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
1.1.1.53  root     3714: #endif
1.1.1.32  root     3715:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3716:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3717:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3718:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3719:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3720:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3721: };
                   3722: 
                   3723: static const struct {
                   3724:        UINT16 code;
                   3725:        char *message_english;
                   3726:        char *message_japanese;
                   3727: } param_error_table[] = {
                   3728:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3729:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3730:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3731:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3732:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3733:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3734:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3735:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3736:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3737:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3738:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3739: };
                   3740: 
                   3741: static const struct {
                   3742:        UINT16 code;
                   3743:        char *message_english;
                   3744:        char *message_japanese;
                   3745: } critical_error_table[] = {
                   3746:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3747:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3748:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3749:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3750:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3751:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3752:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3753:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3754:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3755:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3756:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3757:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3758:        {0x0C,  "General failure", "�G���[�ł�."},
                   3759:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3760:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3761:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3762:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3763:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3764:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3765:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3766:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3767:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3768: };
                   3769: 
1.1.1.20  root     3770: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3771: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3772: void msdos_putch(UINT8 data);
1.1.1.50  root     3773: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3774: #ifdef USE_SERVICE_THREAD
                   3775: void msdos_putch_tmp(UINT8 data);
                   3776: #endif
1.1.1.45  root     3777: const char *msdos_short_path(const char *path);
1.1.1.44  root     3778: bool msdos_is_valid_drive(int drv);
                   3779: bool msdos_is_removable_drive(int drv);
                   3780: bool msdos_is_cdrom_drive(int drv);
                   3781: bool msdos_is_remote_drive(int drv);
                   3782: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3783: 
1.1       root     3784: // process info
                   3785: 
                   3786: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3787: {
                   3788:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3789:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3790:                        memset(&process[i], 0, sizeof(process_t));
                   3791:                        process[i].psp = psp_seg;
                   3792:                        return(&process[i]);
                   3793:                }
                   3794:        }
                   3795:        fatalerror("too many processes\n");
                   3796:        return(NULL);
                   3797: }
                   3798: 
1.1.1.52  root     3799: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3800: {
                   3801:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3802:                if(process[i].psp == psp_seg) {
                   3803:                        return(&process[i]);
                   3804:                }
                   3805:        }
1.1.1.33  root     3806:        if(show_error) {
                   3807:                fatalerror("invalid psp address\n");
                   3808:        }
1.1       root     3809:        return(NULL);
                   3810: }
                   3811: 
1.1.1.23  root     3812: void msdos_sda_update(int psp_seg)
                   3813: {
                   3814:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3815:        
                   3816:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3817:                if(process[i].psp == psp_seg) {
                   3818:                        sda->switchar = process[i].switchar;
                   3819:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3820:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3821:                        sda->current_psp = process[i].psp;
                   3822:                        break;
                   3823:                }
                   3824:        }
                   3825:        sda->malloc_strategy = malloc_strategy;
                   3826:        sda->return_code = retval;
                   3827:        sda->current_drive = _getdrive();
                   3828: }
                   3829: 
1.1.1.13  root     3830: // dta info
                   3831: 
                   3832: void msdos_dta_info_init()
                   3833: {
1.1.1.14  root     3834:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3835:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3836:        }
                   3837: }
                   3838: 
                   3839: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3840: {
                   3841:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3842:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3843:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3844:                        if(free_dta == NULL) {
1.1.1.13  root     3845:                                free_dta = &dtalist[i];
                   3846:                        }
1.1.1.14  root     3847:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3848:                        return(&dtalist[i]);
                   3849:                }
                   3850:        }
1.1.1.14  root     3851:        if(free_dta) {
1.1.1.13  root     3852:                free_dta->psp = psp_seg;
                   3853:                free_dta->dta = dta_laddr;
                   3854:                return(free_dta);
                   3855:        }
                   3856:        fatalerror("too many dta\n");
                   3857:        return(NULL);
                   3858: }
                   3859: 
                   3860: void msdos_dta_info_free(UINT16 psp_seg)
                   3861: {
1.1.1.14  root     3862:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3863:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3864:                        FindClose(dtalist[i].find_handle);
                   3865:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3866:                }
                   3867:        }
                   3868: }
                   3869: 
1.1       root     3870: void msdos_cds_update(int drv)
                   3871: {
1.1.1.44  root     3872:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     3873:        
1.1.1.44  root     3874:        memset(cds, 0, 88);
                   3875:        
                   3876:        if(msdos_is_valid_drive(drv)) {
                   3877:                char path[MAX_PATH];
                   3878:                if(msdos_is_remote_drive(drv)) {
                   3879:                        cds->drive_attrib = 0xc000;     // network drive
                   3880:                } else if(msdos_is_subst_drive(drv)) {
                   3881:                        cds->drive_attrib = 0x5000;     // subst drive
                   3882:                } else {
                   3883:                        cds->drive_attrib = 0x4000;     // physical drive
                   3884:                }
                   3885:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   3886:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   3887:                }
                   3888:        }
                   3889:        if(cds->path_name[0] == '\0') {
                   3890:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3891:        }
                   3892:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   3893:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   3894:        cds->word_1 = cds->word_2 = 0xffff;
                   3895:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   3896:        cds->bs_offset = 2;
                   3897: }
                   3898: 
1.1.1.45  root     3899: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     3900: {
                   3901:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   3902:        
                   3903:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     3904: }
                   3905: 
1.1.1.17  root     3906: // nls information tables
                   3907: 
                   3908: // uppercase table (func 6502h)
                   3909: void msdos_upper_table_update()
                   3910: {
                   3911:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3912:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3913:                UINT8 c[4];
1.1.1.33  root     3914:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3915:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3916:                c[0] = 0x80 + i;
                   3917:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3918:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3919:        }
                   3920: }
                   3921: 
1.1.1.23  root     3922: // lowercase table (func 6503h)
                   3923: void msdos_lower_table_update()
                   3924: {
                   3925:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3926:        for(unsigned i = 0; i < 0x80; ++i) {
                   3927:                UINT8 c[4];
1.1.1.33  root     3928:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3929:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3930:                c[0] = 0x80 + i;
                   3931:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3932:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3933:        }
                   3934: }
                   3935: 
1.1.1.17  root     3936: // filename uppercase table (func 6504h)
                   3937: void msdos_filename_upper_table_init()
                   3938: {
                   3939:        // depended on (file)system, not on active codepage
                   3940:        // temporary solution: just filling data
                   3941:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3942:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3943:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3944:        }
                   3945: }
                   3946: 
                   3947: // filaname terminator table (func 6505h)
                   3948: void msdos_filename_terminator_table_init()
                   3949: {
                   3950:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3951:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3952:        
                   3953:        data[2] = 1;            // marker? (permissible character value)
                   3954:        data[3] = 0x00;         // 00h...FFh
                   3955:        data[4] = 0xff;
                   3956:        data[5] = 0;            // marker? (excluded character)
                   3957:        data[6] = 0x00;         // 00h...20h
                   3958:        data[7] = 0x20;
                   3959:        data[8] = 2;            // marker? (illegal characters for filename)
                   3960:        data[9] = (UINT8)strlen(illegal_chars);
                   3961:        memcpy(data + 10, illegal_chars, data[9]);
                   3962:        
                   3963:        // total length
                   3964:        *(UINT16 *)data = (10 - 2) + data[9];
                   3965: }
                   3966: 
                   3967: // collating table (func 6506h)
                   3968: void msdos_collating_table_update()
                   3969: {
                   3970:        // temporary solution: just filling data
                   3971:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3972:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3973:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3974:        }
                   3975: }
                   3976: 
1.1       root     3977: // dbcs
                   3978: 
                   3979: void msdos_dbcs_table_update()
                   3980: {
                   3981:        UINT8 dbcs_data[DBCS_SIZE];
                   3982:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3983:        
                   3984:        CPINFO info;
                   3985:        GetCPInfo(active_code_page, &info);
                   3986:        
                   3987:        if(info.MaxCharSize != 1) {
                   3988:                for(int i = 0;; i += 2) {
                   3989:                        UINT8 lo = info.LeadByte[i + 0];
                   3990:                        UINT8 hi = info.LeadByte[i + 1];
                   3991:                        dbcs_data[2 + i + 0] = lo;
                   3992:                        dbcs_data[2 + i + 1] = hi;
                   3993:                        if(lo == 0 && hi == 0) {
                   3994:                                dbcs_data[0] = i + 2;
                   3995:                                break;
                   3996:                        }
                   3997:                }
                   3998:        } else {
                   3999:                dbcs_data[0] = 2;       // ???
                   4000:        }
                   4001:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   4002: }
                   4003: 
1.1.1.17  root     4004: void msdos_dbcs_table_finish()
                   4005: {
1.1.1.32  root     4006:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     4007:                _setmbcp(system_code_page);
                   4008:        }
1.1.1.32  root     4009:        if(console_code_page != GetConsoleCP()) {
                   4010:                SetConsoleCP(console_code_page);
                   4011:                SetConsoleOutputCP(console_code_page);
                   4012:        }
1.1.1.17  root     4013: }
                   4014: 
                   4015: void msdos_nls_tables_init()
1.1       root     4016: {
1.1.1.32  root     4017:        active_code_page = console_code_page = GetConsoleCP();
                   4018:        system_code_page = _getmbcp();
                   4019:        
                   4020:        if(active_code_page != system_code_page) {
                   4021:                if(_setmbcp(active_code_page) != 0) {
                   4022:                        active_code_page = system_code_page;
                   4023:                }
                   4024:        }
                   4025:        
1.1.1.17  root     4026:        msdos_upper_table_update();
1.1.1.23  root     4027:        msdos_lower_table_update();
1.1.1.17  root     4028:        msdos_filename_terminator_table_init();
                   4029:        msdos_filename_upper_table_init();
                   4030:        msdos_collating_table_update();
1.1       root     4031:        msdos_dbcs_table_update();
                   4032: }
                   4033: 
1.1.1.17  root     4034: void msdos_nls_tables_update()
1.1       root     4035: {
1.1.1.17  root     4036:        msdos_dbcs_table_update();
                   4037:        msdos_upper_table_update();
1.1.1.23  root     4038:        msdos_lower_table_update();
                   4039: //     msdos_collating_table_update();
1.1       root     4040: }
                   4041: 
                   4042: int msdos_lead_byte_check(UINT8 code)
                   4043: {
                   4044:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   4045:        
                   4046:        for(int i = 0;; i += 2) {
                   4047:                UINT8 lo = dbcs_table[i + 0];
                   4048:                UINT8 hi = dbcs_table[i + 1];
                   4049:                if(lo == 0 && hi == 0) {
                   4050:                        break;
                   4051:                }
                   4052:                if(lo <= code && code <= hi) {
                   4053:                        return(1);
                   4054:                }
                   4055:        }
                   4056:        return(0);
                   4057: }
                   4058: 
1.1.1.20  root     4059: int msdos_ctrl_code_check(UINT8 code)
                   4060: {
1.1.1.22  root     4061:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     4062: }
                   4063: 
1.1.1.36  root     4064: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   4065: {
                   4066:        int is_kanji_1st = 0;
                   4067:        int is_kanji_2nd = 0;
                   4068:        
                   4069:        for(int p = 0;; p++) {
                   4070:                if(is_kanji_1st) {
                   4071:                        is_kanji_1st = 0;
                   4072:                        is_kanji_2nd = 1;
                   4073:                } else if(msdos_lead_byte_check(buf[p])) {
                   4074:                        is_kanji_1st = 1;
                   4075:                }
                   4076:                if(p == n) {
                   4077:                        return(is_kanji_2nd);
                   4078:                }
                   4079:                is_kanji_2nd = 0;
                   4080:        }
                   4081: }
                   4082: 
1.1       root     4083: // file control
                   4084: 
1.1.1.45  root     4085: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     4086: {
                   4087:        static char tmp[MAX_PATH];
                   4088:        
                   4089:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     4090:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     4091:                memcpy(tmp, path + 1, strlen(path) - 2);
                   4092:        } else {
                   4093:                strcpy(tmp, path);
                   4094:        }
                   4095:        return(tmp);
                   4096: }
                   4097: 
1.1.1.45  root     4098: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     4099: {
                   4100:        static char tmp[MAX_PATH];
                   4101:        
                   4102:        strcpy(tmp, path);
1.1.1.45  root     4103:        
                   4104:        // for example "C:\" case, the end separator should not be removed
                   4105:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   4106:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     4107:        }
                   4108:        return(tmp);
                   4109: }
                   4110: 
1.1.1.45  root     4111: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     4112: {
                   4113:        static char tmp[MAX_PATH];
1.1.1.45  root     4114:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     4115:        
                   4116:        if(strlen(tmp_dir) == 0) {
                   4117:                strcpy(tmp, file);
                   4118:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   4119:                sprintf(tmp, "%s%s", tmp_dir, file);
                   4120:        } else {
                   4121:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   4122:        }
                   4123:        return(tmp);
                   4124: }
                   4125: 
1.1.1.45  root     4126: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     4127: {
                   4128:        static char tmp[MAX_PATH];
                   4129:        
                   4130:        if(lfn) {
                   4131:                strcpy(tmp, path);
                   4132:        } else {
                   4133:                // remove space in the path
1.1.1.45  root     4134:                const char *src = path;
                   4135:                char *dst = tmp;
1.1       root     4136:                
                   4137:                while(*src != '\0') {
                   4138:                        if(msdos_lead_byte_check(*src)) {
                   4139:                                *dst++ = *src++;
                   4140:                                *dst++ = *src++;
                   4141:                        } else if(*src != ' ') {
                   4142:                                *dst++ = *src++;
                   4143:                        } else {
                   4144:                                src++;  // skip space
                   4145:                        }
                   4146:                }
                   4147:                *dst = '\0';
                   4148:        }
1.1.1.14  root     4149:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   4150:                // redirect C:\COMMAND.COM to comspec_path
                   4151:                strcpy(tmp, comspec_path);
                   4152:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   4153:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   4154:                static int root_drive_protected = -1;
                   4155:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   4156:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   4157:                
                   4158:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   4159:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   4160:                        strcpy(name, name_temp);
                   4161:                        name_temp[0] = '\0';
                   4162:                        
                   4163:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   4164:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   4165:                                if(root_drive_protected == -1) {
                   4166:                                        FILE *fp = NULL;
                   4167:                                        
                   4168:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   4169:                                        root_drive_protected = 1;
                   4170:                                        try {
                   4171:                                                if((fp = fopen(temp, "w")) != NULL) {
                   4172:                                                        if(fprintf(fp, "TEST") == 4) {
                   4173:                                                                root_drive_protected = 0;
                   4174:                                                        }
                   4175:                                                }
                   4176:                                        } catch(...) {
                   4177:                                        }
                   4178:                                        if(fp != NULL) {
                   4179:                                                fclose(fp);
                   4180:                                        }
                   4181:                                        if(_access(temp, 0) == 0) {
                   4182:                                                remove(temp);
                   4183:                                        }
                   4184:                                }
                   4185:                                if(root_drive_protected == 1) {
                   4186:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   4187:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   4188:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4189:                                        }
                   4190:                                }
                   4191:                        }
                   4192:                }
                   4193:        }
1.1       root     4194:        return(tmp);
                   4195: }
                   4196: 
1.1.1.45  root     4197: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4198: {
1.1.1.32  root     4199:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4200:        static char env_path[ENV_SIZE];
                   4201:        char tmp[ENV_SIZE], *token;
                   4202:        
                   4203:        memset(env_path, 0, sizeof(env_path));
                   4204:        strcpy(tmp, src);
                   4205:        token = my_strtok(tmp, ";");
                   4206:        
                   4207:        while(token != NULL) {
                   4208:                if(token[0] != '\0') {
1.1.1.45  root     4209:                        const char *path = msdos_remove_double_quote(token);
                   4210:                        char short_path[MAX_PATH];
1.1.1.32  root     4211:                        if(path != NULL && strlen(path) != 0) {
                   4212:                                if(env_path[0] != '\0') {
                   4213:                                        strcat(env_path, ";");
                   4214:                                }
1.1.1.28  root     4215:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4216:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4217:                                } else {
                   4218:                                        my_strupr(short_path);
1.1.1.32  root     4219:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4220:                                }
                   4221:                        }
                   4222:                }
                   4223:                token = my_strtok(NULL, ";");
                   4224:        }
                   4225:        return(env_path);
                   4226: }
                   4227: 
1.1.1.45  root     4228: bool match(const char *text, const char *pattern)
1.1       root     4229: {
1.1.1.24  root     4230:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4231:        switch(*pattern) {
1.1       root     4232:        case '\0':
                   4233:                return !*text;
                   4234:        case '*':
1.1.1.14  root     4235:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4236:        case '?':
                   4237:                return *text && match(text + 1, pattern + 1);
                   4238:        default:
                   4239:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4240:        }
                   4241: }
                   4242: 
1.1.1.45  root     4243: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4244: {
1.1.1.45  root     4245:        const char *p = NULL;
1.1       root     4246:        
1.1.1.14  root     4247:        if(!*volume) {
                   4248:                return false;
                   4249:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4250:                return msdos_match_volume_label(p + 1, volume);
                   4251:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4252:                return msdos_match_volume_label(p + 1, volume);
                   4253:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4254:                char tmp[MAX_PATH];
                   4255:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4256:                return match(volume, tmp);
1.1       root     4257:        } else {
                   4258:                return match(volume, path);
                   4259:        }
                   4260: }
                   4261: 
1.1.1.45  root     4262: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4263: {
                   4264:        static char tmp[MAX_PATH];
                   4265:        char name[9], ext[4];
                   4266:        
                   4267:        memset(name, 0, sizeof(name));
                   4268:        memcpy(name, fcb->file_name, 8);
                   4269:        strcpy(name, msdos_trimmed_path(name, 0));
                   4270:        
                   4271:        memset(ext, 0, sizeof(ext));
                   4272:        memcpy(ext, fcb->file_name + 8, 3);
                   4273:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4274:        
                   4275:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4276:                strcpy(name, "*");
                   4277:        }
                   4278:        if(ext[0] == '\0') {
                   4279:                strcpy(tmp, name);
                   4280:        } else {
                   4281:                if(strcmp(ext, "???") == 0) {
                   4282:                        strcpy(ext, "*");
                   4283:                }
                   4284:                sprintf(tmp, "%s.%s", name, ext);
                   4285:        }
                   4286:        return(tmp);
                   4287: }
                   4288: 
1.1.1.45  root     4289: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4290: {
                   4291:        char *ext = my_strchr(path, '.');
                   4292:        
                   4293:        memset(fcb->file_name, 0x20, 8 + 3);
                   4294:        if(ext != NULL && path[0] != '.') {
                   4295:                *ext = '\0';
                   4296:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4297:        }
                   4298:        memcpy(fcb->file_name, path, strlen(path));
                   4299: }
                   4300: 
1.1.1.45  root     4301: const char *msdos_short_path(const char *path)
1.1       root     4302: {
                   4303:        static char tmp[MAX_PATH];
                   4304:        
1.1.1.24  root     4305:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4306:                strcpy(tmp, path);
                   4307:        }
1.1       root     4308:        my_strupr(tmp);
                   4309:        return(tmp);
                   4310: }
                   4311: 
1.1.1.45  root     4312: const char *msdos_short_name(WIN32_FIND_DATA *fd)
1.1.1.13  root     4313: {
                   4314:        static char tmp[MAX_PATH];
1.1.1.45  root     4315:        
1.1.1.14  root     4316:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4317:                strcpy(tmp, fd->cAlternateFileName);
                   4318:        } else {
                   4319:                strcpy(tmp, fd->cFileName);
                   4320:        }
                   4321:        my_strupr(tmp);
                   4322:        return(tmp);
                   4323: }
                   4324: 
1.1.1.45  root     4325: const char *msdos_short_full_path(const char *path)
1.1       root     4326: {
                   4327:        static char tmp[MAX_PATH];
                   4328:        char full[MAX_PATH], *name;
                   4329:        
1.1.1.14  root     4330:        // Full works with non-existent files, but Short does not
1.1       root     4331:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4332:        *tmp = '\0';
                   4333:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4334:                name[-1] = '\0';
                   4335:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4336:                if(len == 0) {
                   4337:                        strcpy(tmp, full);
                   4338:                } else {
                   4339:                        tmp[len++] = '\\';
                   4340:                        strcpy(tmp + len, name);
                   4341:                }
                   4342:        }
1.1       root     4343:        my_strupr(tmp);
                   4344:        return(tmp);
                   4345: }
                   4346: 
1.1.1.45  root     4347: const char *msdos_short_full_dir(const char *path)
1.1       root     4348: {
                   4349:        static char tmp[MAX_PATH];
                   4350:        char full[MAX_PATH], *name;
                   4351:        
                   4352:        GetFullPathName(path, MAX_PATH, full, &name);
                   4353:        name[-1] = '\0';
1.1.1.24  root     4354:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4355:                strcpy(tmp, full);
                   4356:        }
1.1       root     4357:        my_strupr(tmp);
                   4358:        return(tmp);
                   4359: }
                   4360: 
1.1.1.45  root     4361: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4362: {
1.1.1.45  root     4363:        static char trimmed[MAX_PATH];
                   4364:        
                   4365:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4366: #if 0
                   4367:        // I have forgotten the reason of this routine... :-(
1.1       root     4368:        if(_access(trimmed, 0) != 0) {
                   4369:                process_t *process = msdos_process_info_get(current_psp);
                   4370:                static char tmp[MAX_PATH];
                   4371:                
                   4372:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4373:                if(_access(tmp, 0) == 0) {
                   4374:                        return(tmp);
                   4375:                }
                   4376:        }
1.1.1.14  root     4377: #endif
1.1       root     4378:        return(trimmed);
                   4379: }
                   4380: 
1.1.1.45  root     4381: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4382: {
                   4383:        char full[MAX_PATH], *name;
                   4384:        
1.1.1.24  root     4385:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4386:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4387:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4388:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4389:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4390:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4391:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4392:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4393:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4394:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4395:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4396:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4397:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4398:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4399:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4400:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4401:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4402:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4403:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4404:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4405:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4406:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4407:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4408:                        return(true);
                   4409:                } else if(name != NULL) {
                   4410:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4411:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4412:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4413: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4414:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4415:                                return(true);
                   4416:                        }
                   4417:                }
1.1.1.24  root     4418:        }
                   4419:        return(false);
1.1.1.11  root     4420: }
                   4421: 
1.1.1.45  root     4422: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4423: {
1.1.1.14  root     4424:        char full[MAX_PATH], *name;
1.1.1.8   root     4425:        
1.1.1.24  root     4426:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4427:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4428:        }
                   4429:        return(false);
                   4430: }
                   4431: 
1.1.1.45  root     4432: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4433: {
                   4434:        char full[MAX_PATH], *name;
                   4435:        
                   4436:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4437:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4438:                        return(1);
                   4439:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4440:                        return(2);
                   4441:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4442:                        return(3);
                   4443:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4444:                        return(4);
1.1.1.24  root     4445:                }
                   4446:        }
1.1.1.29  root     4447:        return(0);
                   4448: }
                   4449: 
1.1.1.45  root     4450: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4451: {
                   4452:        // 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     4453:        const char *p = NULL;
1.1.1.37  root     4454:        
                   4455:        if((p = strstr(path, ":")) != NULL) {
                   4456:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4457:                
                   4458:                // baud rate
                   4459:                int baud = max(110, min(9600, atoi(p + 1)));
                   4460:                UINT16 divisor = 115200 / baud;
                   4461:                
                   4462:                if((p = strstr(p + 1, ",")) != NULL) {
                   4463:                        // parity
                   4464:                        if(p[1] == 'N' || p[1] == 'n') {
                   4465:                                selector = (selector & ~0x38) | 0x00;
                   4466:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4467:                                selector = (selector & ~0x38) | 0x08;
                   4468:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4469:                                selector = (selector & ~0x38) | 0x18;
                   4470:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4471:                                selector = (selector & ~0x38) | 0x28;
                   4472:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4473:                                selector = (selector & ~0x38) | 0x38;
                   4474:                        }
                   4475:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4476:                                // word length
                   4477:                                if(p[1] == '8') {
                   4478:                                        selector = (selector & ~0x03) | 0x03;
                   4479:                                } else if(p[1] == '7') {
                   4480:                                        selector = (selector & ~0x03) | 0x02;
                   4481:                                } else if(p[1] == '6') {
                   4482:                                        selector = (selector & ~0x03) | 0x01;
                   4483:                                } else if(p[1] == '5') {
                   4484:                                        selector = (selector & ~0x03) | 0x00;
                   4485:                                }
                   4486:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4487:                                        // stop bits
                   4488:                                        float bits = atof(p + 1);
                   4489:                                        if(bits > 1.0F) {
                   4490:                                                selector |= 0x04;
                   4491:                                        } else {
                   4492:                                                selector &= ~0x04;
                   4493:                                        }
                   4494:                                }
                   4495:                        }
                   4496:                }
                   4497:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4498:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4499:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4500:                sio_write(sio_port - 1, 3, selector);
                   4501:        }
                   4502: }
                   4503: 
1.1.1.45  root     4504: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4505: {
                   4506:        char full[MAX_PATH], *name;
                   4507:        
                   4508:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4509:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4510:                        return(1);
                   4511:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4512:                        return(1);
                   4513:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4514:                        return(2);
                   4515:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4516:                        return(3);
                   4517:                }
                   4518:        }
                   4519:        return(0);
                   4520: }
                   4521: 
1.1.1.44  root     4522: bool msdos_is_valid_drive(int drv)
                   4523: {
                   4524:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4525: }
                   4526: 
                   4527: bool msdos_is_removable_drive(int drv)
                   4528: {
                   4529:        char volume[] = "A:\\";
                   4530:        
                   4531:        volume[0] = 'A' + drv;
                   4532:        
                   4533:        return(GetDriveType(volume) == DRIVE_REMOVABLE);
                   4534: }
                   4535: 
                   4536: bool msdos_is_cdrom_drive(int drv)
                   4537: {
                   4538:        char volume[] = "A:\\";
                   4539:        
                   4540:        volume[0] = 'A' + drv;
                   4541:        
                   4542:        return(GetDriveType(volume) == DRIVE_CDROM);
                   4543: }
                   4544: 
                   4545: bool msdos_is_remote_drive(int drv)
                   4546: {
                   4547:        char volume[] = "A:\\";
                   4548:        
                   4549:        volume[0] = 'A' + drv;
                   4550:        
                   4551:        return(GetDriveType(volume) == DRIVE_REMOTE);
                   4552: }
                   4553: 
                   4554: bool msdos_is_subst_drive(int drv)
                   4555: {
                   4556:        char device[] = "A:", path[MAX_PATH];
                   4557:        
                   4558:        device[0] = 'A' + drv;
                   4559:        
                   4560:        if(QueryDosDevice(device, path, MAX_PATH)) {
                   4561:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4562:                        return(true);
                   4563:                }
                   4564:        }
                   4565:        return(false);
                   4566: }
                   4567: 
1.1.1.45  root     4568: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4569: {
                   4570:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4571:        WIN32_FIND_DATA FindData;
                   4572:        HANDLE hFind;
                   4573:        
                   4574:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4575:                FindClose(hFind);
                   4576:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4577:        }
                   4578:        return(false);
1.1.1.8   root     4579: }
                   4580: 
1.1.1.45  root     4581: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4582: {
                   4583:        static char tmp[MAX_PATH];
1.1.1.28  root     4584:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4585:        
1.1.1.28  root     4586:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4587:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4588:                sprintf(file_name, "COMMAND.COM");
                   4589:                if(_access(tmp, 0) == 0) {
                   4590:                        return(tmp);
                   4591:                }
                   4592:        }
1.1.1.28  root     4593:        
                   4594:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4595:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4596:                sprintf(file_name, "COMMAND.COM");
                   4597:                if(_access(tmp, 0) == 0) {
                   4598:                        return(tmp);
                   4599:                }
                   4600:        }
1.1.1.28  root     4601:        
                   4602:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4603:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4604:                if(_access(tmp, 0) == 0) {
                   4605:                        return(tmp);
                   4606:                }
                   4607:        }
1.1.1.28  root     4608:        
                   4609:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4610:        strcpy(path, env_path);
                   4611:        char *token = my_strtok(path, ";");
1.1.1.9   root     4612:        while(token != NULL) {
1.1.1.14  root     4613:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4614:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4615:                        if(_access(tmp, 0) == 0) {
                   4616:                                return(tmp);
                   4617:                        }
                   4618:                }
                   4619:                token = my_strtok(NULL, ";");
                   4620:        }
                   4621:        return(NULL);
                   4622: }
                   4623: 
1.1.1.14  root     4624: int msdos_drive_number(const char *path)
1.1       root     4625: {
                   4626:        char tmp[MAX_PATH], *name;
                   4627:        
1.1.1.45  root     4628:        if(GetFullPathName(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
                   4629:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4630:                        return(tmp[0] - 'a');
                   4631:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4632:                        return(tmp[0] - 'A');
                   4633:                }
1.1       root     4634:        }
1.1.1.45  root     4635: //     return(msdos_drive_number("."));
                   4636:        return(_getdrive() - 1);
1.1       root     4637: }
                   4638: 
1.1.1.45  root     4639: const char *msdos_volume_label(const char *path)
1.1       root     4640: {
                   4641:        static char tmp[MAX_PATH];
                   4642:        char volume[] = "A:\\";
                   4643:        
                   4644:        if(path[1] == ':') {
                   4645:                volume[0] = path[0];
                   4646:        } else {
                   4647:                volume[0] = 'A' + _getdrive() - 1;
                   4648:        }
                   4649:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4650:                memset(tmp, 0, sizeof(tmp));
                   4651:        }
                   4652:        return(tmp);
                   4653: }
                   4654: 
1.1.1.45  root     4655: const char *msdos_short_volume_label(const char *label)
1.1       root     4656: {
                   4657:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4658:        const char *src = label;
1.1       root     4659:        int remain = strlen(label);
                   4660:        char *dst_n = tmp;
                   4661:        char *dst_e = tmp + 9;
                   4662:        
                   4663:        strcpy(tmp, "        .   ");
                   4664:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4665:                if(msdos_lead_byte_check(*src)) {
                   4666:                        if(++i == 8) {
                   4667:                                break;
                   4668:                        }
                   4669:                        *dst_n++ = *src++;
                   4670:                        remain--;
                   4671:                }
                   4672:                *dst_n++ = *src++;
                   4673:                remain--;
                   4674:        }
                   4675:        if(remain > 0) {
                   4676:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4677:                        if(msdos_lead_byte_check(*src)) {
                   4678:                                if(++i == 3) {
                   4679:                                        break;
                   4680:                                }
                   4681:                                *dst_e++ = *src++;
                   4682:                                remain--;
                   4683:                        }
                   4684:                        *dst_e++ = *src++;
                   4685:                        remain--;
                   4686:                }
                   4687:                *dst_e = '\0';
                   4688:        } else {
                   4689:                *dst_n = '\0';
                   4690:        }
                   4691:        my_strupr(tmp);
                   4692:        return(tmp);
                   4693: }
                   4694: 
1.1.1.13  root     4695: errno_t msdos_maperr(unsigned long oserrno)
                   4696: {
                   4697:        _doserrno = oserrno;
1.1.1.14  root     4698:        switch(oserrno) {
1.1.1.13  root     4699:        case ERROR_FILE_NOT_FOUND:         // 2
                   4700:        case ERROR_PATH_NOT_FOUND:         // 3
                   4701:        case ERROR_INVALID_DRIVE:          // 15
                   4702:        case ERROR_NO_MORE_FILES:          // 18
                   4703:        case ERROR_BAD_NETPATH:            // 53
                   4704:        case ERROR_BAD_NET_NAME:           // 67
                   4705:        case ERROR_BAD_PATHNAME:           // 161
                   4706:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4707:                return ENOENT;
                   4708:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4709:                return EMFILE;
                   4710:        case ERROR_ACCESS_DENIED:          // 5
                   4711:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4712:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4713:        case ERROR_CANNOT_MAKE:            // 82
                   4714:        case ERROR_FAIL_I24:               // 83
                   4715:        case ERROR_DRIVE_LOCKED:           // 108
                   4716:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4717:        case ERROR_NOT_LOCKED:             // 158
                   4718:        case ERROR_LOCK_FAILED:            // 167
                   4719:                return EACCES;
                   4720:        case ERROR_INVALID_HANDLE:         // 6
                   4721:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4722:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4723:                return EBADF;
                   4724:        case ERROR_ARENA_TRASHED:          // 7
                   4725:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4726:        case ERROR_INVALID_BLOCK:          // 9
                   4727:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4728:                return ENOMEM;
                   4729:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4730:                return E2BIG;
                   4731:        case ERROR_BAD_FORMAT:             // 11
                   4732:                return ENOEXEC;
                   4733:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4734:                return EXDEV;
                   4735:        case ERROR_FILE_EXISTS:            // 80
                   4736:        case ERROR_ALREADY_EXISTS:         // 183
                   4737:                return EEXIST;
                   4738:        case ERROR_NO_PROC_SLOTS:          // 89
                   4739:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4740:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4741:                return EAGAIN;
                   4742:        case ERROR_BROKEN_PIPE:            // 109
                   4743:                return EPIPE;
                   4744:        case ERROR_DISK_FULL:              // 112
                   4745:                return ENOSPC;
                   4746:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4747:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4748:                return ECHILD;
                   4749:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4750:                return ENOTEMPTY;
                   4751:        }
1.1.1.14  root     4752:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4753:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4754:                return EACCES;
                   4755:        }
1.1.1.14  root     4756:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4757:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4758:                return ENOEXEC;
                   4759:        }
                   4760:        return EINVAL;
                   4761: }
                   4762: 
1.1.1.45  root     4763: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4764: {
1.1.1.14  root     4765:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4766:                return(_open(path, oflag));
1.1.1.13  root     4767:        }
1.1.1.14  root     4768:        
                   4769:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4770:        DWORD disposition;
1.1.1.14  root     4771:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4772:        default:
1.1.1.13  root     4773:        case _O_EXCL:
                   4774:                disposition = OPEN_EXISTING;
                   4775:                break;
                   4776:        case _O_CREAT:
                   4777:                disposition = OPEN_ALWAYS;
                   4778:                break;
                   4779:        case _O_CREAT | _O_EXCL:
                   4780:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4781:                disposition = CREATE_NEW;
                   4782:                break;
                   4783:        case _O_TRUNC:
                   4784:        case _O_TRUNC | _O_EXCL:
                   4785:                disposition = TRUNCATE_EXISTING;
                   4786:                break;
                   4787:        case _O_CREAT | _O_TRUNC:
                   4788:                disposition = CREATE_ALWAYS;
                   4789:                break;
                   4790:        }
1.1.1.14  root     4791:        
1.1.1.45  root     4792:        HANDLE h = CreateFile(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4793:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4794:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4795:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4796:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4797:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.45  root     4798:                h = CreateFile(path, GENERIC_READ,
1.1.1.13  root     4799:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4800:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4801:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4802:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4803:                        return(-1);
1.1.1.13  root     4804:                }
                   4805:        }
1.1.1.14  root     4806:        
1.1.1.13  root     4807:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4808:        if(fd == -1) {
1.1.1.13  root     4809:                CloseHandle(h);
                   4810:        }
1.1.1.45  root     4811:        return(fd);
                   4812: }
                   4813: 
                   4814: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4815: {
                   4816:        int fd = -1;
                   4817:        
                   4818:        *sio_port = *lpt_port = 0;
                   4819:        
                   4820:        if(msdos_is_con_path(path)) {
                   4821:                // MODE.COM opens CON device with read/write mode :-(
                   4822:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   4823:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   4824:                        oflag |= _O_RDONLY;
                   4825:                }
                   4826:                if((fd = msdos_open("CON", oflag)) == -1) {
                   4827: //                     fd = msdos_open("NUL", oflag);
                   4828:                }
                   4829:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   4830:                fd = msdos_open("NUL", oflag);
                   4831:                msdos_set_comm_params(*sio_port, path);
                   4832:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   4833:                fd = msdos_open("NUL", oflag);
                   4834:        } else if(msdos_is_device_path(path)) {
                   4835:                fd = msdos_open("NUL", oflag);
                   4836: //     } else if(oflag & _O_CREAT) {
                   4837: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   4838: //     } else {
                   4839: //             fd = _open(path, oflag);
                   4840:        }
                   4841:        return(fd);
                   4842: }
                   4843: 
                   4844: UINT16 msdos_device_info(const char *path)
                   4845: {
                   4846:        if(msdos_is_con_path(path)) {
                   4847:                return(0x80d3);
                   4848:        } else if(msdos_is_comm_path(path)) {
                   4849:                return(0x80a0);
                   4850:        } else if(msdos_is_prn_path(path)) {
                   4851: //             return(0xa8c0);
                   4852:                return(0x80a0);
                   4853:        } else if(msdos_is_device_path(path)) {
                   4854:                if(strstr(path, "EMMXXXX0") != NULL) {
                   4855:                        return(0xc0c0);
                   4856:                } else if(strstr(path, "MSCD001") != NULL) {
                   4857:                        return(0xc880);
                   4858:                } else {
                   4859:                        return(0x8084);
                   4860:                }
                   4861:        } else {
                   4862:                return(msdos_drive_number(path));
                   4863:        }
1.1.1.13  root     4864: }
                   4865: 
1.1.1.52  root     4866: 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     4867: {
                   4868:        static int id = 0;
                   4869:        char full[MAX_PATH], *name;
                   4870:        
                   4871:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4872:                strcpy(file_handler[fd].path, full);
                   4873:        } else {
                   4874:                strcpy(file_handler[fd].path, path);
                   4875:        }
1.1.1.14  root     4876:        // isatty makes no distinction between CON & NUL
                   4877:        // GetFileSize fails on CON, succeeds on NUL
                   4878:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     4879:                if(info == 0x80d3) {
                   4880:                        info = 0x8084;
                   4881:                }
1.1.1.14  root     4882:                atty = 0;
                   4883:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     4884: //             info = msdos_drive_number(".");
                   4885:                info = msdos_drive_number(path);
1.1.1.14  root     4886:        }
1.1       root     4887:        file_handler[fd].valid = 1;
                   4888:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     4889:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4890:        file_handler[fd].mode = mode;
                   4891:        file_handler[fd].info = info;
                   4892:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     4893:        file_handler[fd].sio_port = sio_port;
                   4894:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4895:        
                   4896:        // init system file table
                   4897:        if(fd < 20) {
                   4898:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4899:                
                   4900:                memset(sft, 0, 0x3b);
                   4901:                
                   4902:                *(UINT16 *)(sft + 0x00) = 1;
                   4903:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4904:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4905:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4906:                
                   4907:                if(!(file_handler[fd].info & 0x80)) {
                   4908:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4909:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4910:                        
                   4911:                        FILETIME time, local;
                   4912:                        HANDLE hHandle;
                   4913:                        WORD dos_date = 0, dos_time = 0;
                   4914:                        DWORD file_size = 0;
                   4915:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4916:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4917:                                        FileTimeToLocalFileTime(&time, &local);
                   4918:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4919:                                }
                   4920:                                file_size = GetFileSize(hHandle, NULL);
                   4921:                        }
                   4922:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4923:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4924:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4925:                }
                   4926:                
                   4927:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4928:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4929:                my_strupr(fname);
                   4930:                my_strupr(ext);
                   4931:                memset(sft + 0x20, 0x20, 11);
                   4932:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4933:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4934:                
                   4935:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4936:        }
1.1       root     4937: }
                   4938: 
                   4939: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4940: {
                   4941:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4942:        file_handler[dst].valid = 1;
                   4943:        file_handler[dst].id = file_handler[src].id;
                   4944:        file_handler[dst].atty = file_handler[src].atty;
                   4945:        file_handler[dst].mode = file_handler[src].mode;
                   4946:        file_handler[dst].info = file_handler[src].info;
                   4947:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     4948:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   4949:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4950: }
                   4951: 
1.1.1.20  root     4952: void msdos_file_handler_close(int fd)
1.1       root     4953: {
                   4954:        file_handler[fd].valid = 0;
1.1.1.21  root     4955:        
                   4956:        if(fd < 20) {
                   4957:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4958:        }
1.1       root     4959: }
                   4960: 
1.1.1.14  root     4961: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4962: {
1.1.1.14  root     4963:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4964:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4965:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4966: }
                   4967: 
                   4968: // find file
                   4969: 
                   4970: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4971: {
                   4972:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4973:                return(0);      // search directory only !!!
                   4974:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4975:                return(0);
                   4976:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4977:                return(0);
                   4978:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4979:                return(0);
                   4980:        } else if((attribute & required_mask) != required_mask) {
                   4981:                return(0);
                   4982:        } else {
                   4983:                return(1);
                   4984:        }
                   4985: }
                   4986: 
1.1.1.13  root     4987: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4988: {
1.1.1.14  root     4989:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     4990:                return(1);
1.1.1.13  root     4991:        }
                   4992:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4993:        if(len > 12) {
1.1.1.42  root     4994:                return(0);
1.1.1.13  root     4995:        }
                   4996:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4997:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     4998:                return(0);
1.1.1.13  root     4999:        }
1.1.1.42  root     5000:        return(1);
1.1.1.13  root     5001: }
                   5002: 
1.1       root     5003: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   5004: {
                   5005:        FILETIME local;
                   5006:        
                   5007:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   5008:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   5009:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   5010:        
                   5011:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   5012:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   5013:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   5014:        
                   5015:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   5016:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   5017:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   5018: }
                   5019: 
                   5020: // i/o
                   5021: 
                   5022: void msdos_stdio_reopen()
                   5023: {
                   5024:        if(!file_handler[0].valid) {
                   5025:                _dup2(DUP_STDIN, 0);
                   5026:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   5027:        }
                   5028:        if(!file_handler[1].valid) {
                   5029:                _dup2(DUP_STDOUT, 1);
                   5030:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   5031:        }
                   5032:        if(!file_handler[2].valid) {
                   5033:                _dup2(DUP_STDERR, 2);
                   5034:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   5035:        }
1.1.1.21  root     5036:        if(!file_handler[3].valid) {
                   5037:                _dup2(DUP_STDAUX, 3);
                   5038:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   5039:        }
                   5040:        if(!file_handler[4].valid) {
                   5041:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     5042: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   5043:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     5044:        }
                   5045:        for(int i = 0; i < 5; i++) {
                   5046:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   5047:                        msdos_psp_set_file_table(i, i, current_psp);
                   5048:                }
                   5049:        }
1.1       root     5050: }
                   5051: 
1.1.1.37  root     5052: int msdos_read(int fd, void *buffer, unsigned int count)
                   5053: {
                   5054:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5055:                // read from serial port
                   5056:                int read = 0;
                   5057:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5058:                        UINT8 *buf = (UINT8 *)buffer;
                   5059:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5060:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5061:                        DWORD timeout = timeGetTime() + 1000;
                   5062:                        while(read < count) {
                   5063:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   5064:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   5065:                                        timeout = timeGetTime() + 1000;
                   5066:                                } else {
                   5067:                                        if(timeGetTime() > timeout) {
                   5068:                                                break;
                   5069:                                        }
                   5070:                                        Sleep(10);
1.1.1.37  root     5071:                                }
                   5072:                        }
                   5073:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5074:                }
                   5075:                return(read);
                   5076:        }
                   5077:        return(_read(fd, buffer, count));
                   5078: }
                   5079: 
1.1       root     5080: int msdos_kbhit()
                   5081: {
                   5082:        msdos_stdio_reopen();
                   5083:        
1.1.1.20  root     5084:        process_t *process = msdos_process_info_get(current_psp);
                   5085:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5086:        
                   5087:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5088:                // stdin is redirected to file
1.1.1.20  root     5089:                return(eof(fd) == 0);
1.1       root     5090:        }
                   5091:        
                   5092:        // check keyboard status
1.1.1.35  root     5093:        if(key_recv != 0) {
1.1       root     5094:                return(1);
                   5095:        }
1.1.1.35  root     5096:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5097: #ifdef USE_SERVICE_THREAD
                   5098:                EnterCriticalSection(&key_buf_crit_sect);
                   5099: #endif
1.1.1.55  root     5100:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5101: #ifdef USE_SERVICE_THREAD
                   5102:                LeaveCriticalSection(&key_buf_crit_sect);
                   5103: #endif
                   5104:                if(!empty) return(1);
                   5105:        }
                   5106:        return(_kbhit());
1.1       root     5107: }
                   5108: 
                   5109: int msdos_getch_ex(int echo)
                   5110: {
                   5111:        static char prev = 0;
                   5112:        
                   5113:        msdos_stdio_reopen();
                   5114:        
1.1.1.20  root     5115:        process_t *process = msdos_process_info_get(current_psp);
                   5116:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5117:        
                   5118:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5119:                // stdin is redirected to file
                   5120: retry:
                   5121:                char data;
1.1.1.37  root     5122:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     5123:                        char tmp = data;
                   5124:                        if(data == 0x0a) {
                   5125:                                if(prev == 0x0d) {
                   5126:                                        goto retry; // CRLF -> skip LF
                   5127:                                } else {
                   5128:                                        data = 0x0d; // LF only -> CR
                   5129:                                }
                   5130:                        }
                   5131:                        prev = tmp;
                   5132:                        return(data);
                   5133:                }
                   5134:                return(EOF);
                   5135:        }
                   5136:        
                   5137:        // input from console
1.1.1.5   root     5138:        int key_char, key_scan;
1.1.1.33  root     5139:        if(key_recv != 0) {
1.1.1.5   root     5140:                key_char = (key_code >> 0) & 0xff;
                   5141:                key_scan = (key_code >> 8) & 0xff;
                   5142:                key_code >>= 16;
1.1.1.33  root     5143:                key_recv >>= 16;
1.1.1.5   root     5144:        } else {
1.1.1.54  root     5145:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35  root     5146:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5147: #ifdef USE_SERVICE_THREAD
                   5148:                                EnterCriticalSection(&key_buf_crit_sect);
                   5149: #endif
1.1.1.55  root     5150:                                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5151: #ifdef USE_SERVICE_THREAD
                   5152:                                LeaveCriticalSection(&key_buf_crit_sect);
                   5153: #endif
                   5154:                                if(!empty) break;
                   5155:                        }
1.1.1.23  root     5156:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   5157:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   5158:                                if(_kbhit()) {
1.1.1.32  root     5159:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5160: #ifdef USE_SERVICE_THREAD
                   5161:                                                EnterCriticalSection(&key_buf_crit_sect);
                   5162: #endif
1.1.1.51  root     5163:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     5164: #ifdef USE_SERVICE_THREAD
                   5165:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   5166: #endif
1.1.1.32  root     5167:                                        }
1.1.1.23  root     5168:                                } else {
                   5169:                                        Sleep(10);
                   5170:                                }
                   5171:                        } else {
                   5172:                                if(!update_key_buffer()) {
                   5173:                                        Sleep(10);
                   5174:                                }
1.1.1.14  root     5175:                        }
                   5176:                }
1.1.1.54  root     5177:                if(m_exit) {
1.1.1.33  root     5178:                        // insert CR to terminate input loops
1.1.1.14  root     5179:                        key_char = 0x0d;
                   5180:                        key_scan = 0;
1.1.1.32  root     5181:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5182: #ifdef USE_SERVICE_THREAD
                   5183:                        EnterCriticalSection(&key_buf_crit_sect);
                   5184: #endif
1.1.1.51  root     5185:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     5186: #ifdef USE_SERVICE_THREAD
                   5187:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5188: #endif
1.1.1.5   root     5189:                }
1.1       root     5190:        }
                   5191:        if(echo && key_char) {
                   5192:                msdos_putch(key_char);
                   5193:        }
                   5194:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5195: }
                   5196: 
                   5197: inline int msdos_getch()
                   5198: {
                   5199:        return(msdos_getch_ex(0));
                   5200: }
                   5201: 
                   5202: inline int msdos_getche()
                   5203: {
                   5204:        return(msdos_getch_ex(1));
                   5205: }
                   5206: 
                   5207: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5208: {
1.1.1.37  root     5209:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5210:                // write to serial port
1.1.1.38  root     5211:                int written = 0;
1.1.1.37  root     5212:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5213:                        UINT8 *buf = (UINT8 *)buffer;
                   5214:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5215:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5216:                        DWORD timeout = timeGetTime() + 1000;
                   5217:                        while(written < count) {
                   5218:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5219:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5220:                                        timeout = timeGetTime() + 1000;
                   5221:                                } else {
                   5222:                                        if(timeGetTime() > timeout) {
                   5223:                                                break;
                   5224:                                        }
                   5225:                                        Sleep(10);
                   5226:                                }
1.1.1.37  root     5227:                        }
                   5228:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5229:                }
1.1.1.38  root     5230:                return(written);
1.1.1.37  root     5231:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5232:                // write to printer port
                   5233:                UINT8 *buf = (UINT8 *)buffer;
                   5234:                for(unsigned int i = 0; i < count; i++) {
                   5235: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5236:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5237:                }
                   5238:                return(count);
                   5239:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5240:                // CR+LF -> LF
1.1.1.37  root     5241:                static int is_cr = 0;
1.1       root     5242:                UINT8 *buf = (UINT8 *)buffer;
                   5243:                for(unsigned int i = 0; i < count; i++) {
                   5244:                        UINT8 data = buf[i];
                   5245:                        if(is_cr) {
                   5246:                                if(data != 0x0a) {
                   5247:                                        UINT8 tmp = 0x0d;
                   5248:                                        _write(1, &tmp, 1);
                   5249:                                }
                   5250:                                _write(1, &data, 1);
                   5251:                                is_cr = 0;
                   5252:                        } else if(data == 0x0d) {
                   5253:                                is_cr = 1;
                   5254:                        } else {
                   5255:                                _write(1, &data, 1);
                   5256:                        }
                   5257:                }
                   5258:                return(count);
                   5259:        }
1.1.1.14  root     5260:        vram_flush();
1.1       root     5261:        return(_write(fd, buffer, count));
                   5262: }
                   5263: 
                   5264: void msdos_putch(UINT8 data)
1.1.1.50  root     5265: {
                   5266:        msdos_stdio_reopen();
                   5267:        
                   5268:        process_t *process = msdos_process_info_get(current_psp);
                   5269:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5270:        
                   5271:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5272:                // stdout is redirected to file
                   5273:                msdos_write(fd, &data, 1);
                   5274:                return;
                   5275:        }
                   5276:        
                   5277:        // call int 29h ?
1.1.1.58  root     5278:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     5279:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5280:                // int 29h is not hooked, no need to call int 29h
                   5281:                msdos_putch_fast(data);
                   5282: #ifdef USE_SERVICE_THREAD
                   5283:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5284:                // XXX: in usually we should not reach here
                   5285:                // this is called from service thread to echo the input
                   5286:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5287:                msdos_putch_fast(data);
                   5288: #endif
1.1.1.51  root     5289:        } else if(in_service_29h) {
1.1.1.50  root     5290:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5291:                msdos_putch_fast(data);
                   5292:        } else {
                   5293:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5294:                in_service_29h = true;
1.1.1.50  root     5295:                try {
                   5296:                        UINT32 tmp_pc = m_pc;
                   5297:                        UINT16 tmp_ax = REG16(AX);
                   5298:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5299:                        
                   5300:                        // call int 29h routine is at fffc:0027
                   5301:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5302:                        REG8(AL) = data;
                   5303:                        
                   5304:                        // run cpu until call int 29h routine is done
1.1.1.54  root     5305:                        while(!m_exit && tmp_pc != m_pc) {
1.1.1.50  root     5306:                                try {
                   5307:                                        hardware_run_cpu();
                   5308:                                } catch(...) {
                   5309:                                }
                   5310:                        }
                   5311:                        REG16(AX) = tmp_ax;
                   5312:                        REG16(BX) = tmp_bx;
                   5313:                } catch(...) {
                   5314:                }
1.1.1.51  root     5315:                in_service_29h = false;
1.1.1.50  root     5316:        }
                   5317: }
                   5318: 
                   5319: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5320: #ifdef USE_SERVICE_THREAD
                   5321: {
                   5322:        EnterCriticalSection(&putch_crit_sect);
                   5323:        msdos_putch_tmp(data);
                   5324:        LeaveCriticalSection(&putch_crit_sect);
                   5325: }
                   5326: void msdos_putch_tmp(UINT8 data)
                   5327: #endif
1.1       root     5328: {
1.1.1.34  root     5329:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5330:        SMALL_RECT rect;
                   5331:        COORD co;
1.1       root     5332:        static int p = 0;
                   5333:        static int is_kanji = 0;
                   5334:        static int is_esc = 0;
                   5335:        static int stored_x;
                   5336:        static int stored_y;
                   5337:        static WORD stored_a;
1.1.1.20  root     5338:        static char tmp[64], out[64];
1.1       root     5339:        
1.1.1.23  root     5340:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5341:        
                   5342:        // output to console
                   5343:        tmp[p++] = data;
                   5344:        
1.1.1.14  root     5345:        vram_flush();
                   5346:        
1.1       root     5347:        if(is_kanji) {
                   5348:                // kanji character
                   5349:                is_kanji = 0;
                   5350:        } else if(is_esc) {
                   5351:                // escape sequense
                   5352:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5353:                        p = is_esc = 0;
                   5354:                } else if(tmp[1] == '=' && p == 4) {
                   5355:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5356:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5357:                        SetConsoleCursorPosition(hStdout, co);
                   5358:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5359:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5360:                        cursor_moved = false;
1.1.1.59! root     5361:                        cursor_moved_by_crtc = false;
1.1       root     5362:                        p = is_esc = 0;
                   5363:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
1.1.1.59! root     5364:                        if(cursor_moved_by_crtc) {
        !          5365:                                if(!restore_console_on_exit) {
        !          5366:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
        !          5367:                                        scr_top = csbi.srWindow.Top;
        !          5368:                                }
        !          5369:                                co.X = mem[0x450 + REG8(BH) * 2];
        !          5370:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
        !          5371:                                SetConsoleCursorPosition(hStdout, co);
        !          5372:                                cursor_moved_by_crtc = false;
        !          5373:                        }
1.1       root     5374:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5375:                        co.X = csbi.dwCursorPosition.X;
                   5376:                        co.Y = csbi.dwCursorPosition.Y;
                   5377:                        WORD wAttributes = csbi.wAttributes;
                   5378:                        
                   5379:                        if(tmp[1] == 'D') {
                   5380:                                co.Y++;
                   5381:                        } else if(tmp[1] == 'E') {
                   5382:                                co.X = 0;
                   5383:                                co.Y++;
                   5384:                        } else if(tmp[1] == 'M') {
                   5385:                                co.Y--;
                   5386:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5387:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5388:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5389:                                co.X = 0;
                   5390:                                co.Y = csbi.srWindow.Top;
1.1       root     5391:                        } else if(tmp[1] == '[') {
                   5392:                                int param[256], params = 0;
                   5393:                                memset(param, 0, sizeof(param));
                   5394:                                for(int i = 2; i < p; i++) {
                   5395:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5396:                                                param[params] *= 10;
                   5397:                                                param[params] += tmp[i] - '0';
                   5398:                                        } else {
                   5399:                                                params++;
                   5400:                                        }
                   5401:                                }
                   5402:                                if(data == 'A') {
1.1.1.14  root     5403:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5404:                                } else if(data == 'B') {
1.1.1.14  root     5405:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5406:                                } else if(data == 'C') {
1.1.1.14  root     5407:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5408:                                } else if(data == 'D') {
1.1.1.14  root     5409:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5410:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5411:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5412:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5413:                                } else if(data == 'J') {
1.1.1.14  root     5414:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5415:                                        if(param[0] == 0) {
                   5416:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5417:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5418:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5419:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5420:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5421:                                                }
                   5422:                                        } else if(param[0] == 1) {
1.1.1.14  root     5423:                                                if(co.Y > csbi.srWindow.Top) {
                   5424:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5425:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5426:                                                }
                   5427:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5428:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5429:                                        } else if(param[0] == 2) {
1.1.1.14  root     5430:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5431:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5432:                                                co.X = co.Y = 0;
                   5433:                                        }
                   5434:                                } else if(data == 'K') {
1.1.1.14  root     5435:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5436:                                        if(param[0] == 0) {
                   5437:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5438:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5439:                                        } else if(param[0] == 1) {
                   5440:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5441:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5442:                                        } else if(param[0] == 2) {
                   5443:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5444:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5445:                                        }
                   5446:                                } else if(data == 'L') {
1.1.1.14  root     5447:                                        if(params == 0) {
                   5448:                                                param[0] = 1;
1.1       root     5449:                                        }
1.1.1.14  root     5450:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5451:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5452:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5453:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5454:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5455:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5456:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5457:                                        co.X = 0;
                   5458:                                } else if(data == 'M') {
1.1.1.14  root     5459:                                        if(params == 0) {
                   5460:                                                param[0] = 1;
                   5461:                                        }
                   5462:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5463:                                                clear_scr_buffer(csbi.wAttributes);
                   5464:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5465:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5466:                                        } else {
1.1.1.14  root     5467:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5468:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5469:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5470:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5471:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5472:                                        }
                   5473:                                        co.X = 0;
                   5474:                                } else if(data == 'h') {
                   5475:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.59! root     5476: //                                             CONSOLE_CURSOR_INFO ci_new;
        !          5477: //                                             GetConsoleCursorInfo(hStdout, &ci_new);
        !          5478:                                                if(ci_new.bVisible) {
        !          5479:                                                        ci_new.bVisible = FALSE;
        !          5480: //                                                     SetConsoleCursorInfo(hStdout, &ci_new);
1.1       root     5481:                                                }
                   5482:                                        }
                   5483:                                } else if(data == 'l') {
                   5484:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.59! root     5485: //                                             CONSOLE_CURSOR_INFO ci_new;
        !          5486: //                                             GetConsoleCursorInfo(hStdout, &ci_new);
        !          5487:                                                if(!ci_new.bVisible) {
        !          5488:                                                        ci_new.bVisible = TRUE;
        !          5489: //                                                     SetConsoleCursorInfo(hStdout, &ci_new);
1.1       root     5490:                                                }
                   5491:                                        }
                   5492:                                } else if(data == 'm') {
                   5493:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5494:                                        int reverse = 0, hidden = 0;
                   5495:                                        for(int i = 0; i < params; i++) {
                   5496:                                                if(param[i] == 1) {
                   5497:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5498:                                                } else if(param[i] == 4) {
                   5499:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5500:                                                } else if(param[i] == 7) {
                   5501:                                                        reverse = 1;
                   5502:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5503:                                                        hidden = 1;
                   5504:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5505:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5506:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5507:                                                                param[i] -= 16;
                   5508:                                                        } else {
                   5509:                                                                param[i] -= 30;
                   5510:                                                        }
                   5511:                                                        if(param[i] & 1) {
                   5512:                                                                wAttributes |= FOREGROUND_RED;
                   5513:                                                        }
                   5514:                                                        if(param[i] & 2) {
                   5515:                                                                wAttributes |= FOREGROUND_GREEN;
                   5516:                                                        }
                   5517:                                                        if(param[i] & 4) {
                   5518:                                                                wAttributes |= FOREGROUND_BLUE;
                   5519:                                                        }
                   5520:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5521:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5522:                                                        if((param[i] - 40) & 1) {
                   5523:                                                                wAttributes |= BACKGROUND_RED;
                   5524:                                                        }
                   5525:                                                        if((param[i] - 40) & 2) {
                   5526:                                                                wAttributes |= BACKGROUND_GREEN;
                   5527:                                                        }
                   5528:                                                        if((param[i] - 40) & 4) {
                   5529:                                                                wAttributes |= BACKGROUND_BLUE;
                   5530:                                                        }
                   5531:                                                }
                   5532:                                        }
                   5533:                                        if(reverse) {
                   5534:                                                wAttributes &= ~0xff;
                   5535:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5536:                                        }
                   5537:                                        if(hidden) {
                   5538:                                                wAttributes &= ~0x0f;
                   5539:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5540:                                        }
                   5541:                                } else if(data == 'n') {
                   5542:                                        if(param[0] == 6) {
                   5543:                                                char tmp[16];
                   5544:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5545:                                                int len = strlen(tmp);
1.1.1.32  root     5546:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5547: #ifdef USE_SERVICE_THREAD
                   5548:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5549: #endif
1.1.1.32  root     5550:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5551:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5552:                                                        }
1.1.1.35  root     5553: #ifdef USE_SERVICE_THREAD
                   5554:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5555: #endif
1.1       root     5556:                                                }
                   5557:                                        }
                   5558:                                } else if(data == 's') {
                   5559:                                        stored_x = co.X;
                   5560:                                        stored_y = co.Y;
                   5561:                                        stored_a = wAttributes;
                   5562:                                } else if(data == 'u') {
                   5563:                                        co.X = stored_x;
                   5564:                                        co.Y = stored_y;
                   5565:                                        wAttributes = stored_a;
                   5566:                                }
                   5567:                        }
                   5568:                        if(co.X < 0) {
                   5569:                                co.X = 0;
                   5570:                        } else if(co.X >= csbi.dwSize.X) {
                   5571:                                co.X = csbi.dwSize.X - 1;
                   5572:                        }
1.1.1.14  root     5573:                        if(co.Y < csbi.srWindow.Top) {
                   5574:                                co.Y = csbi.srWindow.Top;
                   5575:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5576:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5577:                        }
                   5578:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5579:                                SetConsoleCursorPosition(hStdout, co);
                   5580:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5581:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5582:                                cursor_moved = false;
                   5583:                        }
                   5584:                        if(wAttributes != csbi.wAttributes) {
                   5585:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5586:                        }
                   5587:                        p = is_esc = 0;
                   5588:                }
                   5589:                return;
                   5590:        } else {
                   5591:                if(msdos_lead_byte_check(data)) {
                   5592:                        is_kanji = 1;
                   5593:                        return;
                   5594:                } else if(data == 0x1b) {
                   5595:                        is_esc = 1;
                   5596:                        return;
                   5597:                }
                   5598:        }
1.1.1.20  root     5599:        
                   5600:        DWORD q = 0, num;
                   5601:        is_kanji = 0;
                   5602:        for(int i = 0; i < p; i++) {
                   5603:                UINT8 c = tmp[i];
                   5604:                if(is_kanji) {
                   5605:                        is_kanji = 0;
                   5606:                } else if(msdos_lead_byte_check(data)) {
                   5607:                        is_kanji = 1;
                   5608:                } else if(msdos_ctrl_code_check(data)) {
                   5609:                        out[q++] = '^';
                   5610:                        c += 'A' - 1;
                   5611:                }
                   5612:                out[q++] = c;
                   5613:        }
1.1.1.59! root     5614:        if(cursor_moved_by_crtc) {
        !          5615:                if(!restore_console_on_exit) {
        !          5616:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
        !          5617:                        scr_top = csbi.srWindow.Top;
        !          5618:                }
        !          5619:                co.X = mem[0x450 + REG8(BH) * 2];
        !          5620:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
        !          5621:                SetConsoleCursorPosition(hStdout, co);
        !          5622:                cursor_moved_by_crtc = false;
        !          5623:        }
1.1.1.34  root     5624:        if(q == 1 && out[0] == 0x08) {
                   5625:                // back space
                   5626:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5627:                if(csbi.dwCursorPosition.X > 0) {
                   5628:                        co.X = csbi.dwCursorPosition.X - 1;
                   5629:                        co.Y = csbi.dwCursorPosition.Y;
                   5630:                        SetConsoleCursorPosition(hStdout, co);
                   5631:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5632:                        co.X = csbi.dwSize.X - 1;
                   5633:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5634:                        SetConsoleCursorPosition(hStdout, co);
                   5635:                } else {
                   5636:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5637:                }
                   5638:        } else {
                   5639:                WriteConsole(hStdout, out, q, &num, NULL);
                   5640:        }
1.1       root     5641:        p = 0;
1.1.1.14  root     5642:        
1.1.1.15  root     5643:        if(!restore_console_on_exit) {
                   5644:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5645:                scr_top = csbi.srWindow.Top;
                   5646:        }
1.1       root     5647:        cursor_moved = true;
                   5648: }
                   5649: 
                   5650: int msdos_aux_in()
                   5651: {
1.1.1.21  root     5652:        msdos_stdio_reopen();
                   5653:        
1.1.1.20  root     5654:        process_t *process = msdos_process_info_get(current_psp);
                   5655:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5656:        
                   5657:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5658:                char data = 0;
1.1.1.37  root     5659:                msdos_read(fd, &data, 1);
1.1       root     5660:                return(data);
                   5661:        } else {
                   5662:                return(EOF);
                   5663:        }
                   5664: }
                   5665: 
                   5666: void msdos_aux_out(char data)
                   5667: {
1.1.1.21  root     5668:        msdos_stdio_reopen();
                   5669:        
1.1.1.20  root     5670:        process_t *process = msdos_process_info_get(current_psp);
                   5671:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5672:        
                   5673:        if(fd < process->max_files && file_handler[fd].valid) {
                   5674:                msdos_write(fd, &data, 1);
1.1       root     5675:        }
                   5676: }
                   5677: 
                   5678: void msdos_prn_out(char data)
                   5679: {
1.1.1.21  root     5680:        msdos_stdio_reopen();
                   5681:        
1.1.1.20  root     5682:        process_t *process = msdos_process_info_get(current_psp);
                   5683:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5684:        
                   5685:        if(fd < process->max_files && file_handler[fd].valid) {
                   5686:                msdos_write(fd, &data, 1);
1.1       root     5687:        }
                   5688: }
                   5689: 
                   5690: // memory control
                   5691: 
1.1.1.52  root     5692: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5693: {
                   5694:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5695:        
                   5696:        mcb->mz = mz;
                   5697:        mcb->psp = psp;
1.1.1.30  root     5698:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5699:        
                   5700:        if(prog_name != NULL) {
                   5701:                memset(mcb->prog_name, 0, 8);
                   5702:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5703:        }
1.1       root     5704:        return(mcb);
                   5705: }
                   5706: 
                   5707: void msdos_mcb_check(mcb_t *mcb)
                   5708: {
                   5709:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5710:                #if 0
                   5711:                        // shutdown now !!!
                   5712:                        fatalerror("broken memory control block\n");
                   5713:                #else
                   5714:                        // return error code and continue
                   5715:                        throw(0x07); // broken memory control block
                   5716:                #endif
1.1       root     5717:        }
                   5718: }
                   5719: 
1.1.1.39  root     5720: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5721: {
                   5722:        int mcb_seg = seg - 1;
                   5723:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5724:        msdos_mcb_check(mcb);
                   5725:        
1.1.1.30  root     5726:        if(mcb->paragraphs > paragraphs) {
1.1       root     5727:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5728:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5729:                
                   5730:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5731:                mcb->mz = 'M';
1.1.1.30  root     5732:                mcb->paragraphs = paragraphs;
1.1       root     5733:        }
                   5734: }
                   5735: 
                   5736: void msdos_mem_merge(int seg)
                   5737: {
                   5738:        int mcb_seg = seg - 1;
                   5739:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5740:        msdos_mcb_check(mcb);
                   5741:        
                   5742:        while(1) {
                   5743:                if(mcb->mz == 'Z') {
                   5744:                        break;
                   5745:                }
1.1.1.30  root     5746:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5747:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5748:                msdos_mcb_check(next_mcb);
                   5749:                
                   5750:                if(next_mcb->psp != 0) {
                   5751:                        break;
                   5752:                }
                   5753:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5754:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5755:        }
                   5756: }
                   5757: 
1.1.1.8   root     5758: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5759: {
                   5760:        while(1) {
                   5761:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5762:                bool last_block;
1.1       root     5763:                
1.1.1.14  root     5764:                if(mcb->psp == 0) {
                   5765:                        msdos_mem_merge(mcb_seg + 1);
                   5766:                } else {
                   5767:                        msdos_mcb_check(mcb);
                   5768:                }
1.1.1.33  root     5769:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5770:                        // check if the next is dummy mcb to link to umb
                   5771:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5772:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5773:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5774:                }
                   5775:                if(!(new_process && !last_block)) {
1.1.1.30  root     5776:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5777:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5778:                                mcb->psp = current_psp;
                   5779:                                return(mcb_seg + 1);
                   5780:                        }
                   5781:                }
                   5782:                if(mcb->mz == 'Z') {
                   5783:                        break;
                   5784:                }
1.1.1.30  root     5785:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5786:        }
                   5787:        return(-1);
                   5788: }
                   5789: 
                   5790: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5791: {
                   5792:        int mcb_seg = seg - 1;
                   5793:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5794:        msdos_mcb_check(mcb);
1.1.1.30  root     5795:        int current_paragraphs = mcb->paragraphs;
1.1       root     5796:        
                   5797:        msdos_mem_merge(seg);
1.1.1.30  root     5798:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5799:                if(max_paragraphs) {
1.1.1.30  root     5800:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5801:                }
1.1       root     5802:                msdos_mem_split(seg, current_paragraphs);
                   5803:                return(-1);
                   5804:        }
                   5805:        msdos_mem_split(seg, paragraphs);
                   5806:        return(0);
                   5807: }
                   5808: 
                   5809: void msdos_mem_free(int seg)
                   5810: {
                   5811:        int mcb_seg = seg - 1;
                   5812:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5813:        msdos_mcb_check(mcb);
                   5814:        
                   5815:        mcb->psp = 0;
                   5816:        msdos_mem_merge(seg);
                   5817: }
                   5818: 
1.1.1.8   root     5819: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5820: {
                   5821:        int max_paragraphs = 0;
                   5822:        
                   5823:        while(1) {
                   5824:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5825:                bool last_block;
                   5826:                
1.1       root     5827:                msdos_mcb_check(mcb);
                   5828:                
1.1.1.33  root     5829:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5830:                        // check if the next is dummy mcb to link to umb
                   5831:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5832:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5833:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5834:                }
                   5835:                if(!(new_process && !last_block)) {
1.1.1.30  root     5836:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5837:                                max_paragraphs = mcb->paragraphs;
1.1       root     5838:                        }
                   5839:                }
                   5840:                if(mcb->mz == 'Z') {
                   5841:                        break;
                   5842:                }
1.1.1.30  root     5843:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5844:        }
1.1.1.14  root     5845:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5846: }
                   5847: 
1.1.1.8   root     5848: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5849: {
                   5850:        int last_seg = -1;
                   5851:        
                   5852:        while(1) {
                   5853:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5854:                msdos_mcb_check(mcb);
                   5855:                
1.1.1.14  root     5856:                if(mcb->psp == psp) {
1.1.1.8   root     5857:                        last_seg = mcb_seg;
                   5858:                }
1.1.1.14  root     5859:                if(mcb->mz == 'Z') {
                   5860:                        break;
                   5861:                }
1.1.1.30  root     5862:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5863:        }
                   5864:        return(last_seg);
                   5865: }
                   5866: 
1.1.1.19  root     5867: int msdos_mem_get_umb_linked()
                   5868: {
1.1.1.33  root     5869:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5870:        msdos_mcb_check(mcb);
1.1.1.19  root     5871:        
1.1.1.33  root     5872:        if(mcb->mz == 'M') {
                   5873:                return(-1);
1.1.1.19  root     5874:        }
                   5875:        return(0);
                   5876: }
                   5877: 
1.1.1.33  root     5878: void msdos_mem_link_umb()
1.1.1.19  root     5879: {
1.1.1.33  root     5880:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5881:        msdos_mcb_check(mcb);
1.1.1.19  root     5882:        
1.1.1.33  root     5883:        mcb->mz = 'M';
                   5884:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     5885:        
                   5886:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5887: }
                   5888: 
1.1.1.33  root     5889: void msdos_mem_unlink_umb()
1.1.1.19  root     5890: {
1.1.1.33  root     5891:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5892:        msdos_mcb_check(mcb);
1.1.1.19  root     5893:        
1.1.1.33  root     5894:        mcb->mz = 'Z';
                   5895:        mcb->paragraphs = 0;
1.1.1.39  root     5896:        
                   5897:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5898: }
                   5899: 
1.1.1.29  root     5900: #ifdef SUPPORT_HMA
                   5901: 
                   5902: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5903: {
                   5904:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5905:        
                   5906:        mcb->ms[0] = 'M';
                   5907:        mcb->ms[1] = 'S';
                   5908:        mcb->owner = owner;
                   5909:        mcb->size = size;
                   5910:        mcb->next = next;
                   5911:        return(mcb);
                   5912: }
                   5913: 
                   5914: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5915: {
                   5916:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5917: }
                   5918: 
                   5919: int msdos_hma_mem_split(int offset, int size)
                   5920: {
                   5921:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5922:        
                   5923:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5924:                return(-1);
                   5925:        }
                   5926:        if(mcb->size >= size + 0x10) {
                   5927:                int new_offset = offset + 0x10 + size;
                   5928:                int new_size = mcb->size - 0x10 - size;
                   5929:                
                   5930:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5931:                mcb->size = size;
                   5932:                mcb->next = new_offset;
                   5933:                return(0);
                   5934:        }
                   5935:        return(-1);
                   5936: }
                   5937: 
                   5938: void msdos_hma_mem_merge(int offset)
                   5939: {
                   5940:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5941:        
                   5942:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5943:                return;
                   5944:        }
                   5945:        while(1) {
                   5946:                if(mcb->next == 0) {
                   5947:                        break;
                   5948:                }
                   5949:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5950:                
                   5951:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5952:                        return;
                   5953:                }
                   5954:                if(next_mcb->owner != 0) {
                   5955:                        break;
                   5956:                }
                   5957:                mcb->size += 0x10 + next_mcb->size;
                   5958:                mcb->next = next_mcb->next;
                   5959:        }
                   5960: }
                   5961: 
                   5962: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5963: {
                   5964:        int offset = 0x10; // first mcb in HMA
                   5965:        
                   5966:        while(1) {
                   5967:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5968:                
                   5969:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5970:                        return(-1);
                   5971:                }
                   5972:                if(mcb->owner == 0) {
                   5973:                        msdos_hma_mem_merge(offset);
                   5974:                }
                   5975:                if(mcb->owner == 0 && mcb->size >= size) {
                   5976:                        msdos_hma_mem_split(offset, size);
                   5977:                        mcb->owner = owner;
                   5978:                        return(offset);
                   5979:                }
                   5980:                if(mcb->next == 0) {
                   5981:                        break;
                   5982:                }
                   5983:                offset = mcb->next;
                   5984:        }
                   5985:        return(-1);
                   5986: }
                   5987: 
                   5988: int msdos_hma_mem_realloc(int offset, int size)
                   5989: {
                   5990:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5991:        
                   5992:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5993:                return(-1);
                   5994:        }
                   5995:        if(mcb->size < size) {
                   5996:                return(-1);
                   5997:        }
                   5998:        msdos_hma_mem_split(offset, size);
                   5999:        return(0);
                   6000: }
                   6001: 
                   6002: void msdos_hma_mem_free(int offset)
                   6003: {
                   6004:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6005:        
                   6006:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6007:                return;
                   6008:        }
                   6009:        mcb->owner = 0;
                   6010:        msdos_hma_mem_merge(offset);
                   6011: }
                   6012: 
                   6013: int msdos_hma_mem_get_free(int *available_offset)
                   6014: {
                   6015:        int offset = 0x10; // first mcb in HMA
                   6016:        int size = 0;
                   6017:        
                   6018:        while(1) {
                   6019:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6020:                
                   6021:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6022:                        return(0);
                   6023:                }
                   6024:                if(mcb->owner == 0 && size < mcb->size) {
                   6025:                        if(available_offset != NULL) {
                   6026:                                *available_offset = offset;
                   6027:                        }
                   6028:                        size = mcb->size;
                   6029:                }
                   6030:                if(mcb->next == 0) {
                   6031:                        break;
                   6032:                }
                   6033:                offset = mcb->next;
                   6034:        }
                   6035:        return(size);
                   6036: }
                   6037: 
                   6038: #endif
                   6039: 
1.1       root     6040: // environment
                   6041: 
1.1.1.45  root     6042: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     6043: {
                   6044:        char *dst = (char *)(mem + (env_seg << 4));
                   6045:        
                   6046:        while(1) {
                   6047:                if(dst[0] == 0) {
                   6048:                        break;
                   6049:                }
                   6050:                dst += strlen(dst) + 1;
                   6051:        }
                   6052:        *dst++ = 0; // end of environment
                   6053:        *dst++ = 1; // top of argv[0]
                   6054:        *dst++ = 0;
                   6055:        memcpy(dst, argv, strlen(argv));
                   6056:        dst += strlen(argv);
                   6057:        *dst++ = 0;
                   6058:        *dst++ = 0;
                   6059: }
                   6060: 
1.1.1.45  root     6061: const char *msdos_env_get_argv(int env_seg)
1.1       root     6062: {
                   6063:        static char env[ENV_SIZE];
                   6064:        char *src = env;
                   6065:        
                   6066:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6067:        while(1) {
                   6068:                if(src[0] == 0) {
                   6069:                        if(src[1] == 1) {
                   6070:                                return(src + 3);
                   6071:                        }
                   6072:                        break;
                   6073:                }
                   6074:                src += strlen(src) + 1;
                   6075:        }
                   6076:        return(NULL);
                   6077: }
                   6078: 
1.1.1.45  root     6079: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     6080: {
                   6081:        static char env[ENV_SIZE];
                   6082:        char *src = env;
                   6083:        
                   6084:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6085:        while(1) {
                   6086:                if(src[0] == 0) {
                   6087:                        break;
                   6088:                }
                   6089:                int len = strlen(src);
                   6090:                char *n = my_strtok(src, "=");
                   6091:                char *v = src + strlen(n) + 1;
                   6092:                
                   6093:                if(_stricmp(name, n) == 0) {
                   6094:                        return(v);
                   6095:                }
                   6096:                src += len + 1;
                   6097:        }
                   6098:        return(NULL);
                   6099: }
                   6100: 
1.1.1.45  root     6101: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     6102: {
                   6103:        char env[ENV_SIZE];
                   6104:        char *src = env;
                   6105:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     6106:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     6107:        int done = 0;
                   6108:        
                   6109:        memcpy(src, dst, ENV_SIZE);
                   6110:        memset(dst, 0, ENV_SIZE);
                   6111:        while(1) {
                   6112:                if(src[0] == 0) {
                   6113:                        break;
                   6114:                }
                   6115:                int len = strlen(src);
                   6116:                char *n = my_strtok(src, "=");
                   6117:                char *v = src + strlen(n) + 1;
                   6118:                char tmp[1024];
                   6119:                
                   6120:                if(_stricmp(name, n) == 0) {
                   6121:                        sprintf(tmp, "%s=%s", n, value);
                   6122:                        done = 1;
                   6123:                } else {
                   6124:                        sprintf(tmp, "%s=%s", n, v);
                   6125:                }
                   6126:                memcpy(dst, tmp, strlen(tmp));
                   6127:                dst += strlen(tmp) + 1;
                   6128:                src += len + 1;
                   6129:        }
                   6130:        if(!done) {
                   6131:                char tmp[1024];
                   6132:                
                   6133:                sprintf(tmp, "%s=%s", name, value);
                   6134:                memcpy(dst, tmp, strlen(tmp));
                   6135:                dst += strlen(tmp) + 1;
                   6136:        }
                   6137:        if(argv) {
                   6138:                *dst++ = 0; // end of environment
                   6139:                *dst++ = 1; // top of argv[0]
                   6140:                *dst++ = 0;
                   6141:                memcpy(dst, argv, strlen(argv));
                   6142:                dst += strlen(argv);
                   6143:                *dst++ = 0;
                   6144:                *dst++ = 0;
                   6145:        }
                   6146: }
                   6147: 
                   6148: // process
                   6149: 
1.1.1.8   root     6150: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     6151: {
                   6152:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6153:        
                   6154:        memset(psp, 0, PSP_SIZE);
                   6155:        psp->exit[0] = 0xcd;
                   6156:        psp->exit[1] = 0x20;
1.1.1.8   root     6157:        psp->first_mcb = mcb_seg;
1.1.1.46  root     6158: #if 1
1.1.1.49  root     6159:        psp->call5[0] = 0xcd;   // int 30h
                   6160:        psp->call5[1] = 0x30;
1.1.1.46  root     6161:        psp->call5[2] = 0xc3;   // ret
                   6162: #else
                   6163:        psp->call5[0] = 0x8a;   // mov ah, cl
                   6164:        psp->call5[1] = 0xe1;
                   6165:        psp->call5[2] = 0xcd;   // int 21h
                   6166:        psp->call5[3] = 0x21;
                   6167:        psp->call5[4] = 0xc3;   // ret
                   6168: #endif
1.1       root     6169:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6170:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6171:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6172:        psp->parent_psp = parent_psp;
1.1.1.20  root     6173:        if(parent_psp == (UINT16)-1) {
                   6174:                for(int i = 0; i < 20; i++) {
                   6175:                        if(file_handler[i].valid) {
                   6176:                                psp->file_table[i] = i;
                   6177:                        } else {
                   6178:                                psp->file_table[i] = 0xff;
                   6179:                        }
1.1       root     6180:                }
1.1.1.20  root     6181:        } else {
                   6182:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     6183:        }
                   6184:        psp->env_seg = env_seg;
                   6185:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6186:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6187:        psp->file_table_size = 20;
                   6188:        psp->file_table_ptr.w.l = 0x18;
                   6189:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6190:        psp->service[0] = 0xcd;
                   6191:        psp->service[1] = 0x21;
                   6192:        psp->service[2] = 0xcb;
                   6193:        return(psp);
                   6194: }
                   6195: 
1.1.1.20  root     6196: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6197: {
                   6198:        if(psp_seg && fd < 20) {
                   6199:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6200:                psp->file_table[fd] = value;
                   6201:        }
                   6202: }
                   6203: 
                   6204: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6205: {
                   6206:        if(psp_seg && fd < 20) {
                   6207:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6208:                fd = psp->file_table[fd];
                   6209:        }
                   6210:        return fd;
                   6211: }
                   6212: 
1.1.1.52  root     6213: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     6214: {
                   6215:        // load command file
                   6216:        int fd = -1;
1.1.1.45  root     6217:        int sio_port = 0;
                   6218:        int lpt_port = 0;
1.1       root     6219:        int dos_command = 0;
1.1.1.24  root     6220:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6221:        char pipe_stdin_path[MAX_PATH] = {0};
                   6222:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6223:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6224:        
                   6225:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6226:        int opt_len = mem[opt_ofs];
                   6227:        memset(opt, 0, sizeof(opt));
                   6228:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6229:        
1.1.1.14  root     6230:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6231:                // this is a batch file, run command.com
                   6232:                char tmp[MAX_PATH];
                   6233:                if(opt_len != 0) {
                   6234:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6235:                } else {
                   6236:                        sprintf(tmp, "/C %s", cmd);
                   6237:                }
                   6238:                strcpy(opt, tmp);
                   6239:                opt_len = strlen(opt);
                   6240:                mem[opt_ofs] = opt_len;
                   6241:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6242:                strcpy(command, comspec_path);
                   6243:                strcpy(name_tmp, "COMMAND.COM");
                   6244:        } else {
                   6245:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6246:                        // redirect C:\COMMAND.COM to comspec_path
                   6247:                        strcpy(command, comspec_path);
                   6248:                } else {
                   6249:                        strcpy(command, cmd);
                   6250:                }
1.1.1.24  root     6251:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   6252:                        return(-1);
                   6253:                }
1.1.1.14  root     6254:                memset(name_tmp, 0, sizeof(name_tmp));
                   6255:                strcpy(name_tmp, name);
                   6256:                
                   6257:                // check command.com
1.1.1.38  root     6258:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6259:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6260:                        if(opt_len == 0) {
                   6261: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6262:                                process_t *current_process = NULL;
                   6263:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6264:                                        if(process[i].psp == current_psp) {
                   6265:                                                current_process = &process[i];
                   6266:                                                break;
                   6267:                                        }
                   6268:                                }
                   6269:                                if(current_process != NULL) {
                   6270:                                        param->cmd_line.dw = current_process->dta.dw;
                   6271:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6272:                                        opt_len = mem[opt_ofs];
                   6273:                                        memset(opt, 0, sizeof(opt));
                   6274:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6275:                                }
                   6276:                        }
                   6277:                        for(int i = 0; i < opt_len; i++) {
                   6278:                                if(opt[i] == ' ') {
                   6279:                                        continue;
                   6280:                                }
                   6281:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6282:                                        for(int j = i + 3; j < opt_len; j++) {
                   6283:                                                if(opt[j] == ' ') {
                   6284:                                                        continue;
                   6285:                                                }
                   6286:                                                char *token = my_strtok(opt + j, " ");
                   6287:                                                
1.1.1.38  root     6288:                                                strcpy(command, token);
                   6289:                                                char tmp[MAX_PATH];
                   6290:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6291:                                                strcpy(opt, "");
                   6292:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6293:                                                        if(tmp[i] != ' ') {
                   6294:                                                                strcpy(opt, tmp + i);
                   6295:                                                                break;
                   6296:                                                        }
                   6297:                                                }
                   6298:                                                strcpy(tmp, opt);
1.1.1.38  root     6299:                                                
                   6300:                                                if(al == 0x00) {
1.1.1.39  root     6301:                                                        #define GET_FILE_PATH() { \
                   6302:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6303:                                                                        token++; \
                   6304:                                                                } \
                   6305:                                                                token++; \
                   6306:                                                                while(*token == ' ') { \
                   6307:                                                                        token++; \
                   6308:                                                                } \
                   6309:                                                                char *ptr = token; \
                   6310:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6311:                                                                        ptr++; \
                   6312:                                                                } \
                   6313:                                                                *ptr = '\0'; \
                   6314:                                                        }
                   6315:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6316:                                                                GET_FILE_PATH();
1.1.1.38  root     6317:                                                                strcpy(pipe_stdin_path, token);
                   6318:                                                                strcpy(opt, tmp);
                   6319:                                                        }
1.1.1.39  root     6320:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6321:                                                                GET_FILE_PATH();
1.1.1.38  root     6322:                                                                strcpy(pipe_stdout_path, token);
                   6323:                                                                strcpy(opt, tmp);
                   6324:                                                        }
1.1.1.39  root     6325:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6326:                                                                GET_FILE_PATH();
                   6327:                                                                strcpy(pipe_stderr_path, token);
                   6328:                                                                strcpy(opt, tmp);
                   6329:                                                        }
                   6330:                                                        #undef GET_FILE_PATH
                   6331:                                                        
                   6332:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6333:                                                                *token = '\0';
                   6334:                                                        }
                   6335:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6336:                                                                *token = '\0';
                   6337:                                                        }
                   6338:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6339:                                                                *token = '\0';
                   6340:                                                        }
1.1.1.38  root     6341:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6342:                                                                *token = '\0';
                   6343:                                                        }
                   6344:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6345:                                                                *token = '\0';
                   6346:                                                        }
1.1.1.14  root     6347:                                                }
1.1.1.39  root     6348:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6349:                                                        opt[i] = '\0';
                   6350:                                                }
1.1.1.38  root     6351:                                                opt_len = strlen(opt);
                   6352:                                                mem[opt_ofs] = opt_len;
                   6353:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6354:                                                dos_command = 1;
1.1.1.14  root     6355:                                                break;
1.1       root     6356:                                        }
                   6357:                                }
1.1.1.14  root     6358:                                break;
1.1       root     6359:                        }
                   6360:                }
                   6361:        }
                   6362:        
                   6363:        // load command file
                   6364:        strcpy(path, command);
                   6365:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6366:                sprintf(path, "%s.COM", command);
                   6367:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6368:                        sprintf(path, "%s.EXE", command);
                   6369:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6370:                                sprintf(path, "%s.BAT", command);
                   6371:                                if(_access(path, 0) == 0) {
                   6372:                                        // this is a batch file, run command.com
                   6373:                                        char tmp[MAX_PATH];
                   6374:                                        if(opt_len != 0) {
                   6375:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6376:                                        } else {
                   6377:                                                sprintf(tmp, "/C %s", path);
                   6378:                                        }
                   6379:                                        strcpy(opt, tmp);
                   6380:                                        opt_len = strlen(opt);
                   6381:                                        mem[opt_ofs] = opt_len;
                   6382:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6383:                                        strcpy(path, comspec_path);
                   6384:                                        strcpy(name_tmp, "COMMAND.COM");
                   6385:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6386:                                } else {
                   6387:                                        // search path in parent environments
                   6388:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6389:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6390:                                        if(env != NULL) {
                   6391:                                                char env_path[4096];
                   6392:                                                strcpy(env_path, env);
                   6393:                                                char *token = my_strtok(env_path, ";");
                   6394:                                                
                   6395:                                                while(token != NULL) {
                   6396:                                                        if(strlen(token) != 0) {
                   6397:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6398:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6399:                                                                        break;
                   6400:                                                                }
                   6401:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6402:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6403:                                                                        break;
                   6404:                                                                }
                   6405:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6406:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6407:                                                                        break;
                   6408:                                                                }
                   6409:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6410:                                                                if(_access(path, 0) == 0) {
                   6411:                                                                        // this is a batch file, run command.com
                   6412:                                                                        char tmp[MAX_PATH];
                   6413:                                                                        if(opt_len != 0) {
                   6414:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6415:                                                                        } else {
                   6416:                                                                                sprintf(tmp, "/C %s", path);
                   6417:                                                                        }
                   6418:                                                                        strcpy(opt, tmp);
                   6419:                                                                        opt_len = strlen(opt);
                   6420:                                                                        mem[opt_ofs] = opt_len;
                   6421:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6422:                                                                        strcpy(path, comspec_path);
                   6423:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6424:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6425:                                                                        break;
                   6426:                                                                }
1.1.1.8   root     6427:                                                        }
1.1.1.14  root     6428:                                                        token = my_strtok(NULL, ";");
1.1       root     6429:                                                }
                   6430:                                        }
                   6431:                                }
                   6432:                        }
                   6433:                }
                   6434:        }
                   6435:        if(fd == -1) {
1.1.1.38  root     6436:                // we can not find command.com in the path, so open comspec_path
                   6437:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6438:                        strcpy(command, comspec_path);
                   6439:                        strcpy(path, command);
                   6440:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6441:                }
                   6442:        }
                   6443:        if(fd == -1) {
1.1.1.52  root     6444:                if(!first_process && al == 0 && dos_command) {
1.1       root     6445:                        // may be dos command
                   6446:                        char tmp[MAX_PATH];
1.1.1.52  root     6447:                        if(opt_len != 0) {
                   6448:                                sprintf(tmp, "%s %s", command, opt);
                   6449:                        } else {
                   6450:                                sprintf(tmp, "%s", command);
                   6451:                        }
                   6452:                        retval = system(tmp);
1.1       root     6453:                        return(0);
                   6454:                } else {
                   6455:                        return(-1);
                   6456:                }
                   6457:        }
1.1.1.52  root     6458:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6459:        _read(fd, file_buffer, sizeof(file_buffer));
                   6460:        _close(fd);
                   6461:        
1.1.1.52  root     6462:        // check if this is win32 program
                   6463:        if(!first_process && al == 0) {
                   6464:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
                   6465:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
                   6466:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
                   6467:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
                   6468:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
                   6469:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
                   6470:                                char tmp[MAX_PATH];
                   6471:                                if(opt_len != 0) {
                   6472:                                        sprintf(tmp, "\"%s\" %s", path, opt);
                   6473:                                } else {
                   6474:                                        sprintf(tmp, "\"%s\"", path);
                   6475:                                }
                   6476:                                retval = system(tmp);
                   6477:                                return(0);
                   6478:                        }
                   6479:                }
                   6480:        }
                   6481:        
1.1       root     6482:        // copy environment
1.1.1.29  root     6483:        int umb_linked, env_seg, psp_seg;
1.1       root     6484:        
1.1.1.29  root     6485:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6486:                msdos_mem_unlink_umb();
                   6487:        }
1.1.1.8   root     6488:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6489:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6490:                        if(umb_linked != 0) {
                   6491:                                msdos_mem_link_umb();
                   6492:                        }
                   6493:                        return(-1);
                   6494:                }
1.1       root     6495:        }
                   6496:        if(param->env_seg == 0) {
                   6497:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6498:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6499:        } else {
                   6500:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6501:        }
                   6502:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6503:        
                   6504:        // check exe header
                   6505:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6506:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6507:        UINT16 cs, ss, ip, sp;
                   6508:        
                   6509:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6510:                // memory allocation
                   6511:                int header_size = header->header_size * 16;
                   6512:                int load_size = header->pages * 512 - header_size;
                   6513:                if(header_size + load_size < 512) {
                   6514:                        load_size = 512 - header_size;
                   6515:                }
                   6516:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6517:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6518:                        msdos_mem_free(env_seg);
                   6519:                        return(-1);
                   6520:                }
                   6521:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6522:                if(paragraphs > free_paragraphs) {
                   6523:                        paragraphs = free_paragraphs;
                   6524:                }
1.1.1.8   root     6525:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6526:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6527:                                if(umb_linked != 0) {
                   6528:                                        msdos_mem_link_umb();
                   6529:                                }
                   6530:                                msdos_mem_free(env_seg);
                   6531:                                return(-1);
                   6532:                        }
1.1       root     6533:                }
                   6534:                // relocation
                   6535:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6536:                for(int i = 0; i < header->relocations; i++) {
                   6537:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6538:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6539:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6540:                }
                   6541:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6542:                // segments
                   6543:                cs = header->init_cs + start_seg;
                   6544:                ss = header->init_ss + start_seg;
                   6545:                ip = header->init_ip;
                   6546:                sp = header->init_sp - 2; // for symdeb
                   6547:        } else {
                   6548:                // memory allocation
                   6549:                paragraphs = free_paragraphs;
1.1.1.8   root     6550:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6551:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6552:                                if(umb_linked != 0) {
                   6553:                                        msdos_mem_link_umb();
                   6554:                                }
                   6555:                                msdos_mem_free(env_seg);
                   6556:                                return(-1);
                   6557:                        }
1.1       root     6558:                }
                   6559:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6560:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6561:                // segments
                   6562:                cs = ss = psp_seg;
                   6563:                ip = 0x100;
                   6564:                sp = 0xfffe;
                   6565:        }
1.1.1.29  root     6566:        if(umb_linked != 0) {
                   6567:                msdos_mem_link_umb();
                   6568:        }
1.1       root     6569:        
                   6570:        // create psp
1.1.1.3   root     6571:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6572:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6573:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6574:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6575:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6576:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6577:        
                   6578:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6579:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6580:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6581:        
1.1.1.4   root     6582:        for(int i = 0; i < 8; i++) {
                   6583:                if(name_tmp[i] == '.') {
                   6584:                        mcb_psp->prog_name[i] = '\0';
                   6585:                        break;
                   6586:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6587:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6588:                        i++;
                   6589:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6590:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6591:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6592:                } else {
                   6593:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6594:                }
                   6595:        }
                   6596:        
1.1       root     6597:        // process info
                   6598:        process_t *process = msdos_process_info_create(psp_seg);
                   6599:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6600: #ifdef USE_DEBUGGER
                   6601:        strcpy(process->module_path, path);
                   6602: #endif
1.1       root     6603:        process->dta.w.l = 0x80;
                   6604:        process->dta.w.h = psp_seg;
                   6605:        process->switchar = '/';
                   6606:        process->max_files = 20;
                   6607:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6608:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6609:        process->parent_ds = SREG(DS);
1.1.1.31  root     6610:        process->parent_es = SREG(ES);
1.1       root     6611:        
                   6612:        current_psp = psp_seg;
1.1.1.23  root     6613:        msdos_sda_update(current_psp);
1.1       root     6614:        
                   6615:        if(al == 0x00) {
                   6616:                int_10h_feh_called = int_10h_ffh_called = false;
                   6617:                
1.1.1.38  root     6618:                // pipe
                   6619:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6620: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6621:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6622:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6623:                        } else {
                   6624:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6625:                        }
                   6626:                        if(fd != -1) {
                   6627:                                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     6628:                                psp->file_table[0] = fd;
                   6629:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6630:                        }
                   6631:                }
                   6632:                if(pipe_stdout_path[0] != '\0') {
                   6633:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6634:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6635:                                DeleteFile(pipe_stdout_path);
                   6636:                        }
1.1.1.45  root     6637: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6638:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6639:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6640:                        } else {
                   6641:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6642:                        }
                   6643:                        if(fd != -1) {
                   6644:                                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     6645:                                psp->file_table[1] = fd;
                   6646:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6647:                        }
                   6648:                }
1.1.1.39  root     6649:                if(pipe_stderr_path[0] != '\0') {
                   6650:                        if(_access(pipe_stderr_path, 0) == 0) {
                   6651:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6652:                                DeleteFile(pipe_stderr_path);
                   6653:                        }
1.1.1.45  root     6654: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6655:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6656:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6657:                        } else {
                   6658:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6659:                        }
                   6660:                        if(fd != -1) {
                   6661:                                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     6662:                                psp->file_table[2] = fd;
                   6663:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6664:                        }
                   6665:                }
1.1.1.38  root     6666:                
1.1       root     6667:                // registers and segments
                   6668:                REG16(AX) = REG16(BX) = 0x00;
                   6669:                REG16(CX) = 0xff;
                   6670:                REG16(DX) = psp_seg;
                   6671:                REG16(SI) = ip;
                   6672:                REG16(DI) = sp;
                   6673:                REG16(SP) = sp;
1.1.1.3   root     6674:                SREG(DS) = SREG(ES) = psp_seg;
                   6675:                SREG(SS) = ss;
                   6676:                i386_load_segment_descriptor(DS);
                   6677:                i386_load_segment_descriptor(ES);
                   6678:                i386_load_segment_descriptor(SS);
1.1       root     6679:                
                   6680:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6681:                i386_jmp_far(cs, ip);
                   6682:        } else if(al == 0x01) {
                   6683:                // copy ss:sp and cs:ip to param block
                   6684:                param->sp = sp;
                   6685:                param->ss = ss;
                   6686:                param->ip = ip;
                   6687:                param->cs = cs;
1.1.1.31  root     6688:                
                   6689:                // the AX value to be passed to the child program is put on top of the child's stack
                   6690:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6691:        }
                   6692:        return(0);
                   6693: }
                   6694: 
                   6695: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6696: {
                   6697:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6698:        
                   6699:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6700:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6701:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6702:        
1.1.1.3   root     6703:        SREG(SS) = psp->stack.w.h;
                   6704:        i386_load_segment_descriptor(SS);
1.1       root     6705:        REG16(SP) = psp->stack.w.l;
                   6706:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6707:        
1.1.1.28  root     6708: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6709:        process_t *current_process = NULL;
                   6710:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6711:                if(process[i].psp == psp_seg) {
                   6712:                        current_process = &process[i];
                   6713:                        break;
                   6714:                }
                   6715:        }
                   6716:        if(current_process == NULL) {
                   6717:                throw(0x1f); // general failure
                   6718:        }
                   6719:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6720:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6721:        if(current_process->called_by_int2eh) {
                   6722:                REG16(AX) = ret;
                   6723:        }
                   6724:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6725:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6726:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6727:        i386_load_segment_descriptor(ES);
1.1       root     6728:        
                   6729:        if(mem_free) {
1.1.1.8   root     6730:                int mcb_seg;
                   6731:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6732:                        msdos_mem_free(mcb_seg + 1);
                   6733:                }
                   6734:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6735:                        msdos_mem_free(mcb_seg + 1);
                   6736:                }
1.1       root     6737:                
                   6738:                for(int i = 0; i < MAX_FILES; i++) {
                   6739:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6740:                                _close(i);
1.1.1.20  root     6741:                                msdos_file_handler_close(i);
                   6742:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6743:                        }
                   6744:                }
1.1.1.13  root     6745:                msdos_dta_info_free(psp_seg);
1.1       root     6746:        }
1.1.1.14  root     6747:        msdos_stdio_reopen();
1.1       root     6748:        
1.1.1.28  root     6749:        memset(current_process, 0, sizeof(process_t));
1.1       root     6750:        
                   6751:        current_psp = psp->parent_psp;
                   6752:        retval = ret;
1.1.1.23  root     6753:        msdos_sda_update(current_psp);
1.1       root     6754: }
                   6755: 
                   6756: // drive
                   6757: 
1.1.1.42  root     6758: int pcbios_update_drive_param(int drive_num, int force_update);
                   6759: 
1.1       root     6760: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6761: {
1.1.1.41  root     6762:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6763:                return(0);
                   6764:        }
1.1.1.42  root     6765:        pcbios_update_drive_param(drive_num, force_update);
                   6766:        
                   6767:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6768:        *seg = DPB_TOP >> 4;
                   6769:        *ofs = sizeof(dpb_t) * drive_num;
                   6770:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6771:        
                   6772:        memset(dpb, 0, sizeof(dpb_t));
                   6773:        
1.1.1.41  root     6774:        dpb->drive_num = drive_num;
                   6775:        dpb->unit_num = drive_num;
1.1.1.42  root     6776:        
                   6777:        if(drive_param->valid) {
                   6778:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6779:                
                   6780:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6781:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6782:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6783:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6784:                switch(geo->MediaType) {
                   6785:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6786:                        dpb->media_type = 0xff;
                   6787:                        break;
                   6788:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6789:                        dpb->media_type = 0xfe;
                   6790:                        break;
                   6791:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6792:                        dpb->media_type = 0xfd;
                   6793:                        break;
                   6794:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6795:                        dpb->media_type = 0xfc;
                   6796:                        break;
                   6797:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6798:                case F3_1Pt2_512:
                   6799:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6800:                case F5_720_512:
                   6801:                        dpb->media_type = 0xf9;
                   6802:                        break;
                   6803:                case FixedMedia:        // hard disk
                   6804:                case RemovableMedia:
                   6805:                case Unknown:
                   6806:                        dpb->media_type = 0xf8;
                   6807:                        break;
                   6808:                default:
                   6809:                        dpb->media_type = 0xf0;
                   6810:                        break;
                   6811:                }
                   6812:        }
1.1.1.41  root     6813:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6814:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6815:        dpb->info_sector = 0xffff;
                   6816:        dpb->backup_boot_sector = 0xffff;
                   6817:        dpb->free_clusters = 0xffff;
                   6818:        dpb->free_search_cluster = 0xffffffff;
                   6819:        
                   6820:        return(drive_param->valid);
1.1       root     6821: }
                   6822: 
                   6823: // pc bios
                   6824: 
1.1.1.35  root     6825: #ifdef USE_SERVICE_THREAD
                   6826: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6827: {
                   6828: #if defined(HAS_I386)
                   6829:        if(m_SF != 0) {
                   6830:                m_SF = 0;
1.1.1.49  root     6831:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6832:        } else {
                   6833:                m_SF = 1;
1.1.1.49  root     6834:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6835:        }
                   6836: #else
                   6837:        if(m_SignVal < 0) {
                   6838:                m_SignVal = 0;
1.1.1.49  root     6839:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6840:        } else {
                   6841:                m_SignVal = -1;
1.1.1.49  root     6842:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6843:        }
                   6844: #endif
1.1.1.59! root     6845:        // dummy loop to wait BIOS/DOS service is done is at fffc:0013
1.1.1.49  root     6846:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     6847:        in_service = true;
                   6848:        service_exit = false;
                   6849:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6850: }
                   6851: 
                   6852: void finish_service_loop()
                   6853: {
                   6854:        if(in_service && service_exit) {
                   6855: #if defined(HAS_I386)
                   6856:                if(m_SF != 0) {
                   6857:                        m_SF = 0;
                   6858:                } else {
                   6859:                        m_SF = 1;
                   6860:                }
                   6861: #else
                   6862:                if(m_SignVal < 0) {
                   6863:                        m_SignVal = 0;
                   6864:                } else {
                   6865:                        m_SignVal = -1;
                   6866:                }
                   6867: #endif
                   6868:                in_service = false;
                   6869:        }
                   6870: }
                   6871: #endif
                   6872: 
1.1.1.19  root     6873: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6874: {
                   6875:        static unsigned __int64 start_msec_since_midnight = 0;
                   6876:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6877:        
                   6878:        if(start_msec_since_midnight == 0) {
                   6879:                SYSTEMTIME time;
                   6880:                GetLocalTime(&time);
                   6881:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6882:                start_msec_since_hostboot = cur_msec;
                   6883:        }
                   6884:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6885:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6886:        return (UINT32)tick;
                   6887: }
                   6888: 
                   6889: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6890: {
                   6891:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6892:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6893:        
                   6894:        if(prev_tick > next_tick) {
                   6895:                mem[0x470] = 1;
                   6896:        }
                   6897:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6898: }
                   6899: 
1.1.1.14  root     6900: inline void pcbios_irq0()
                   6901: {
                   6902:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6903:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6904: }
                   6905: 
1.1.1.16  root     6906: int pcbios_get_text_vram_address(int page)
1.1       root     6907: {
                   6908:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6909:                return TEXT_VRAM_TOP;
1.1       root     6910:        } else {
1.1.1.14  root     6911:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6912:        }
                   6913: }
                   6914: 
1.1.1.16  root     6915: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6916: {
1.1.1.14  root     6917:        if(!int_10h_feh_called) {
1.1.1.16  root     6918:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6919:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6920:                return SHADOW_BUF_TOP;
                   6921:        } else {
1.1.1.14  root     6922:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6923:        }
                   6924: }
                   6925: 
1.1.1.16  root     6926: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6927: {
1.1.1.16  root     6928:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6929: }
                   6930: 
1.1.1.56  root     6931: bool pcbios_set_font_size(int width, int height)
                   6932: {
                   6933:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   6934:        return(set_console_font_size(hStdout, width, height));
                   6935: }
                   6936: 
1.1.1.16  root     6937: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6938: {
1.1.1.14  root     6939:        // clear the existing screen, not just the new one
                   6940:        int clr_height = max(height, scr_height);
                   6941:        
1.1.1.16  root     6942:        if(scr_width != width || scr_height != height) {
                   6943:                change_console_size(width, height);
1.1.1.14  root     6944:        }
                   6945:        mem[0x462] = 0;
                   6946:        *(UINT16 *)(mem + 0x44e) = 0;
                   6947:        
1.1.1.16  root     6948:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6949:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6950:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6951:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     6952:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     6953:        
1.1.1.23  root     6954:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6955:        if(clr_screen) {
1.1.1.14  root     6956:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6957:                        mem[ofs++] = 0x20;
                   6958:                        mem[ofs++] = 0x07;
                   6959:                }
                   6960:                
1.1.1.35  root     6961: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6962:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6963: #endif
1.1.1.14  root     6964:                for(int y = 0; y < clr_height; y++) {
                   6965:                        for(int x = 0; x < scr_width; x++) {
                   6966:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6967:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6968:                        }
                   6969:                }
                   6970:                SMALL_RECT rect;
1.1.1.14  root     6971:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6972:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6973:                vram_length_char = vram_last_length_char = 0;
                   6974:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6975: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6976:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6977: #endif
1.1       root     6978:        }
1.1.1.14  root     6979:        COORD co;
                   6980:        co.X = 0;
                   6981:        co.Y = scr_top;
                   6982:        SetConsoleCursorPosition(hStdout, co);
                   6983:        cursor_moved = true;
1.1.1.59! root     6984:        cursor_moved_by_crtc = false;
1.1.1.14  root     6985:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6986: }
                   6987: 
1.1.1.36  root     6988: void pcbios_update_cursor_position()
                   6989: {
                   6990:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6991:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6992:        if(!restore_console_on_exit) {
                   6993:                scr_top = csbi.srWindow.Top;
                   6994:        }
                   6995:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6996:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6997: }
                   6998: 
1.1.1.16  root     6999: inline void pcbios_int_10h_00h()
                   7000: {
                   7001:        switch(REG8(AL) & 0x7f) {
                   7002:        case 0x70: // v-text mode
                   7003:        case 0x71: // extended cga v-text mode
                   7004:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   7005:                break;
                   7006:        default:
                   7007:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   7008:                break;
                   7009:        }
                   7010:        if(REG8(AL) & 0x80) {
                   7011:                mem[0x487] |= 0x80;
                   7012:        } else {
                   7013:                mem[0x487] &= ~0x80;
                   7014:        }
                   7015:        mem[0x449] = REG8(AL) & 0x7f;
                   7016: }
                   7017: 
1.1       root     7018: inline void pcbios_int_10h_01h()
                   7019: {
1.1.1.13  root     7020:        mem[0x460] = REG8(CL);
                   7021:        mem[0x461] = REG8(CH);
1.1.1.14  root     7022:        
1.1.1.59! root     7023: //     HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
        !          7024: //     CONSOLE_CURSOR_INFO ci_new;
        !          7025: //     GetConsoleCursorInfo(hStdout, &ci_new);
1.1.1.58  root     7026:        
1.1.1.59! root     7027:        BOOL bVisible = ((REG8(CH) & 0x20) == 0 || (REG8(CH) & 7) > (REG8(CL) & 7));
1.1.1.58  root     7028:        DWORD dwSize = ((REG8(CL) & 7) + 1) * 100 / 8;
                   7029:        
1.1.1.59! root     7030: //     if(ci_new.bVisible != bVisible || ci_new.dwSize != dwSize) {
        !          7031:                ci_new.bVisible = bVisible;
        !          7032:                ci_new.dwSize = dwSize;
        !          7033: //             SetConsoleCursorInfo(hStdout, &ci);
        !          7034: //     }
1.1       root     7035: }
                   7036: 
                   7037: inline void pcbios_int_10h_02h()
                   7038: {
1.1.1.14  root     7039:        // continuously setting the cursor effectively stops it blinking
1.1.1.59! root     7040:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2] || cursor_moved_by_crtc)) {
1.1       root     7041:                COORD co;
                   7042:                co.X = REG8(DL);
1.1.1.14  root     7043:                co.Y = REG8(DH) + scr_top;
                   7044:                
                   7045:                // some programs hide the cursor by moving it off screen
                   7046:                static bool hidden = false;
1.1.1.23  root     7047:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.59! root     7048: //             CONSOLE_CURSOR_INFO ci_new;
        !          7049: //             GetConsoleCursorInfo(hStdout, &ci_new);
1.1.1.14  root     7050:                
                   7051:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
1.1.1.59! root     7052:                        if(ci_new.bVisible) {
        !          7053:                                ci_new.bVisible = FALSE;
1.1.1.14  root     7054: //                             SetConsoleCursorInfo(hStdout, &ci);
                   7055:                                hidden = true;
                   7056:                        }
                   7057:                } else if(hidden) {
1.1.1.59! root     7058:                        if(!ci_new.bVisible) {
        !          7059:                                ci_new.bVisible = TRUE;
1.1.1.14  root     7060: //                             SetConsoleCursorInfo(hStdout, &ci);
                   7061:                        }
                   7062:                        hidden = false;
                   7063:                }
1.1.1.59! root     7064:                cursor_moved_by_crtc = false;
1.1       root     7065:        }
1.1.1.14  root     7066:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   7067:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     7068: }
                   7069: 
                   7070: inline void pcbios_int_10h_03h()
                   7071: {
1.1.1.14  root     7072:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7073:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7074:        REG8(CL) = mem[0x460];
                   7075:        REG8(CH) = mem[0x461];
                   7076: }
                   7077: 
                   7078: inline void pcbios_int_10h_05h()
                   7079: {
1.1.1.14  root     7080:        if(REG8(AL) >= vram_pages) {
                   7081:                return;
                   7082:        }
                   7083:        if(mem[0x462] != REG8(AL)) {
                   7084:                vram_flush();
                   7085:                
1.1.1.23  root     7086:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7087:                SMALL_RECT rect;
1.1.1.14  root     7088:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   7089:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7090:                
1.1.1.16  root     7091:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     7092:                        for(int x = 0; x < scr_width; x++) {
                   7093:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7094:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7095:                        }
                   7096:                }
1.1.1.16  root     7097:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     7098:                        for(int x = 0; x < scr_width; x++) {
                   7099:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   7100:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     7101:                        }
                   7102:                }
1.1.1.14  root     7103:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7104:                
                   7105:                COORD co;
1.1.1.14  root     7106:                co.X = mem[0x450 + REG8(AL) * 2];
                   7107:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   7108:                if(co.Y < scr_top + scr_height) {
                   7109:                        SetConsoleCursorPosition(hStdout, co);
                   7110:                }
1.1.1.59! root     7111:                cursor_moved_by_crtc = false;
1.1       root     7112:        }
1.1.1.14  root     7113:        mem[0x462] = REG8(AL);
                   7114:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   7115:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     7116:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     7117:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     7118:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     7119:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     7120:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7121: }
                   7122: 
                   7123: inline void pcbios_int_10h_06h()
                   7124: {
1.1.1.14  root     7125:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7126:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7127:                return;
                   7128:        }
                   7129:        vram_flush();
                   7130:        
1.1.1.23  root     7131:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7132:        SMALL_RECT rect;
1.1.1.14  root     7133:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   7134:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   7135:        
                   7136:        int right = min(REG8(DL), scr_width - 1);
                   7137:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7138:        
                   7139:        if(REG8(AL) == 0) {
1.1.1.14  root     7140:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7141:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7142:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7143:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7144:                        }
                   7145:                }
                   7146:        } else {
1.1.1.14  root     7147:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     7148:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7149:                                if(y2 <= bottom) {
                   7150:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7151:                                } else {
1.1.1.14  root     7152:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7153:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7154:                                }
1.1.1.14  root     7155:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7156:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7157:                        }
                   7158:                }
                   7159:        }
1.1.1.14  root     7160:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7161: }
                   7162: 
                   7163: inline void pcbios_int_10h_07h()
                   7164: {
1.1.1.14  root     7165:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7166:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7167:                return;
                   7168:        }
                   7169:        vram_flush();
                   7170:        
1.1.1.23  root     7171:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7172:        SMALL_RECT rect;
1.1.1.14  root     7173:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   7174:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   7175:        
                   7176:        int right = min(REG8(DL), scr_width - 1);
                   7177:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7178:        
                   7179:        if(REG8(AL) == 0) {
1.1.1.14  root     7180:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7181:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7182:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7183:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7184:                        }
                   7185:                }
                   7186:        } else {
1.1.1.14  root     7187:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     7188:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7189:                                if(y2 >= REG8(CH)) {
                   7190:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7191:                                } else {
1.1.1.14  root     7192:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7193:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7194:                                }
1.1.1.14  root     7195:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7196:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7197:                        }
                   7198:                }
                   7199:        }
1.1.1.14  root     7200:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7201: }
                   7202: 
                   7203: inline void pcbios_int_10h_08h()
                   7204: {
                   7205:        COORD co;
                   7206:        DWORD num;
                   7207:        
1.1.1.14  root     7208:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7209:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7210:        
                   7211:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7212:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7213:                co.Y += scr_top;
                   7214:                vram_flush();
1.1       root     7215:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   7216:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   7217:                REG8(AL) = scr_char[0];
                   7218:                REG8(AH) = scr_attr[0];
                   7219:        } else {
1.1.1.16  root     7220:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     7221:        }
                   7222: }
                   7223: 
                   7224: inline void pcbios_int_10h_09h()
                   7225: {
                   7226:        COORD co;
                   7227:        
1.1.1.14  root     7228:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7229:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7230:        
1.1.1.16  root     7231:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7232:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7233:        
                   7234:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7235: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7236:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7237: #endif
1.1.1.16  root     7238:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7239:                while(dest < end) {
                   7240:                        write_text_vram_char(dest - vram, REG8(AL));
                   7241:                        mem[dest++] = REG8(AL);
                   7242:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7243:                        mem[dest++] = REG8(BL);
1.1       root     7244:                }
1.1.1.35  root     7245: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7246:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7247: #endif
1.1       root     7248:        } else {
1.1.1.14  root     7249:                while(dest < end) {
1.1       root     7250:                        mem[dest++] = REG8(AL);
                   7251:                        mem[dest++] = REG8(BL);
                   7252:                }
                   7253:        }
                   7254: }
                   7255: 
                   7256: inline void pcbios_int_10h_0ah()
                   7257: {
                   7258:        COORD co;
                   7259:        
1.1.1.14  root     7260:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7261:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7262:        
1.1.1.16  root     7263:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7264:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7265:        
                   7266:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7267: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7268:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7269: #endif
1.1.1.16  root     7270:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7271:                while(dest < end) {
                   7272:                        write_text_vram_char(dest - vram, REG8(AL));
                   7273:                        mem[dest++] = REG8(AL);
                   7274:                        dest++;
1.1       root     7275:                }
1.1.1.35  root     7276: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7277:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7278: #endif
1.1       root     7279:        } else {
1.1.1.14  root     7280:                while(dest < end) {
1.1       root     7281:                        mem[dest++] = REG8(AL);
                   7282:                        dest++;
                   7283:                }
                   7284:        }
                   7285: }
                   7286: 
1.1.1.40  root     7287: HDC get_console_window_device_context()
                   7288: {
                   7289:        static HWND hwndFound = 0;
                   7290:        
                   7291:        if(hwndFound == 0) {
                   7292:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   7293:                char pszNewWindowTitle[1024];
                   7294:                char pszOldWindowTitle[1024];
                   7295:                
                   7296:                GetConsoleTitle(pszOldWindowTitle, 1024);
                   7297:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
                   7298:                SetConsoleTitle(pszNewWindowTitle);
                   7299:                Sleep(100);
                   7300:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
                   7301:                SetConsoleTitle(pszOldWindowTitle);
                   7302:        }
                   7303:        return GetDC(hwndFound);
                   7304: }
                   7305: 
                   7306: inline void pcbios_int_10h_0ch()
                   7307: {
                   7308:        HDC hdc = get_console_window_device_context();
                   7309:        
                   7310:        if(hdc != NULL) {
                   7311:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7312:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7313:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7314:                
                   7315:                if(REG8(AL) & 0x80) {
                   7316:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7317:                        if(color != CLR_INVALID) {
                   7318:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7319:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7320:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7321:                        }
                   7322:                }
                   7323:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7324:        }
                   7325: }
                   7326: 
                   7327: inline void pcbios_int_10h_0dh()
                   7328: {
                   7329:        HDC hdc = get_console_window_device_context();
                   7330:        BYTE r = 0;
                   7331:        BYTE g = 0;
                   7332:        BYTE b = 0;
                   7333:        
                   7334:        if(hdc != NULL) {
                   7335:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7336:                if(color != CLR_INVALID) {
                   7337:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7338:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7339:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7340:                }
                   7341:        }
                   7342:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7343: }
                   7344: 
1.1       root     7345: inline void pcbios_int_10h_0eh()
                   7346: {
1.1.1.59! root     7347:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
        !          7348:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     7349:        DWORD num;
                   7350:        COORD co;
                   7351:        
1.1.1.59! root     7352:        if(cursor_moved_by_crtc) {
        !          7353:                if(!restore_console_on_exit) {
        !          7354:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
        !          7355:                        scr_top = csbi.srWindow.Top;
        !          7356:                }
        !          7357:                co.X = mem[0x450 + REG8(BH) * 2];
        !          7358:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
        !          7359:                SetConsoleCursorPosition(hStdout, co);
        !          7360:                cursor_moved_by_crtc = false;
        !          7361:        }
1.1.1.54  root     7362:        co.X = mem[0x450 + mem[0x462] * 2];
                   7363:        co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14  root     7364:        
                   7365:        if(REG8(AL) == 7) {
                   7366:                //MessageBeep(-1);
                   7367:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7368:                if(REG8(AL) == 10) {
                   7369:                        vram_flush();
                   7370:                }
1.1.1.23  root     7371:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7372:                cursor_moved = true;
                   7373:        } else {
1.1.1.54  root     7374:                int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35  root     7375: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7376:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7377: #endif
1.1.1.54  root     7378:                int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
                   7379:                write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7380: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7381:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7382: #endif
1.1.1.54  root     7383:                
                   7384:                if(++co.X == scr_width) {
                   7385:                        co.X = 0;
                   7386:                        if(++co.Y == scr_height) {
                   7387:                                vram_flush();
                   7388:                                WriteConsole(hStdout, "\n", 1, &num, NULL);
1.1.1.14  root     7389:                                cursor_moved = true;
                   7390:                        }
                   7391:                }
1.1.1.54  root     7392:                if(!cursor_moved) {
                   7393:                        co.Y += scr_top;
                   7394:                        SetConsoleCursorPosition(hStdout, co);
                   7395:                        cursor_moved = true;
                   7396:                }
1.1.1.14  root     7397:                mem[dest] = REG8(AL);
                   7398:        }
1.1       root     7399: }
                   7400: 
                   7401: inline void pcbios_int_10h_0fh()
                   7402: {
                   7403:        REG8(AL) = mem[0x449];
                   7404:        REG8(AH) = mem[0x44a];
                   7405:        REG8(BH) = mem[0x462];
                   7406: }
                   7407: 
1.1.1.14  root     7408: inline void pcbios_int_10h_11h()
                   7409: {
                   7410:        switch(REG8(AL)) {
1.1.1.58  root     7411:        case 0x00:
                   7412:        case 0x10:
                   7413:                if(REG8(BH) != 0 && pcbios_set_font_size(8, REG8(BH))) {
                   7414:                        pcbios_set_console_size(80, (25 * 16) / REG8(BH), true);
                   7415:                } else {
                   7416:                        m_CF = 1; // never failed in real PC BIOS
                   7417:                }
                   7418:                break;
1.1.1.16  root     7419:        case 0x01:
1.1.1.14  root     7420:        case 0x11:
1.1.1.58  root     7421: //             if(pcbios_set_font_size(8, 14)) {
                   7422:                if(pcbios_set_font_size(8, 12)) {
                   7423:                        pcbios_set_console_size(80, 28, true); // 28 = 25 * 16 / 14
                   7424:                } else {
                   7425:                        m_CF = 1; // never failed in real PC BIOS
1.1.1.56  root     7426:                }
1.1.1.14  root     7427:                break;
1.1.1.16  root     7428:        case 0x02:
1.1.1.14  root     7429:        case 0x12:
1.1.1.58  root     7430:                if(pcbios_set_font_size(8, 8)) {
                   7431:                        pcbios_set_console_size(80, 50, true); // 50 = 25 * 16 / 8
                   7432:                } else {
                   7433:                        m_CF = 1; // never failed in real PC BIOS
1.1.1.56  root     7434:                }
1.1.1.14  root     7435:                break;
1.1.1.16  root     7436:        case 0x04:
1.1.1.14  root     7437:        case 0x14:
1.1.1.58  root     7438: //             if(pcbios_set_font_size(8, 16)) {
                   7439:                if(pcbios_set_font_size(8, 18)) {
                   7440:                        pcbios_set_console_size(80, 25, true);
                   7441:                } else {
                   7442:                        m_CF = 1; // never failed in real PC BIOS
1.1.1.56  root     7443:                }
1.1.1.58  root     7444:                break;
                   7445:        case 0x18:
                   7446: //             if(pcbios_set_font_size(8, 16)) {
                   7447:                if(pcbios_set_font_size(8, 18)) {
1.1.1.56  root     7448:                        pcbios_set_console_size(80, 50, true);
                   7449:                } else {
1.1.1.58  root     7450:                        m_CF = 1; // never failed in real PC BIOS
1.1.1.56  root     7451:                }
1.1.1.14  root     7452:                break;
                   7453:        case 0x30:
                   7454:                SREG(ES) = 0;
                   7455:                i386_load_segment_descriptor(ES);
                   7456:                REG16(BP) = 0;
                   7457:                REG16(CX) = mem[0x485];
                   7458:                REG8(DL) = mem[0x484];
                   7459:                break;
1.1.1.54  root     7460:        default:
                   7461:                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));
                   7462:                m_CF = 1;
                   7463:                break;
1.1.1.14  root     7464:        }
                   7465: }
                   7466: 
                   7467: inline void pcbios_int_10h_12h()
                   7468: {
1.1.1.16  root     7469:        switch(REG8(BL)) {
                   7470:        case 0x10:
1.1.1.14  root     7471:                REG16(BX) = 0x0003;
                   7472:                REG16(CX) = 0x0009;
1.1.1.16  root     7473:                break;
1.1.1.14  root     7474:        }
                   7475: }
                   7476: 
1.1       root     7477: inline void pcbios_int_10h_13h()
                   7478: {
1.1.1.3   root     7479:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7480:        COORD co;
                   7481:        DWORD num;
                   7482:        
                   7483:        co.X = REG8(DL);
1.1.1.14  root     7484:        co.Y = REG8(DH) + scr_top;
                   7485:        
                   7486:        vram_flush();
1.1       root     7487:        
                   7488:        switch(REG8(AL)) {
                   7489:        case 0x00:
                   7490:        case 0x01:
                   7491:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7492:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7493:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7494:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7495:                        SetConsoleCursorPosition(hStdout, co);
                   7496:                        
                   7497:                        if(csbi.wAttributes != REG8(BL)) {
                   7498:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7499:                        }
1.1.1.14  root     7500:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   7501:                        
1.1       root     7502:                        if(csbi.wAttributes != REG8(BL)) {
                   7503:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7504:                        }
                   7505:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7506:                                if(!restore_console_on_exit) {
                   7507:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7508:                                        scr_top = csbi.srWindow.Top;
                   7509:                                }
1.1.1.14  root     7510:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7511:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7512:                                SetConsoleCursorPosition(hStdout, co);
                   7513:                        } else {
                   7514:                                cursor_moved = true;
                   7515:                        }
1.1.1.59! root     7516:                        cursor_moved_by_crtc = false;
1.1       root     7517:                } else {
1.1.1.3   root     7518:                        m_CF = 1;
1.1       root     7519:                }
                   7520:                break;
                   7521:        case 0x02:
                   7522:        case 0x03:
                   7523:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7524:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7525:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7526:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7527:                        SetConsoleCursorPosition(hStdout, co);
                   7528:                        
                   7529:                        WORD wAttributes = csbi.wAttributes;
                   7530:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7531:                                if(wAttributes != mem[ofs + 1]) {
                   7532:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7533:                                        wAttributes = mem[ofs + 1];
                   7534:                                }
1.1.1.14  root     7535:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7536:                        }
                   7537:                        if(csbi.wAttributes != wAttributes) {
                   7538:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7539:                        }
                   7540:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7541:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7542:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7543:                                SetConsoleCursorPosition(hStdout, co);
                   7544:                        } else {
                   7545:                                cursor_moved = true;
                   7546:                        }
1.1.1.59! root     7547:                        cursor_moved_by_crtc = false;
1.1       root     7548:                } else {
1.1.1.3   root     7549:                        m_CF = 1;
1.1       root     7550:                }
                   7551:                break;
                   7552:        case 0x10:
                   7553:        case 0x11:
                   7554:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7555:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7556:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7557:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7558:                        for(int i = 0; i < num; i++) {
                   7559:                                mem[ofs++] = scr_char[i];
                   7560:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7561:                                if(REG8(AL) & 0x01) {
1.1       root     7562:                                        mem[ofs++] = 0;
                   7563:                                        mem[ofs++] = 0;
                   7564:                                }
                   7565:                        }
                   7566:                } else {
1.1.1.16  root     7567:                        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     7568:                                mem[ofs++] = mem[src++];
                   7569:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7570:                                if(REG8(AL) & 0x01) {
1.1       root     7571:                                        mem[ofs++] = 0;
                   7572:                                        mem[ofs++] = 0;
                   7573:                                }
1.1.1.14  root     7574:                                if(++co.X == scr_width) {
                   7575:                                        if(++co.Y == scr_height) {
1.1       root     7576:                                                break;
                   7577:                                        }
                   7578:                                        co.X = 0;
                   7579:                                }
                   7580:                        }
                   7581:                }
                   7582:                break;
1.1.1.45  root     7583:        case 0x12: // ???
                   7584:        case 0x13: // ???
1.1       root     7585:        case 0x20:
                   7586:        case 0x21:
                   7587:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7588:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7589:                        int len = min(REG16(CX), scr_width * scr_height);
                   7590:                        for(int i = 0; i < len; i++) {
1.1       root     7591:                                scr_char[i] = mem[ofs++];
                   7592:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7593:                                if(REG8(AL) & 0x01) {
1.1       root     7594:                                        ofs += 2;
                   7595:                                }
                   7596:                        }
1.1.1.14  root     7597:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7598:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7599:                } else {
1.1.1.16  root     7600:                        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     7601:                                mem[dest++] = mem[ofs++];
                   7602:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7603:                                if(REG8(AL) & 0x01) {
1.1       root     7604:                                        ofs += 2;
                   7605:                                }
1.1.1.14  root     7606:                                if(++co.X == scr_width) {
                   7607:                                        if(++co.Y == scr_height) {
1.1       root     7608:                                                break;
                   7609:                                        }
                   7610:                                        co.X = 0;
                   7611:                                }
                   7612:                        }
                   7613:                }
                   7614:                break;
                   7615:        default:
1.1.1.22  root     7616:                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     7617:                m_CF = 1;
1.1       root     7618:                break;
                   7619:        }
                   7620: }
                   7621: 
1.1.1.30  root     7622: inline void pcbios_int_10h_18h()
                   7623: {
                   7624:        switch(REG8(AL)) {
                   7625:        case 0x00:
                   7626:        case 0x01:
                   7627: //             REG8(AL) = 0x86;
                   7628:                REG8(AL) = 0x00;
                   7629:                break;
                   7630:        default:
                   7631:                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));
                   7632:                m_CF = 1;
                   7633:                break;
                   7634:        }
                   7635: }
                   7636: 
1.1.1.14  root     7637: inline void pcbios_int_10h_1ah()
                   7638: {
                   7639:        switch(REG8(AL)) {
                   7640:        case 0x00:
                   7641:                REG8(AL) = 0x1a;
                   7642:                REG8(BL) = 0x08;
                   7643:                REG8(BH) = 0x00;
                   7644:                break;
                   7645:        default:
1.1.1.22  root     7646:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     7647:                m_CF = 1;
                   7648:                break;
                   7649:        }
                   7650: }
                   7651: 
1.1       root     7652: inline void pcbios_int_10h_1dh()
                   7653: {
                   7654:        switch(REG8(AL)) {
1.1.1.43  root     7655:        case 0x00:
                   7656:                // DOS/V Shift Status Line Control is not supported
                   7657:                m_CF = 1;
                   7658:                break;
1.1       root     7659:        case 0x01:
                   7660:                break;
                   7661:        case 0x02:
                   7662:                REG16(BX) = 0;
                   7663:                break;
                   7664:        default:
1.1.1.22  root     7665:                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));
                   7666:                m_CF = 1;
                   7667:                break;
                   7668:        }
                   7669: }
                   7670: 
                   7671: inline void pcbios_int_10h_4fh()
                   7672: {
                   7673:        switch(REG8(AL)) {
                   7674:        case 0x00:
                   7675:                REG8(AH) = 0x02; // not supported
                   7676:                break;
                   7677:        case 0x01:
                   7678:        case 0x02:
                   7679:        case 0x03:
                   7680:        case 0x04:
                   7681:        case 0x05:
                   7682:        case 0x06:
                   7683:        case 0x07:
                   7684:        case 0x08:
                   7685:        case 0x09:
                   7686:        case 0x0a:
                   7687:        case 0x0b:
                   7688:        case 0x0c:
                   7689:                REG8(AH) = 0x01; // failed
                   7690:                break;
                   7691:        default:
                   7692:                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     7693:                m_CF = 1;
1.1       root     7694:                break;
                   7695:        }
                   7696: }
                   7697: 
                   7698: inline void pcbios_int_10h_82h()
                   7699: {
                   7700:        static UINT8 mode = 0;
                   7701:        
                   7702:        switch(REG8(AL)) {
1.1.1.22  root     7703:        case 0x00:
1.1       root     7704:                if(REG8(BL) != 0xff) {
                   7705:                        mode = REG8(BL);
                   7706:                }
                   7707:                REG8(AL) = mode;
                   7708:                break;
                   7709:        default:
1.1.1.22  root     7710:                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     7711:                m_CF = 1;
1.1       root     7712:                break;
                   7713:        }
                   7714: }
                   7715: 
1.1.1.22  root     7716: inline void pcbios_int_10h_83h()
                   7717: {
                   7718:        static UINT8 mode = 0;
                   7719:        
                   7720:        switch(REG8(AL)) {
                   7721:        case 0x00:
                   7722:                REG16(AX) = 0; // offset???
                   7723:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7724:                i386_load_segment_descriptor(ES);
                   7725:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7726:                break;
                   7727:        default:
                   7728:                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));
                   7729:                m_CF = 1;
                   7730:                break;
                   7731:        }
                   7732: }
                   7733: 
                   7734: inline void pcbios_int_10h_90h()
                   7735: {
                   7736:        REG8(AL) = mem[0x449];
                   7737: }
                   7738: 
                   7739: inline void pcbios_int_10h_91h()
                   7740: {
                   7741:        REG8(AL) = 0x04; // VGA
                   7742: }
                   7743: 
                   7744: inline void pcbios_int_10h_efh()
                   7745: {
                   7746:        REG16(DX) = 0xffff;
                   7747: }
                   7748: 
1.1       root     7749: inline void pcbios_int_10h_feh()
                   7750: {
                   7751:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7752:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7753:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7754:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7755:        }
                   7756:        int_10h_feh_called = true;
                   7757: }
                   7758: 
                   7759: inline void pcbios_int_10h_ffh()
                   7760: {
                   7761:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7762:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7763:                COORD co;
                   7764:                DWORD num;
                   7765:                
1.1.1.14  root     7766:                vram_flush();
                   7767:                
                   7768:                co.X = (REG16(DI) >> 1) % scr_width;
                   7769:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7770:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7771:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7772:                int len;
                   7773:                for(len = 0; ofs < end; len++) {
                   7774:                        scr_char[len] = mem[ofs++];
                   7775:                        scr_attr[len] = mem[ofs++];
                   7776:                }
                   7777:                co.Y += scr_top;
                   7778:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7779:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7780:        }
                   7781:        int_10h_ffh_called = true;
                   7782: }
                   7783: 
1.1.1.42  root     7784: int pcbios_update_drive_param(int drive_num, int force_update)
                   7785: {
                   7786:        if(drive_num >= 0 && drive_num < 26) {
                   7787:                drive_param_t *drive_param = &drive_params[drive_num];
                   7788:                
                   7789:                if(force_update || !drive_param->initialized) {
                   7790:                        drive_param->valid = 0;
                   7791:                        char dev[64];
                   7792:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7793:                        
                   7794:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7795:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7796:                                DWORD dwSize;
                   7797:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7798:                                        drive_param->valid = 1;
                   7799:                                }
                   7800:                                CloseHandle(hFile);
                   7801:                        }
                   7802:                        drive_param->initialized = 1;
                   7803:                }
                   7804:                return(drive_param->valid);
                   7805:        }
                   7806:        return(0);
                   7807: }
                   7808: 
                   7809: inline void pcbios_int_13h_00h()
                   7810: {
                   7811:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7812:        
                   7813:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7814:                REG8(AH) = 0x00; // successful completion
                   7815:        } else {
                   7816:                if(REG8(DL) & 0x80) {
                   7817:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7818:                } else {
                   7819:                        REG8(AH) = 0x80; // timeout (not ready)
                   7820:                }
                   7821:                m_CF = 1;
                   7822:        }
                   7823: }
                   7824: 
                   7825: inline void pcbios_int_13h_02h()
                   7826: {
                   7827:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7828:        
                   7829:        if(REG8(AL) == 0) {
                   7830:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7831:                m_CF = 1;
                   7832:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7833:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7834:                m_CF = 1;
                   7835:        } else {
                   7836:                drive_param_t *drive_param = &drive_params[drive_num];
                   7837:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7838:                char dev[64];
                   7839:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7840:                
                   7841:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7842:                if(hFile == INVALID_HANDLE_VALUE) {
                   7843:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7844:                        m_CF = 1;
                   7845:                } else {
                   7846:                        UINT32 sector_num = REG8(AL);
                   7847:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7848:                        UINT32 head = REG8(DH);
                   7849:                        UINT32 sector = REG8(CL) & 0x3f;
                   7850:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7851:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7852:                        DWORD dwSize;
                   7853:                        
                   7854: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7855: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7856: //                             m_CF = 1;
                   7857: //                     } else 
                   7858:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7859:                                REG8(AH) = 0x04; // sector not found/read error
                   7860:                                m_CF = 1;
                   7861:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7862:                                REG8(AH) = 0x04; // sector not found/read error
                   7863:                                m_CF = 1;
                   7864:                        } else {
                   7865:                                REG8(AH) = 0x00; // successful completion
                   7866:                        }
                   7867:                        CloseHandle(hFile);
                   7868:                }
                   7869:        }
                   7870: }
                   7871: 
                   7872: inline void pcbios_int_13h_03h()
                   7873: {
                   7874:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7875:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7876:        
                   7877:        if(REG8(AL) == 0) {
                   7878:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7879:                m_CF = 1;
                   7880:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7881:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7882:                m_CF = 1;
                   7883:        } else if(!drive_params[drive_num].is_fdd()) {
                   7884:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7885:                m_CF = 1;
                   7886:        } else {
                   7887:                drive_param_t *drive_param = &drive_params[drive_num];
                   7888:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7889:                char dev[64];
                   7890:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7891:                
                   7892:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7893:                if(hFile == INVALID_HANDLE_VALUE) {
                   7894:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7895:                        m_CF = 1;
                   7896:                } else {
                   7897:                        UINT32 sector_num = REG8(AL);
                   7898:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7899:                        UINT32 head = REG8(DH);
                   7900:                        UINT32 sector = REG8(CL) & 0x3f;
                   7901:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7902:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7903:                        DWORD dwSize;
                   7904:                        
                   7905: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7906: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7907: //                             m_CF = 1;
                   7908: //                     } else 
                   7909:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7910:                                REG8(AH) = 0x04; // sector not found/read error
                   7911:                                m_CF = 1;
                   7912:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7913:                                REG8(AH) = 0x04; // sector not found/read error
                   7914:                                m_CF = 1;
                   7915:                        } else {
                   7916:                                REG8(AH) = 0x00; // successful completion
                   7917:                        }
                   7918:                        CloseHandle(hFile);
                   7919:                }
                   7920:        }
                   7921: }
                   7922: 
                   7923: inline void pcbios_int_13h_04h()
                   7924: {
                   7925:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7926:        
                   7927:        if(REG8(AL) == 0) {
                   7928:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7929:                m_CF = 1;
                   7930:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7931:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7932:                m_CF = 1;
                   7933:        } else {
                   7934:                drive_param_t *drive_param = &drive_params[drive_num];
                   7935:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7936:                char dev[64];
                   7937:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7938:                
                   7939:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7940:                if(hFile == INVALID_HANDLE_VALUE) {
                   7941:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7942:                        m_CF = 1;
                   7943:                } else {
                   7944:                        UINT32 sector_num = REG8(AL);
                   7945:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7946:                        UINT32 head = REG8(DH);
                   7947:                        UINT32 sector = REG8(CL) & 0x3f;
                   7948:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7949:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7950:                        DWORD dwSize;
                   7951:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   7952:                        
                   7953: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7954: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7955: //                             m_CF = 1;
                   7956: //                     } else 
                   7957:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7958:                                REG8(AH) = 0x04; // sector not found/read error
                   7959:                                m_CF = 1;
                   7960:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7961:                                REG8(AH) = 0x04; // sector not found/read error
                   7962:                                m_CF = 1;
                   7963:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   7964:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   7965:                                m_CF = 1;
                   7966:                        } else {
                   7967:                                REG8(AH) = 0x00; // successful completion
                   7968:                        }
                   7969:                        free(tmp_buffer);
                   7970:                        CloseHandle(hFile);
                   7971:                }
                   7972:        }
                   7973: }
                   7974: 
                   7975: inline void pcbios_int_13h_08h()
                   7976: {
                   7977:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7978:        
                   7979:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7980:                drive_param_t *drive_param = &drive_params[drive_num];
                   7981:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7982:                
                   7983:                REG16(AX) = 0x0000;
                   7984:                switch(geo->MediaType) {
                   7985:                case F5_360_512:
                   7986:                case F5_320_512:
                   7987:                case F5_320_1024:
                   7988:                case F5_180_512:
                   7989:                case F5_160_512:
                   7990:                        REG8(BL) = 0x01; // 320K/360K disk
                   7991:                        break;
                   7992:                case F5_1Pt2_512:
                   7993:                case F3_1Pt2_512:
                   7994:                case F3_1Pt23_1024:
                   7995:                case F5_1Pt23_1024:
                   7996:                        REG8(BL) = 0x02; // 1.2M disk
                   7997:                        break;
                   7998:                case F3_720_512:
                   7999:                case F3_640_512:
                   8000:                case F5_640_512:
                   8001:                case F5_720_512:
                   8002:                        REG8(BL) = 0x03; // 720K disk
                   8003:                        break;
                   8004:                case F3_1Pt44_512:
                   8005:                        REG8(BL) = 0x04; // 1.44M disk
                   8006:                        break;
                   8007:                case F3_2Pt88_512:
                   8008:                        REG8(BL) = 0x06; // 2.88M disk
                   8009:                        break;
                   8010:                case RemovableMedia:
                   8011:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   8012:                        break;
                   8013:                default:
                   8014:                        REG8(BL) = 0x00; // unknown
                   8015:                        break;
                   8016:                }
                   8017:                if(REG8(DL) & 0x80) {
                   8018:                        switch(GetLogicalDrives() & 0x0c) {
                   8019:                        case 0x00: REG8(DL) = 0x00; break;
                   8020:                        case 0x04:
                   8021:                        case 0x08: REG8(DL) = 0x01; break;
                   8022:                        case 0x0c: REG8(DL) = 0x02; break;
                   8023:                        }
                   8024:                } else {
                   8025:                        switch(GetLogicalDrives() & 0x03) {
                   8026:                        case 0x00: REG8(DL) = 0x00; break;
                   8027:                        case 0x01:
                   8028:                        case 0x02: REG8(DL) = 0x01; break;
                   8029:                        case 0x03: REG8(DL) = 0x02; break;
                   8030:                        }
                   8031:                }
                   8032:                REG8(DH) = drive_param->head_num();
                   8033:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   8034:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   8035:                REG8(CH) = cyl & 0xff;
                   8036:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   8037:        } else {
                   8038:                REG8(AH) = 0x07;
                   8039:                m_CF = 1;
                   8040:        }
                   8041: }
                   8042: 
                   8043: inline void pcbios_int_13h_10h()
                   8044: {
                   8045:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8046:        
                   8047:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8048:                REG8(AH) = 0x00; // successful completion
                   8049:        } else {
                   8050:                if(REG8(DL) & 0x80) {
                   8051:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   8052:                } else {
                   8053:                        REG8(AH) = 0x80; // timeout (not ready)
                   8054:                }
                   8055:                m_CF = 1;
                   8056:        }
                   8057: }
                   8058: 
                   8059: inline void pcbios_int_13h_15h()
                   8060: {
                   8061:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8062:        
                   8063:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8064:                if(REG8(DL) & 0x80) {
                   8065:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   8066:                } else {
                   8067:                        REG8(AH) = 0x03; // hard disk
                   8068:                }
                   8069:        } else {
                   8070:                REG8(AH) = 0x00; // no such drive
                   8071:        }
                   8072: }
                   8073: 
1.1.1.43  root     8074: inline void pcbios_int_13h_41h()
                   8075: {
                   8076:        if(REG16(BX) == 0x55aa) {
                   8077:                // IBM/MS INT 13 Extensions is not installed
                   8078:                REG8(AH) = 0x01;
                   8079:                m_CF = 1;
                   8080:        } else {
                   8081:                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));
                   8082:                REG8(AH) = 0x01;
                   8083:                m_CF = 1;
                   8084:        }
                   8085: }
                   8086: 
1.1.1.25  root     8087: inline void pcbios_int_14h_00h()
                   8088: {
1.1.1.29  root     8089:        if(REG16(DX) < 4) {
1.1.1.25  root     8090:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   8091:                UINT8 selector = sio_read(REG16(DX), 3);
                   8092:                selector &= ~0x3f;
                   8093:                selector |= REG8(AL) & 0x1f;
                   8094:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   8095:                sio_write(REG16(DX), 3, selector | 0x80);
                   8096:                sio_write(REG16(DX), 0, divisor & 0xff);
                   8097:                sio_write(REG16(DX), 1, divisor >> 8);
                   8098:                sio_write(REG16(DX), 3, selector);
                   8099:                REG8(AH) = sio_read(REG16(DX), 5);
                   8100:                REG8(AL) = sio_read(REG16(DX), 6);
                   8101:        } else {
                   8102:                REG8(AH) = 0x80;
                   8103:        }
                   8104: }
                   8105: 
                   8106: inline void pcbios_int_14h_01h()
                   8107: {
1.1.1.29  root     8108:        if(REG16(DX) < 4) {
1.1.1.25  root     8109:                UINT8 selector = sio_read(REG16(DX), 3);
                   8110:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8111:                sio_write(REG16(DX), 0, REG8(AL));
                   8112:                sio_write(REG16(DX), 3, selector);
                   8113:                REG8(AH) = sio_read(REG16(DX), 5);
                   8114:        } else {
                   8115:                REG8(AH) = 0x80;
                   8116:        }
                   8117: }
                   8118: 
                   8119: inline void pcbios_int_14h_02h()
                   8120: {
1.1.1.29  root     8121:        if(REG16(DX) < 4) {
1.1.1.25  root     8122:                UINT8 selector = sio_read(REG16(DX), 3);
                   8123:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8124:                REG8(AL) = sio_read(REG16(DX), 0);
                   8125:                sio_write(REG16(DX), 3, selector);
                   8126:                REG8(AH) = sio_read(REG16(DX), 5);
                   8127:        } else {
                   8128:                REG8(AH) = 0x80;
                   8129:        }
                   8130: }
                   8131: 
                   8132: inline void pcbios_int_14h_03h()
                   8133: {
1.1.1.29  root     8134:        if(REG16(DX) < 4) {
1.1.1.25  root     8135:                REG8(AH) = sio_read(REG16(DX), 5);
                   8136:                REG8(AL) = sio_read(REG16(DX), 6);
                   8137:        } else {
                   8138:                REG8(AH) = 0x80;
                   8139:        }
                   8140: }
                   8141: 
                   8142: inline void pcbios_int_14h_04h()
                   8143: {
1.1.1.29  root     8144:        if(REG16(DX) < 4) {
1.1.1.25  root     8145:                UINT8 selector = sio_read(REG16(DX), 3);
                   8146:                if(REG8(CH) <= 0x03) {
                   8147:                        selector = (selector & ~0x03) | REG8(CH);
                   8148:                }
                   8149:                if(REG8(BL) == 0x00) {
                   8150:                        selector &= ~0x04;
                   8151:                } else if(REG8(BL) == 0x01) {
                   8152:                        selector |= 0x04;
                   8153:                }
                   8154:                if(REG8(BH) == 0x00) {
                   8155:                        selector = (selector & ~0x38) | 0x00;
                   8156:                } else if(REG8(BH) == 0x01) {
                   8157:                        selector = (selector & ~0x38) | 0x08;
                   8158:                } else if(REG8(BH) == 0x02) {
                   8159:                        selector = (selector & ~0x38) | 0x18;
                   8160:                } else if(REG8(BH) == 0x03) {
                   8161:                        selector = (selector & ~0x38) | 0x28;
                   8162:                } else if(REG8(BH) == 0x04) {
                   8163:                        selector = (selector & ~0x38) | 0x38;
                   8164:                }
                   8165:                if(REG8(AL) == 0x00) {
                   8166:                        selector |= 0x40;
                   8167:                } else if(REG8(AL) == 0x01) {
                   8168:                        selector &= ~0x40;
                   8169:                }
                   8170:                if(REG8(CL) <= 0x0b) {
                   8171:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   8172:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   8173:                        sio_write(REG16(DX), 3, selector | 0x80);
                   8174:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   8175:                        sio_write(REG16(DX), 1, divisor >> 8);
                   8176:                }
                   8177:                sio_write(REG16(DX), 3, selector);
                   8178:                REG8(AH) = sio_read(REG16(DX), 5);
                   8179:                REG8(AL) = sio_read(REG16(DX), 6);
                   8180:        } else {
                   8181:                REG8(AH) = 0x80;
                   8182:        }
                   8183: }
                   8184: 
                   8185: inline void pcbios_int_14h_05h()
                   8186: {
1.1.1.29  root     8187:        if(REG16(DX) < 4) {
1.1.1.25  root     8188:                if(REG8(AL) == 0x00) {
                   8189:                        REG8(BL) = sio_read(REG16(DX), 4);
                   8190:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8191:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8192:                } else if(REG8(AL) == 0x01) {
                   8193:                        sio_write(REG16(DX), 4, REG8(BL));
                   8194:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8195:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8196:                } else {
                   8197:                        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));
                   8198:                }
                   8199:        } else {
                   8200:                REG8(AH) = 0x80;
                   8201:        }
                   8202: }
                   8203: 
1.1.1.14  root     8204: inline void pcbios_int_15h_10h()
                   8205: {
1.1.1.22  root     8206:        switch(REG8(AL)) {
                   8207:        case 0x00:
1.1.1.14  root     8208:                Sleep(10);
1.1.1.35  root     8209:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     8210:                break;
                   8211:        default:
                   8212:                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     8213:                REG8(AH) = 0x86;
                   8214:                m_CF = 1;
                   8215:        }
                   8216: }
                   8217: 
1.1       root     8218: inline void pcbios_int_15h_23h()
                   8219: {
                   8220:        switch(REG8(AL)) {
1.1.1.22  root     8221:        case 0x00:
1.1.1.8   root     8222:                REG8(CL) = cmos_read(0x2d);
                   8223:                REG8(CH) = cmos_read(0x2e);
1.1       root     8224:                break;
1.1.1.22  root     8225:        case 0x01:
1.1.1.8   root     8226:                cmos_write(0x2d, REG8(CL));
                   8227:                cmos_write(0x2e, REG8(CH));
1.1       root     8228:                break;
                   8229:        default:
1.1.1.22  root     8230:                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     8231:                REG8(AH) = 0x86;
1.1.1.3   root     8232:                m_CF = 1;
1.1       root     8233:                break;
                   8234:        }
                   8235: }
                   8236: 
                   8237: inline void pcbios_int_15h_24h()
                   8238: {
                   8239:        switch(REG8(AL)) {
1.1.1.22  root     8240:        case 0x00:
1.1.1.3   root     8241:                i386_set_a20_line(0);
1.1       root     8242:                REG8(AH) = 0;
                   8243:                break;
1.1.1.22  root     8244:        case 0x01:
1.1.1.3   root     8245:                i386_set_a20_line(1);
1.1       root     8246:                REG8(AH) = 0;
                   8247:                break;
1.1.1.22  root     8248:        case 0x02:
1.1       root     8249:                REG8(AH) = 0;
1.1.1.3   root     8250:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     8251:                REG16(CX) = 0;
                   8252:                break;
1.1.1.22  root     8253:        case 0x03:
1.1       root     8254:                REG16(AX) = 0;
                   8255:                REG16(BX) = 0;
                   8256:                break;
1.1.1.22  root     8257:        default:
                   8258:                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));
                   8259:                REG8(AH) = 0x86;
                   8260:                m_CF = 1;
                   8261:                break;
1.1       root     8262:        }
                   8263: }
                   8264: 
                   8265: inline void pcbios_int_15h_49h()
                   8266: {
1.1.1.27  root     8267:        REG8(AH) = 0x00;
                   8268:        REG8(BL) = 0x00; // DOS/V
1.1       root     8269: }
                   8270: 
1.1.1.22  root     8271: inline void pcbios_int_15h_50h()
                   8272: {
                   8273:        switch(REG8(AL)) {
                   8274:        case 0x00:
                   8275:        case 0x01:
                   8276:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8277:                        REG8(AH) = 0x01; // invalid font type in bh
                   8278:                        m_CF = 1;
1.1.1.27  root     8279:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8280:                        REG8(AH) = 0x02; // bl not zero
                   8281:                        m_CF = 1;
                   8282:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8283:                        REG8(AH) = 0x04; // invalid code page
                   8284:                        m_CF = 1;
1.1.1.27  root     8285:                } else if(REG8(AL) == 0x01) {
                   8286:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8287:                        m_CF = 1;
1.1.1.27  root     8288:                } else {
1.1.1.49  root     8289:                        // dummy font read routine is at fffc:000d
                   8290:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8291:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8292:                        REG16(BX) = 0x000d;
1.1.1.27  root     8293:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8294:                }
                   8295:                break;
                   8296:        default:
                   8297:                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));
                   8298:                REG8(AH) = 0x86;
                   8299:                m_CF = 1;
                   8300:                break;
                   8301:        }
                   8302: }
                   8303: 
1.1.1.30  root     8304: inline void pcbios_int_15h_53h()
                   8305: {
                   8306:        switch(REG8(AL)) {
                   8307:        case 0x00:
                   8308:                // APM is not installed
                   8309:                REG8(AH) = 0x86;
                   8310:                m_CF = 1;
                   8311:                break;
                   8312:        default:
                   8313:                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));
                   8314:                REG8(AH) = 0x86;
                   8315:                m_CF = 1;
                   8316:                break;
                   8317:        }
                   8318: }
                   8319: 
1.1.1.43  root     8320: inline void pcbios_int_15h_84h()
                   8321: {
                   8322:        // joystick support (from DOSBox)
                   8323:        switch(REG16(DX)) {
                   8324:        case 0x00:
                   8325:                REG16(AX) = 0x00f0;
                   8326:                REG16(DX) = 0x0201;
                   8327:                m_CF = 1;
                   8328:                break;
                   8329:        case 0x01:
                   8330:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8331:                m_CF = 1;
                   8332:                break;
                   8333:        default:
                   8334:                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));
                   8335:                REG8(AH) = 0x86;
                   8336:                m_CF = 1;
                   8337:                break;
                   8338:        }
                   8339: }
1.1.1.35  root     8340: 
                   8341: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8342: {
                   8343:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8344:        UINT32 msec = usec / 1000;
                   8345:        
1.1.1.54  root     8346:        while(msec && !m_exit) {
1.1.1.14  root     8347:                UINT32 tmp = min(msec, 100);
                   8348:                if(msec - tmp < 10) {
                   8349:                        tmp = msec;
                   8350:                }
                   8351:                Sleep(tmp);
                   8352:                msec -= tmp;
                   8353:        }
1.1.1.35  root     8354:        
                   8355: #ifdef USE_SERVICE_THREAD
                   8356:        service_exit = true;
                   8357: #endif
                   8358:        return(0);
                   8359: }
                   8360: 
                   8361: inline void pcbios_int_15h_86h()
                   8362: {
                   8363:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8364: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8365:                if(!in_service && !in_service_29h) {
                   8366:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8367:                } else {
                   8368: #endif
                   8369:                        pcbios_int_15h_86h_thread(NULL);
                   8370:                        REQUEST_HARDWRE_UPDATE();
                   8371: #ifdef USE_SERVICE_THREAD
                   8372:                }
1.1.1.35  root     8373: #endif
                   8374:        }
1.1       root     8375: }
                   8376: 
                   8377: inline void pcbios_int_15h_87h()
                   8378: {
                   8379:        // copy extended memory (from DOSBox)
                   8380:        int len = REG16(CX) * 2;
1.1.1.3   root     8381:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8382:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8383:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8384:        memcpy(mem + dst, mem + src, len);
                   8385:        REG16(AX) = 0x00;
                   8386: }
                   8387: 
                   8388: inline void pcbios_int_15h_88h()
                   8389: {
1.1.1.17  root     8390:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8391: }
                   8392: 
                   8393: inline void pcbios_int_15h_89h()
                   8394: {
1.1.1.21  root     8395: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8396:        // switch to protected mode (from DOSBox)
                   8397:        write_io_byte(0x20, 0x10);
                   8398:        write_io_byte(0x21, REG8(BH));
                   8399:        write_io_byte(0x21, 0x00);
                   8400:        write_io_byte(0xa0, 0x10);
                   8401:        write_io_byte(0xa1, REG8(BL));
                   8402:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8403:        i386_set_a20_line(1);
                   8404:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8405:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8406:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8407:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8408:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8409: #if defined(HAS_I386)
                   8410:        m_cr[0] |= 1;
                   8411: #else
                   8412:        m_msw |= 1;
                   8413: #endif
                   8414:        SREG(DS) = 0x18;
                   8415:        SREG(ES) = 0x20;
                   8416:        SREG(SS) = 0x28;
                   8417:        i386_load_segment_descriptor(DS);
                   8418:        i386_load_segment_descriptor(ES);
                   8419:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8420:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8421:        REG16(SP) += 6;
1.1.1.3   root     8422: #if defined(HAS_I386)
1.1.1.21  root     8423:        UINT32 flags = get_flags();
                   8424:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8425:        set_flags(flags);
1.1.1.3   root     8426: #else
1.1.1.21  root     8427:        UINT32 flags = CompressFlags();
                   8428:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8429:        ExpandFlags(flags);
1.1.1.3   root     8430: #endif
1.1       root     8431:        REG16(AX) = 0x00;
1.1.1.21  root     8432:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8433: #else
1.1.1.21  root     8434:        // i86/i186/v30: protected mode is not supported
1.1       root     8435:        REG8(AH) = 0x86;
1.1.1.3   root     8436:        m_CF = 1;
1.1       root     8437: #endif
                   8438: }
                   8439: 
1.1.1.21  root     8440: inline void pcbios_int_15h_8ah()
                   8441: {
                   8442:        UINT32 size = MAX_MEM - 0x100000;
                   8443:        REG16(AX) = size & 0xffff;
                   8444:        REG16(DX) = size >> 16;
                   8445: }
                   8446: 
1.1.1.54  root     8447: #ifdef EXT_BIOS_TOP
                   8448: inline void pcbios_int_15h_c1h()
                   8449: {
                   8450:        SREG(ES) = EXT_BIOS_TOP >> 4;
                   8451:        i386_load_segment_descriptor(ES);
                   8452: }
                   8453: #endif
                   8454: 
                   8455: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
                   8456: {
                   8457:        // from DOSBox DoPS2Callback()
                   8458:        UINT16 mdat = 0x08;
                   8459:        INT16 xdiff = mouse.position.x - mouse.prev_position.x;
                   8460:        INT16 ydiff = mouse.prev_position.y - mouse.position.y;
                   8461:        
1.1.1.59! root     8462: #if 1
        !          8463:        if(xdiff > +16) xdiff = +16;
        !          8464:        if(xdiff < -16) xdiff = -16;
        !          8465:        if(ydiff > +16) ydiff = +16;
        !          8466:        if(ydiff < -16) ydiff = -16;
        !          8467: #endif
        !          8468:        
1.1.1.54  root     8469:        if(mouse.buttons[0].status) {
                   8470:                mdat |= 0x01;
                   8471:        }
                   8472:        if(mouse.buttons[1].status) {
                   8473:                mdat |= 0x02;
                   8474:        }
                   8475:        mouse.prev_position.x = mouse.position.x;
                   8476:        mouse.prev_position.y = mouse.position.y;
1.1.1.59! root     8477:        
1.1.1.54  root     8478:        if((xdiff > 0xff) || (xdiff < -0xff)) {
                   8479:                mdat |= 0x40;   // x overflow
                   8480:        }
                   8481:        if((ydiff > 0xff) || (ydiff < -0xff)) {
                   8482:                mdat |= 0x80;   // y overflow
                   8483:        }
                   8484:        xdiff %= 256;
                   8485:        ydiff %= 256;
                   8486:        if(xdiff < 0) {
                   8487:                xdiff = (0x100 + xdiff);
                   8488:                mdat |= 0x10;
                   8489:        }
                   8490:        if(ydiff < 0) {
                   8491:                ydiff = (0x100 + ydiff);
                   8492:                mdat |= 0x20;
                   8493:        }
                   8494:        *data_1st = (UINT16)mdat;
                   8495:        *data_2nd = (UINT16)(xdiff % 256);
                   8496:        *data_3rd = (UINT16)(ydiff % 256);
                   8497: }
                   8498: 
                   8499: inline void pcbios_int_15h_c2h()
                   8500: {
                   8501:        static UINT8 sampling_rate = 5;
                   8502:        static UINT8 resolution = 2;
                   8503:        static UINT8 scaling = 1;
                   8504:        
                   8505:        switch(REG8(AL)) {
                   8506:        case 0x00:
1.1.1.59! root     8507:                if(REG8(BH) == 0x00) {
        !          8508: //                     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
        !          8509:                        pic[1].imr |= 0x10; // disable irq12
        !          8510:                        mouse.enabled_ps2 = false;
        !          8511:                        REG8(AH) = 0x00; // successful
        !          8512:                } else if(REG8(BH) == 0x01) {
        !          8513:                        if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
        !          8514:                                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
        !          8515:                        }
        !          8516:                        pic[1].imr &= ~0x10; // enable irq12
        !          8517:                        mouse.enabled_ps2 = true;
1.1.1.54  root     8518:                        REG8(AH) = 0x00; // successful
                   8519:                } else {
                   8520:                        REG8(AH) = 0x01; // invalid function
                   8521:                        m_CF = 1;
                   8522:                }
                   8523:                break;
                   8524:        case 0x01:
                   8525:                REG8(BH) = 0x00; // device id
                   8526:                REG8(BL) = 0xaa; // mouse
                   8527:        case 0x05:
1.1.1.59! root     8528: //             SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
        !          8529:                pic[1].imr |= 0x10; // disable irq12
        !          8530:                mouse.enabled_ps2 = false;
1.1.1.54  root     8531:                sampling_rate = 5;
                   8532:                resolution = 2;
                   8533:                scaling = 1;
                   8534:                REG8(AH) = 0x00; // successful
                   8535:                break;
                   8536:        case 0x02:
                   8537:                sampling_rate = REG8(BH);
                   8538:                REG8(AH) = 0x00; // successful
                   8539:                break;
                   8540:        case 0x03:
                   8541:                resolution = REG8(BH);
                   8542:                REG8(AH) = 0x00; // successful
                   8543:                break;
                   8544:        case 0x04:
                   8545:                REG8(BH) = 0x00; // device id
                   8546:                REG8(AH) = 0x00; // successful
                   8547:                break;
                   8548:        case 0x06:
                   8549:                switch(REG8(BH)) {
                   8550:                case 0x00:
                   8551:                        REG8(BL) = 0x00;
                   8552:                        if(mouse.buttons[1].status) {
                   8553:                                REG8(BL) |= 0x01;
                   8554:                        }
                   8555:                        if(mouse.buttons[0].status) {
                   8556:                                REG8(BL) |= 0x04;
                   8557:                        }
                   8558:                        if(scaling == 2) {
                   8559:                                REG8(BL) |= 0x10;
                   8560:                        }
                   8561:                        REG8(CL) = resolution;
                   8562:                        switch(sampling_rate) {
                   8563:                        case 0:  REG8(DL) =  10; break;
                   8564:                        case 1:  REG8(DL) =  20; break;
                   8565:                        case 2:  REG8(DL) =  40; break;
                   8566:                        case 3:  REG8(DL) =  60; break;
                   8567:                        case 4:  REG8(DL) =  80; break;
                   8568: //                     case 5:  REG8(DL) = 100; break;
                   8569:                        case 6:  REG8(DL) = 200; break;
                   8570:                        default: REG8(DL) = 100; break;
                   8571:                        }
                   8572:                        REG8(AH) = 0x00; // successful
                   8573:                        break;
                   8574:                case 0x01:
                   8575:                case 0x02:
                   8576:                        scaling = REG8(BH);
                   8577:                        REG8(AH) = 0x00; // successful
                   8578:                        break;
                   8579:                default:
                   8580:                        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));
                   8581:                        REG8(AH) = 0x01; // invalid function
                   8582:                        m_CF = 1;
                   8583:                        break;
                   8584:                }
                   8585:                break;
                   8586:        case 0x07: // set device handler addr
                   8587:                mouse.call_addr_ps2.w.l = REG16(BX);
                   8588:                mouse.call_addr_ps2.w.h = SREG(ES);
                   8589:                REG8(AH) = 0x00; // successful
                   8590:                break;
                   8591:        case 0x08:
                   8592:                REG8(AH) = 0x00; // successful
                   8593:                break;
                   8594:        case 0x09:
                   8595:                {
                   8596:                        UINT16 data_1st, data_2nd, data_3rd;
                   8597:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   8598:                        REG8(BL) = (UINT8)(data_1st & 0xff);
                   8599:                        REG8(CL) = (UINT8)(data_2nd & 0xff);
                   8600:                        REG8(DL) = (UINT8)(data_3rd & 0xff);
                   8601:                }
                   8602:                REG8(AH) = 0x00; // successful
                   8603:                break;
                   8604:        default:
                   8605:                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));
                   8606: //             REG8(AH) = 0x86;
                   8607:                REG8(AH) = 0x01; // invalid function
                   8608:                m_CF = 1;
                   8609:                break;
                   8610:        }
                   8611: }
                   8612: 
1.1.1.3   root     8613: #if defined(HAS_I386)
1.1       root     8614: inline void pcbios_int_15h_c9h()
                   8615: {
                   8616:        REG8(AH) = 0x00;
                   8617:        REG8(CH) = cpu_type;
                   8618:        REG8(CL) = cpu_step;
                   8619: }
1.1.1.3   root     8620: #endif
1.1       root     8621: 
                   8622: inline void pcbios_int_15h_cah()
                   8623: {
                   8624:        switch(REG8(AL)) {
1.1.1.22  root     8625:        case 0x00:
1.1       root     8626:                if(REG8(BL) > 0x3f) {
                   8627:                        REG8(AH) = 0x03;
1.1.1.3   root     8628:                        m_CF = 1;
1.1       root     8629:                } else if(REG8(BL) < 0x0e) {
                   8630:                        REG8(AH) = 0x04;
1.1.1.3   root     8631:                        m_CF = 1;
1.1       root     8632:                } else {
1.1.1.8   root     8633:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8634:                }
                   8635:                break;
1.1.1.22  root     8636:        case 0x01:
1.1       root     8637:                if(REG8(BL) > 0x3f) {
                   8638:                        REG8(AH) = 0x03;
1.1.1.3   root     8639:                        m_CF = 1;
1.1       root     8640:                } else if(REG8(BL) < 0x0e) {
                   8641:                        REG8(AH) = 0x04;
1.1.1.3   root     8642:                        m_CF = 1;
1.1       root     8643:                } else {
1.1.1.8   root     8644:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8645:                }
                   8646:                break;
                   8647:        default:
1.1.1.22  root     8648:                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     8649:                REG8(AH) = 0x86;
1.1.1.3   root     8650:                m_CF = 1;
1.1       root     8651:                break;
                   8652:        }
                   8653: }
                   8654: 
1.1.1.22  root     8655: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8656: {
1.1.1.22  root     8657:        switch(REG8(AL)) {
                   8658: #if defined(HAS_I386)
                   8659:        case 0x01:
                   8660:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8661:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8662:                break;
1.1.1.17  root     8663: #endif
1.1.1.22  root     8664:        default:
                   8665:                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));
                   8666:                REG8(AH) = 0x86;
                   8667:                m_CF = 1;
                   8668:                break;
                   8669:        }
                   8670: }
1.1.1.17  root     8671: 
1.1.1.55  root     8672: bool pcbios_is_key_buffer_empty()
                   8673: {
                   8674:        return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
                   8675: }
                   8676: 
1.1.1.51  root     8677: void pcbios_clear_key_buffer()
                   8678: {
                   8679:        key_buf_char->clear();
                   8680:        key_buf_scan->clear();
                   8681:        
                   8682:        // update key buffer
                   8683:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8684: }
                   8685: 
                   8686: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8687: {
                   8688:        // update key buffer
                   8689:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8690:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8691:        UINT16 next = tail + 2;
                   8692:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8693:                next = *(UINT16 *)(mem + 0x480);
                   8694:        }
                   8695:        if(next != head) {
                   8696:                *(UINT16 *)(mem + 0x41c) = next;
                   8697:                mem[0x400 + (tail++)] = key_char;
                   8698:                mem[0x400 + (tail++)] = key_scan;
1.1.1.55  root     8699:        } else {
                   8700:                // store to extra key buffer
                   8701:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8702:                        key_buf_char->write(key_char);
                   8703:                        key_buf_scan->write(key_scan);
                   8704:                }
1.1.1.51  root     8705:        }
                   8706: }
                   8707: 
                   8708: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8709: {
                   8710:        // update key buffer
                   8711:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8712:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8713:        UINT16 next = head + 2;
                   8714:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8715:                next = *(UINT16 *)(mem + 0x480);
                   8716:        }
                   8717:        if(head != tail) {
                   8718:                *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55  root     8719:                *key_char = mem[0x400 + (head++)];
                   8720:                *key_scan = mem[0x400 + (head++)];
                   8721:                
                   8722:                // restore from extra key buffer
                   8723:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8724:                        if(!key_buf_char->empty()) {
                   8725:                                pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
                   8726:                        }
                   8727:                }
                   8728:                return(true);
                   8729:        } else {
                   8730:                *key_char = 0x00;
                   8731:                *key_scan = 0x00;
                   8732:                return(false);
1.1.1.51  root     8733:        }
                   8734: }
                   8735: 
1.1.1.33  root     8736: void pcbios_update_key_code(bool wait)
1.1       root     8737: {
1.1.1.32  root     8738:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8739: #ifdef USE_SERVICE_THREAD
                   8740:                EnterCriticalSection(&key_buf_crit_sect);
                   8741: #endif
1.1.1.55  root     8742:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     8743: #ifdef USE_SERVICE_THREAD
                   8744:                LeaveCriticalSection(&key_buf_crit_sect);
                   8745: #endif
                   8746:                if(empty) {
1.1.1.32  root     8747:                        if(!update_key_buffer()) {
1.1.1.33  root     8748:                                if(wait) {
1.1.1.32  root     8749:                                        Sleep(10);
                   8750:                                } else {
                   8751:                                        maybe_idle();
                   8752:                                }
1.1.1.14  root     8753:                        }
                   8754:                }
1.1.1.34  root     8755:        }
                   8756:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8757: #ifdef USE_SERVICE_THREAD
                   8758:                EnterCriticalSection(&key_buf_crit_sect);
                   8759: #endif
1.1.1.51  root     8760:                int key_char, key_scan;
                   8761:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8762:                        key_code  = key_char << 0;
                   8763:                        key_code |= key_scan << 8;
1.1.1.35  root     8764:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8765:                }
                   8766:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8767:                        key_code |= key_char << 16;
                   8768:                        key_code |= key_scan << 24;
1.1.1.33  root     8769:                        key_recv |= 0xffff0000;
1.1.1.32  root     8770:                }
1.1.1.35  root     8771: #ifdef USE_SERVICE_THREAD
                   8772:                LeaveCriticalSection(&key_buf_crit_sect);
                   8773: #endif
1.1       root     8774:        }
                   8775: }
                   8776: 
1.1.1.35  root     8777: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8778: {
1.1.1.54  root     8779:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     8780:                pcbios_update_key_code(true);
1.1       root     8781:        }
1.1.1.33  root     8782:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8783:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8784:                        if(REG8(AH) == 0x10) {
                   8785:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8786:                        } else {
                   8787:                                key_code = ((key_code >> 16) & 0xff00);
                   8788:                        }
                   8789:                        key_recv >>= 16;
1.1       root     8790:                }
                   8791:        }
                   8792:        REG16(AX) = key_code & 0xffff;
                   8793:        key_code >>= 16;
1.1.1.33  root     8794:        key_recv >>= 16;
1.1.1.35  root     8795:        
                   8796: #ifdef USE_SERVICE_THREAD
                   8797:        service_exit = true;
                   8798: #endif
                   8799:        return(0);
                   8800: }
                   8801: 
                   8802: inline void pcbios_int_16h_00h()
                   8803: {
                   8804: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8805:        if(!in_service && !in_service_29h) {
                   8806:                start_service_loop(pcbios_int_16h_00h_thread);
                   8807:        } else {
                   8808: #endif
                   8809:                pcbios_int_16h_00h_thread(NULL);
                   8810:                REQUEST_HARDWRE_UPDATE();
                   8811: #ifdef USE_SERVICE_THREAD
                   8812:        }
1.1.1.35  root     8813: #endif
1.1       root     8814: }
                   8815: 
                   8816: inline void pcbios_int_16h_01h()
                   8817: {
1.1.1.33  root     8818:        if(key_recv == 0) {
                   8819:                pcbios_update_key_code(false);
1.1.1.5   root     8820:        }
1.1.1.33  root     8821:        if(key_recv != 0) {
                   8822:                UINT32 key_code_tmp = key_code;
                   8823:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8824:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8825:                                if(REG8(AH) == 0x11) {
                   8826:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8827:                                } else {
                   8828:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8829:                                }
                   8830:                        }
1.1       root     8831:                }
1.1.1.5   root     8832:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8833: #if defined(HAS_I386)
1.1.1.33  root     8834:                m_ZF = 0;
                   8835: #else
                   8836:                m_ZeroVal = 1;
                   8837: #endif
                   8838:        } else {
                   8839: #if defined(HAS_I386)
                   8840:                m_ZF = 1;
1.1.1.3   root     8841: #else
1.1.1.33  root     8842:                m_ZeroVal = 0;
1.1.1.3   root     8843: #endif
1.1.1.33  root     8844:        }
1.1       root     8845: }
                   8846: 
                   8847: inline void pcbios_int_16h_02h()
                   8848: {
                   8849:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8850:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8851:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8852:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8853:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8854:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8855:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8856:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8857: }
                   8858: 
                   8859: inline void pcbios_int_16h_03h()
                   8860: {
                   8861:        static UINT16 status = 0;
                   8862:        
                   8863:        switch(REG8(AL)) {
                   8864:        case 0x05:
                   8865:                status = REG16(BX);
                   8866:                break;
                   8867:        case 0x06:
                   8868:                REG16(BX) = status;
                   8869:                break;
                   8870:        default:
1.1.1.3   root     8871:                m_CF = 1;
1.1       root     8872:                break;
                   8873:        }
                   8874: }
                   8875: 
                   8876: inline void pcbios_int_16h_05h()
                   8877: {
1.1.1.32  root     8878:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8879: #ifdef USE_SERVICE_THREAD
                   8880:                EnterCriticalSection(&key_buf_crit_sect);
                   8881: #endif
1.1.1.51  root     8882:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     8883: #ifdef USE_SERVICE_THREAD
                   8884:                LeaveCriticalSection(&key_buf_crit_sect);
                   8885: #endif
1.1.1.32  root     8886:        }
1.1       root     8887:        REG8(AL) = 0x00;
                   8888: }
                   8889: 
                   8890: inline void pcbios_int_16h_12h()
                   8891: {
                   8892:        pcbios_int_16h_02h();
                   8893:        
                   8894:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8895:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8896:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8897:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8898:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8899:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8900:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8901:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8902: }
                   8903: 
                   8904: inline void pcbios_int_16h_13h()
                   8905: {
                   8906:        static UINT16 status = 0;
                   8907:        
                   8908:        switch(REG8(AL)) {
                   8909:        case 0x00:
                   8910:                status = REG16(DX);
                   8911:                break;
                   8912:        case 0x01:
                   8913:                REG16(DX) = status;
                   8914:                break;
                   8915:        default:
1.1.1.22  root     8916:                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     8917:                m_CF = 1;
1.1       root     8918:                break;
                   8919:        }
                   8920: }
                   8921: 
                   8922: inline void pcbios_int_16h_14h()
                   8923: {
                   8924:        static UINT8 status = 0;
                   8925:        
                   8926:        switch(REG8(AL)) {
                   8927:        case 0x00:
                   8928:        case 0x01:
                   8929:                status = REG8(AL);
                   8930:                break;
                   8931:        case 0x02:
                   8932:                REG8(AL) = status;
                   8933:                break;
                   8934:        default:
1.1.1.22  root     8935:                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     8936:                m_CF = 1;
1.1       root     8937:                break;
                   8938:        }
                   8939: }
                   8940: 
1.1.1.24  root     8941: inline void pcbios_int_16h_55h()
                   8942: {
                   8943:        switch(REG8(AL)) {
                   8944:        case 0x00:
                   8945:                // keyboard tsr is not present
                   8946:                break;
                   8947:        case 0xfe:
                   8948:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8949:                break;
                   8950:        case 0xff:
                   8951:                break;
                   8952:        default:
                   8953:                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));
                   8954:                m_CF = 1;
                   8955:                break;
                   8956:        }
                   8957: }
                   8958: 
1.1.1.30  root     8959: inline void pcbios_int_16h_6fh()
                   8960: {
                   8961:        switch(REG8(AL)) {
                   8962:        case 0x00:
                   8963:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8964:                break;
                   8965:        default:
                   8966:                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));
                   8967:                m_CF = 1;
                   8968:                break;
                   8969:        }
                   8970: }
                   8971: 
1.1.1.37  root     8972: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8973: {
                   8974:        UINT8 hi = jis >> 8;
                   8975:        UINT8 lo = jis & 0xff;
                   8976:        
                   8977:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8978:        hi = (hi - 0x21) / 2 + 0x81;
                   8979:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8980:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8981:        
                   8982:        return((hi << 8) + lo);
                   8983: }
                   8984: 
                   8985: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8986: {
                   8987:        UINT8 hi = sjis >> 8;
                   8988:        UINT8 lo = sjis & 0xff;
                   8989:        
                   8990:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8991:                return(0x2121);
                   8992:        }
                   8993:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8994:                return(0x2121);
                   8995:        }
                   8996:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8997:                // gaiji
                   8998:                if(lo >= 0x40 && lo <= 0x7e) {
                   8999:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   9000:                }
                   9001:                if(lo >= 0x80 && lo <= 0x9e) {
                   9002:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   9003:                }
                   9004:                if(lo >= 0x9f && lo <= 0xfc) {
                   9005:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   9006:                }
                   9007:        }
                   9008:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   9009:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   9010:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   9011:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   9012:        
                   9013:        return((hi << 8) + lo);
                   9014: }
                   9015: 
1.1.1.38  root     9016: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   9017: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   9018: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     9019: 
                   9020: void pcbios_printer_out(int c, UINT8 data)
                   9021: {
                   9022:        if(pio[c].conv_mode) {
                   9023:                if(pio[c].sjis_hi != 0) {
                   9024:                        if(!pio[c].jis_mode) {
                   9025:                                printer_out(c, 0x1c);
                   9026:                                printer_out(c, 0x26);
                   9027:                                pio[c].jis_mode = true;
                   9028:                        }
                   9029:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   9030:                        printer_out(c, jis >> 8);
                   9031:                        printer_out(c, jis & 0xff);
                   9032:                        pio[c].sjis_hi = 0;
                   9033:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   9034:                        printer_out(c, data);
                   9035:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9036:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9037:                        }
                   9038:                        pio[c].esc_len++;
                   9039:                        
                   9040:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9041:                        case 0x33: // 1Bh 33h XX
                   9042:                        case 0x4a: // 1Bh 4Ah XX
                   9043:                        case 0x4e: // 1Bh 4Eh XX
                   9044:                        case 0x51: // 1Bh 51h XX
                   9045:                        case 0x55: // 1Bh 55h XX
                   9046:                        case 0x6c: // 1Bh 6Ch XX
                   9047:                        case 0x71: // 1Bh 71h XX
                   9048:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     9049:                                if(pio[c].esc_len == 3) {
                   9050:                                        pio[c].esc_buf[0] = 0x00;
                   9051:                                }
                   9052:                                break;
1.1.1.38  root     9053:                        case 0x24: // 1Bh 24h XX XX
                   9054:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     9055:                                if(pio[c].esc_len == 4) {
                   9056:                                        pio[c].esc_buf[0] = 0x00;
                   9057:                                }
                   9058:                                break;
1.1.1.38  root     9059:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     9060:                                if(pio[c].esc_len >= 3) {
                   9061:                                        switch(pio[c].esc_buf[2]) {
                   9062:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   9063:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   9064:                                                        pio[c].esc_buf[0] = 0x00;
                   9065:                                                }
                   9066:                                                break;
                   9067:                                        case 32: case 33: case 38: case 39: case 40:
                   9068:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   9069:                                                        pio[c].esc_buf[0] = 0x00;
                   9070:                                                }
                   9071:                                                break;
1.1.1.38  root     9072:                                        case 71: case 72: case 73:
                   9073:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   9074:                                                        pio[c].esc_buf[0] = 0x00;
                   9075:                                                }
                   9076:                                                break;
1.1.1.37  root     9077:                                        default:
                   9078:                                                pio[c].esc_buf[0] = 0x00;
                   9079:                                                break;
                   9080:                                        }
                   9081:                                }
                   9082:                                break;
1.1.1.38  root     9083:                        case 0x40: // 1Bh 40h
1.1.1.37  root     9084:                                if(pio[c].jis_mode) {
                   9085:                                        printer_out(c, 0x1c);
                   9086:                                        printer_out(c, 0x2e);
                   9087:                                        pio[c].jis_mode = false;
                   9088:                                }
                   9089:                                pio[c].esc_buf[0] = 0x00;
                   9090:                                break;
1.1.1.38  root     9091:                        case 0x42: // 1Bh 42h data 00h
                   9092:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9093:                                if(pio[c].esc_len >= 3 && data == 0) {
                   9094:                                        pio[c].esc_buf[0] = 0x00;
                   9095:                                }
                   9096:                                break;
1.1.1.38  root     9097:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     9098:                                if(pio[c].esc_len >= 3 && data != 0) {
                   9099:                                        pio[c].esc_buf[0] = 0x00;
                   9100:                                }
                   9101:                                break;
1.1.1.38  root     9102:                        default: // 1Bh XX
1.1.1.37  root     9103:                                pio[c].esc_buf[0] = 0x00;
                   9104:                                break;
                   9105:                        }
                   9106:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   9107:                        printer_out(c, data);
                   9108:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9109:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9110:                        }
                   9111:                        pio[c].esc_len++;
                   9112:                        
                   9113:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9114:                        case 0x21: // 1Ch 21h XX
                   9115:                        case 0x2d: // 1Ch 2Dh XX
                   9116:                        case 0x57: // 1Ch 57h XX
                   9117:                        case 0x6b: // 1Ch 6Bh XX
                   9118:                        case 0x72: // 1Ch 72h XX
                   9119:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     9120:                                if(pio[c].esc_len == 3) {
                   9121:                                        pio[c].esc_buf[0] = 0x00;
                   9122:                                }
                   9123:                                break;
1.1.1.38  root     9124:                        case 0x26: // 1Ch 26h
1.1.1.37  root     9125:                                pio[c].jis_mode = true;
                   9126:                                pio[c].esc_buf[0] = 0x00;
                   9127:                                break;
1.1.1.38  root     9128:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     9129:                                pio[c].jis_mode = false;
                   9130:                                pio[c].esc_buf[0] = 0x00;
                   9131:                                break;
1.1.1.38  root     9132:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     9133:                                if(pio[c].esc_len == 76) {
                   9134:                                        pio[c].esc_buf[0] = 0x00;
                   9135:                                }
                   9136:                                break;
1.1.1.38  root     9137:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9138:                                if(pio[c].esc_len == 6) {
                   9139:                                        pio[c].esc_buf[0] = 0x00;
                   9140:                                }
                   9141:                                break;
1.1.1.38  root     9142:                        case 0x53: // 1Ch 53h XX XX
                   9143:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     9144:                                if(pio[c].esc_len == 4) {
                   9145:                                        pio[c].esc_buf[0] = 0x00;
                   9146:                                }
                   9147:                                break;
1.1.1.38  root     9148:                        default: // 1Ch XX
1.1.1.37  root     9149:                                pio[c].esc_buf[0] = 0x00;
                   9150:                                break;
                   9151:                        }
                   9152:                } else if(data == 0x1b || data == 0x1c) {
                   9153:                        printer_out(c, data);
                   9154:                        pio[c].esc_buf[0] = data;
                   9155:                        pio[c].esc_len = 1;
                   9156:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   9157:                        pio[c].sjis_hi = data;
                   9158:                } else {
                   9159:                        if(pio[c].jis_mode) {
                   9160:                                printer_out(c, 0x1c);
                   9161:                                printer_out(c, 0x2e);
                   9162:                                pio[c].jis_mode = false;
                   9163:                        }
                   9164:                        printer_out(c, data);
                   9165:                }
                   9166:        } else {
                   9167:                if(pio[c].jis_mode) {
                   9168:                        printer_out(c, 0x1c);
                   9169:                        printer_out(c, 0x2e);
                   9170:                        pio[c].jis_mode = false;
                   9171:                }
                   9172:                printer_out(c, data);
                   9173:        }
                   9174: }
                   9175: 
                   9176: inline void pcbios_int_17h_00h()
                   9177: {
                   9178:        if(REG16(DX) < 3) {
                   9179:                pcbios_printer_out(REG16(DX), REG8(AL));
                   9180:                REG8(AH) = 0xd0;
                   9181:        }
                   9182: }
                   9183: 
                   9184: inline void pcbios_int_17h_01h()
                   9185: {
                   9186:        if(REG16(DX) < 3) {
                   9187:                REG8(AH) = 0xd0;
                   9188:        }
                   9189: }
                   9190: 
                   9191: inline void pcbios_int_17h_02h()
                   9192: {
                   9193:        if(REG16(DX) < 3) {
                   9194:                REG8(AH) = 0xd0;
                   9195:        }
                   9196: }
                   9197: 
                   9198: inline void pcbios_int_17h_03h()
                   9199: {
                   9200:        switch(REG8(AL)) {
                   9201:        case 0x00:
                   9202:                if(REG16(DX) < 3) {
                   9203:                        if(pio[REG16(DX)].jis_mode) {
                   9204:                                printer_out(REG16(DX), 0x1c);
                   9205:                                printer_out(REG16(DX), 0x2e);
                   9206:                                pio[REG16(DX)].jis_mode = false;
                   9207:                        }
                   9208:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   9209:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   9210:                        }
                   9211:                        REG16(CX) = 0x0000;
                   9212:                        REG8(AH) = 0xd0;
                   9213:                }
                   9214:                break;
                   9215:        default:
                   9216:                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));
                   9217:                break;
                   9218:        }
                   9219: }
                   9220: 
                   9221: inline void pcbios_int_17h_50h()
                   9222: {
                   9223:        switch(REG8(AL)) {
                   9224:        case 0x00:
                   9225:                if(REG16(DX) < 3) {
                   9226:                        if(REG16(BX) = 0x0001) {
                   9227:                                pio[REG16(DX)].conv_mode = false;
                   9228:                                REG8(AL) = 0x00;
                   9229:                        } else if(REG16(BX) = 0x0051) {
                   9230:                                pio[REG16(DX)].conv_mode = true;
                   9231:                                REG8(AL) = 0x00;
                   9232:                        } else {
                   9233:                                REG8(AL) = 0x01;
                   9234:                        }
                   9235:                } else {
                   9236:                        REG8(AL) = 0x02;
                   9237:                }
                   9238:                break;
                   9239:        case 0x01:
                   9240:                if(REG16(DX) < 3) {
                   9241:                        if(pio[REG16(DX)].conv_mode) {
                   9242:                                REG16(BX) = 0x0051;
                   9243:                        } else {
                   9244:                                REG16(BX) = 0x0001;
                   9245:                        }
                   9246:                        REG8(AL) = 0x00;
                   9247:                } else {
                   9248:                        REG8(AL) = 0x02;
                   9249:                }
                   9250:                break;
                   9251:        default:
                   9252:                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));
                   9253:                break;
                   9254:        }
                   9255: }
                   9256: 
                   9257: inline void pcbios_int_17h_51h()
                   9258: {
                   9259:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9260:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9261:        } else {
                   9262:                REG16(DX) = 0x0000;
                   9263:        }
                   9264: }
                   9265: 
                   9266: inline void pcbios_int_17h_52h()
                   9267: {
                   9268:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9269:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9270:        } else {
                   9271:                REG16(DX) = 0x0000;
                   9272:        }
                   9273: }
                   9274: 
                   9275: inline void pcbios_int_17h_84h()
                   9276: {
                   9277:        if(REG16(DX) < 3) {
                   9278:                if(pio[REG16(DX)].jis_mode) {
                   9279:                        printer_out(REG16(DX), 0x1c);
                   9280:                        printer_out(REG16(DX), 0x2e);
                   9281:                        pio[REG16(DX)].jis_mode = false;
                   9282:                }
                   9283:                printer_out(REG16(DX), REG8(AL));
                   9284:                REG8(AH) = 0xd0;
                   9285:        }
                   9286: }
                   9287: 
                   9288: inline void pcbios_int_17h_85h()
                   9289: {
                   9290:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9291: }
                   9292: 
1.1       root     9293: inline void pcbios_int_1ah_00h()
                   9294: {
1.1.1.19  root     9295:        pcbios_update_daily_timer_counter(timeGetTime());
                   9296:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9297:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9298:        REG8(AL) = mem[0x470];
                   9299:        mem[0x470] = 0;
1.1       root     9300: }
                   9301: 
                   9302: inline int to_bcd(int t)
                   9303: {
                   9304:        int u = (t % 100) / 10;
                   9305:        return (u << 4) | (t % 10);
                   9306: }
                   9307: 
                   9308: inline void pcbios_int_1ah_02h()
                   9309: {
                   9310:        SYSTEMTIME time;
                   9311:        
                   9312:        GetLocalTime(&time);
                   9313:        REG8(CH) = to_bcd(time.wHour);
                   9314:        REG8(CL) = to_bcd(time.wMinute);
                   9315:        REG8(DH) = to_bcd(time.wSecond);
                   9316:        REG8(DL) = 0x00;
                   9317: }
                   9318: 
                   9319: inline void pcbios_int_1ah_04h()
                   9320: {
                   9321:        SYSTEMTIME time;
                   9322:        
                   9323:        GetLocalTime(&time);
                   9324:        REG8(CH) = to_bcd(time.wYear / 100);
                   9325:        REG8(CL) = to_bcd(time.wYear);
                   9326:        REG8(DH) = to_bcd(time.wMonth);
                   9327:        REG8(DL) = to_bcd(time.wDay);
                   9328: }
                   9329: 
                   9330: inline void pcbios_int_1ah_0ah()
                   9331: {
                   9332:        SYSTEMTIME time;
                   9333:        FILETIME file_time;
                   9334:        WORD dos_date, dos_time;
                   9335:        
                   9336:        GetLocalTime(&time);
                   9337:        SystemTimeToFileTime(&time, &file_time);
                   9338:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9339:        REG16(CX) = dos_date;
                   9340: }
                   9341: 
                   9342: // msdos system call
                   9343: 
1.1.1.43  root     9344: inline void msdos_int_21h_56h(int lfn);
                   9345: 
1.1       root     9346: inline void msdos_int_21h_00h()
                   9347: {
1.1.1.3   root     9348:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9349: }
                   9350: 
1.1.1.35  root     9351: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9352: {
                   9353:        REG8(AL) = msdos_getche();
1.1.1.33  root     9354:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9355:        
1.1.1.35  root     9356: #ifdef USE_SERVICE_THREAD
                   9357:        service_exit = true;
                   9358: #endif
                   9359:        return(0);
                   9360: }
                   9361: 
                   9362: inline void msdos_int_21h_01h()
                   9363: {
                   9364: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9365:        if(!in_service && !in_service_29h &&
1.1.1.58  root     9366:           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9367:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9368:                // msdos_putch() will be used in this service
                   9369:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9370:                start_service_loop(msdos_int_21h_01h_thread);
                   9371:        } else {
                   9372: #endif
                   9373:                msdos_int_21h_01h_thread(NULL);
                   9374:                REQUEST_HARDWRE_UPDATE();
                   9375: #ifdef USE_SERVICE_THREAD
                   9376:        }
1.1.1.35  root     9377: #endif
1.1       root     9378: }
                   9379: 
                   9380: inline void msdos_int_21h_02h()
                   9381: {
1.1.1.33  root     9382:        UINT8 data = REG8(DL);
                   9383:        msdos_putch(data);
                   9384:        REG8(AL) = data;
                   9385:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9386: }
                   9387: 
                   9388: inline void msdos_int_21h_03h()
                   9389: {
                   9390:        REG8(AL) = msdos_aux_in();
                   9391: }
                   9392: 
                   9393: inline void msdos_int_21h_04h()
                   9394: {
                   9395:        msdos_aux_out(REG8(DL));
                   9396: }
                   9397: 
                   9398: inline void msdos_int_21h_05h()
                   9399: {
                   9400:        msdos_prn_out(REG8(DL));
                   9401: }
                   9402: 
                   9403: inline void msdos_int_21h_06h()
                   9404: {
                   9405:        if(REG8(DL) == 0xff) {
                   9406:                if(msdos_kbhit()) {
                   9407:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9408: #if defined(HAS_I386)
                   9409:                        m_ZF = 0;
                   9410: #else
                   9411:                        m_ZeroVal = 1;
                   9412: #endif
1.1       root     9413:                } else {
                   9414:                        REG8(AL) = 0;
1.1.1.3   root     9415: #if defined(HAS_I386)
                   9416:                        m_ZF = 1;
                   9417: #else
                   9418:                        m_ZeroVal = 0;
                   9419: #endif
1.1.1.14  root     9420:                        maybe_idle();
1.1       root     9421:                }
                   9422:        } else {
1.1.1.33  root     9423:                UINT8 data = REG8(DL);
                   9424:                msdos_putch(data);
                   9425:                REG8(AL) = data;
1.1       root     9426:        }
                   9427: }
                   9428: 
1.1.1.35  root     9429: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9430: {
                   9431:        REG8(AL) = msdos_getch();
1.1.1.26  root     9432:        
1.1.1.35  root     9433: #ifdef USE_SERVICE_THREAD
                   9434:        service_exit = true;
                   9435: #endif
                   9436:        return(0);
1.1       root     9437: }
                   9438: 
1.1.1.35  root     9439: inline void msdos_int_21h_07h()
                   9440: {
                   9441: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9442:        if(!in_service && !in_service_29h) {
                   9443:                start_service_loop(msdos_int_21h_07h_thread);
                   9444:        } else {
                   9445: #endif
                   9446:                msdos_int_21h_07h_thread(NULL);
                   9447:                REQUEST_HARDWRE_UPDATE();
                   9448: #ifdef USE_SERVICE_THREAD
                   9449:        }
1.1.1.35  root     9450: #endif
                   9451: }
                   9452: 
                   9453: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9454: {
                   9455:        REG8(AL) = msdos_getch();
1.1.1.33  root     9456:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9457:        
1.1.1.35  root     9458: #ifdef USE_SERVICE_THREAD
                   9459:        service_exit = true;
                   9460: #endif
                   9461:        return(0);
                   9462: }
                   9463: 
                   9464: inline void msdos_int_21h_08h()
                   9465: {
                   9466: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9467:        if(!in_service && !in_service_29h) {
                   9468:                start_service_loop(msdos_int_21h_08h_thread);
                   9469:        } else {
                   9470: #endif
                   9471:                msdos_int_21h_08h_thread(NULL);
                   9472:                REQUEST_HARDWRE_UPDATE();
                   9473: #ifdef USE_SERVICE_THREAD
                   9474:        }
1.1.1.35  root     9475: #endif
1.1       root     9476: }
                   9477: 
                   9478: inline void msdos_int_21h_09h()
                   9479: {
1.1.1.21  root     9480:        msdos_stdio_reopen();
                   9481:        
1.1.1.20  root     9482:        process_t *process = msdos_process_info_get(current_psp);
                   9483:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9484:        
1.1.1.14  root     9485:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9486:        int len = 0;
1.1       root     9487:        
1.1.1.14  root     9488:        while(str[len] != '$' && len < 0x10000) {
                   9489:                len++;
                   9490:        }
1.1.1.20  root     9491:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9492:                // stdout is redirected to file
1.1.1.20  root     9493:                msdos_write(fd, str, len);
1.1       root     9494:        } else {
                   9495:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9496:                        msdos_putch(str[i]);
1.1       root     9497:                }
                   9498:        }
1.1.1.33  root     9499:        REG8(AL) = '$';
                   9500:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9501: }
                   9502: 
1.1.1.35  root     9503: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9504: {
1.1.1.3   root     9505:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9506:        int max = mem[ofs] - 1;
                   9507:        UINT8 *buf = mem + ofs + 2;
                   9508:        int chr, p = 0;
                   9509:        
                   9510:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9511:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9512:                        p = 0;
1.1.1.33  root     9513:                        msdos_putch(0x03);
                   9514:                        msdos_putch(0x0d);
                   9515:                        msdos_putch(0x0a);
1.1.1.26  root     9516:                        break;
1.1.1.33  root     9517:                } else if(ctrl_break_pressed) {
                   9518:                        // skip this byte
1.1.1.26  root     9519:                } else if(chr == 0x00) {
1.1       root     9520:                        // skip 2nd byte
                   9521:                        msdos_getch();
                   9522:                } else if(chr == 0x08) {
                   9523:                        // back space
                   9524:                        if(p > 0) {
                   9525:                                p--;
1.1.1.20  root     9526:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9527:                                        msdos_putch(0x08);
                   9528:                                        msdos_putch(0x08);
                   9529:                                        msdos_putch(0x20);
                   9530:                                        msdos_putch(0x20);
                   9531:                                        msdos_putch(0x08);
                   9532:                                        msdos_putch(0x08);
1.1.1.36  root     9533:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9534:                                        p--;
                   9535:                                        msdos_putch(0x08);
                   9536:                                        msdos_putch(0x08);
                   9537:                                        msdos_putch(0x20);
                   9538:                                        msdos_putch(0x20);
                   9539:                                        msdos_putch(0x08);
                   9540:                                        msdos_putch(0x08);
1.1.1.34  root     9541:                                } else {
                   9542:                                        msdos_putch(0x08);
                   9543:                                        msdos_putch(0x20);
                   9544:                                        msdos_putch(0x08);
                   9545:                                }
                   9546:                        }
                   9547:                } else if(chr == 0x1b) {
                   9548:                        // escape
                   9549:                        while(p > 0) {
                   9550:                                p--;
                   9551:                                if(msdos_ctrl_code_check(buf[p])) {
                   9552:                                        msdos_putch(0x08);
                   9553:                                        msdos_putch(0x08);
                   9554:                                        msdos_putch(0x20);
                   9555:                                        msdos_putch(0x20);
                   9556:                                        msdos_putch(0x08);
                   9557:                                        msdos_putch(0x08);
1.1.1.20  root     9558:                                } else {
1.1.1.34  root     9559:                                        msdos_putch(0x08);
                   9560:                                        msdos_putch(0x20);
                   9561:                                        msdos_putch(0x08);
1.1.1.20  root     9562:                                }
1.1       root     9563:                        }
                   9564:                } else if(p < max) {
                   9565:                        buf[p++] = chr;
                   9566:                        msdos_putch(chr);
                   9567:                }
                   9568:        }
                   9569:        buf[p] = 0x0d;
                   9570:        mem[ofs + 1] = p;
1.1.1.33  root     9571:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9572:        
1.1.1.35  root     9573: #ifdef USE_SERVICE_THREAD
                   9574:        service_exit = true;
                   9575: #endif
                   9576:        return(0);
                   9577: }
                   9578: 
                   9579: inline void msdos_int_21h_0ah()
                   9580: {
                   9581:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9582: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9583:                if(!in_service && !in_service_29h &&
1.1.1.58  root     9584:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9585:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9586:                        // msdos_putch() will be used in this service
                   9587:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9588:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9589:                } else {
                   9590: #endif
                   9591:                        msdos_int_21h_0ah_thread(NULL);
                   9592:                        REQUEST_HARDWRE_UPDATE();
                   9593: #ifdef USE_SERVICE_THREAD
                   9594:                }
1.1.1.35  root     9595: #endif
                   9596:        }
1.1       root     9597: }
                   9598: 
                   9599: inline void msdos_int_21h_0bh()
                   9600: {
                   9601:        if(msdos_kbhit()) {
                   9602:                REG8(AL) = 0xff;
                   9603:        } else {
                   9604:                REG8(AL) = 0x00;
1.1.1.14  root     9605:                maybe_idle();
1.1       root     9606:        }
1.1.1.33  root     9607:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9608: }
                   9609: 
                   9610: inline void msdos_int_21h_0ch()
                   9611: {
                   9612:        // clear key buffer
1.1.1.21  root     9613:        msdos_stdio_reopen();
                   9614:        
1.1.1.20  root     9615:        process_t *process = msdos_process_info_get(current_psp);
                   9616:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9617:        
                   9618:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9619:                // stdin is redirected to file
                   9620:        } else {
                   9621:                while(msdos_kbhit()) {
                   9622:                        msdos_getch();
                   9623:                }
                   9624:        }
                   9625:        
                   9626:        switch(REG8(AL)) {
                   9627:        case 0x01:
                   9628:                msdos_int_21h_01h();
                   9629:                break;
                   9630:        case 0x06:
                   9631:                msdos_int_21h_06h();
                   9632:                break;
                   9633:        case 0x07:
                   9634:                msdos_int_21h_07h();
                   9635:                break;
                   9636:        case 0x08:
                   9637:                msdos_int_21h_08h();
                   9638:                break;
                   9639:        case 0x0a:
                   9640:                msdos_int_21h_0ah();
                   9641:                break;
                   9642:        default:
1.1.1.48  root     9643:                // the buffer is flushed but no input is attempted
1.1       root     9644:                break;
                   9645:        }
                   9646: }
                   9647: 
                   9648: inline void msdos_int_21h_0dh()
                   9649: {
                   9650: }
                   9651: 
                   9652: inline void msdos_int_21h_0eh()
                   9653: {
                   9654:        if(REG8(DL) < 26) {
                   9655:                _chdrive(REG8(DL) + 1);
                   9656:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9657:                msdos_sda_update(current_psp);
1.1       root     9658:        }
                   9659:        REG8(AL) = 26; // zdrive
                   9660: }
                   9661: 
1.1.1.14  root     9662: inline void msdos_int_21h_0fh()
                   9663: {
                   9664:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9665:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9666:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9667:        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     9668:        
1.1.1.14  root     9669:        if(hFile == INVALID_HANDLE_VALUE) {
                   9670:                REG8(AL) = 0xff;
                   9671:        } else {
                   9672:                REG8(AL) = 0;
                   9673:                fcb->current_block = 0;
                   9674:                fcb->record_size = 128;
                   9675:                fcb->file_size = GetFileSize(hFile, NULL);
                   9676:                fcb->handle = hFile;
                   9677:                fcb->cur_record = 0;
                   9678:        }
                   9679: }
                   9680: 
                   9681: inline void msdos_int_21h_10h()
                   9682: {
                   9683:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9684:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9685:        
                   9686:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9687: }
                   9688: 
1.1       root     9689: inline void msdos_int_21h_11h()
                   9690: {
1.1.1.3   root     9691:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9692:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9693:        
                   9694:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9695:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9696:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9697:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9698:        const char *path = msdos_fcb_path(fcb);
1.1       root     9699:        WIN32_FIND_DATA fd;
                   9700:        
1.1.1.13  root     9701:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9702:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9703:                FindClose(dtainfo->find_handle);
                   9704:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9705:        }
                   9706:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9707:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9708:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9709:        
1.1.1.14  root     9710:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9711:                dtainfo->allowable_mask &= ~8;
1.1       root     9712:        }
1.1.1.14  root     9713:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9714:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9715:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9716:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9717:                                FindClose(dtainfo->find_handle);
                   9718:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9719:                                break;
                   9720:                        }
                   9721:                }
                   9722:        }
1.1.1.13  root     9723:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9724:                if(ext_fcb->flag == 0xff) {
                   9725:                        ext_find->flag = 0xff;
                   9726:                        memset(ext_find->reserved, 0, 5);
                   9727:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9728:                }
                   9729:                find->drive = _getdrive();
1.1.1.13  root     9730:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9731:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9732:                find->nt_res = 0;
                   9733:                msdos_find_file_conv_local_time(&fd);
                   9734:                find->create_time_ms = 0;
                   9735:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9736:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9737:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9738:                find->cluster_hi = find->cluster_lo = 0;
                   9739:                find->file_size = fd.nFileSizeLow;
                   9740:                REG8(AL) = 0x00;
1.1.1.14  root     9741:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9742:                if(ext_fcb->flag == 0xff) {
                   9743:                        ext_find->flag = 0xff;
                   9744:                        memset(ext_find->reserved, 0, 5);
                   9745:                        ext_find->attribute = 8;
                   9746:                }
                   9747:                find->drive = _getdrive();
                   9748:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9749:                find->attribute = 8;
                   9750:                find->nt_res = 0;
                   9751:                msdos_find_file_conv_local_time(&fd);
                   9752:                find->create_time_ms = 0;
                   9753:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9754:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9755:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9756:                find->cluster_hi = find->cluster_lo = 0;
                   9757:                find->file_size = 0;
1.1.1.14  root     9758:                dtainfo->allowable_mask &= ~8;
1.1       root     9759:                REG8(AL) = 0x00;
                   9760:        } else {
                   9761:                REG8(AL) = 0xff;
                   9762:        }
                   9763: }
                   9764: 
                   9765: inline void msdos_int_21h_12h()
                   9766: {
1.1.1.3   root     9767:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9768: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9769:        
                   9770:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9771:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9772:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9773:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9774:        WIN32_FIND_DATA fd;
                   9775:        
1.1.1.13  root     9776:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9777:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9778:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9779:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9780:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9781:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9782:                                        FindClose(dtainfo->find_handle);
                   9783:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9784:                                        break;
                   9785:                                }
                   9786:                        }
                   9787:                } else {
1.1.1.13  root     9788:                        FindClose(dtainfo->find_handle);
                   9789:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9790:                }
                   9791:        }
1.1.1.13  root     9792:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9793:                if(ext_fcb->flag == 0xff) {
                   9794:                        ext_find->flag = 0xff;
                   9795:                        memset(ext_find->reserved, 0, 5);
                   9796:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9797:                }
                   9798:                find->drive = _getdrive();
1.1.1.13  root     9799:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9800:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9801:                find->nt_res = 0;
                   9802:                msdos_find_file_conv_local_time(&fd);
                   9803:                find->create_time_ms = 0;
                   9804:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9805:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9806:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9807:                find->cluster_hi = find->cluster_lo = 0;
                   9808:                find->file_size = fd.nFileSizeLow;
                   9809:                REG8(AL) = 0x00;
1.1.1.14  root     9810:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9811:                if(ext_fcb->flag == 0xff) {
                   9812:                        ext_find->flag = 0xff;
                   9813:                        memset(ext_find->reserved, 0, 5);
                   9814:                        ext_find->attribute = 8;
                   9815:                }
                   9816:                find->drive = _getdrive();
                   9817:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9818:                find->attribute = 8;
                   9819:                find->nt_res = 0;
                   9820:                msdos_find_file_conv_local_time(&fd);
                   9821:                find->create_time_ms = 0;
                   9822:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9823:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9824:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9825:                find->cluster_hi = find->cluster_lo = 0;
                   9826:                find->file_size = 0;
1.1.1.14  root     9827:                dtainfo->allowable_mask &= ~8;
1.1       root     9828:                REG8(AL) = 0x00;
                   9829:        } else {
                   9830:                REG8(AL) = 0xff;
                   9831:        }
                   9832: }
                   9833: 
                   9834: inline void msdos_int_21h_13h()
                   9835: {
1.1.1.3   root     9836:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9837:                REG8(AL) = 0xff;
                   9838:        } else {
                   9839:                REG8(AL) = 0x00;
                   9840:        }
                   9841: }
                   9842: 
1.1.1.16  root     9843: inline void msdos_int_21h_14h()
                   9844: {
                   9845:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9846:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9847:        process_t *process = msdos_process_info_get(current_psp);
                   9848:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9849:        DWORD num = 0;
                   9850:        
                   9851:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9852:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9853:                REG8(AL) = 1;
                   9854:        } else {
                   9855:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9856:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9857:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9858:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9859:        }
                   9860: }
                   9861: 
                   9862: inline void msdos_int_21h_15h()
1.1.1.14  root     9863: {
                   9864:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9865:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9866:        process_t *process = msdos_process_info_get(current_psp);
                   9867:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9868:        DWORD num = 0;
1.1.1.14  root     9869:        
1.1.1.16  root     9870:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9871:                REG8(AL) = 1;
                   9872:        } else {
                   9873:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9874:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9875:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9876:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9877:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9878:        }
                   9879: }
                   9880: 
                   9881: inline void msdos_int_21h_16h()
                   9882: {
                   9883:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9884:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9885:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9886:        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     9887:        
1.1.1.14  root     9888:        if(hFile == INVALID_HANDLE_VALUE) {
                   9889:                REG8(AL) = 0xff;
                   9890:        } else {
                   9891:                REG8(AL) = 0;
                   9892:                fcb->current_block = 0;
                   9893:                fcb->record_size = 128;
                   9894:                fcb->file_size = 0;
                   9895:                fcb->handle = hFile;
                   9896:                fcb->cur_record = 0;
                   9897:        }
                   9898: }
                   9899: 
1.1.1.16  root     9900: inline void msdos_int_21h_17h()
                   9901: {
                   9902:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9903:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     9904: //     const char *path_src = msdos_fcb_path(fcb_src);
                   9905:        char path_src[MAX_PATH];
                   9906:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   9907:        
1.1.1.16  root     9908:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9909:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     9910: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   9911:        char path_dst[MAX_PATH];
                   9912:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9913:        
                   9914:        if(rename(path_src, path_dst)) {
                   9915:                REG8(AL) = 0xff;
                   9916:        } else {
                   9917:                REG8(AL) = 0;
                   9918:        }
                   9919: }
                   9920: 
1.1       root     9921: inline void msdos_int_21h_18h()
                   9922: {
                   9923:        REG8(AL) = 0x00;
                   9924: }
                   9925: 
                   9926: inline void msdos_int_21h_19h()
                   9927: {
                   9928:        REG8(AL) = _getdrive() - 1;
                   9929: }
                   9930: 
                   9931: inline void msdos_int_21h_1ah()
                   9932: {
                   9933:        process_t *process = msdos_process_info_get(current_psp);
                   9934:        
                   9935:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9936:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9937:        msdos_sda_update(current_psp);
1.1       root     9938: }
                   9939: 
                   9940: inline void msdos_int_21h_1bh()
                   9941: {
                   9942:        int drive_num = _getdrive() - 1;
                   9943:        UINT16 seg, ofs;
                   9944:        
                   9945:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9946:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9947:                REG8(AL) = dpb->highest_sector_num + 1;
                   9948:                REG16(CX) = dpb->bytes_per_sector;
                   9949:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9950:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9951:        } else {
                   9952:                REG8(AL) = 0xff;
1.1.1.3   root     9953:                m_CF = 1;
1.1       root     9954:        }
                   9955: 
                   9956: }
                   9957: 
                   9958: inline void msdos_int_21h_1ch()
                   9959: {
1.1.1.41  root     9960:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9961:        UINT16 seg, ofs;
                   9962:        
                   9963:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9964:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9965:                REG8(AL) = dpb->highest_sector_num + 1;
                   9966:                REG16(CX) = dpb->bytes_per_sector;
                   9967:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9968:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9969:        } else {
                   9970:                REG8(AL) = 0xff;
1.1.1.3   root     9971:                m_CF = 1;
1.1       root     9972:        }
                   9973: 
                   9974: }
                   9975: 
                   9976: inline void msdos_int_21h_1dh()
                   9977: {
                   9978:        REG8(AL) = 0;
                   9979: }
                   9980: 
                   9981: inline void msdos_int_21h_1eh()
                   9982: {
                   9983:        REG8(AL) = 0;
                   9984: }
                   9985: 
                   9986: inline void msdos_int_21h_1fh()
                   9987: {
                   9988:        int drive_num = _getdrive() - 1;
                   9989:        UINT16 seg, ofs;
                   9990:        
                   9991:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9992:                REG8(AL) = 0;
1.1.1.3   root     9993:                SREG(DS) = seg;
                   9994:                i386_load_segment_descriptor(DS);
1.1       root     9995:                REG16(BX) = ofs;
                   9996:        } else {
                   9997:                REG8(AL) = 0xff;
1.1.1.3   root     9998:                m_CF = 1;
1.1       root     9999:        }
                   10000: }
                   10001: 
                   10002: inline void msdos_int_21h_20h()
                   10003: {
                   10004:        REG8(AL) = 0;
                   10005: }
                   10006: 
1.1.1.14  root     10007: inline void msdos_int_21h_21h()
                   10008: {
                   10009:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10010:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10011:        
                   10012:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10013:                REG8(AL) = 1;
                   10014:        } else {
                   10015:                process_t *process = msdos_process_info_get(current_psp);
                   10016:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10017:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     10018:                DWORD num = 0;
1.1.1.14  root     10019:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10020:                        REG8(AL) = 1;
                   10021:                } else {
                   10022:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10023:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     10024:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     10025:                }
                   10026:        }
                   10027: }
                   10028: 
                   10029: inline void msdos_int_21h_22h()
                   10030: {
                   10031:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10032:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10033:        
                   10034:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10035:                REG8(AL) = 0xff;
                   10036:        } else {
                   10037:                process_t *process = msdos_process_info_get(current_psp);
                   10038:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     10039:                DWORD num = 0;
1.1.1.14  root     10040:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   10041:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10042:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10043:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     10044:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     10045:        }
                   10046: }
                   10047: 
1.1.1.16  root     10048: inline void msdos_int_21h_23h()
                   10049: {
                   10050:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10051:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10052:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     10053:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10054:        
                   10055:        if(hFile == INVALID_HANDLE_VALUE) {
                   10056:                REG8(AL) = 0xff;
                   10057:        } else {
                   10058:                UINT32 size = GetFileSize(hFile, NULL);
                   10059:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   10060:                REG8(AL) = 0;
                   10061:        }
                   10062: }
                   10063: 
                   10064: inline void msdos_int_21h_24h()
                   10065: {
                   10066:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10067:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10068:        
                   10069:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   10070: }
                   10071: 
1.1       root     10072: inline void msdos_int_21h_25h()
                   10073: {
                   10074:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     10075:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     10076: }
                   10077: 
                   10078: inline void msdos_int_21h_26h()
                   10079: {
                   10080:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   10081:        
                   10082:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     10083:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     10084:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10085:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10086:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10087:        psp->parent_psp = 0;
                   10088: }
                   10089: 
1.1.1.16  root     10090: inline void msdos_int_21h_27h()
                   10091: {
                   10092:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10093:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10094:        
                   10095:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10096:                REG8(AL) = 1;
                   10097:        } else {
                   10098:                process_t *process = msdos_process_info_get(current_psp);
                   10099:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10100:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   10101:                DWORD num = 0;
                   10102:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   10103:                        REG8(AL) = 1;
                   10104:                } else {
                   10105:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10106:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10107:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10108:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10109:                }
                   10110:        }
                   10111: }
                   10112: 
                   10113: inline void msdos_int_21h_28h()
                   10114: {
                   10115:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10116:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10117:        
                   10118:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10119:                REG8(AL) = 0xff;
                   10120:        } else {
                   10121:                process_t *process = msdos_process_info_get(current_psp);
                   10122:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10123:                DWORD num = 0;
                   10124:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   10125:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10126:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10127:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10128:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   10129:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10130:        }
                   10131: }
                   10132: 
1.1       root     10133: inline void msdos_int_21h_29h()
                   10134: {
1.1.1.20  root     10135:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   10136:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     10137:        UINT8 drv = 0;
                   10138:        char sep_chars[] = ":.;,=+";
                   10139:        char end_chars[] = "\\<>|/\"[]";
                   10140:        char spc_chars[] = " \t";
                   10141:        
1.1.1.20  root     10142:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   10143:        buffer[1023] = 0;
                   10144:        memset(name, 0x20, sizeof(name));
                   10145:        memset(ext, 0x20, sizeof(ext));
                   10146:        
1.1       root     10147:        if(REG8(AL) & 1) {
1.1.1.20  root     10148:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   10149:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     10150:                        ofs++;
                   10151:                }
                   10152:        }
1.1.1.20  root     10153:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     10154:        
1.1.1.24  root     10155:        if(buffer[ofs + 1] == ':') {
                   10156:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   10157:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     10158:                        ofs += 2;
1.1.1.24  root     10159:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10160:                                ofs++;
                   10161:                        }
                   10162:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   10163:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     10164:                        ofs += 2;
1.1.1.24  root     10165:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10166:                                ofs++;
                   10167:                        }
1.1       root     10168:                }
                   10169:        }
1.1.1.20  root     10170:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10171:                UINT8 c = buffer[ofs];
                   10172:                if(is_kanji) {
                   10173:                        is_kanji = 0;
                   10174:                } else if(msdos_lead_byte_check(c)) {
                   10175:                        is_kanji = 1;
                   10176:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10177:                        break;
                   10178:                } else if(c >= 'a' && c <= 'z') {
                   10179:                        c -= 0x20;
                   10180:                }
                   10181:                ofs++;
                   10182:                name[i] = c;
                   10183:        }
1.1.1.20  root     10184:        if(buffer[ofs] == '.') {
1.1       root     10185:                ofs++;
1.1.1.20  root     10186:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10187:                        UINT8 c = buffer[ofs];
                   10188:                        if(is_kanji) {
                   10189:                                is_kanji = 0;
                   10190:                        } else if(msdos_lead_byte_check(c)) {
                   10191:                                is_kanji = 1;
                   10192:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10193:                                break;
                   10194:                        } else if(c >= 'a' && c <= 'z') {
                   10195:                                c -= 0x20;
                   10196:                        }
                   10197:                        ofs++;
                   10198:                        ext[i] = c;
                   10199:                }
                   10200:        }
1.1.1.20  root     10201:        int si = REG16(SI) + ofs;
1.1.1.3   root     10202:        int ds = SREG(DS);
1.1       root     10203:        while(si > 0xffff) {
                   10204:                si -= 0x10;
                   10205:                ds++;
                   10206:        }
                   10207:        REG16(SI) = si;
1.1.1.3   root     10208:        SREG(DS) = ds;
                   10209:        i386_load_segment_descriptor(DS);
1.1       root     10210:        
1.1.1.3   root     10211:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     10212:        if(!(REG8(AL) & 2) || drv != 0) {
                   10213:                fcb[0] = drv;
                   10214:        }
                   10215:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   10216:                memcpy(fcb + 1, name, 8);
                   10217:        }
                   10218:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   10219:                memcpy(fcb + 9, ext, 3);
                   10220:        }
                   10221:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     10222:                if(fcb[i] == '*') {
                   10223:                        found_star = 1;
                   10224:                }
                   10225:                if(found_star) {
                   10226:                        fcb[i] = '?';
                   10227:                }
                   10228:        }
1.1.1.20  root     10229:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     10230:                if(fcb[i] == '*') {
                   10231:                        found_star = 1;
                   10232:                }
                   10233:                if(found_star) {
                   10234:                        fcb[i] = '?';
                   10235:                }
                   10236:        }
                   10237:        
1.1.1.44  root     10238:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     10239:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   10240:                        REG8(AL) = 0x01;
1.1.1.20  root     10241:                } else {
                   10242:                        REG8(AL) = 0x00;
1.1       root     10243:                }
                   10244:        } else {
                   10245:                REG8(AL) = 0xff;
                   10246:        }
                   10247: }
                   10248: 
                   10249: inline void msdos_int_21h_2ah()
                   10250: {
                   10251:        SYSTEMTIME sTime;
                   10252:        
                   10253:        GetLocalTime(&sTime);
                   10254:        REG16(CX) = sTime.wYear;
                   10255:        REG8(DH) = (UINT8)sTime.wMonth;
                   10256:        REG8(DL) = (UINT8)sTime.wDay;
                   10257:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10258: }
                   10259: 
                   10260: inline void msdos_int_21h_2bh()
                   10261: {
1.1.1.14  root     10262:        REG8(AL) = 0xff;
1.1       root     10263: }
                   10264: 
                   10265: inline void msdos_int_21h_2ch()
                   10266: {
                   10267:        SYSTEMTIME sTime;
                   10268:        
                   10269:        GetLocalTime(&sTime);
                   10270:        REG8(CH) = (UINT8)sTime.wHour;
                   10271:        REG8(CL) = (UINT8)sTime.wMinute;
                   10272:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10273:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10274: }
                   10275: 
                   10276: inline void msdos_int_21h_2dh()
                   10277: {
                   10278:        REG8(AL) = 0x00;
                   10279: }
                   10280: 
                   10281: inline void msdos_int_21h_2eh()
                   10282: {
                   10283:        process_t *process = msdos_process_info_get(current_psp);
                   10284:        
                   10285:        process->verify = REG8(AL);
                   10286: }
                   10287: 
                   10288: inline void msdos_int_21h_2fh()
                   10289: {
                   10290:        process_t *process = msdos_process_info_get(current_psp);
                   10291:        
                   10292:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10293:        SREG(ES) = process->dta.w.h;
                   10294:        i386_load_segment_descriptor(ES);
1.1       root     10295: }
                   10296: 
                   10297: inline void msdos_int_21h_30h()
                   10298: {
                   10299:        // Version Flag / OEM
1.1.1.27  root     10300:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10301: #ifdef SUPPORT_HMA
                   10302:                REG16(BX) = 0x0000;
                   10303: #else
                   10304:                REG16(BX) = 0x1000; // DOS is in HMA
                   10305: #endif
1.1       root     10306:        } else {
1.1.1.27  root     10307:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10308:                // but this is not correct on Windows 98 SE
                   10309: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10310:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10311:        }
1.1.1.27  root     10312:        REG16(CX) = 0x0000;
1.1.1.30  root     10313:        REG8(AL) = dos_major_version;   // 7
                   10314:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10315: }
                   10316: 
                   10317: inline void msdos_int_21h_31h()
                   10318: {
1.1.1.29  root     10319:        try {
                   10320:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10321:        } catch(...) {
                   10322:                // recover the broken mcb
                   10323:                int mcb_seg = current_psp - 1;
                   10324:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10325:                
1.1.1.29  root     10326:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10327:                        mcb->mz = 'M';
                   10328:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10329:                        
1.1.1.29  root     10330:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10331:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10332:                        } else {
1.1.1.39  root     10333:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10334:                        }
                   10335:                } else {
                   10336:                        mcb->mz = 'Z';
1.1.1.30  root     10337:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10338:                }
                   10339:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10340:        }
1.1       root     10341:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10342: }
                   10343: 
                   10344: inline void msdos_int_21h_32h()
                   10345: {
                   10346:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10347:        UINT16 seg, ofs;
                   10348:        
                   10349:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10350:                REG8(AL) = 0;
1.1.1.3   root     10351:                SREG(DS) = seg;
                   10352:                i386_load_segment_descriptor(DS);
1.1       root     10353:                REG16(BX) = ofs;
                   10354:        } else {
                   10355:                REG8(AL) = 0xff;
1.1.1.3   root     10356:                m_CF = 1;
1.1       root     10357:        }
                   10358: }
                   10359: 
                   10360: inline void msdos_int_21h_33h()
                   10361: {
                   10362:        char path[MAX_PATH];
1.1.1.48  root     10363:        char drive = 3; // C:
1.1       root     10364:        
                   10365:        switch(REG8(AL)) {
                   10366:        case 0x00:
1.1.1.33  root     10367:                REG8(DL) = ctrl_break_checking;
1.1       root     10368:                break;
                   10369:        case 0x01:
1.1.1.33  root     10370:                ctrl_break_checking = REG8(DL);
                   10371:                break;
                   10372:        case 0x02:
                   10373:                {
                   10374:                        UINT8 old = ctrl_break_checking;
                   10375:                        ctrl_break_checking = REG8(DL);
                   10376:                        REG8(DL) = old;
                   10377:                }
                   10378:                break;
                   10379:        case 0x03:
                   10380:        case 0x04:
                   10381:                // DOS 4.0+ - Unused
1.1       root     10382:                break;
                   10383:        case 0x05:
1.1.1.48  root     10384:                if(GetSystemDirectory(path, MAX_PATH) != 0) {
                   10385:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10386:                                drive = path[0] - 'a' + 1;
                   10387:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10388:                                drive = path[0] - 'A' + 1;
                   10389:                        }
1.1       root     10390:                }
1.1.1.48  root     10391:                REG8(DL) = (UINT8)drive;
1.1       root     10392:                break;
                   10393:        case 0x06:
1.1.1.2   root     10394:                // MS-DOS version (7.10)
1.1       root     10395:                REG8(BL) = 7;
1.1.1.2   root     10396:                REG8(BH) = 10;
1.1       root     10397:                REG8(DL) = 0;
1.1.1.29  root     10398: #ifdef SUPPORT_HMA
                   10399:                REG8(DH) = 0x00;
                   10400: #else
                   10401:                REG8(DH) = 0x10; // DOS is in HMA
                   10402: #endif
1.1       root     10403:                break;
1.1.1.6   root     10404:        case 0x07:
                   10405:                if(REG8(DL) == 0) {
                   10406:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10407:                } else if(REG8(DL) == 1) {
                   10408:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10409:                }
                   10410:                break;
1.1       root     10411:        default:
1.1.1.22  root     10412:                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     10413: //             REG16(AX) = 0x01;
                   10414: //             m_CF = 1;
                   10415:                REG8(AL) = 0xff;
1.1       root     10416:                break;
                   10417:        }
                   10418: }
                   10419: 
1.1.1.23  root     10420: inline void msdos_int_21h_34h()
                   10421: {
                   10422:        SREG(ES) = SDA_TOP >> 4;
                   10423:        i386_load_segment_descriptor(ES);
                   10424:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   10425: }
                   10426: 
1.1       root     10427: inline void msdos_int_21h_35h()
                   10428: {
                   10429:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10430:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10431:        i386_load_segment_descriptor(ES);
1.1       root     10432: }
                   10433: 
                   10434: inline void msdos_int_21h_36h()
                   10435: {
                   10436:        struct _diskfree_t df = {0};
                   10437:        
                   10438:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10439:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10440:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10441:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10442:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10443:        } else {
                   10444:                REG16(AX) = 0xffff;
                   10445:        }
                   10446: }
                   10447: 
                   10448: inline void msdos_int_21h_37h()
                   10449: {
1.1.1.22  root     10450:        static UINT8 dev_flag = 0xff;
1.1       root     10451:        
                   10452:        switch(REG8(AL)) {
                   10453:        case 0x00:
1.1.1.22  root     10454:                {
                   10455:                        process_t *process = msdos_process_info_get(current_psp);
                   10456:                        REG8(AL) = 0x00;
                   10457:                        REG8(DL) = process->switchar;
                   10458:                }
1.1       root     10459:                break;
                   10460:        case 0x01:
1.1.1.22  root     10461:                {
                   10462:                        process_t *process = msdos_process_info_get(current_psp);
                   10463:                        REG8(AL) = 0x00;
                   10464:                        process->switchar = REG8(DL);
1.1.1.23  root     10465:                        msdos_sda_update(current_psp);
1.1.1.22  root     10466:                }
                   10467:                break;
                   10468:        case 0x02:
                   10469:                REG8(DL) = dev_flag;
                   10470:                break;
                   10471:        case 0x03:
                   10472:                dev_flag = REG8(DL);
                   10473:                break;
                   10474:        case 0xd0:
                   10475:        case 0xd1:
                   10476:        case 0xd2:
                   10477:        case 0xd3:
                   10478:        case 0xd4:
                   10479:        case 0xd5:
                   10480:        case 0xd6:
                   10481:        case 0xd7:
                   10482:        case 0xdc:
                   10483:        case 0xdd:
                   10484:        case 0xde:
                   10485:        case 0xdf:
1.1.1.48  root     10486:                // DIET v1.43e
                   10487: //             REG16(AX) = 1;
                   10488:                REG8(AL) = 0xff;
1.1       root     10489:                break;
                   10490:        default:
1.1.1.22  root     10491:                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     10492: //             REG16(AX) = 1;
                   10493:                REG8(AL) = 0xff;
1.1       root     10494:                break;
                   10495:        }
                   10496: }
                   10497: 
1.1.1.52  root     10498: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10499: {
                   10500:        char LCdata[80];
                   10501:        
1.1.1.19  root     10502:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     10503:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10504:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     10505:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10506:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     10507:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10508:        ci->date_format = *LCdata - '0';
1.1.1.42  root     10509:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10510:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     10511:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10512:        *ci->date_sep = *LCdata;
1.1.1.42  root     10513:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10514:        *ci->dec_sep = *LCdata;
1.1.1.42  root     10515:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10516:        *ci->list_sep = *LCdata;
1.1.1.42  root     10517:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10518:        *ci->thou_sep = *LCdata;
1.1.1.42  root     10519:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10520:        *ci->time_sep = *LCdata;
1.1.1.42  root     10521:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10522:        if(strchr(LCdata, 'H') != NULL) {
                   10523:                ci->time_format = 1;
                   10524:        }
1.1.1.49  root     10525:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10526:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.42  root     10527:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10528:        return atoi(LCdata);
                   10529: }
                   10530: 
1.1.1.42  root     10531: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10532: {
                   10533:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10534: }
                   10535: 
1.1.1.43  root     10536: void set_country_info(country_info_t *ci, int size)
                   10537: {
                   10538:        char LCdata[80];
                   10539:        
                   10540:        if(size >= 0x00 + 2) {
                   10541:                memset(LCdata, 0, sizeof(LCdata));
                   10542:                *LCdata = '0' + ci->date_format;
                   10543:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   10544:        }
                   10545:        if(size >= 0x02 + 5) {
                   10546:                memset(LCdata, 0, sizeof(LCdata));
                   10547:                memcpy(LCdata, &ci->currency_symbol, 4);
                   10548:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   10549:        }
                   10550:        if(size >= 0x07 + 2) {
                   10551:                memset(LCdata, 0, sizeof(LCdata));
                   10552:                *LCdata = *ci->thou_sep;
                   10553:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   10554:        }
                   10555:        if(size >= 0x09 + 2) {
                   10556:                memset(LCdata, 0, sizeof(LCdata));
                   10557:                *LCdata = *ci->dec_sep;
                   10558:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   10559:        }
                   10560:        if(size >= 0x0b + 2) {
                   10561:                memset(LCdata, 0, sizeof(LCdata));
                   10562:                *LCdata = *ci->date_sep;
                   10563:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   10564:        }
                   10565:        if(size >= 0x0d + 2) {
                   10566:                memset(LCdata, 0, sizeof(LCdata));
                   10567:                *LCdata = *ci->time_sep;
                   10568:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   10569:        }
                   10570:        if(size >= 0x0f + 1) {
                   10571:                memset(LCdata, 0, sizeof(LCdata));
                   10572:                *LCdata = '0' + ci->currency_format;
                   10573:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   10574:        }
                   10575:        if(size >= 0x10 + 1) {
                   10576:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   10577:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   10578:        }
                   10579:        if(size >= 0x11 + 1) {
                   10580:                // FIXME: is time format always H/h:mm:ss ???
                   10581:                if(ci->time_format & 1) {
                   10582:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10583:                } else {
                   10584:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10585:                }
                   10586:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   10587:        }
                   10588:        if(size >= 0x12 + 4) {
                   10589:                // 12h  DWORD   address of case map routine
                   10590:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10591:        }
                   10592:        if(size >= 0x16 + 2) {
                   10593:                memset(LCdata, 0, sizeof(LCdata));
                   10594:                *LCdata = *ci->list_sep;
                   10595:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10596:        }
                   10597: }
                   10598: 
1.1.1.42  root     10599: #ifndef SUBLANG_SWAHILI
                   10600:        #define SUBLANG_SWAHILI 0x01
                   10601: #endif
                   10602: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10603:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10604: #endif
                   10605: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10606:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10607: #endif
                   10608: #ifndef LANG_BANGLA
                   10609:        #define LANG_BANGLA 0x45
                   10610: #endif
                   10611: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10612:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10613: #endif
                   10614: 
                   10615: static const struct {
                   10616:        int code;
                   10617:        USHORT usPrimaryLanguage;
                   10618:        USHORT usSubLanguage;
                   10619: } country_table[] = {
                   10620:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10621:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10622:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10623:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10624:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10625:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10626: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10627: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10628: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10629: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10630: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10631:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10632:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10633: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10634:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10635: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10636:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10637:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10638:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10639:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10640:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10641:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10642:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10643: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10644: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10645:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10646:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10647:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10648:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10649:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10650: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10651: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10652: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10653:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10654: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10655: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10656: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10657: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10658:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10659:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10660:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10661: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10662:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10663:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10664:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10665:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10666:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10667:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10668:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10669:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10670:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10671:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10672:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10673:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10674:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10675: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10676:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10677:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10678:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10679:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10680:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10681:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10682:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10683:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10684:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10685:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10686: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10687:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10688: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10689:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10690:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10691:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10692:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10693:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10694:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10695:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10696:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10697: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10698:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10699: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10700: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10701:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10702: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10703:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10704:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10705:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10706:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10707:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10708:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10709:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10710: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10711: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10712:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10713: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10714:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10715:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10716:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10717:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10718: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10719: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10720: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10721:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10722:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10723:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10724:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10725:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10726:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10727:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10728:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10729:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10730:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10731:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10732:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10733:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10734:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10735:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10736:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10737:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10738:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10739:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10740:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10741:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10742: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10743:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10744: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10745:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10746:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10747:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10748:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10749:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10750:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10751:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10752:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10753:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10754:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10755:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10756:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10757:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10758:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10759:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10760:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10761:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10762:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10763:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10764:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10765:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10766:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10767:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10768:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10769:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10770: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10771:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10772:        {-1, 0, 0},
                   10773: };
                   10774: 
1.1.1.14  root     10775: inline void msdos_int_21h_38h()
                   10776: {
                   10777:        switch(REG8(AL)) {
                   10778:        case 0x00:
1.1.1.19  root     10779:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10780:                break;
                   10781:        default:
1.1.1.42  root     10782:                for(int i = 0;; i++) {
                   10783:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10784:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10785:                                break;
                   10786:                        } else if(country_table[i].code == -1) {
                   10787: //                             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));
                   10788: //                             REG16(AX) = 2;
                   10789: //                             m_CF = 1;
1.1.1.48  root     10790:                                // get current coutry info
1.1.1.42  root     10791:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10792:                                break;
                   10793:                        }
                   10794:                }
1.1.1.14  root     10795:                break;
                   10796:        }
                   10797: }
                   10798: 
1.1       root     10799: inline void msdos_int_21h_39h(int lfn)
                   10800: {
1.1.1.3   root     10801:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10802:                REG16(AX) = errno;
1.1.1.3   root     10803:                m_CF = 1;
1.1       root     10804:        }
                   10805: }
                   10806: 
                   10807: inline void msdos_int_21h_3ah(int lfn)
                   10808: {
1.1.1.3   root     10809:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10810:                REG16(AX) = errno;
1.1.1.3   root     10811:                m_CF = 1;
1.1       root     10812:        }
                   10813: }
                   10814: 
                   10815: inline void msdos_int_21h_3bh(int lfn)
                   10816: {
1.1.1.45  root     10817:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10818:        
                   10819:        if(_chdir(path)) {
1.1.1.17  root     10820:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10821:                m_CF = 1;
1.1.1.44  root     10822:        } else {
                   10823:                int drv = _getdrive() - 1;
                   10824:                if(path[1] == ':') {
                   10825:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10826:                                drv = path[0] - 'A';
                   10827:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10828:                                drv = path[0] - 'a';
                   10829:                        }
                   10830:                }
                   10831:                msdos_cds_update(drv, path);
1.1       root     10832:        }
                   10833: }
                   10834: 
                   10835: inline void msdos_int_21h_3ch()
                   10836: {
1.1.1.45  root     10837:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10838:        int attr = GetFileAttributes(path);
1.1.1.37  root     10839:        int fd = -1;
                   10840:        int sio_port = 0;
                   10841:        int lpt_port = 0;
1.1       root     10842:        
1.1.1.45  root     10843:        if(msdos_is_device_path(path)) {
                   10844:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10845:        } else {
                   10846:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10847:        }
                   10848:        if(fd != -1) {
                   10849:                if(attr == -1) {
                   10850:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10851:                }
                   10852:                SetFileAttributes(path, attr);
                   10853:                REG16(AX) = fd;
1.1.1.45  root     10854:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10855:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10856:        } else {
                   10857:                REG16(AX) = errno;
1.1.1.3   root     10858:                m_CF = 1;
1.1       root     10859:        }
                   10860: }
                   10861: 
                   10862: inline void msdos_int_21h_3dh()
                   10863: {
1.1.1.45  root     10864:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10865:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10866:        int fd = -1;
                   10867:        int sio_port = 0;
                   10868:        int lpt_port = 0;
1.1       root     10869:        
                   10870:        if(mode < 0x03) {
1.1.1.45  root     10871:                if(msdos_is_device_path(path)) {
                   10872:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10873:                } else {
1.1.1.13  root     10874:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10875:                }
1.1       root     10876:                if(fd != -1) {
                   10877:                        REG16(AX) = fd;
1.1.1.45  root     10878:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10879:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10880:                } else {
                   10881:                        REG16(AX) = errno;
1.1.1.3   root     10882:                        m_CF = 1;
1.1       root     10883:                }
                   10884:        } else {
                   10885:                REG16(AX) = 0x0c;
1.1.1.3   root     10886:                m_CF = 1;
1.1       root     10887:        }
                   10888: }
                   10889: 
                   10890: inline void msdos_int_21h_3eh()
                   10891: {
                   10892:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10893:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10894:        
1.1.1.20  root     10895:        if(fd < process->max_files && file_handler[fd].valid) {
                   10896:                _close(fd);
                   10897:                msdos_file_handler_close(fd);
                   10898:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10899:        } else {
                   10900:                REG16(AX) = 0x06;
1.1.1.3   root     10901:                m_CF = 1;
1.1       root     10902:        }
                   10903: }
                   10904: 
1.1.1.35  root     10905: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10906: {
                   10907:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10908:        int max = REG16(CX);
                   10909:        int p = 0;
                   10910:        
                   10911:        while(max > p) {
                   10912:                int chr = msdos_getch();
                   10913:                
                   10914:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10915:                        p = 0;
                   10916:                        buf[p++] = 0x0d;
                   10917:                        if(max > p) {
                   10918:                                buf[p++] = 0x0a;
                   10919:                        }
                   10920:                        msdos_putch(0x03);
                   10921:                        msdos_putch(0x0d);
                   10922:                        msdos_putch(0x0a);
                   10923:                        break;
                   10924:                } else if(ctrl_break_pressed) {
                   10925:                        // skip this byte
                   10926:                } else if(chr == 0x00) {
                   10927:                        // skip 2nd byte
                   10928:                        msdos_getch();
                   10929:                } else if(chr == 0x0d) {
                   10930:                        // carriage return
                   10931:                        buf[p++] = 0x0d;
                   10932:                        if(max > p) {
                   10933:                                buf[p++] = 0x0a;
                   10934:                        }
                   10935:                        msdos_putch('\n');
                   10936:                        break;
                   10937:                } else if(chr == 0x08) {
                   10938:                        // back space
                   10939:                        if(p > 0) {
                   10940:                                p--;
                   10941:                                if(msdos_ctrl_code_check(buf[p])) {
                   10942:                                        msdos_putch(0x08);
                   10943:                                        msdos_putch(0x08);
                   10944:                                        msdos_putch(0x20);
                   10945:                                        msdos_putch(0x20);
                   10946:                                        msdos_putch(0x08);
                   10947:                                        msdos_putch(0x08);
1.1.1.36  root     10948:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10949:                                        p--;
                   10950:                                        msdos_putch(0x08);
                   10951:                                        msdos_putch(0x08);
                   10952:                                        msdos_putch(0x20);
                   10953:                                        msdos_putch(0x20);
                   10954:                                        msdos_putch(0x08);
                   10955:                                        msdos_putch(0x08);
1.1.1.35  root     10956:                                } else {
                   10957:                                        msdos_putch(0x08);
                   10958:                                        msdos_putch(0x20);
                   10959:                                        msdos_putch(0x08);
                   10960:                                }
                   10961:                        }
                   10962:                } else if(chr == 0x1b) {
                   10963:                        // escape
                   10964:                        while(p > 0) {
                   10965:                                p--;
                   10966:                                if(msdos_ctrl_code_check(buf[p])) {
                   10967:                                        msdos_putch(0x08);
                   10968:                                        msdos_putch(0x08);
                   10969:                                        msdos_putch(0x20);
                   10970:                                        msdos_putch(0x20);
                   10971:                                        msdos_putch(0x08);
                   10972:                                        msdos_putch(0x08);
                   10973:                                } else {
                   10974:                                        msdos_putch(0x08);
                   10975:                                        msdos_putch(0x20);
                   10976:                                        msdos_putch(0x08);
                   10977:                                }
                   10978:                        }
                   10979:                } else {
                   10980:                        buf[p++] = chr;
                   10981:                        msdos_putch(chr);
                   10982:                }
                   10983:        }
                   10984:        REG16(AX) = p;
                   10985:        
                   10986: #ifdef USE_SERVICE_THREAD
                   10987:        service_exit = true;
                   10988: #endif
                   10989:        return(0);
                   10990: }
                   10991: 
1.1       root     10992: inline void msdos_int_21h_3fh()
                   10993: {
                   10994:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10995:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10996:        
1.1.1.20  root     10997:        if(fd < process->max_files && file_handler[fd].valid) {
                   10998:                if(file_mode[file_handler[fd].mode].in) {
                   10999:                        if(file_handler[fd].atty) {
1.1       root     11000:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     11001:                                if(REG16(CX) != 0) {
                   11002: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     11003:                                        if(!in_service && !in_service_29h &&
1.1.1.58  root     11004:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     11005:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   11006:                                                // msdos_putch() will be used in this service
                   11007:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   11008:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   11009:                                        } else {
                   11010: #endif
                   11011:                                                msdos_int_21h_3fh_thread(NULL);
                   11012:                                                REQUEST_HARDWRE_UPDATE();
                   11013: #ifdef USE_SERVICE_THREAD
                   11014:                                        }
1.1.1.35  root     11015: #endif
                   11016:                                } else {
                   11017:                                        REG16(AX) = 0;
1.1       root     11018:                                }
                   11019:                        } else {
1.1.1.37  root     11020:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11021:                        }
                   11022:                } else {
                   11023:                        REG16(AX) = 0x05;
1.1.1.3   root     11024:                        m_CF = 1;
1.1       root     11025:                }
                   11026:        } else {
                   11027:                REG16(AX) = 0x06;
1.1.1.3   root     11028:                m_CF = 1;
1.1       root     11029:        }
                   11030: }
                   11031: 
                   11032: inline void msdos_int_21h_40h()
                   11033: {
                   11034:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11035:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11036:        
1.1.1.20  root     11037:        if(fd < process->max_files && file_handler[fd].valid) {
                   11038:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     11039:                        if(REG16(CX)) {
1.1.1.20  root     11040:                                if(file_handler[fd].atty) {
1.1       root     11041:                                        // BX is stdout/stderr or is redirected to stdout
                   11042:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     11043:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     11044:                                        }
                   11045:                                        REG16(AX) = REG16(CX);
                   11046:                                } else {
1.1.1.20  root     11047:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11048:                                }
                   11049:                        } else {
1.1.1.20  root     11050:                                UINT32 pos = _tell(fd);
                   11051:                                _lseek(fd, 0, SEEK_END);
                   11052:                                UINT32 size = _tell(fd);
1.1.1.12  root     11053:                                if(pos < size) {
1.1.1.20  root     11054:                                        _lseek(fd, pos, SEEK_SET);
                   11055:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     11056:                                } else {
                   11057:                                        for(UINT32 i = size; i < pos; i++) {
                   11058:                                                UINT8 tmp = 0;
1.1.1.23  root     11059:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     11060:                                        }
1.1.1.20  root     11061:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     11062:                                }
1.1.1.23  root     11063:                                REG16(AX) = 0;
1.1       root     11064:                        }
                   11065:                } else {
                   11066:                        REG16(AX) = 0x05;
1.1.1.3   root     11067:                        m_CF = 1;
1.1       root     11068:                }
                   11069:        } else {
                   11070:                REG16(AX) = 0x06;
1.1.1.3   root     11071:                m_CF = 1;
1.1       root     11072:        }
                   11073: }
                   11074: 
                   11075: inline void msdos_int_21h_41h(int lfn)
                   11076: {
1.1.1.3   root     11077:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11078:                REG16(AX) = errno;
1.1.1.3   root     11079:                m_CF = 1;
1.1       root     11080:        }
                   11081: }
                   11082: 
                   11083: inline void msdos_int_21h_42h()
                   11084: {
                   11085:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11086:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11087:        
1.1.1.20  root     11088:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11089:                if(REG8(AL) < 0x03) {
1.1.1.35  root     11090:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     11091:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   11092:                        UINT32 pos = _tell(fd);
1.1       root     11093:                        REG16(AX) = pos & 0xffff;
                   11094:                        REG16(DX) = (pos >> 16);
                   11095:                } else {
                   11096:                        REG16(AX) = 0x01;
1.1.1.3   root     11097:                        m_CF = 1;
1.1       root     11098:                }
                   11099:        } else {
                   11100:                REG16(AX) = 0x06;
1.1.1.3   root     11101:                m_CF = 1;
1.1       root     11102:        }
                   11103: }
                   11104: 
                   11105: inline void msdos_int_21h_43h(int lfn)
                   11106: {
1.1.1.45  root     11107:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     11108:        int attr;
                   11109:        
1.1.1.14  root     11110:        if(!lfn && REG8(AL) > 2) {
                   11111:                REG16(AX) = 0x01;
                   11112:                m_CF = 1;
                   11113:                return;
                   11114:        }
                   11115:        switch(REG8(lfn ? BL : AL)) {
1.1       root     11116:        case 0x00:
                   11117:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     11118:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   11119:                } else {
                   11120:                        REG16(AX) = (UINT16)GetLastError();
                   11121:                        m_CF = 1;
                   11122:                }
                   11123:                break;
                   11124:        case 0x01:
                   11125:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   11126:                        REG16(AX) = (UINT16)GetLastError();
                   11127:                        m_CF = 1;
                   11128:                }
                   11129:                break;
                   11130:        case 0x02:
                   11131:                {
1.1.1.45  root     11132:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
                   11133:                        if(compressed_size != INVALID_FILE_SIZE) {
                   11134:                                if(compressed_size != 0) {
                   11135:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   11136:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   11137:                                                file_size = GetFileSize(hFile, NULL);
                   11138:                                                CloseHandle(hFile);
                   11139:                                        }
                   11140:                                        if(compressed_size == file_size) {
                   11141:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11142:                                                // this isn't correct if the file is in the NTFS MFT
                   11143:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   11144:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   11145:                                                }
1.1.1.14  root     11146:                                        }
                   11147:                                }
1.1.1.45  root     11148:                                REG16(AX) = LOWORD(compressed_size);
                   11149:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     11150:                        } else {
                   11151:                                REG16(AX) = (UINT16)GetLastError();
                   11152:                                m_CF = 1;
1.1       root     11153:                        }
1.1.1.14  root     11154:                }
                   11155:                break;
                   11156:        case 0x03:
                   11157:        case 0x05:
                   11158:        case 0x07:
1.1.1.48  root     11159:                if(lfn) {
1.1.1.14  root     11160:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   11161:                        if(hFile != INVALID_HANDLE_VALUE) {
                   11162:                                FILETIME local, time;
                   11163:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   11164:                                if(REG8(BL) == 7) {
                   11165:                                        ULARGE_INTEGER hund;
                   11166:                                        hund.LowPart = local.dwLowDateTime;
                   11167:                                        hund.HighPart = local.dwHighDateTime;
                   11168:                                        hund.QuadPart += REG16(SI) * 100000;
                   11169:                                        local.dwLowDateTime = hund.LowPart;
                   11170:                                        local.dwHighDateTime = hund.HighPart;
                   11171:                                }
                   11172:                                LocalFileTimeToFileTime(&local, &time);
                   11173:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   11174:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   11175:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   11176:                                        REG16(AX) = (UINT16)GetLastError();
                   11177:                                        m_CF = 1;
                   11178:                                }
                   11179:                                CloseHandle(hFile);
                   11180:                        } else {
                   11181:                                REG16(AX) = (UINT16)GetLastError();
                   11182:                                m_CF = 1;
1.1       root     11183:                        }
1.1.1.48  root     11184:                } else {
                   11185:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   11186:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   11187:                        // 214307 DR DOS 6.0 - Set File Owner
                   11188: //                     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));
                   11189:                        REG16(AX) = 0x01;
                   11190:                        m_CF = 1;
1.1.1.14  root     11191:                }
                   11192:                break;
                   11193:        case 0x04:
                   11194:        case 0x06:
                   11195:        case 0x08:
1.1.1.48  root     11196:                if(lfn) {
1.1.1.14  root     11197:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   11198:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   11199:                                FILETIME *time, local;
                   11200:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   11201:                                                   0x06 ? &fad.ftLastAccessTime :
                   11202:                                                          &fad.ftCreationTime;
                   11203:                                FileTimeToLocalFileTime(time, &local);
                   11204:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   11205:                                if(REG8(BL) == 0x08) {
                   11206:                                        ULARGE_INTEGER hund;
                   11207:                                        hund.LowPart = local.dwLowDateTime;
                   11208:                                        hund.HighPart = local.dwHighDateTime;
                   11209:                                        hund.QuadPart /= 100000;
                   11210:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   11211:                                }
                   11212:                        } else {
                   11213:                                REG16(AX) = (UINT16)GetLastError();
                   11214:                                m_CF = 1;
1.1       root     11215:                        }
1.1.1.48  root     11216:                } else {
                   11217:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   11218:                        // 214306 DR DOS 6.0 - Get File Owner
                   11219: //                     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));
                   11220:                        REG16(AX) = 0x01;
                   11221:                        m_CF = 1;
1.1.1.14  root     11222:                }
                   11223:                break;
1.1.1.43  root     11224:        case 0xff:
1.1.1.48  root     11225:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     11226:                        if(REG8(CL) == 0x39) {
                   11227:                                msdos_int_21h_39h(1);
                   11228:                                break;
                   11229:                        } else if(REG8(CL) == 0x56) {
                   11230:                                msdos_int_21h_56h(1);
                   11231:                                break;
                   11232:                        }
                   11233:                }
1.1.1.14  root     11234:        default:
1.1.1.22  root     11235:                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     11236:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     11237:                m_CF = 1;
                   11238:                break;
                   11239:        }
                   11240: }
                   11241: 
                   11242: inline void msdos_int_21h_44h()
                   11243: {
1.1.1.22  root     11244:        static UINT16 iteration_count = 0;
                   11245:        
1.1.1.44  root     11246:        process_t *process;
                   11247:        int fd, drv;
1.1.1.14  root     11248:        
                   11249:        switch(REG8(AL)) {
                   11250:        case 0x00:
                   11251:        case 0x01:
                   11252:        case 0x02:
                   11253:        case 0x03:
                   11254:        case 0x04:
                   11255:        case 0x05:
                   11256:        case 0x06:
                   11257:        case 0x07:
1.1.1.44  root     11258:                process = msdos_process_info_get(current_psp);
                   11259:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11260:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11261:                        REG16(AX) = 0x06;
                   11262:                        m_CF = 1;
                   11263:                        return;
1.1.1.14  root     11264:                }
                   11265:                break;
                   11266:        case 0x08:
                   11267:        case 0x09:
1.1.1.44  root     11268:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11269:                if(!msdos_is_valid_drive(drv)) {
                   11270:                        // invalid drive
1.1.1.14  root     11271:                        REG16(AX) = 0x0f;
                   11272:                        m_CF = 1;
                   11273:                        return;
1.1       root     11274:                }
                   11275:                break;
                   11276:        }
                   11277:        switch(REG8(AL)) {
1.1.1.48  root     11278:        case 0x00: // Get Device Information
1.1.1.20  root     11279:                REG16(DX) = file_handler[fd].info;
1.1       root     11280:                break;
1.1.1.48  root     11281:        case 0x01: // Set Device Information
1.1.1.45  root     11282:                if(REG8(DH) != 0) {
                   11283: //                     REG16(AX) = 0x0d; // data invalid
                   11284: //                     m_CF = 1;
                   11285:                        file_handler[fd].info = REG16(DX);
                   11286:                } else {
                   11287:                        file_handler[fd].info &= 0xff00;
                   11288:                        file_handler[fd].info |= REG8(DL);
                   11289:                }
1.1       root     11290:                break;
1.1.1.48  root     11291:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11292:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11293:                        // from DOSBox
                   11294:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11295:                        case 0x00:
                   11296:                                if(REG16(CX) >= 6) {
                   11297:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11298:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11299:                                        REG16(AX) = 6; // number of bytes actually read
                   11300:                                } else {
                   11301:                                        REG16(AX) = 0x0d; // data invalid
                   11302:                                        m_CF = 1;
                   11303:                                }
                   11304:                                break;
                   11305:                        case 0x01:
                   11306:                                if(REG16(CX) >= 6) {
                   11307:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11308:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11309:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11310:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11311:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11312:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11313:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11314:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11315:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11316:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11317:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11318:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11319:                                                } else {
                   11320:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11321:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11322:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11323:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11324:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11325:                                                }
                   11326:                                        }
                   11327:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11328:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11329:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11330:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11331:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11332:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11333:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11334:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11335:                                        
                   11336:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   11337:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   11338:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11339:                                        REG16(AX) = 6; // number of bytes actually read
                   11340:                                } else {
                   11341:                                        REG16(AX) = 0x0d; // data invalid
                   11342:                                        m_CF = 1;
                   11343:                                }
                   11344:                                break;
                   11345:                        case 0x02:
                   11346:                                if(REG16(CX) >= 2) {
                   11347:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11348:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11349:                                        REG16(AX) = 2; // number of bytes actually read
                   11350:                                } else {
                   11351:                                        REG16(AX) = 0x0d; // data invalid
                   11352:                                        m_CF = 1;
                   11353:                                }
                   11354:                                break;
                   11355:                        case 0x03:
                   11356:                                if(REG16(CX) >= 4) {
                   11357:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11358:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11359:                                        REG16(AX) = 4; // number of bytes actually read
                   11360:                                } else {
                   11361:                                        REG16(AX) = 0x0d; // data invalid
                   11362:                                        m_CF = 1;
                   11363:                                }
                   11364:                                break;
                   11365:                        default:
                   11366:                                REG16(AX) = 0x01; // function number invalid
                   11367:                                m_CF = 1;
                   11368:                        }
                   11369:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11370:                        if(REG16(CX) >= 5) {
                   11371:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11372:                                REG16(AX) = 5; // number of bytes actually read
                   11373:                        } else {
                   11374:                                REG16(AX) = 0x0d; // data invalid
                   11375:                                m_CF = 1;
                   11376:                        }
                   11377:                } else {
                   11378: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11379: //                     REG16(AX) = REG16(CX);
                   11380:                        REG16(AX) = 0x05; // access denied
                   11381:                        m_CF = 1;
                   11382:                }
                   11383:                break;
1.1.1.48  root     11384:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11385: //             REG16(AX) = 0x05;
                   11386: //             m_CF = 1;
                   11387:                REG16(AX) = 0x00; // success
                   11388:                break;
1.1.1.48  root     11389:        case 0x04: // Read From Block Device Control Channel
                   11390:        case 0x05: // Write To Block Device Control Channel
1.1       root     11391:                REG16(AX) = 0x05;
1.1.1.3   root     11392:                m_CF = 1;
1.1       root     11393:                break;
1.1.1.48  root     11394:        case 0x06: // Get Input Status
1.1.1.20  root     11395:                if(file_mode[file_handler[fd].mode].in) {
                   11396:                        if(file_handler[fd].atty) {
1.1.1.14  root     11397:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11398:                        } else {
1.1.1.20  root     11399:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11400:                        }
1.1.1.14  root     11401:                } else {
                   11402:                        REG8(AL) = 0x00;
1.1       root     11403:                }
                   11404:                break;
1.1.1.48  root     11405:        case 0x07: // Get Output Status
1.1.1.20  root     11406:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11407:                        REG8(AL) = 0xff;
                   11408:                } else {
                   11409:                        REG8(AL) = 0x00;
1.1       root     11410:                }
                   11411:                break;
1.1.1.48  root     11412:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11413:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11414:                        // removable drive
                   11415:                        REG16(AX) = 0x00;
1.1       root     11416:                } else {
1.1.1.14  root     11417:                        // fixed drive
                   11418:                        REG16(AX) = 0x01;
1.1       root     11419:                }
                   11420:                break;
1.1.1.48  root     11421:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11422:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11423:                        // remote drive
                   11424:                        REG16(DX) = 0x1000;
1.1.1.44  root     11425:                } else if(msdos_is_subst_drive(drv)) {
                   11426:                        // subst drive
                   11427:                        REG16(DX) = 0x8000;
1.1       root     11428:                } else {
1.1.1.14  root     11429:                        // local drive
1.1.1.44  root     11430:                        REG16(DX) = 0x0000;
1.1       root     11431:                }
                   11432:                break;
1.1.1.48  root     11433:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11434:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11435:                        REG16(DX) = 0x8000;
                   11436:                } else {
                   11437:                        REG16(DX) = 0x0000;
                   11438:                }
1.1.1.21  root     11439:                break;
1.1.1.48  root     11440:        case 0x0b: // Set Sharing Retry Count
1.1       root     11441:                break;
1.1.1.48  root     11442:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11443:                if(REG8(CL) == 0x45) {
                   11444:                        // set iteration (retry) count
                   11445:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11446:                } else if(REG8(CL) == 0x4a) {
                   11447:                        // select code page
                   11448:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11449:                        msdos_nls_tables_update();
1.1.1.44  root     11450:                } else if(REG8(CL) == 0x4c) {
                   11451:                        // start code-page preparation
                   11452:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11453:                        int count = 1, offset = 0;
                   11454:                        if(active_code_page != 437) {
                   11455:                                ids[count++] = active_code_page;
                   11456:                        }
                   11457:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11458:                                ids[count++] = system_code_page;
                   11459:                        }
                   11460:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11461:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11462:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11463:                        for(int i = 0; i < count; i++) {
                   11464:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11465:                        }
                   11466:                } else if(REG8(CL) == 0x4d) {
                   11467:                        // end code-page preparation
                   11468:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11469:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11470:                } else if(REG8(CL) == 0x5f) {
                   11471:                        // set display information
                   11472:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11473:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11474:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11475:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11476:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11477:                                
                   11478:                                if(cur_width != new_width || cur_height != new_height) {
                   11479:                                        pcbios_set_console_size(new_width, new_height, true);
                   11480:                                }
                   11481:                        }
1.1.1.22  root     11482:                } else if(REG8(CL) == 0x65) {
                   11483:                        // get iteration (retry) count
                   11484:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11485:                } else if(REG8(CL) == 0x6a) {
                   11486:                        // query selected code page
                   11487:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11488:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11489:                        
                   11490:                        CPINFO info;
                   11491:                        GetCPInfo(active_code_page, &info);
                   11492:                        
                   11493:                        if(info.MaxCharSize != 1) {
                   11494:                                for(int i = 0;; i++) {
                   11495:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11496:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11497:                                        
                   11498:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11499:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11500:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11501:                                        
                   11502:                                        if(lo == 0 && hi == 0) {
                   11503:                                                break;
                   11504:                                        }
                   11505:                                }
                   11506:                        }
1.1.1.44  root     11507:                } else if(REG8(CL) == 0x6b) {
                   11508:                        // query prepare list
                   11509:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11510:                        int count = 1, offset = 0;
                   11511:                        if(active_code_page != 437) {
                   11512:                                ids[count++] = active_code_page;
                   11513:                        }
                   11514:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11515:                                ids[count++] = system_code_page;
                   11516:                        }
                   11517:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11518:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11519:                        for(int i = 0; i < count; i++) {
                   11520:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11521:                        }
                   11522:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11523:                        for(int i = 0; i < count; i++) {
                   11524:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11525:                        }
1.1.1.22  root     11526:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11527:                        // get display information
1.1.1.50  root     11528:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11529:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11530:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11531:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11532:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11533:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11534:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11535:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11536:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11537:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11538:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11539:                } else {
                   11540:                        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));
                   11541:                        REG16(AX) = 0x01; // invalid function
                   11542:                        m_CF = 1;
                   11543:                }
                   11544:                break;
1.1.1.48  root     11545:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11546:                if(REG8(CL) == 0x40) {
                   11547:                        // set device parameters
1.1.1.48  root     11548: //             } else if(REG8(CL) == 0x41) {
                   11549: //                     // write logical device track
                   11550: //             } else if(REG8(CL) == 0x42) {
                   11551: //                     // format and verify logical device track
1.1.1.22  root     11552:                } else if(REG8(CL) == 0x46) {
                   11553:                        // set volume serial number
1.1.1.48  root     11554:                } else if(REG8(CL) == 0x47) {
                   11555:                        // set access flag
                   11556: //             } else if(REG8(CL) == 0x48) {
                   11557: //                     // set media lock state
                   11558: //             } else if(REG8(CL) == 0x49) {
                   11559: //                     // eject media in drive
1.1.1.22  root     11560:                } else if(REG8(CL) == 0x4a) {
                   11561:                        // lock logical volume
                   11562:                } else if(REG8(CL) == 0x4b) {
                   11563:                        // lock physical volume
                   11564:                } else if(REG8(CL) == 0x60) {
                   11565:                        // get device parameters
1.1.1.42  root     11566:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11567:                        
1.1.1.42  root     11568:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11569:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11570:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11571:                                
                   11572:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11573:                                switch(geo->MediaType) {
                   11574:                                case F5_360_512:
                   11575:                                case F5_320_512:
                   11576:                                case F5_320_1024:
                   11577:                                case F5_180_512:
                   11578:                                case F5_160_512:
                   11579:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11580:                                        break;
                   11581:                                case F5_1Pt2_512:
                   11582:                                case F3_1Pt2_512:
                   11583:                                case F3_1Pt23_1024:
                   11584:                                case F5_1Pt23_1024:
                   11585:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11586:                                        break;
                   11587:                                case F3_720_512:
                   11588:                                case F3_640_512:
                   11589:                                case F5_640_512:
                   11590:                                case F5_720_512:
                   11591:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11592:                                        break;
                   11593:                                case F8_256_128:
                   11594:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11595:                                        break;
                   11596:                                case FixedMedia:
                   11597:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11598:                                        break;
                   11599:                                case F3_1Pt44_512:
                   11600:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11601:                                        break;
                   11602:                                case F3_2Pt88_512:
                   11603:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11604:                                        break;
                   11605:                                default:
                   11606:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11607: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11608:                                        break;
1.1.1.22  root     11609:                                }
1.1.1.42  root     11610:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11611:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11612:                                switch(geo->MediaType) {
                   11613:                                case F5_360_512:
                   11614:                                case F5_320_512:
                   11615:                                case F5_320_1024:
                   11616:                                case F5_180_512:
                   11617:                                case F5_160_512:
                   11618:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11619:                                        break;
                   11620:                                default:
                   11621:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11622:                                        break;
                   11623:                                }
                   11624:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11625:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11626:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11627:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11628:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11629:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11630:                                switch(geo->MediaType) {
                   11631:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11632:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11633:                                        break;
                   11634:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11635:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11636:                                        break;
                   11637:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11638:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11639:                                        break;
                   11640:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11641:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11642:                                        break;
                   11643:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11644:                                case F3_1Pt2_512:
                   11645:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11646:                                case F5_720_512:
                   11647:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11648:                                        break;
                   11649:                                case FixedMedia:        // hard disk
                   11650:                                case RemovableMedia:
                   11651:                                case Unknown:
                   11652:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11653:                                        break;
                   11654:                                default:
                   11655:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11656:                                        break;
                   11657:                                }
                   11658:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11659:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11660:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11661:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11662:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11663:                                // 21h  BYTE    device type
                   11664:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11665:                        } else {
                   11666:                                REG16(AX) = 0x0f; // invalid drive
                   11667:                                m_CF = 1;
                   11668:                        }
1.1.1.48  root     11669: //             } else if(REG8(CL) == 0x61) {
                   11670: //                     // read logical device track
                   11671: //             } else if(REG8(CL) == 0x62) {
                   11672: //                     // verify logical device track
1.1.1.22  root     11673:                } else if(REG8(CL) == 0x66) {
                   11674:                        // get volume serial number
                   11675:                        char path[] = "A:\\";
                   11676:                        char volume_label[MAX_PATH];
                   11677:                        DWORD serial_number = 0;
                   11678:                        char file_system[MAX_PATH];
                   11679:                        
                   11680:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11681:                        
                   11682:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11683:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11684:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11685:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11686:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11687:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11688:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11689:                        } else {
                   11690:                                REG16(AX) = 0x0f; // invalid drive
                   11691:                                m_CF = 1;
                   11692:                        }
                   11693:                } else if(REG8(CL) == 0x67) {
                   11694:                        // get access flag
                   11695:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11696:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11697:                } else if(REG8(CL) == 0x68) {
                   11698:                        // sense media type
1.1.1.42  root     11699:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11700:                        
1.1.1.42  root     11701:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11702:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11703:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11704:                                
                   11705:                                switch(geo->MediaType) {
                   11706:                                case F3_720_512:
                   11707:                                case F5_720_512:
                   11708:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11709:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11710:                                        break;
                   11711:                                case F3_1Pt44_512:
                   11712:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11713:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11714:                                        break;
                   11715:                                case F3_2Pt88_512:
                   11716:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11717:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11718:                                        break;
                   11719:                                default:
                   11720:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11721:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11722:                                        break;
1.1.1.22  root     11723:                                }
                   11724:                        } else {
                   11725:                                REG16(AX) = 0x0f; // invalid drive
                   11726:                                m_CF = 1;
                   11727:                        }
                   11728:                } else if(REG8(CL) == 0x6a) {
                   11729:                        // unlock logical volume
                   11730:                } else if(REG8(CL) == 0x6b) {
                   11731:                        // unlock physical volume
1.1.1.48  root     11732: //             } else if(REG8(CL) == 0x6c) {
                   11733: //                     // get lock flag
                   11734: //             } else if(REG8(CL) == 0x6d) {
                   11735: //                     // enumerate open files
                   11736: //             } else if(REG8(CL) == 0x6e) {
                   11737: //                     // find swap file
                   11738: //             } else if(REG8(CL) == 0x6f) {
                   11739: //                     // get drive map information
                   11740: //             } else if(REG8(CL) == 0x70) {
                   11741: //                     // get current lock state
                   11742: //             } else if(REG8(CL) == 0x71) {
                   11743: //                     // get first cluster
1.1.1.22  root     11744:                } else {
                   11745:                        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));
                   11746:                        REG16(AX) = 0x01; // invalid function
                   11747:                        m_CF = 1;
                   11748:                }
                   11749:                break;
1.1.1.48  root     11750:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11751:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11752:                        REG16(AX) = 0x0f; // invalid drive
                   11753:                        m_CF = 1;
                   11754:                } else {
                   11755:                        REG8(AL) = 0;
1.1.1.22  root     11756:                }
                   11757:                break;
1.1.1.48  root     11758:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11759:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11760:                        REG16(AX) = 0x0f; // invalid drive
                   11761:                        m_CF = 1;
1.1.1.22  root     11762:                }
                   11763:                break;
1.1.1.48  root     11764:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11765:                switch(REG8(CL)) {
                   11766:                case 0x45:
                   11767:                case 0x4a:
1.1.1.48  root     11768:                case 0x4c:
                   11769:                case 0x4d:
1.1.1.22  root     11770:                case 0x65:
                   11771:                case 0x6a:
1.1.1.48  root     11772:                case 0x6b:
1.1.1.22  root     11773:                case 0x7f:
                   11774:                        REG16(AX) = 0x0000; // supported
                   11775:                        break;
                   11776:                default:
                   11777:                        REG8(AL) = 0x01; // ioctl capability not available
                   11778:                        m_CF = 1;
                   11779:                        break;
                   11780:                }
                   11781:                break;
1.1.1.48  root     11782:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     11783:                switch(REG8(CL)) {
                   11784:                case 0x40:
                   11785:                case 0x46:
                   11786:                case 0x4a:
                   11787:                case 0x4b:
                   11788:                case 0x60:
                   11789:                case 0x66:
                   11790:                case 0x67:
                   11791:                case 0x68:
                   11792:                case 0x6a:
                   11793:                case 0x6b:
1.1.1.48  root     11794:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   11795:                                // CH = 00h     Unknown
                   11796:                                // CH = 01h     COMn:
                   11797:                                // CH = 03h     CON
                   11798:                                // CH = 05h     LPTn:
                   11799:                                REG16(AX) = 0x0000; // supported
                   11800:                                break;
                   11801:                        }
1.1.1.22  root     11802:                default:
                   11803:                        REG8(AL) = 0x01; // ioctl capability not available
                   11804:                        m_CF = 1;
                   11805:                        break;
                   11806:                }
                   11807:                break;
1.1.1.48  root     11808:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   11809:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   11810:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   11811:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   11812:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   11813:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   11814:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   11815:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   11816:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   11817:        case 0x59: // DR Multiuser DOS 5.0 - API
                   11818:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     11819:                m_CF = 1;
                   11820:                break;
1.1       root     11821:        default:
1.1.1.22  root     11822:                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     11823:                REG16(AX) = 0x01;
1.1.1.3   root     11824:                m_CF = 1;
1.1       root     11825:                break;
                   11826:        }
                   11827: }
                   11828: 
                   11829: inline void msdos_int_21h_45h()
                   11830: {
                   11831:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11832:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11833:        
1.1.1.20  root     11834:        if(fd < process->max_files && file_handler[fd].valid) {
                   11835:                int dup_fd = _dup(fd);
                   11836:                if(dup_fd != -1) {
                   11837:                        REG16(AX) = dup_fd;
                   11838:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11839: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11840:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11841:                } else {
                   11842:                        REG16(AX) = errno;
1.1.1.3   root     11843:                        m_CF = 1;
1.1       root     11844:                }
                   11845:        } else {
                   11846:                REG16(AX) = 0x06;
1.1.1.3   root     11847:                m_CF = 1;
1.1       root     11848:        }
                   11849: }
                   11850: 
                   11851: inline void msdos_int_21h_46h()
                   11852: {
                   11853:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11854:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11855:        int dup_fd = REG16(CX);
                   11856:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11857:        
1.1.1.20  root     11858:        if(REG16(BX) == REG16(CX)) {
                   11859:                REG16(AX) = 0x06;
                   11860:                m_CF = 1;
                   11861:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11862:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11863:                        _close(tmp_fd);
                   11864:                        msdos_file_handler_close(tmp_fd);
                   11865:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11866:                }
                   11867:                if(_dup2(fd, dup_fd) != -1) {
                   11868:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11869: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11870:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11871:                } else {
                   11872:                        REG16(AX) = errno;
1.1.1.3   root     11873:                        m_CF = 1;
1.1       root     11874:                }
                   11875:        } else {
                   11876:                REG16(AX) = 0x06;
1.1.1.3   root     11877:                m_CF = 1;
1.1       root     11878:        }
                   11879: }
                   11880: 
                   11881: inline void msdos_int_21h_47h(int lfn)
                   11882: {
                   11883:        char path[MAX_PATH];
                   11884:        
                   11885:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     11886:                if(!lfn) {
                   11887:                        strcpy(path, msdos_short_path(path));
                   11888:                }
1.1       root     11889:                if(path[1] == ':') {
                   11890:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     11891:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11892:                } else {
1.1.1.45  root     11893:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11894:                }
                   11895:        } else {
                   11896:                REG16(AX) = errno;
1.1.1.3   root     11897:                m_CF = 1;
1.1       root     11898:        }
                   11899: }
                   11900: 
                   11901: inline void msdos_int_21h_48h()
                   11902: {
1.1.1.19  root     11903:        int seg, umb_linked;
1.1       root     11904:        
1.1.1.8   root     11905:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11906:                // unlink umb not to allocate memory in umb
                   11907:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11908:                        msdos_mem_unlink_umb();
                   11909:                }
1.1.1.8   root     11910:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11911:                        REG16(AX) = seg;
                   11912:                } else {
                   11913:                        REG16(AX) = 0x08;
                   11914:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11915:                        m_CF = 1;
                   11916:                }
1.1.1.19  root     11917:                if(umb_linked != 0) {
                   11918:                        msdos_mem_link_umb();
                   11919:                }
1.1.1.8   root     11920:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11921:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11922:                        REG16(AX) = seg;
                   11923:                } else {
                   11924:                        REG16(AX) = 0x08;
                   11925:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11926:                        m_CF = 1;
                   11927:                }
                   11928:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11929:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11930:                        REG16(AX) = seg;
                   11931:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11932:                        REG16(AX) = seg;
                   11933:                } else {
                   11934:                        REG16(AX) = 0x08;
                   11935:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11936:                        m_CF = 1;
                   11937:                }
1.1       root     11938:        }
                   11939: }
                   11940: 
                   11941: inline void msdos_int_21h_49h()
                   11942: {
1.1.1.14  root     11943:        int mcb_seg = SREG(ES) - 1;
                   11944:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11945:        
                   11946:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11947:                msdos_mem_free(SREG(ES));
                   11948:        } else {
1.1.1.33  root     11949:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11950:                m_CF = 1;
                   11951:        }
1.1       root     11952: }
                   11953: 
                   11954: inline void msdos_int_21h_4ah()
                   11955: {
1.1.1.14  root     11956:        int mcb_seg = SREG(ES) - 1;
                   11957:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11958:        int max_paragraphs;
                   11959:        
1.1.1.14  root     11960:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11961:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11962:                        REG16(AX) = 0x08;
                   11963:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11964:                        m_CF = 1;
                   11965:                }
                   11966:        } else {
1.1.1.33  root     11967:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11968:                m_CF = 1;
1.1       root     11969:        }
                   11970: }
                   11971: 
                   11972: inline void msdos_int_21h_4bh()
                   11973: {
1.1.1.3   root     11974:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11975:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11976:        
                   11977:        switch(REG8(AL)) {
                   11978:        case 0x00:
                   11979:        case 0x01:
                   11980:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11981:                        REG16(AX) = 0x02;
1.1.1.3   root     11982:                        m_CF = 1;
1.1       root     11983:                }
                   11984:                break;
1.1.1.14  root     11985:        case 0x03:
                   11986:                {
                   11987:                        int fd;
                   11988:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11989:                                REG16(AX) = 0x02;
                   11990:                                m_CF = 1;
                   11991:                                break;
                   11992:                        }
                   11993:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11994:                        _close(fd);
                   11995:                        
                   11996:                        UINT16 *overlay = (UINT16 *)param;
                   11997:                        
                   11998:                        // check exe header
                   11999:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   12000:                        int header_size = 0;
                   12001:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   12002:                                header_size = header->header_size * 16;
                   12003:                                // relocation
                   12004:                                int start_seg = overlay[1];
                   12005:                                for(int i = 0; i < header->relocations; i++) {
                   12006:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   12007:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   12008:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   12009:                                }
                   12010:                        }
                   12011:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   12012:                }
                   12013:                break;
1.1.1.48  root     12014:        case 0x04:
                   12015:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   12016: //     case 0x05:
                   12017: //             // DOS 5+ - Set Execution State
                   12018:        case 0x80:
                   12019:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   12020:        case 0xf0:
                   12021:        case 0xf1:
                   12022:                // DIET v1.10+
1.1.1.43  root     12023:        case 0xfd:
                   12024:        case 0xfe:
                   12025:                // unknown function called in FreeCOM
                   12026:                REG16(AX) = 0x01;
                   12027:                m_CF = 1;
                   12028:                break;
1.1       root     12029:        default:
1.1.1.22  root     12030:                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     12031:                REG16(AX) = 0x01;
1.1.1.3   root     12032:                m_CF = 1;
1.1       root     12033:                break;
                   12034:        }
                   12035: }
                   12036: 
                   12037: inline void msdos_int_21h_4ch()
                   12038: {
                   12039:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   12040: }
                   12041: 
                   12042: inline void msdos_int_21h_4dh()
                   12043: {
                   12044:        REG16(AX) = retval;
                   12045: }
                   12046: 
                   12047: inline void msdos_int_21h_4eh()
                   12048: {
                   12049:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12050:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12051:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     12052:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12053:        WIN32_FIND_DATA fd;
                   12054:        
1.1.1.14  root     12055:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12056:        find->find_magic = FIND_MAGIC;
                   12057:        find->dta_index = dtainfo - dtalist;
1.1       root     12058:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12059:        dtainfo->allowable_mask = REG8(CL);
1.1.1.58  root     12060:        // note: SO1 dir command sets 0x3f, but only directories and volue label are found if bit3 is set :-(
                   12061:        if((dtainfo->allowable_mask & 0x3f) == 0x3f) {
                   12062:                dtainfo->allowable_mask &= ~0x08;
                   12063:        }
1.1.1.14  root     12064:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12065:        
1.1.1.14  root     12066:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12067:                dtainfo->allowable_mask &= ~8;
1.1       root     12068:        }
1.1.1.14  root     12069:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12070:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12071:                      !msdos_find_file_has_8dot3name(&fd)) {
                   12072:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12073:                                FindClose(dtainfo->find_handle);
                   12074:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12075:                                break;
                   12076:                        }
                   12077:                }
                   12078:        }
1.1.1.13  root     12079:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12080:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12081:                msdos_find_file_conv_local_time(&fd);
                   12082:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12083:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12084:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12085:                REG16(AX) = 0;
1.1.1.14  root     12086:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12087:                find->attrib = 8;
                   12088:                find->size = 0;
                   12089:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12090:                dtainfo->allowable_mask &= ~8;
1.1       root     12091:                REG16(AX) = 0;
                   12092:        } else {
                   12093:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12094:                m_CF = 1;
1.1       root     12095:        }
                   12096: }
                   12097: 
                   12098: inline void msdos_int_21h_4fh()
                   12099: {
                   12100:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12101:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12102:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     12103:        WIN32_FIND_DATA fd;
                   12104:        
1.1.1.14  root     12105:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   12106:                REG16(AX) = 0x12;
                   12107:                m_CF = 1;
                   12108:                return;
                   12109:        }
                   12110:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     12111:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12112:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12113:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12114:                              !msdos_find_file_has_8dot3name(&fd)) {
                   12115:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12116:                                        FindClose(dtainfo->find_handle);
                   12117:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12118:                                        break;
                   12119:                                }
                   12120:                        }
                   12121:                } else {
1.1.1.13  root     12122:                        FindClose(dtainfo->find_handle);
                   12123:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12124:                }
                   12125:        }
1.1.1.13  root     12126:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12127:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12128:                msdos_find_file_conv_local_time(&fd);
                   12129:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12130:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12131:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12132:                REG16(AX) = 0;
1.1.1.14  root     12133:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12134:                find->attrib = 8;
                   12135:                find->size = 0;
                   12136:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12137:                dtainfo->allowable_mask &= ~8;
1.1       root     12138:                REG16(AX) = 0;
                   12139:        } else {
                   12140:                REG16(AX) = 0x12;
1.1.1.3   root     12141:                m_CF = 1;
1.1       root     12142:        }
                   12143: }
                   12144: 
                   12145: inline void msdos_int_21h_50h()
                   12146: {
1.1.1.8   root     12147:        if(current_psp != REG16(BX)) {
                   12148:                process_t *process = msdos_process_info_get(current_psp);
                   12149:                if(process != NULL) {
                   12150:                        process->psp = REG16(BX);
                   12151:                }
                   12152:                current_psp = REG16(BX);
1.1.1.23  root     12153:                msdos_sda_update(current_psp);
1.1.1.8   root     12154:        }
1.1       root     12155: }
                   12156: 
                   12157: inline void msdos_int_21h_51h()
                   12158: {
                   12159:        REG16(BX) = current_psp;
                   12160: }
                   12161: 
                   12162: inline void msdos_int_21h_52h()
                   12163: {
1.1.1.25  root     12164:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     12165:        i386_load_segment_descriptor(ES);
1.1.1.25  root     12166:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     12167: }
                   12168: 
1.1.1.43  root     12169: inline void msdos_int_21h_53h()
                   12170: {
                   12171:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   12172:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   12173:        
                   12174:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   12175:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   12176:        dpb->shift_count = 0;
                   12177:        dpb->reserved_sectors = 0;
                   12178:        dpb->fat_num = bpb->fat_num;
                   12179:        dpb->root_entries = bpb->root_entries;
                   12180:        dpb->first_data_sector = 0;
                   12181:        if(bpb->sectors_per_cluster != 0) {
                   12182:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   12183:        } else {
                   12184:                dpb->highest_cluster_num = 0;
                   12185:        }
                   12186:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   12187:        dpb->first_dir_sector = 0;
                   12188:        dpb->device_driver_header = 0;
                   12189:        dpb->media_type = bpb->media_type;
                   12190:        dpb->drive_accessed = 0;
                   12191:        dpb->next_dpb_ofs = 0xffff;
                   12192:        dpb->next_dpb_seg = 0xffff;
                   12193:        dpb->first_free_cluster = 0;
                   12194:        dpb->free_clusters = 0xffff;
                   12195: }
                   12196: 
1.1       root     12197: inline void msdos_int_21h_54h()
                   12198: {
                   12199:        process_t *process = msdos_process_info_get(current_psp);
                   12200:        
                   12201:        REG8(AL) = process->verify;
                   12202: }
                   12203: 
                   12204: inline void msdos_int_21h_55h()
                   12205: {
                   12206:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   12207:        
                   12208:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   12209:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   12210:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   12211:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   12212:        psp->parent_psp = current_psp;
                   12213: }
                   12214: 
                   12215: inline void msdos_int_21h_56h(int lfn)
                   12216: {
                   12217:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     12218:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   12219:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     12220:        
                   12221:        if(rename(src, dst)) {
                   12222:                REG16(AX) = errno;
1.1.1.3   root     12223:                m_CF = 1;
1.1       root     12224:        }
                   12225: }
                   12226: 
                   12227: inline void msdos_int_21h_57h()
                   12228: {
                   12229:        FILETIME time, local;
1.1.1.14  root     12230:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     12231:        HANDLE hHandle;
1.1       root     12232:        
1.1.1.21  root     12233:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     12234:                REG16(AX) = (UINT16)GetLastError();
                   12235:                m_CF = 1;
                   12236:                return;
                   12237:        }
                   12238:        ctime = atime = mtime = NULL;
                   12239:        
1.1       root     12240:        switch(REG8(AL)) {
                   12241:        case 0x00:
1.1.1.6   root     12242:        case 0x01:
1.1.1.14  root     12243:                mtime = &time;
1.1.1.6   root     12244:                break;
                   12245:        case 0x04:
                   12246:        case 0x05:
1.1.1.14  root     12247:                atime = &time;
1.1       root     12248:                break;
1.1.1.6   root     12249:        case 0x06:
                   12250:        case 0x07:
1.1.1.14  root     12251:                ctime = &time;
                   12252:                break;
                   12253:        default:
1.1.1.22  root     12254:                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     12255:                REG16(AX) = 0x01;
                   12256:                m_CF = 1;
                   12257:                return;
                   12258:        }
                   12259:        if(REG8(AL) & 1) {
1.1       root     12260:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12261:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12262:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12263:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12264:                        m_CF = 1;
1.1       root     12265:                }
1.1.1.14  root     12266:        } else {
1.1.1.21  root     12267:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12268:                        // assume a device and use the current time
                   12269:                        GetSystemTimeAsFileTime(&time);
                   12270:                }
                   12271:                FileTimeToLocalFileTime(&time, &local);
                   12272:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12273:        }
                   12274: }
                   12275: 
                   12276: inline void msdos_int_21h_58h()
                   12277: {
                   12278:        switch(REG8(AL)) {
                   12279:        case 0x00:
1.1.1.7   root     12280:                REG16(AX) = malloc_strategy;
                   12281:                break;
                   12282:        case 0x01:
1.1.1.24  root     12283: //             switch(REG16(BX)) {
                   12284:                switch(REG8(BL)) {
1.1.1.7   root     12285:                case 0x0000:
                   12286:                case 0x0001:
                   12287:                case 0x0002:
                   12288:                case 0x0040:
                   12289:                case 0x0041:
                   12290:                case 0x0042:
                   12291:                case 0x0080:
                   12292:                case 0x0081:
                   12293:                case 0x0082:
                   12294:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12295:                        msdos_sda_update(current_psp);
1.1.1.7   root     12296:                        break;
                   12297:                default:
1.1.1.22  root     12298:                        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     12299:                        REG16(AX) = 0x01;
                   12300:                        m_CF = 1;
                   12301:                        break;
                   12302:                }
                   12303:                break;
                   12304:        case 0x02:
1.1.1.19  root     12305:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12306:                break;
                   12307:        case 0x03:
1.1.1.24  root     12308: //             switch(REG16(BX)) {
                   12309:                switch(REG8(BL)) {
1.1.1.7   root     12310:                case 0x0000:
1.1.1.19  root     12311:                        msdos_mem_unlink_umb();
                   12312:                        break;
1.1.1.7   root     12313:                case 0x0001:
1.1.1.19  root     12314:                        msdos_mem_link_umb();
1.1.1.7   root     12315:                        break;
                   12316:                default:
1.1.1.22  root     12317:                        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     12318:                        REG16(AX) = 0x01;
                   12319:                        m_CF = 1;
                   12320:                        break;
                   12321:                }
1.1       root     12322:                break;
                   12323:        default:
1.1.1.22  root     12324:                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     12325:                REG16(AX) = 0x01;
1.1.1.3   root     12326:                m_CF = 1;
1.1       root     12327:                break;
                   12328:        }
                   12329: }
                   12330: 
                   12331: inline void msdos_int_21h_59h()
                   12332: {
1.1.1.47  root     12333:        if(REG16(BX) == 0x0000) {
                   12334:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12335:                
                   12336:                REG16(AX) = sda->extended_error_code;
                   12337:                REG8(BH)  = sda->error_class;
                   12338:                REG8(BL)  = sda->suggested_action;
                   12339:                REG8(CH)  = sda->locus_of_last_error;
                   12340:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12341:                if(sda->int21h_5d0ah_called != 0) {
                   12342:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12343:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12344: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12345:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12346: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12347: //                     i386_load_segment_descriptor(DS);
                   12348:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12349:                        i386_load_segment_descriptor(ES);
                   12350:                }
                   12351:                sda->int21h_5d0ah_called = 0;
                   12352: //     } else if(REG16(BX) == 0x0001) {
                   12353: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12354:        } else {
                   12355:                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));
                   12356:                REG16(AX) = 0x01;
                   12357:                m_CF = 1;
                   12358:        }
1.1       root     12359: }
                   12360: 
                   12361: inline void msdos_int_21h_5ah()
                   12362: {
1.1.1.3   root     12363:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12364:        int len = strlen(path);
                   12365:        char tmp[MAX_PATH];
                   12366:        
                   12367:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   12368:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12369:                
                   12370:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12371:                REG16(AX) = fd;
                   12372:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12373:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12374:                
                   12375:                strcpy(path, tmp);
                   12376:                int dx = REG16(DX) + len;
1.1.1.3   root     12377:                int ds = SREG(DS);
1.1       root     12378:                while(dx > 0xffff) {
                   12379:                        dx -= 0x10;
                   12380:                        ds++;
                   12381:                }
                   12382:                REG16(DX) = dx;
1.1.1.3   root     12383:                SREG(DS) = ds;
                   12384:                i386_load_segment_descriptor(DS);
1.1       root     12385:        } else {
                   12386:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12387:                m_CF = 1;
1.1       root     12388:        }
                   12389: }
                   12390: 
                   12391: inline void msdos_int_21h_5bh()
                   12392: {
1.1.1.45  root     12393:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12394:        
1.1.1.45  root     12395: //     if(msdos_is_existing_file(path)) {
                   12396:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12397:                // already exists
                   12398:                REG16(AX) = 0x50;
1.1.1.3   root     12399:                m_CF = 1;
1.1       root     12400:        } else {
                   12401:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12402:                
                   12403:                if(fd != -1) {
                   12404:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12405:                        REG16(AX) = fd;
                   12406:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12407:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12408:                } else {
                   12409:                        REG16(AX) = errno;
1.1.1.3   root     12410:                        m_CF = 1;
1.1       root     12411:                }
                   12412:        }
                   12413: }
                   12414: 
                   12415: inline void msdos_int_21h_5ch()
                   12416: {
                   12417:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12418:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12419:        
1.1.1.20  root     12420:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12421:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12422:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12423:                        UINT32 pos = _tell(fd);
                   12424:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12425:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12426:                                REG16(AX) = errno;
1.1.1.3   root     12427:                                m_CF = 1;
1.1       root     12428:                        }
1.1.1.20  root     12429:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12430:                        
1.1       root     12431:                        // some seconds may be passed in _locking()
1.1.1.35  root     12432:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12433:                } else {
                   12434:                        REG16(AX) = 0x01;
1.1.1.3   root     12435:                        m_CF = 1;
1.1       root     12436:                }
                   12437:        } else {
                   12438:                REG16(AX) = 0x06;
1.1.1.3   root     12439:                m_CF = 1;
1.1       root     12440:        }
                   12441: }
                   12442: 
1.1.1.22  root     12443: inline void msdos_int_21h_5dh()
                   12444: {
                   12445:        switch(REG8(AL)) {
1.1.1.45  root     12446:        case 0x00:
                   12447:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12448:                        // current system
                   12449:                        static bool reenter = false;
                   12450:                        if(!reenter) {
                   12451:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12452:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12453:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12454:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12455:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12456:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12457:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12458:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12459:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12460:                                i386_load_segment_descriptor(DS);
                   12461:                                i386_load_segment_descriptor(ES);
                   12462:                                reenter = true;
                   12463:                                try {
                   12464:                                        msdos_syscall(0x21);
                   12465:                                } catch(...) {
                   12466:                                }
                   12467:                                reenter = false;
                   12468:                        }
                   12469:                } else {
                   12470:                        REG16(AX) = 0x49; //  network software not installed
                   12471:                        m_CF = 1;
                   12472:                }
                   12473:                break;
1.1.1.22  root     12474:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     12475:                SREG(DS) = (SDA_TOP >> 4);
                   12476:                i386_load_segment_descriptor(DS);
                   12477:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12478:                REG16(CX) = 0x80;
                   12479:                REG16(DX) = 0x1a;
                   12480:                break;
1.1.1.45  root     12481:        case 0x07: // get redirected printer mode
                   12482:        case 0x08: // set redirected printer mode
                   12483:        case 0x09: // flush redirected printer output
                   12484:                REG16(AX) = 0x49; //  network software not installed
                   12485:                m_CF = 1;
                   12486:                break;
1.1.1.43  root     12487:        case 0x0a: // set extended error information
                   12488:                {
                   12489:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12490:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12491:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12492:                        // XXX: which one is correct ???
                   12493: #if 1
                   12494:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12495:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12496:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12497:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12498:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12499: #else
                   12500:                        // PC DOS 7 Technical Update
                   12501:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12502:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12503:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12504:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12505: #endif
                   12506:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12507: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12508:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12509: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12510:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12511:                }
                   12512:                break;
1.1.1.23  root     12513:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12514:                REG16(AX) = 0x01;
                   12515:                m_CF = 1;
                   12516:                break;
                   12517:        default:
                   12518:                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));
                   12519:                REG16(AX) = 0x01;
                   12520:                m_CF = 1;
                   12521:                break;
                   12522:        }
                   12523: }
                   12524: 
1.1.1.42  root     12525: inline void msdos_int_21h_5eh()
                   12526: {
                   12527:        switch(REG8(AL)) {
                   12528:        case 0x00:
                   12529:                {
                   12530:                        char name[256] = {0};
                   12531:                        DWORD dwSize = 256;
                   12532:                        
                   12533:                        if(GetComputerName(name, &dwSize)) {
                   12534:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12535:                                for(int i = 0; i < 15; i++) {
                   12536:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12537:                                }
                   12538:                                dest[15] = '\0';
                   12539:                                REG8(CH) = 0x01; // nonzero valid
                   12540:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12541:                        } else {
                   12542:                                REG16(AX) = 0x01;
                   12543:                                m_CF = 1;
                   12544:                        }
                   12545:                }
                   12546:                break;
                   12547:        default:
1.1.1.45  root     12548: //             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));
                   12549: //             REG16(AX) = 0x01;
                   12550:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12551:                m_CF = 1;
                   12552:                break;
                   12553:        }
                   12554: }
                   12555: 
1.1.1.30  root     12556: inline void msdos_int_21h_5fh()
                   12557: {
                   12558:        switch(REG8(AL)) {
1.1.1.42  root     12559:        case 0x05:
1.1.1.44  root     12560:                REG16(BP) = 0;
                   12561:                for(int i = 0; i < 26; i++) {
                   12562:                        if(msdos_is_remote_drive(i)) {
                   12563:                                REG16(BP)++;
1.1.1.42  root     12564:                        }
                   12565:                }
1.1.1.30  root     12566:        case 0x02:
1.1.1.44  root     12567:                for(int i = 0, index = 0; i < 26; i++) {
                   12568:                        if(msdos_is_remote_drive(i)) {
                   12569:                                if(index == REG16(BX)) {
                   12570:                                        char volume[] = "A:";
1.1.1.30  root     12571:                                        volume[0] = 'A' + i;
1.1.1.44  root     12572:                                        DWORD dwSize = 128;
                   12573:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   12574:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   12575:                                        REG8(BH) = 0x00; // valid
                   12576:                                        REG8(BL) = 0x04; // disk drive
                   12577:                                        REG16(CX) = 0x00; // LANtastic
                   12578:                                        return;
1.1.1.30  root     12579:                                }
1.1.1.44  root     12580:                                index++;
1.1.1.30  root     12581:                        }
                   12582:                }
                   12583:                REG16(AX) = 0x12; // no more files
                   12584:                m_CF = 1;
                   12585:                break;
1.1.1.44  root     12586:        case 0x07:
                   12587:                if(msdos_is_valid_drive(REG8(DL))) {
                   12588:                        msdos_cds_update(REG8(DL));
                   12589:                } else {
                   12590:                        REG16(AX) = 0x0f; // invalid drive
                   12591:                        m_CF = 1;
                   12592:                }
                   12593:                break;
                   12594:        case 0x08:
                   12595:                if(msdos_is_valid_drive(REG8(DL))) {
                   12596:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12597:                        cds->drive_attrib = 0x0000;
                   12598:                } else {
                   12599:                        REG16(AX) = 0x0f; // invalid drive
                   12600:                        m_CF = 1;
                   12601:                }
                   12602:                break;
1.1.1.30  root     12603:        default:
1.1.1.45  root     12604: //             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));
                   12605: //             REG16(AX) = 0x01;
                   12606:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12607:                m_CF = 1;
                   12608:                break;
                   12609:        }
                   12610: }
                   12611: 
1.1       root     12612: inline void msdos_int_21h_60h(int lfn)
                   12613: {
1.1.1.45  root     12614:        char full[MAX_PATH];
                   12615:        const char *path = NULL;
1.1.1.14  root     12616:        
1.1       root     12617:        if(lfn) {
1.1.1.14  root     12618:                char *name;
                   12619:                *full = '\0';
1.1.1.3   root     12620:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12621:                switch(REG8(CL)) {
                   12622:                case 1:
                   12623:                        GetShortPathName(full, full, MAX_PATH);
                   12624:                        my_strupr(full);
                   12625:                        break;
                   12626:                case 2:
                   12627:                        GetLongPathName(full, full, MAX_PATH);
                   12628:                        break;
                   12629:                }
                   12630:                path = full;
                   12631:        } else {
                   12632:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12633:        }
                   12634:        if(*path != '\0') {
                   12635:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12636:        } else {
1.1.1.14  root     12637:                REG16(AX) = (UINT16)GetLastError();
                   12638:                m_CF = 1;
1.1       root     12639:        }
                   12640: }
                   12641: 
                   12642: inline void msdos_int_21h_61h()
                   12643: {
                   12644:        REG8(AL) = 0;
                   12645: }
                   12646: 
                   12647: inline void msdos_int_21h_62h()
                   12648: {
                   12649:        REG16(BX) = current_psp;
                   12650: }
                   12651: 
                   12652: inline void msdos_int_21h_63h()
                   12653: {
                   12654:        switch(REG8(AL)) {
                   12655:        case 0x00:
1.1.1.3   root     12656:                SREG(DS) = (DBCS_TABLE >> 4);
                   12657:                i386_load_segment_descriptor(DS);
1.1       root     12658:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12659:                REG8(AL) = 0x00;
                   12660:                break;
1.1.1.22  root     12661:        case 0x01: // set korean input mode
                   12662:        case 0x02: // get korean input mode
                   12663:                REG8(AL) = 0xff; // not supported
                   12664:                break;
1.1       root     12665:        default:
1.1.1.22  root     12666:                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     12667:                REG16(AX) = 0x01;
1.1.1.3   root     12668:                m_CF = 1;
1.1       root     12669:                break;
                   12670:        }
                   12671: }
                   12672: 
1.1.1.25  root     12673: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12674: {
1.1.1.25  root     12675:        switch(func) {
1.1.1.17  root     12676:        case 0x01:
                   12677:                if(REG16(CX) >= 5) {
1.1.1.19  root     12678:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12679:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12680:                                REG16(CX) = sizeof(data);
                   12681:                        ZeroMemory(data, sizeof(data));
                   12682:                        data[0] = 0x01;
                   12683:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12684:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12685:                        *(UINT16 *)(data + 5) = active_code_page;
                   12686:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12687: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12688:                } else {
1.1.1.25  root     12689:                        return(0x08); // insufficient memory
1.1.1.17  root     12690:                }
                   12691:                break;
                   12692:        case 0x02:
                   12693:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12694:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12695:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12696: //             REG16(AX) = active_code_page;
1.1.1.17  root     12697:                REG16(CX) = 0x05;
                   12698:                break;
1.1.1.23  root     12699:        case 0x03:
                   12700:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12701:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12702:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12703: //             REG16(AX) = active_code_page;
1.1.1.23  root     12704:                REG16(CX) = 0x05;
                   12705:                break;
1.1.1.17  root     12706:        case 0x04:
                   12707:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12708:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12709:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12710: //             REG16(AX) = active_code_page;
1.1.1.17  root     12711:                REG16(CX) = 0x05;
                   12712:                break;
                   12713:        case 0x05:
                   12714:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12715:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12716:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12717: //             REG16(AX) = active_code_page;
1.1.1.17  root     12718:                REG16(CX) = 0x05;
                   12719:                break;
                   12720:        case 0x06:
                   12721:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12722:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12723:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12724: //             REG16(AX) = active_code_page;
1.1.1.17  root     12725:                REG16(CX) = 0x05;
                   12726:                break;
1.1       root     12727:        case 0x07:
1.1.1.3   root     12728:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12729:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12730:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12731: //             REG16(AX) = active_code_page;
1.1       root     12732:                REG16(CX) = 0x05;
                   12733:                break;
1.1.1.25  root     12734:        default:
                   12735:                return(0x01); // function number invalid
                   12736:        }
                   12737:        return(0x00);
                   12738: }
                   12739: 
                   12740: inline void msdos_int_21h_65h()
                   12741: {
                   12742:        char tmp[0x10000];
                   12743:        
                   12744:        switch(REG8(AL)) {
1.1.1.43  root     12745:        case 0x00:
                   12746:                if(REG16(CX) >= 7) {
                   12747:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12748:                        REG16(AX) = system_code_page;
                   12749:                } else {
                   12750:                        REG16(AX) = 0x0c;
                   12751:                        m_CF = 1;
                   12752:                }
                   12753:                break;
1.1.1.25  root     12754:        case 0x01:
                   12755:        case 0x02:
                   12756:        case 0x03:
                   12757:        case 0x04:
                   12758:        case 0x05:
                   12759:        case 0x06:
                   12760:        case 0x07:
                   12761:                {
                   12762:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12763:                        if(result) {
                   12764:                                REG16(AX) = result;
                   12765:                                m_CF = 1;
                   12766:                        } else {
                   12767:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12768:                        }
                   12769:                }
                   12770:                break;
1.1       root     12771:        case 0x20:
1.1.1.25  root     12772:        case 0xa0:
1.1.1.19  root     12773:                memset(tmp, 0, sizeof(tmp));
                   12774:                tmp[0] = REG8(DL);
1.1       root     12775:                my_strupr(tmp);
                   12776:                REG8(DL) = tmp[0];
                   12777:                break;
                   12778:        case 0x21:
1.1.1.25  root     12779:        case 0xa1:
1.1       root     12780:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12781:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12782:                my_strupr(tmp);
1.1.1.3   root     12783:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12784:                break;
                   12785:        case 0x22:
1.1.1.25  root     12786:        case 0xa2:
1.1.1.3   root     12787:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12788:                break;
1.1.1.25  root     12789:        case 0x23:
                   12790:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12791:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12792:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12793:                        REG16(AX) = 0x00;
                   12794:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12795:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12796:                        REG16(AX) = 0x01;
                   12797:                } else {
                   12798:                        REG16(AX) = 0x02;
                   12799:                }
                   12800:                break;
1.1       root     12801:        default:
1.1.1.22  root     12802:                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     12803:                REG16(AX) = 0x01;
1.1.1.3   root     12804:                m_CF = 1;
1.1       root     12805:                break;
                   12806:        }
                   12807: }
                   12808: 
                   12809: inline void msdos_int_21h_66h()
                   12810: {
                   12811:        switch(REG8(AL)) {
                   12812:        case 0x01:
                   12813:                REG16(BX) = active_code_page;
                   12814:                REG16(DX) = system_code_page;
                   12815:                break;
                   12816:        case 0x02:
                   12817:                if(active_code_page == REG16(BX)) {
                   12818:                        REG16(AX) = 0xeb41;
                   12819:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12820:                        active_code_page = REG16(BX);
1.1.1.17  root     12821:                        msdos_nls_tables_update();
1.1       root     12822:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12823:                        SetConsoleCP(active_code_page);
                   12824:                        SetConsoleOutputCP(active_code_page);
1.1       root     12825:                } else {
                   12826:                        REG16(AX) = 0x25;
1.1.1.3   root     12827:                        m_CF = 1;
1.1       root     12828:                }
                   12829:                break;
                   12830:        default:
1.1.1.22  root     12831:                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     12832:                REG16(AX) = 0x01;
1.1.1.3   root     12833:                m_CF = 1;
1.1       root     12834:                break;
                   12835:        }
                   12836: }
                   12837: 
                   12838: inline void msdos_int_21h_67h()
                   12839: {
                   12840:        process_t *process = msdos_process_info_get(current_psp);
                   12841:        
                   12842:        if(REG16(BX) <= MAX_FILES) {
                   12843:                process->max_files = max(REG16(BX), 20);
                   12844:        } else {
                   12845:                REG16(AX) = 0x08;
1.1.1.3   root     12846:                m_CF = 1;
1.1       root     12847:        }
                   12848: }
                   12849: 
                   12850: inline void msdos_int_21h_68h()
                   12851: {
                   12852:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12853:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12854:        
1.1.1.20  root     12855:        if(fd < process->max_files && file_handler[fd].valid) {
                   12856:                // fflush(_fdopen(fd, ""));
1.1       root     12857:        } else {
                   12858:                REG16(AX) = 0x06;
1.1.1.3   root     12859:                m_CF = 1;
1.1       root     12860:        }
                   12861: }
                   12862: 
                   12863: inline void msdos_int_21h_69h()
                   12864: {
1.1.1.3   root     12865:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12866:        char path[] = "A:\\";
                   12867:        char volume_label[MAX_PATH];
                   12868:        DWORD serial_number = 0;
                   12869:        char file_system[MAX_PATH];
                   12870:        
                   12871:        if(REG8(BL) == 0) {
                   12872:                path[0] = 'A' + _getdrive() - 1;
                   12873:        } else {
                   12874:                path[0] = 'A' + REG8(BL) - 1;
                   12875:        }
                   12876:        
                   12877:        switch(REG8(AL)) {
                   12878:        case 0x00:
                   12879:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12880:                        info->info_level = 0;
                   12881:                        info->serial_number = serial_number;
                   12882:                        memset(info->volume_label, 0x20, 11);
                   12883:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12884:                        memset(info->file_system, 0x20, 8);
                   12885:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12886:                } else {
                   12887:                        REG16(AX) = errno;
1.1.1.3   root     12888:                        m_CF = 1;
1.1       root     12889:                }
                   12890:                break;
                   12891:        case 0x01:
                   12892:                REG16(AX) = 0x03;
1.1.1.3   root     12893:                m_CF = 1;
1.1.1.45  root     12894:                break;
                   12895:        default:
                   12896:                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));
                   12897:                REG16(AX) = 0x01;
                   12898:                m_CF = 1;
                   12899:                break;
1.1       root     12900:        }
                   12901: }
                   12902: 
                   12903: inline void msdos_int_21h_6ah()
                   12904: {
                   12905:        REG8(AH) = 0x68;
                   12906:        msdos_int_21h_68h();
                   12907: }
                   12908: 
                   12909: inline void msdos_int_21h_6bh()
                   12910: {
1.1.1.45  root     12911:        REG8(AL) = 0x00;
1.1       root     12912: }
                   12913: 
                   12914: inline void msdos_int_21h_6ch(int lfn)
                   12915: {
1.1.1.45  root     12916:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12917:        int mode = REG8(BL) & 0x03;
                   12918:        
                   12919:        if(mode < 0x03) {
1.1.1.29  root     12920:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12921:                        // file exists
                   12922:                        if(REG8(DL) & 1) {
1.1.1.37  root     12923:                                int fd = -1;
                   12924:                                int sio_port = 0;
                   12925:                                int lpt_port = 0;
1.1       root     12926:                                
1.1.1.45  root     12927:                                if(msdos_is_device_path(path)) {
                   12928:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12929:                                } else {
1.1.1.13  root     12930:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12931:                                }
1.1       root     12932:                                if(fd != -1) {
                   12933:                                        REG16(AX) = fd;
                   12934:                                        REG16(CX) = 1;
1.1.1.45  root     12935:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12936:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12937:                                } else {
                   12938:                                        REG16(AX) = errno;
1.1.1.3   root     12939:                                        m_CF = 1;
1.1       root     12940:                                }
                   12941:                        } else if(REG8(DL) & 2) {
                   12942:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12943:                                int fd = -1;
                   12944:                                int sio_port = 0;
                   12945:                                int lpt_port = 0;
1.1       root     12946:                                
1.1.1.45  root     12947:                                if(msdos_is_device_path(path)) {
                   12948:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12949:                                } else {
                   12950:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12951:                                }
                   12952:                                if(fd != -1) {
                   12953:                                        if(attr == -1) {
                   12954:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12955:                                        }
                   12956:                                        SetFileAttributes(path, attr);
                   12957:                                        REG16(AX) = fd;
                   12958:                                        REG16(CX) = 3;
1.1.1.45  root     12959:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12960:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12961:                                } else {
                   12962:                                        REG16(AX) = errno;
1.1.1.3   root     12963:                                        m_CF = 1;
1.1       root     12964:                                }
                   12965:                        } else {
                   12966:                                REG16(AX) = 0x50;
1.1.1.3   root     12967:                                m_CF = 1;
1.1       root     12968:                        }
                   12969:                } else {
                   12970:                        // file not exists
                   12971:                        if(REG8(DL) & 0x10) {
                   12972:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12973:                                
                   12974:                                if(fd != -1) {
                   12975:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12976:                                        REG16(AX) = fd;
                   12977:                                        REG16(CX) = 2;
                   12978:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12979:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12980:                                } else {
                   12981:                                        REG16(AX) = errno;
1.1.1.3   root     12982:                                        m_CF = 1;
1.1       root     12983:                                }
                   12984:                        } else {
                   12985:                                REG16(AX) = 0x02;
1.1.1.3   root     12986:                                m_CF = 1;
1.1       root     12987:                        }
                   12988:                }
                   12989:        } else {
                   12990:                REG16(AX) = 0x0c;
1.1.1.3   root     12991:                m_CF = 1;
1.1       root     12992:        }
                   12993: }
                   12994: 
1.1.1.43  root     12995: inline void msdos_int_21h_70h()
                   12996: {
                   12997:        switch(REG8(AL)) {
1.1.1.48  root     12998:        case 0x00: // get ??? info
                   12999:        case 0x01: // set above info
                   13000: //             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));
                   13001:                REG16(AX) = 0x7000;
                   13002:                m_CF = 1;
                   13003:                break;
                   13004:        case 0x02: // set general internationalization info
1.1.1.43  root     13005:                if(REG16(CX) >= 7) {
                   13006:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   13007:                        msdos_nls_tables_update();
                   13008:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   13009:                        REG16(AX) = system_code_page;
                   13010:                } else {
                   13011:                        REG16(AX) = 0x0c;
                   13012:                        m_CF = 1;
                   13013:                }
                   13014:                break;
                   13015:        default:
                   13016:                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     13017:                REG16(AX) = 0x7000;
1.1.1.43  root     13018:                m_CF = 1;
                   13019:                break;
                   13020:        }
                   13021: }
                   13022: 
1.1       root     13023: inline void msdos_int_21h_710dh()
                   13024: {
                   13025:        // reset drive
                   13026: }
                   13027: 
1.1.1.48  root     13028: inline void msdos_int_21h_7141h()
1.1.1.17  root     13029: {
                   13030:        if(REG16(SI) == 0) {
1.1.1.48  root     13031:                msdos_int_21h_41h(1);
1.1.1.17  root     13032:                return;
                   13033:        }
                   13034:        if(REG16(SI) != 1) {
                   13035:                REG16(AX) = 5;
                   13036:                m_CF = 1;
                   13037:        }
                   13038:        /* wild card and matching attributes... */
                   13039:        char tmp[MAX_PATH * 2];
                   13040:        // copy search pathname (and quick check overrun)
                   13041:        ZeroMemory(tmp, sizeof(tmp));
                   13042:        tmp[MAX_PATH - 1] = '\0';
                   13043:        tmp[MAX_PATH] = 1;
                   13044:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   13045:        
                   13046:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   13047:                REG16(AX) = 1;
                   13048:                m_CF = 1;
                   13049:                return;
                   13050:        }
                   13051:        for(char *s = tmp; *s; ++s) {
                   13052:                if(*s == '/') {
                   13053:                        *s = '\\';
                   13054:                }
                   13055:        }
                   13056:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   13057:        if(tmp_name) {
                   13058:                ++tmp_name;
                   13059:        } else {
                   13060:                tmp_name = strchr(tmp, ':');
                   13061:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   13062:        }
                   13063:        
                   13064:        WIN32_FIND_DATAA fd;
                   13065:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   13066:        if(fh == INVALID_HANDLE_VALUE) {
                   13067:                REG16(AX) = 2;
                   13068:                m_CF = 1;
                   13069:                return;
                   13070:        }
                   13071:        do {
                   13072:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   13073:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     13074:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     13075:                                REG16(AX) = 5;
                   13076:                                m_CF = 1;
                   13077:                                break;
                   13078:                        }
                   13079:                }
                   13080:        } while(FindNextFileA(fh, &fd));
                   13081:        if(!m_CF) {
                   13082:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   13083:                        m_CF = 1;
                   13084:                        REG16(AX) = 2;
                   13085:                }
                   13086:        }
                   13087:        FindClose(fh);
                   13088: }
                   13089: 
1.1       root     13090: inline void msdos_int_21h_714eh()
                   13091: {
                   13092:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13093:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   13094:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13095:        WIN32_FIND_DATA fd;
                   13096:        
1.1.1.13  root     13097:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   13098:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13099:                FindClose(dtainfo->find_handle);
                   13100:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13101:        }
                   13102:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     13103:        dtainfo->allowable_mask = REG8(CL);
                   13104:        dtainfo->required_mask = REG8(CH);
                   13105:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     13106:        
1.1.1.14  root     13107:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   13108:                dtainfo->allowable_mask &= ~8;
1.1       root     13109:        }
1.1.1.14  root     13110:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   13111:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     13112:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   13113:                                FindClose(dtainfo->find_handle);
                   13114:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13115:                                break;
                   13116:                        }
                   13117:                }
                   13118:        }
1.1.1.13  root     13119:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13120:                find->attrib = fd.dwFileAttributes;
                   13121:                msdos_find_file_conv_local_time(&fd);
                   13122:                if(REG16(SI) == 0) {
                   13123:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13124:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13125:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13126:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13127:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13128:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13129:                } else {
                   13130:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13131:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13132:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13133:                }
                   13134:                find->size_hi = fd.nFileSizeHigh;
                   13135:                find->size_lo = fd.nFileSizeLow;
                   13136:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13137:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13138:                REG16(AX) = dtainfo - dtalist + 1;
                   13139:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13140:                // volume label
                   13141:                find->attrib = 8;
                   13142:                find->size_hi = find->size_lo = 0;
                   13143:                strcpy(find->full_name, process->volume_label);
                   13144:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13145:                dtainfo->allowable_mask &= ~8;
                   13146:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     13147:        } else {
                   13148:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     13149:                m_CF = 1;
1.1       root     13150:        }
                   13151: }
                   13152: 
                   13153: inline void msdos_int_21h_714fh()
                   13154: {
                   13155:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13156:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13157:        WIN32_FIND_DATA fd;
                   13158:        
1.1.1.14  root     13159:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13160:                REG16(AX) = 6;
1.1.1.13  root     13161:                m_CF = 1;
                   13162:                return;
                   13163:        }
1.1.1.14  root     13164:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13165:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13166:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     13167:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     13168:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   13169:                                        FindClose(dtainfo->find_handle);
                   13170:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13171:                                        break;
                   13172:                                }
                   13173:                        }
                   13174:                } else {
1.1.1.13  root     13175:                        FindClose(dtainfo->find_handle);
                   13176:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13177:                }
                   13178:        }
1.1.1.13  root     13179:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13180:                find->attrib = fd.dwFileAttributes;
                   13181:                msdos_find_file_conv_local_time(&fd);
                   13182:                if(REG16(SI) == 0) {
                   13183:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13184:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13185:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13186:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13187:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13188:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13189:                } else {
                   13190:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13191:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13192:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13193:                }
                   13194:                find->size_hi = fd.nFileSizeHigh;
                   13195:                find->size_lo = fd.nFileSizeLow;
                   13196:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13197:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13198:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13199:                // volume label
                   13200:                find->attrib = 8;
                   13201:                find->size_hi = find->size_lo = 0;
                   13202:                strcpy(find->full_name, process->volume_label);
                   13203:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13204:                dtainfo->allowable_mask &= ~8;
1.1       root     13205:        } else {
                   13206:                REG16(AX) = 0x12;
1.1.1.3   root     13207:                m_CF = 1;
1.1       root     13208:        }
                   13209: }
                   13210: 
                   13211: inline void msdos_int_21h_71a0h()
                   13212: {
                   13213:        DWORD max_component_len, file_sys_flag;
                   13214:        
1.1.1.14  root     13215:        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))) {
                   13216:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   13217:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     13218:                REG16(CX) = (UINT16)max_component_len;          // 255
                   13219:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   13220:        } else {
                   13221:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13222:                m_CF = 1;
1.1       root     13223:        }
                   13224: }
                   13225: 
                   13226: inline void msdos_int_21h_71a1h()
                   13227: {
1.1.1.14  root     13228:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13229:                REG16(AX) = 6;
1.1.1.13  root     13230:                m_CF = 1;
                   13231:                return;
                   13232:        }
1.1.1.14  root     13233:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13234:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13235:                FindClose(dtainfo->find_handle);
                   13236:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13237:        }
                   13238: }
                   13239: 
                   13240: inline void msdos_int_21h_71a6h()
                   13241: {
                   13242:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13243:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13244:        
1.1.1.3   root     13245:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13246:        struct _stat64 status;
                   13247:        DWORD serial_number = 0;
                   13248:        
1.1.1.20  root     13249:        if(fd < process->max_files && file_handler[fd].valid) {
                   13250:                if(_fstat64(fd, &status) == 0) {
                   13251:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13252:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13253:                                char volume[] = "A:\\";
1.1.1.20  root     13254:                                volume[0] = file_handler[fd].path[1];
1.1       root     13255:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   13256:                        }
1.1.1.20  root     13257:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     13258:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13259:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13260:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13261:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13262:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13263:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13264:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13265:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13266:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13267:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13268:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13269:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13270:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13271:                } else {
                   13272:                        REG16(AX) = errno;
1.1.1.3   root     13273:                        m_CF = 1;
1.1       root     13274:                }
                   13275:        } else {
                   13276:                REG16(AX) = 0x06;
1.1.1.3   root     13277:                m_CF = 1;
1.1       root     13278:        }
                   13279: }
                   13280: 
                   13281: inline void msdos_int_21h_71a7h()
                   13282: {
                   13283:        switch(REG8(BL)) {
                   13284:        case 0x00:
1.1.1.3   root     13285:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13286:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13287:                        m_CF = 1;
1.1       root     13288:                }
                   13289:                break;
                   13290:        case 0x01:
                   13291:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13292:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13293:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13294:                        m_CF = 1;
1.1       root     13295:                }
                   13296:                break;
                   13297:        default:
1.1.1.22  root     13298:                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     13299:                REG16(AX) = 0x7100;
1.1.1.3   root     13300:                m_CF = 1;
1.1       root     13301:                break;
                   13302:        }
                   13303: }
                   13304: 
                   13305: inline void msdos_int_21h_71a8h()
                   13306: {
                   13307:        if(REG8(DH) == 0) {
                   13308:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13309:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13310:                memset(fcb, 0x20, sizeof(fcb));
                   13311:                int len = strlen(tmp);
1.1.1.21  root     13312:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13313:                        if(tmp[i] == '.') {
                   13314:                                pos = 8;
                   13315:                        } else {
                   13316:                                if(msdos_lead_byte_check(tmp[i])) {
                   13317:                                        fcb[pos++] = tmp[i++];
                   13318:                                }
                   13319:                                fcb[pos++] = tmp[i];
                   13320:                        }
                   13321:                }
1.1.1.3   root     13322:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13323:        } else {
1.1.1.3   root     13324:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13325:        }
                   13326: }
                   13327: 
1.1.1.22  root     13328: inline void msdos_int_21h_71aah()
                   13329: {
                   13330:        char drv[] = "A:", path[MAX_PATH];
                   13331:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13332:        
                   13333:        if(REG8(BL) == 0) {
                   13334:                drv[0] = 'A' + _getdrive() - 1;
                   13335:        } else {
                   13336:                drv[0] = 'A' + REG8(BL) - 1;
                   13337:        }
                   13338:        switch(REG8(BH)) {
                   13339:        case 0x00:
1.1.1.44  root     13340:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13341:                        REG16(AX) = 0x0f; // invalid drive
                   13342:                        m_CF = 1;
                   13343:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   13344:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13345:                        m_CF = 1;
                   13346:                }
                   13347:                break;
                   13348:        case 0x01:
1.1.1.44  root     13349:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13350:                        REG16(AX) = 0x0f; // invalid drive
                   13351:                        m_CF = 1;
                   13352:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13353:                        REG16(AX) = 0x0f; // invalid drive
                   13354:                        m_CF = 1;
                   13355:                }
                   13356:                break;
                   13357:        case 0x02:
1.1.1.44  root     13358:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13359:                        REG16(AX) = 0x0f; // invalid drive
                   13360:                        m_CF = 1;
                   13361:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13362:                        REG16(AX) = 0x0f; // invalid drive
                   13363:                        m_CF = 1;
                   13364:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13365:                        REG16(AX) = 0x0f; // invalid drive
                   13366:                        m_CF = 1;
                   13367:                } else {
                   13368:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13369:                }
                   13370:                break;
                   13371:        default:
                   13372:                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     13373:                REG16(AX) = 0x7100;
1.1.1.22  root     13374:                m_CF = 1;
                   13375:                break;
                   13376:        }
                   13377: }
                   13378: 
1.1.1.14  root     13379: inline void msdos_int_21h_7300h()
                   13380: {
1.1.1.44  root     13381:        REG8(AL) = REG8(CL);
                   13382:        REG8(AH) = 0;
1.1.1.14  root     13383: }
                   13384: 
                   13385: inline void msdos_int_21h_7302h()
                   13386: {
                   13387:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13388:        UINT16 seg, ofs;
                   13389:        
                   13390:        if(REG16(CX) < 0x3f) {
                   13391:                REG8(AL) = 0x18;
                   13392:                m_CF = 1;
                   13393:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13394:                REG8(AL) = 0xff;
                   13395:                m_CF = 1;
                   13396:        } else {
                   13397:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13398:        }
                   13399: }
                   13400: 
1.1       root     13401: inline void msdos_int_21h_7303h()
                   13402: {
1.1.1.3   root     13403:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13404:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13405:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13406:        
                   13407:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   13408:                info->size_of_structure = sizeof(ext_space_info_t);
                   13409:                info->structure_version = 0;
                   13410:                info->sectors_per_cluster = sectors_per_cluster;
                   13411:                info->bytes_per_sector = bytes_per_sector;
                   13412:                info->available_clusters_on_drive = free_clusters;
                   13413:                info->total_clusters_on_drive = total_clusters;
                   13414:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13415:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13416:                info->available_allocation_units = free_clusters;       // ???
                   13417:                info->total_allocation_units = total_clusters;          // ???
                   13418:        } else {
                   13419:                REG16(AX) = errno;
1.1.1.3   root     13420:                m_CF = 1;
1.1       root     13421:        }
                   13422: }
                   13423: 
1.1.1.30  root     13424: inline void msdos_int_21h_dbh()
                   13425: {
                   13426:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13427:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13428:        REG8(AL) = dos_info->last_drive;
                   13429: }
                   13430: 
                   13431: inline void msdos_int_21h_dch()
                   13432: {
                   13433:        // Novell NetWare - Connection Services - Get Connection Number
                   13434:        REG8(AL) = 0x00;
                   13435: }
                   13436: 
1.1.1.32  root     13437: inline void msdos_int_24h()
                   13438: {
                   13439:        const char *message = NULL;
                   13440:        int key = 0;
                   13441:        
                   13442:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13443:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13444:                        if(active_code_page == 932) {
                   13445:                                message = critical_error_table[i].message_japanese;
                   13446:                        }
                   13447:                        if(message == NULL) {
                   13448:                                message = critical_error_table[i].message_english;
                   13449:                        }
                   13450:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13451:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13452:                        
                   13453:                        SREG(ES) = WORK_TOP >> 4;
                   13454:                        i386_load_segment_descriptor(ES);
                   13455:                        REG16(DI) = 0x0000;
                   13456:                        break;
                   13457:                }
                   13458:        }
                   13459:        fprintf(stderr, "\n%s", message);
                   13460:        if(!(REG8(AH) & 0x80)) {
                   13461:                if(REG8(AH) & 0x01) {
                   13462:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13463:                } else {
                   13464:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13465:                }
                   13466:        }
                   13467:        fprintf(stderr, "\n");
                   13468:        
1.1.1.33  root     13469:        {
1.1.1.32  root     13470:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13471:        }
1.1.1.32  root     13472:        if(REG8(AH) & 0x10) {
                   13473:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13474:        }
                   13475:        if(REG8(AH) & 0x20) {
                   13476:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13477:        }
                   13478:        if(REG8(AH) & 0x08) {
                   13479:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13480:        }
                   13481:        fprintf(stderr, "? ");
                   13482:        
                   13483:        while(1) {
                   13484:                while(!_kbhit()) {
                   13485:                        Sleep(10);
                   13486:                }
                   13487:                key = _getch();
                   13488:                
                   13489:                if(key == 'I' || key == 'i') {
                   13490:                        if(REG8(AH) & 0x20) {
                   13491:                                REG8(AL) = 0;
                   13492:                                break;
                   13493:                        }
                   13494:                } else if(key == 'R' || key == 'r') {
                   13495:                        if(REG8(AH) & 0x10) {
                   13496:                                REG8(AL) = 1;
                   13497:                                break;
                   13498:                        }
                   13499:                } else if(key == 'A' || key == 'a') {
                   13500:                        REG8(AL) = 2;
                   13501:                        break;
                   13502:                } else if(key == 'F' || key == 'f') {
                   13503:                        if(REG8(AH) & 0x08) {
                   13504:                                REG8(AL) = 3;
                   13505:                                break;
                   13506:                        }
                   13507:                }
                   13508:        }
                   13509:        fprintf(stderr, "%c\n", key);
                   13510: }
                   13511: 
1.1       root     13512: inline void msdos_int_25h()
                   13513: {
                   13514:        UINT16 seg, ofs;
                   13515:        DWORD dwSize;
                   13516:        
1.1.1.3   root     13517: #if defined(HAS_I386)
                   13518:        I386OP(pushf)();
                   13519: #else
                   13520:        PREFIX86(_pushf());
                   13521: #endif
1.1       root     13522:        
                   13523:        if(!(REG8(AL) < 26)) {
                   13524:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13525:                m_CF = 1;
1.1       root     13526:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13527:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13528:                m_CF = 1;
1.1       root     13529:        } else {
                   13530:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13531:                char dev[64];
                   13532:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13533:                
                   13534:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13535:                if(hFile == INVALID_HANDLE_VALUE) {
                   13536:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13537:                        m_CF = 1;
1.1       root     13538:                } else {
1.1.1.19  root     13539:                        UINT32 top_sector  = REG16(DX);
                   13540:                        UINT16 sector_num  = REG16(CX);
                   13541:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13542:                        
                   13543:                        if(sector_num == 0xffff) {
                   13544:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13545:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13546:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13547:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13548:                                buffer_addr = (seg << 4) + ofs;
                   13549:                        }
                   13550: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13551: //                             REG8(AL) = 0x02; // drive not ready
                   13552: //                             m_CF = 1;
                   13553: //                     } else 
                   13554:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13555:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13556:                                m_CF = 1;
1.1.1.19  root     13557:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13558:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13559:                                m_CF = 1;
1.1       root     13560:                        }
                   13561:                        CloseHandle(hFile);
                   13562:                }
                   13563:        }
                   13564: }
                   13565: 
                   13566: inline void msdos_int_26h()
                   13567: {
1.1.1.42  root     13568:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13569:        UINT16 seg, ofs;
                   13570:        DWORD dwSize;
                   13571:        
1.1.1.3   root     13572: #if defined(HAS_I386)
                   13573:        I386OP(pushf)();
                   13574: #else
                   13575:        PREFIX86(_pushf());
                   13576: #endif
1.1       root     13577:        
                   13578:        if(!(REG8(AL) < 26)) {
                   13579:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13580:                m_CF = 1;
1.1       root     13581:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13582:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13583:                m_CF = 1;
1.1       root     13584:        } else {
                   13585:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13586:                char dev[64];
                   13587:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13588:                
                   13589:                if(dpb->media_type == 0xf8) {
                   13590:                        // this drive is not a floppy
1.1.1.6   root     13591: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13592: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13593: //                     }
1.1       root     13594:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13595:                        m_CF = 1;
1.1       root     13596:                } else {
                   13597:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13598:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13599:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13600:                                m_CF = 1;
1.1       root     13601:                        } else {
1.1.1.19  root     13602:                                UINT32 top_sector  = REG16(DX);
                   13603:                                UINT16 sector_num  = REG16(CX);
                   13604:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13605:                                
                   13606:                                if(sector_num == 0xffff) {
                   13607:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13608:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13609:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13610:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13611:                                        buffer_addr = (seg << 4) + ofs;
                   13612:                                }
1.1       root     13613:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13614:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13615:                                        m_CF = 1;
1.1.1.19  root     13616:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13617:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13618:                                        m_CF = 1;
1.1.1.19  root     13619:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13620:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13621:                                        m_CF = 1;
1.1       root     13622:                                }
                   13623:                                CloseHandle(hFile);
                   13624:                        }
                   13625:                }
                   13626:        }
                   13627: }
                   13628: 
                   13629: inline void msdos_int_27h()
                   13630: {
1.1.1.29  root     13631:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13632:        try {
                   13633:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13634:        } catch(...) {
                   13635:                // recover the broken mcb
                   13636:                int mcb_seg = SREG(CS) - 1;
                   13637:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13638:                
1.1.1.29  root     13639:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13640:                        mcb->mz = 'M';
                   13641:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13642:                        
1.1.1.29  root     13643:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13644:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13645:                        } else {
1.1.1.39  root     13646:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13647:                        }
                   13648:                } else {
                   13649:                        mcb->mz = 'Z';
1.1.1.30  root     13650:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13651:                }
                   13652:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13653:        }
1.1.1.3   root     13654:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13655: }
                   13656: 
                   13657: inline void msdos_int_29h()
                   13658: {
1.1.1.50  root     13659:        msdos_putch_fast(REG8(AL));
1.1       root     13660: }
                   13661: 
                   13662: inline void msdos_int_2eh()
                   13663: {
                   13664:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13665:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13666:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13667:        char *token = my_strtok(tmp, " ");
                   13668:        strcpy(command, token);
                   13669:        strcpy(opt, token + strlen(token) + 1);
                   13670:        
                   13671:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13672:        param->env_seg = 0;
                   13673:        param->cmd_line.w.l = 44;
                   13674:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13675:        param->fcb1.w.l = 24;
                   13676:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13677:        param->fcb2.w.l = 24;
                   13678:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13679:        
                   13680:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13681:        
                   13682:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13683:        cmd_line->len = strlen(opt);
                   13684:        strcpy(cmd_line->cmd, opt);
                   13685:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13686:        
1.1.1.28  root     13687:        try {
                   13688:                if(msdos_process_exec(command, param, 0)) {
                   13689:                        REG16(AX) = 0xffff; // error before processing command
                   13690:                } else {
                   13691:                        // set flag to set retval to ax when the started process is terminated
                   13692:                        process_t *process = msdos_process_info_get(current_psp);
                   13693:                        process->called_by_int2eh = true;
                   13694:                }
                   13695:        } catch(...) {
                   13696:                REG16(AX) = 0xffff; // error before processing command
                   13697:        }
1.1       root     13698: }
                   13699: 
1.1.1.29  root     13700: inline void msdos_int_2fh_05h()
                   13701: {
                   13702:        switch(REG8(AL)) {
                   13703:        case 0x00:
1.1.1.49  root     13704:                // critical error handler is installed
1.1.1.32  root     13705:                REG8(AL) = 0xff;
                   13706:                break;
                   13707:        case 0x01:
                   13708:        case 0x02:
                   13709:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13710:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13711:                                const char *message = NULL;
                   13712:                                if(active_code_page == 932) {
                   13713:                                        message = standard_error_table[i].message_japanese;
                   13714:                                }
                   13715:                                if(message == NULL) {
                   13716:                                        message = standard_error_table[i].message_english;
                   13717:                                }
                   13718:                                strcpy((char *)(mem + WORK_TOP), message);
                   13719:                                
                   13720:                                SREG(ES) = WORK_TOP >> 4;
                   13721:                                i386_load_segment_descriptor(ES);
                   13722:                                REG16(DI) = 0x0000;
                   13723:                                REG8(AL) = 0x01;
                   13724:                                break;
                   13725:                        }
                   13726:                }
1.1.1.29  root     13727:                break;
                   13728:        default:
                   13729:                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     13730:                REG16(AX) = 0x01;
1.1.1.29  root     13731:                m_CF = 1;
                   13732:        }
                   13733: }
                   13734: 
1.1.1.44  root     13735: inline void msdos_int_2fh_06h()
                   13736: {
                   13737:        switch(REG8(AL)) {
                   13738:        case 0x00:
                   13739:                // ASSIGN is not installed
1.1.1.49  root     13740: //             REG8(AL) = 0x00;
1.1.1.44  root     13741:                break;
                   13742:        case 0x01:
                   13743:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13744:                REG16(AX) = 0x01;
                   13745:                m_CF = 1;
                   13746:                break;
                   13747:        default:
                   13748:                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));
                   13749:                REG16(AX) = 0x01;
                   13750:                m_CF = 1;
                   13751:                break;
                   13752:        }
                   13753: }
                   13754: 
1.1.1.22  root     13755: inline void msdos_int_2fh_11h()
                   13756: {
                   13757:        switch(REG8(AL)) {
                   13758:        case 0x00:
1.1.1.29  root     13759:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     13760: #ifdef SUPPORT_MSCDEX
                   13761:                        // MSCDEX is installed
                   13762:                        REG8(AL) = 0xff;
                   13763:                        i386_write_stack(0xadad);
                   13764: #else
1.1.1.29  root     13765:                        // MSCDEX is not installed
                   13766: //                     REG8(AL) = 0x00;
1.1.1.53  root     13767: #endif
1.1.1.29  root     13768:                } else {
                   13769:                        // Network Redirector is not installed
                   13770: //                     REG8(AL) = 0x00;
                   13771:                }
1.1.1.22  root     13772:                break;
                   13773:        default:
1.1.1.43  root     13774: //             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     13775:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13776:                m_CF = 1;
                   13777:                break;
                   13778:        }
                   13779: }
                   13780: 
1.1.1.21  root     13781: inline void msdos_int_2fh_12h()
                   13782: {
                   13783:        switch(REG8(AL)) {
1.1.1.22  root     13784:        case 0x00:
1.1.1.29  root     13785:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13786:                REG8(AL) = 0xff;
                   13787:                break;
1.1.1.29  root     13788: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13789:        case 0x02:
                   13790:                {
                   13791:                        UINT16 stack = i386_read_stack();
                   13792:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13793:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13794:                        i386_load_segment_descriptor(ES);
                   13795:                }
                   13796:                break;
1.1.1.30  root     13797:        case 0x03:
                   13798:                SREG(DS) = (DEVICE_TOP >> 4);
                   13799:                i386_load_segment_descriptor(DS);
                   13800:                break;
1.1.1.29  root     13801:        case 0x04:
                   13802:                {
                   13803:                        UINT16 stack = i386_read_stack();
                   13804:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13805: #if defined(HAS_I386)
                   13806:                        m_ZF = (REG8(AL) == '\\');
                   13807: #else
                   13808:                        m_ZeroVal = (REG8(AL) != '\\');
                   13809: #endif
                   13810:                }
                   13811:                break;
                   13812:        case 0x05:
1.1.1.49  root     13813:                {
                   13814:                        UINT16 c = i386_read_stack();
                   13815:                        if((c >> 0) & 0xff) {
                   13816:                                msdos_putch((c >> 0) & 0xff);
                   13817:                        }
                   13818:                        if((c >> 8) & 0xff) {
                   13819:                                msdos_putch((c >> 8) & 0xff);
                   13820:                        }
                   13821:                }
1.1.1.29  root     13822:                break;
1.1.1.49  root     13823: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     13824: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13825: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     13826: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     13827: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13828: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13829: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13830:        case 0x0d:
                   13831:                {
                   13832:                        SYSTEMTIME time;
                   13833:                        FILETIME file_time;
                   13834:                        WORD dos_date, dos_time;
                   13835:                        GetLocalTime(&time);
                   13836:                        SystemTimeToFileTime(&time, &file_time);
                   13837:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13838:                        REG16(AX) = dos_date;
                   13839:                        REG16(DX) = dos_time;
                   13840:                }
                   13841:                break;
                   13842: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13843: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13844: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13845:        case 0x11:
                   13846:                {
                   13847:                        char path[MAX_PATH], *p;
                   13848:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13849:                        my_strupr(path);
                   13850:                        while((p = my_strchr(path, '/')) != NULL) {
                   13851:                                *p = '\\';
                   13852:                        }
                   13853:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13854:                }
                   13855:                break;
                   13856:        case 0x12:
                   13857:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13858:                break;
                   13859:        case 0x13:
                   13860:                {
                   13861:                        char tmp[2] = {0};
                   13862:                        tmp[0] = i386_read_stack();
                   13863:                        my_strupr(tmp);
                   13864:                        REG8(AL) = tmp[0];
                   13865:                }
                   13866:                break;
                   13867:        case 0x14:
                   13868: #if defined(HAS_I386)
                   13869:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13870: #else
                   13871:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13872: #endif
                   13873:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13874:                break;
                   13875: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13876:        case 0x16:
                   13877:                if(REG16(BX) < 20) {
                   13878:                        SREG(ES) = SFT_TOP >> 4;
                   13879:                        i386_load_segment_descriptor(ES);
                   13880:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13881:                        
                   13882:                        // update system file table
                   13883:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13884:                        if(file_handler[REG16(BX)].valid) {
                   13885:                                int count = 0;
                   13886:                                for(int i = 0; i < 20; i++) {
                   13887:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13888:                                                count++;
                   13889:                                        }
                   13890:                                }
                   13891:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13892:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13893:                                _lseek(REG16(BX), 0, SEEK_END);
                   13894:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13895:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13896:                        } else {
                   13897:                                memset(sft, 0, 0x3b);
                   13898:                        }
                   13899:                } else {
                   13900:                        REG16(AX) = 0x06;
                   13901:                        m_CF = 1;
                   13902:                }
                   13903:                break;
1.1.1.49  root     13904:        case 0x17:
                   13905:                {
                   13906:                        UINT16 drive = i386_read_stack();
                   13907:                        if(msdos_is_valid_drive(drive)) {
                   13908:                                msdos_cds_update(drive);
                   13909:                        }
                   13910:                        REG16(SI) = 88 * drive;
                   13911:                        SREG(DS) = (CDS_TOP >> 4);
                   13912:                        i386_load_segment_descriptor(DS);
                   13913:                }
                   13914:                break;
1.1.1.29  root     13915: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13916: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13917:        case 0x1a:
                   13918:                {
                   13919:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13920:                        if(path[1] == ':') {
                   13921:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13922:                                        REG8(AL) = path[0] - 'a' + 1;
                   13923:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13924:                                        REG8(AL) = path[0] - 'A' + 1;
                   13925:                                } else {
                   13926:                                        REG8(AL) = 0xff; // invalid
                   13927:                                }
                   13928:                                strcpy(full, path);
                   13929:                                strcpy(path, full + 2);
                   13930:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13931:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13932:                                        REG8(AL) = full[0] - 'a' + 1;
                   13933:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13934:                                        REG8(AL) = full[0] - 'A' + 1;
                   13935:                                } else {
                   13936:                                        REG8(AL) = 0xff; // invalid
                   13937:                                }
                   13938:                        } else {
                   13939:                                REG8(AL) = 0x00; // default
                   13940:                        }
                   13941:                }
                   13942:                break;
                   13943:        case 0x1b:
                   13944:                {
                   13945:                        int year = REG16(CX) + 1980;
                   13946:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13947:                }
                   13948:                break;
                   13949: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13950: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13951:        case 0x1e:
                   13952:                {
                   13953:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13954:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13955:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13956: #if defined(HAS_I386)
                   13957:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13958: #else
                   13959:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13960: #endif
                   13961:                        } else {
                   13962: #if defined(HAS_I386)
                   13963:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13964: #else
                   13965:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13966: #endif
                   13967:                        }
                   13968:                }
                   13969:                break;
1.1.1.49  root     13970:        case 0x1f:
                   13971:                {
                   13972:                        UINT16 drive = i386_read_stack();
                   13973:                        if(msdos_is_valid_drive(drive)) {
                   13974:                                msdos_cds_update(drive);
                   13975:                        }
                   13976:                        REG16(SI) = 88 * drive;
                   13977:                        SREG(ES) = (CDS_TOP >> 4);
                   13978:                        i386_load_segment_descriptor(ES);
                   13979:                }
                   13980:                break;
1.1.1.21  root     13981:        case 0x20:
                   13982:                {
                   13983:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13984:                        
                   13985:                        if(fd < 20) {
                   13986:                                SREG(ES) = current_psp;
                   13987:                                i386_load_segment_descriptor(ES);
                   13988:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13989:                        } else {
                   13990:                                REG16(AX) = 0x06;
                   13991:                                m_CF = 1;
                   13992:                        }
                   13993:                }
                   13994:                break;
1.1.1.29  root     13995:        case 0x21:
                   13996:                msdos_int_21h_60h(0);
                   13997:                break;
1.1.1.49  root     13998:        case 0x22:
                   13999:                {
                   14000:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14001:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   14002:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   14003:                        }
                   14004:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   14005:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   14006:                        }
                   14007:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   14008:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   14009:                        }
                   14010:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   14011:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   14012:                        }
                   14013:                }
                   14014:                break;
1.1.1.29  root     14015: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   14016: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   14017:        case 0x25:
                   14018:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14019:                break;
                   14020:        case 0x26:
                   14021:                REG8(AL) = REG8(CL);
                   14022:                msdos_int_21h_3dh();
                   14023:                break;
                   14024:        case 0x27:
                   14025:                msdos_int_21h_3eh();
                   14026:                break;
                   14027:        case 0x28:
                   14028:                REG16(AX) = REG16(BP);
                   14029:                msdos_int_21h_42h();
                   14030:                break;
                   14031:        case 0x29:
                   14032:                msdos_int_21h_3fh();
                   14033:                break;
                   14034: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   14035:        case 0x2b:
                   14036:                REG16(AX) = REG16(BP);
                   14037:                msdos_int_21h_44h();
                   14038:                break;
                   14039:        case 0x2c:
                   14040:                REG16(BX) = DEVICE_TOP >> 4;
                   14041:                REG16(AX) = 22;
                   14042:                break;
                   14043:        case 0x2d:
                   14044:                {
                   14045:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14046:                        REG16(AX) = sda->extended_error_code;
                   14047:                }
                   14048:                break;
                   14049:        case 0x2e:
                   14050:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     14051:                        SREG(ES) = 0x0001;
                   14052:                        i386_load_segment_descriptor(ES);
                   14053:                        REG16(DI) = 0x00;
                   14054:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     14055:                        // dummy parameter error message read routine is at fffc:0010
                   14056:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     14057:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     14058:                        REG16(DI) = 0x0010;
1.1.1.22  root     14059:                }
                   14060:                break;
1.1.1.29  root     14061:        case 0x2f:
                   14062:                if(REG16(DX) != 0) {
1.1.1.30  root     14063:                        dos_major_version = REG8(DL);
                   14064:                        dos_minor_version = REG8(DH);
1.1.1.29  root     14065:                } else {
                   14066:                        REG8(DL) = 7;
                   14067:                        REG8(DH) = 10;
                   14068:                }
                   14069:                break;
                   14070: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   14071: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     14072:        default:
                   14073:                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));
                   14074:                REG16(AX) = 0x01;
                   14075:                m_CF = 1;
                   14076:                break;
                   14077:        }
                   14078: }
                   14079: 
1.1.1.30  root     14080: inline void msdos_int_2fh_13h()
                   14081: {
                   14082:        static UINT16 prevDS = 0, prevDX = 0;
                   14083:        static UINT16 prevES = 0, prevBX = 0;
                   14084:        UINT16 tmp;
                   14085:        
                   14086:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   14087:        i386_load_segment_descriptor(DS);
                   14088:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   14089:        
                   14090:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   14091:        i386_load_segment_descriptor(ES);
                   14092:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   14093: }
                   14094: 
1.1.1.22  root     14095: inline void msdos_int_2fh_14h()
                   14096: {
                   14097:        switch(REG8(AL)) {
                   14098:        case 0x00:
1.1.1.29  root     14099:                // NLSFUNC.COM is installed
                   14100:                REG8(AL) = 0xff;
1.1.1.25  root     14101:                break;
                   14102:        case 0x01:
                   14103:        case 0x03:
                   14104:                REG8(AL) = 0x00;
                   14105:                active_code_page = REG16(BX);
                   14106:                msdos_nls_tables_update();
                   14107:                break;
                   14108:        case 0x02:
                   14109:                REG8(AL) = get_extended_country_info(REG16(BP));
                   14110:                break;
                   14111:        case 0x04:
1.1.1.42  root     14112:                for(int i = 0;; i++) {
                   14113:                        if(country_table[i].code == REG16(DX)) {
                   14114:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   14115:                                break;
                   14116:                        } else if(country_table[i].code == -1) {
                   14117:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14118:                                break;
                   14119:                        }
                   14120:                }
1.1.1.25  root     14121:                REG8(AL) = 0x00;
1.1.1.22  root     14122:                break;
                   14123:        default:
                   14124:                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));
                   14125:                REG16(AX) = 0x01;
                   14126:                m_CF = 1;
                   14127:                break;
                   14128:        }
                   14129: }
                   14130: 
                   14131: inline void msdos_int_2fh_15h()
                   14132: {
                   14133:        switch(REG8(AL)) {
1.1.1.29  root     14134:        case 0x00: // CD-ROM - Installation Check
                   14135:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     14136: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     14137:                        // MSCDEX is installed
                   14138:                        REG16(BX) = 0;
                   14139:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     14140:                                if(msdos_is_cdrom_drive(i)) {
                   14141:                                        if(REG16(BX) == 0) {
                   14142:                                                REG16(CX) = i;
1.1.1.43  root     14143:                                        }
1.1.1.44  root     14144:                                        REG16(BX)++;
1.1.1.43  root     14145:                                }
                   14146:                        }
                   14147: #else
1.1.1.29  root     14148:                        // MSCDEX is not installed
                   14149: //                     REG8(AL) = 0x00;
1.1.1.43  root     14150: #endif
1.1.1.29  root     14151:                } else {
                   14152:                        // GRAPHICS.COM is not installed
                   14153: //                     REG8(AL) = 0x00;
                   14154:                }
1.1.1.22  root     14155:                break;
1.1.1.43  root     14156:        case 0x0b:
1.1.1.44  root     14157:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   14158:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   14159:                REG16(BX) = 0xadad;
1.1.1.43  root     14160:                break;
                   14161:        case 0x0d:
1.1.1.44  root     14162:                for(int i = 0, n = 0; i < 26; i++) {
                   14163:                        if(msdos_is_cdrom_drive(i)) {
                   14164:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     14165:                        }
                   14166:                }
                   14167:                break;
1.1.1.22  root     14168:        case 0xff:
1.1.1.29  root     14169:                if(REG16(BX) == 0x0000) {
                   14170:                        // CORELCDX is not installed
                   14171:                } else {
                   14172:                        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));
                   14173:                        REG16(AX) = 0x01;
                   14174:                        m_CF = 1;
                   14175:                }
1.1.1.22  root     14176:                break;
1.1.1.21  root     14177:        default:
1.1.1.22  root     14178:                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     14179:                REG16(AX) = 0x01;
                   14180:                m_CF = 1;
                   14181:                break;
                   14182:        }
                   14183: }
                   14184: 
1.1       root     14185: inline void msdos_int_2fh_16h()
                   14186: {
                   14187:        switch(REG8(AL)) {
                   14188:        case 0x00:
1.1.1.14  root     14189:                if(no_windows) {
1.1.1.29  root     14190:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   14191: //                     REG8(AL) = 0x00;
1.1.1.14  root     14192:                } else {
1.1.1.30  root     14193:                        REG8(AL) = win_major_version;
                   14194:                        REG8(AH) = win_minor_version;
1.1       root     14195:                }
                   14196:                break;
1.1.1.43  root     14197:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     14198:                // from DOSBox
                   14199:                i386_set_a20_line(1);
                   14200:                break;
1.1.1.49  root     14201:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     14202:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   14203:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   14204:                break;
                   14205:        case 0x07:
                   14206:                // Virtual Device Call API
                   14207:                break;
1.1.1.22  root     14208:        case 0x0a:
                   14209:                if(!no_windows) {
                   14210:                        REG16(AX) = 0x0000;
1.1.1.30  root     14211:                        REG8(BH) = win_major_version;
                   14212:                        REG8(BL) = win_minor_version;
1.1.1.49  root     14213: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     14214:                        REG16(CX) = 0x0003; // enhanced
                   14215:                }
                   14216:                break;
1.1.1.30  root     14217:        case 0x0b:
                   14218:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     14219:        case 0x0e:
                   14220:        case 0x0f:
1.1.1.30  root     14221:        case 0x10:
1.1.1.22  root     14222:        case 0x11:
                   14223:        case 0x12:
                   14224:        case 0x13:
                   14225:        case 0x14:
1.1.1.30  root     14226:        case 0x15:
1.1.1.43  root     14227:        case 0x81:
                   14228:        case 0x82:
1.1.1.44  root     14229:        case 0x84:
1.1.1.49  root     14230:        case 0x85:
1.1.1.33  root     14231:        case 0x86:
1.1.1.22  root     14232:        case 0x87:
1.1.1.30  root     14233:        case 0x89:
1.1.1.33  root     14234:        case 0x8a:
1.1.1.22  root     14235:                // function not supported, do not clear AX
                   14236:                break;
1.1.1.14  root     14237:        case 0x80:
                   14238:                Sleep(10);
1.1.1.35  root     14239:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     14240:                REG8(AL) = 0x00;
1.1.1.14  root     14241:                break;
1.1.1.33  root     14242:        case 0x83:
                   14243:                REG16(BX) = 0x01; // system vm id
                   14244:                break;
1.1.1.22  root     14245:        case 0x8e:
                   14246:                REG16(AX) = 0x00; // failed
                   14247:                break;
1.1.1.20  root     14248:        case 0x8f:
                   14249:                switch(REG8(DH)) {
                   14250:                case 0x01:
1.1.1.49  root     14251: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14252: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14253:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14254:                        break;
                   14255:                default:
                   14256:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14257:                        break;
                   14258:                }
                   14259:                break;
1.1       root     14260:        default:
1.1.1.22  root     14261:                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));
                   14262:                REG16(AX) = 0x01;
                   14263:                m_CF = 1;
                   14264:                break;
                   14265:        }
                   14266: }
                   14267: 
                   14268: inline void msdos_int_2fh_19h()
                   14269: {
                   14270:        switch(REG8(AL)) {
                   14271:        case 0x00:
1.1.1.29  root     14272:                // SHELLB.COM is not installed
                   14273: //             REG8(AL) = 0x00;
1.1.1.22  root     14274:                break;
                   14275:        case 0x01:
                   14276:        case 0x02:
                   14277:        case 0x03:
                   14278:        case 0x04:
                   14279:                REG16(AX) = 0x01;
                   14280:                m_CF = 1;
                   14281:                break;
1.1.1.29  root     14282:        case 0x80:
                   14283:                // IBM ROM-DOS v4.0 is not installed
                   14284: //             REG8(AL) = 0x00;
                   14285:                break;
1.1.1.22  root     14286:        default:
                   14287:                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     14288:                REG16(AX) = 0x01;
1.1.1.3   root     14289:                m_CF = 1;
1.1       root     14290:                break;
                   14291:        }
                   14292: }
                   14293: 
                   14294: inline void msdos_int_2fh_1ah()
                   14295: {
                   14296:        switch(REG8(AL)) {
                   14297:        case 0x00:
1.1.1.29  root     14298:                // ANSI.SYS is installed
1.1       root     14299:                REG8(AL) = 0xff;
                   14300:                break;
1.1.1.49  root     14301:        case 0x01:
1.1.1.50  root     14302:                if(REG8(CL) == 0x5f) {
                   14303:                        // set display information
                   14304:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14305:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14306:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14307:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14308:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14309:                                
                   14310:                                if(cur_width != new_width || cur_height != new_height) {
                   14311:                                        pcbios_set_console_size(new_width, new_height, true);
                   14312:                                }
                   14313:                        }
                   14314:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14315:                        // get display information
1.1.1.50  root     14316:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14317:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14318:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14319:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14320:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14321:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14322:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14323:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14324:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14325:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14326:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14327:                } else {
                   14328:                        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));
                   14329:                        REG16(AX) = 0x01;
                   14330:                        m_CF = 1;
                   14331:                }
                   14332:                break;
1.1       root     14333:        default:
1.1.1.22  root     14334:                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));
                   14335:                REG16(AX) = 0x01;
                   14336:                m_CF = 1;
                   14337:                break;
                   14338:        }
                   14339: }
                   14340: 
1.1.1.30  root     14341: inline void msdos_int_2fh_40h()
1.1.1.22  root     14342: {
                   14343:        switch(REG8(AL)) {
                   14344:        case 0x00:
1.1.1.30  root     14345:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14346:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14347:                break;
1.1.1.43  root     14348:        case 0x10:
                   14349:                // OS/2 v2.0+ - Installation Check
                   14350:                REG16(AX) = 0x01;
                   14351:                m_CF = 1;
                   14352:                break;
1.1.1.22  root     14353:        default:
                   14354:                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     14355:                REG16(AX) = 0x01;
1.1.1.3   root     14356:                m_CF = 1;
1.1       root     14357:                break;
                   14358:        }
                   14359: }
                   14360: 
                   14361: inline void msdos_int_2fh_43h()
                   14362: {
                   14363:        switch(REG8(AL)) {
                   14364:        case 0x00:
1.1.1.29  root     14365:                // XMS is installed ?
1.1.1.19  root     14366: #ifdef SUPPORT_XMS
                   14367:                if(support_xms) {
                   14368:                        REG8(AL) = 0x80;
1.1.1.44  root     14369:                }
                   14370: #endif
                   14371:                break;
                   14372:        case 0x08:
                   14373: #ifdef SUPPORT_XMS
                   14374:                if(support_xms) {
                   14375:                        REG8(AL) = 0x43;
                   14376:                        REG8(BL) = 0x01; // IBM PC/AT
                   14377:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14378:                }
1.1.1.19  root     14379: #endif
                   14380:                break;
                   14381:        case 0x10:
                   14382:                SREG(ES) = XMS_TOP >> 4;
                   14383:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14384:                REG16(BX) = 0x15;
1.1       root     14385:                break;
1.1.1.44  root     14386:        case 0xe0:
                   14387:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14388:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14389:                        break;
                   14390:                }
1.1       root     14391:        default:
1.1.1.22  root     14392:                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));
                   14393:                REG16(AX) = 0x01;
                   14394:                m_CF = 1;
                   14395:                break;
                   14396:        }
                   14397: }
                   14398: 
                   14399: inline void msdos_int_2fh_46h()
                   14400: {
                   14401:        switch(REG8(AL)) {
                   14402:        case 0x80:
1.1.1.29  root     14403:                // Windows v3.0 is not installed
                   14404: //             REG8(AL) = 0x00;
1.1.1.22  root     14405:                break;
                   14406:        default:
                   14407:                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));
                   14408:                REG16(AX) = 0x01;
                   14409:                m_CF = 1;
                   14410:                break;
                   14411:        }
                   14412: }
                   14413: 
                   14414: inline void msdos_int_2fh_48h()
                   14415: {
                   14416:        switch(REG8(AL)) {
                   14417:        case 0x00:
1.1.1.29  root     14418:                // DOSKEY is not installed
                   14419: //             REG8(AL) = 0x00;
1.1.1.22  root     14420:                break;
                   14421:        case 0x10:
                   14422:                msdos_int_21h_0ah();
                   14423:                REG16(AX) = 0x00;
                   14424:                break;
                   14425:        default:
                   14426:                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     14427:                REG16(AX) = 0x01;
1.1.1.3   root     14428:                m_CF = 1;
1.1       root     14429:                break;
                   14430:        }
                   14431: }
                   14432: 
                   14433: inline void msdos_int_2fh_4ah()
                   14434: {
                   14435:        switch(REG8(AL)) {
1.1.1.29  root     14436: #ifdef SUPPORT_HMA
                   14437:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14438:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14439:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14440:                                // restore first free mcb in high memory area
                   14441:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14442:                        }
                   14443:                        int offset = 0xffff;
                   14444:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14445:                                REG16(DI) = offset + 0x10;
                   14446:                        } else {
                   14447:                                REG16(DI) = 0xffff;
                   14448:                        }
                   14449:                } else {
                   14450:                        // HMA is already used
                   14451:                        REG16(BX) = 0;
                   14452:                        REG16(DI) = 0xffff;
                   14453:                }
                   14454:                SREG(ES) = 0xffff;
                   14455:                i386_load_segment_descriptor(ES);
                   14456:                break;
                   14457:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14458:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14459:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14460:                                // restore first free mcb in high memory area
                   14461:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14462:                        }
                   14463:                        int size = REG16(BX), offset;
                   14464:                        if((size % 16) != 0) {
                   14465:                                size &= ~15;
                   14466:                                size += 16;
                   14467:                        }
                   14468:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14469:                                REG16(BX) = size;
                   14470:                                REG16(DI) = offset + 0x10;
                   14471:                                is_hma_used_by_int_2fh = true;
                   14472:                        } else {
                   14473:                                REG16(BX) = 0;
                   14474:                                REG16(DI) = 0xffff;
                   14475:                        }
                   14476:                } else {
                   14477:                        // HMA is already used
                   14478:                        REG16(BX) = 0;
                   14479:                        REG16(DI) = 0xffff;
                   14480:                }
                   14481:                SREG(ES) = 0xffff;
                   14482:                i386_load_segment_descriptor(ES);
                   14483:                break;
                   14484:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14485:                if(REG8(DL) == 0x00) {
                   14486:                        if(!is_hma_used_by_xms) {
                   14487:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14488:                                        // restore first free mcb in high memory area
                   14489:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14490:                                        is_hma_used_by_int_2fh = false;
                   14491:                                }
                   14492:                                int size = REG16(BX), offset;
                   14493:                                if((size % 16) != 0) {
                   14494:                                        size &= ~15;
                   14495:                                        size += 16;
                   14496:                                }
                   14497:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14498: //                                     REG16(BX) = size;
                   14499:                                        SREG(ES) = 0xffff;
                   14500:                                        i386_load_segment_descriptor(ES);
                   14501:                                        REG16(DI) = offset + 0x10;
                   14502:                                        is_hma_used_by_int_2fh = true;
                   14503:                                } else {
                   14504:                                        REG16(DI) = 0xffff;
                   14505:                                }
                   14506:                        } else {
                   14507:                                REG16(DI) = 0xffff;
                   14508:                        }
                   14509:                } else if(REG8(DL) == 0x01) {
                   14510:                        if(!is_hma_used_by_xms) {
                   14511:                                int size = REG16(BX);
                   14512:                                if((size % 16) != 0) {
                   14513:                                        size &= ~15;
                   14514:                                        size += 16;
                   14515:                                }
                   14516:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14517:                                        // memory block address is not changed
                   14518:                                } else {
                   14519:                                        REG16(DI) = 0xffff;
                   14520:                                }
                   14521:                        } else {
                   14522:                                REG16(DI) = 0xffff;
                   14523:                        }
                   14524:                } else if(REG8(DL) == 0x02) {
                   14525:                        if(!is_hma_used_by_xms) {
                   14526:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14527:                                        // restore first free mcb in high memory area
                   14528:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14529:                                        is_hma_used_by_int_2fh = false;
                   14530:                                } else {
                   14531:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14532:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14533:                                                is_hma_used_by_int_2fh = false;
                   14534:                                        }
                   14535:                                }
                   14536:                        }
                   14537:                } else {
                   14538:                        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));
                   14539:                        REG16(AX) = 0x01;
                   14540:                        m_CF = 1;
                   14541:                }
                   14542:                break;
                   14543:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14544:                if(!is_hma_used_by_xms) {
                   14545:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14546:                                // restore first free mcb in high memory area
                   14547:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14548:                                is_hma_used_by_int_2fh = false;
                   14549:                        }
                   14550:                        REG16(AX) = 0x0000;
                   14551:                        SREG(ES) = 0xffff;
                   14552:                        i386_load_segment_descriptor(ES);
                   14553:                        REG16(DI) = 0x10;
                   14554:                }
                   14555:                break;
                   14556: #else
1.1       root     14557:        case 0x01:
                   14558:        case 0x02:
1.1.1.29  root     14559:                // HMA is already used
1.1.1.27  root     14560:                REG16(BX) = 0x0000;
1.1.1.3   root     14561:                SREG(ES) = 0xffff;
                   14562:                i386_load_segment_descriptor(ES);
1.1       root     14563:                REG16(DI) = 0xffff;
                   14564:                break;
1.1.1.19  root     14565:        case 0x03:
                   14566:                // unable to allocate
                   14567:                REG16(DI) = 0xffff;
                   14568:                break;
                   14569:        case 0x04:
                   14570:                // function not supported, do not clear AX
                   14571:                break;
1.1.1.29  root     14572: #endif
                   14573:        case 0x10:
1.1.1.42  root     14574:                switch(REG16(BX)) {
                   14575:                case 0x0000:
                   14576:                case 0x0001:
                   14577:                case 0x0002:
                   14578:                case 0x0003:
                   14579:                case 0x0004:
                   14580:                case 0x0005:
                   14581:                case 0x0006:
                   14582:                case 0x0007:
                   14583:                case 0x0008:
                   14584:                case 0x000a:
                   14585:                case 0x1234:
                   14586:                        // SMARTDRV v4.00+ is not installed
                   14587:                        break;
                   14588:                default:
1.1.1.29  root     14589:                        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));
                   14590:                        REG16(AX) = 0x01;
                   14591:                        m_CF = 1;
1.1.1.42  root     14592:                        break;
1.1.1.29  root     14593:                }
                   14594:                break;
                   14595:        case 0x11:
1.1.1.42  root     14596:                switch(REG16(BX)) {
                   14597:                case 0x0000:
                   14598:                case 0x0001:
                   14599:                case 0x0002:
                   14600:                case 0x0003:
                   14601:                case 0x0004:
                   14602:                case 0x0005:
                   14603:                case 0x0006:
                   14604:                case 0x0007:
                   14605:                case 0x0008:
                   14606:                case 0x0009:
                   14607:                case 0x000a:
                   14608:                case 0x000b:
                   14609:                case 0xfffe:
                   14610:                case 0xffff:
1.1.1.29  root     14611:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14612:                        break;
                   14613:                default:
                   14614:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14615:                        REG16(AX) = 0x01;
                   14616:                        m_CF = 1;
                   14617:                        break;
                   14618:                }
                   14619:                break;
                   14620:        case 0x12:
                   14621:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14622:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14623:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14624:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14625:                } else {
                   14626:                        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));
                   14627:                        REG16(AX) = 0x01;
                   14628:                        m_CF = 1;
                   14629:                }
1.1.1.22  root     14630:                break;
1.1.1.42  root     14631:        case 0x13:
                   14632:                // DBLSPACE.BIN is not installed
                   14633:                break;
1.1.1.22  root     14634:        default:
                   14635:                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));
                   14636:                REG16(AX) = 0x01;
                   14637:                m_CF = 1;
                   14638:                break;
                   14639:        }
                   14640: }
                   14641: 
                   14642: inline void msdos_int_2fh_4bh()
                   14643: {
                   14644:        switch(REG8(AL)) {
1.1.1.24  root     14645:        case 0x01:
1.1.1.22  root     14646:        case 0x02:
1.1.1.29  root     14647:                // Task Switcher is not installed
1.1.1.24  root     14648:                break;
                   14649:        case 0x03:
                   14650:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14651:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14652:                break;
1.1.1.30  root     14653:        case 0x04:
                   14654:                REG16(BX) = 0x0000; // free switcher id successfully
                   14655:                break;
1.1.1.43  root     14656:        case 0x05:
                   14657:                REG16(BX) = 0x0000; // no instance data chain
                   14658:                SREG(ES) = 0x0000;
                   14659:                i386_load_segment_descriptor(ES);
                   14660:                break;
1.1       root     14661:        default:
1.1.1.22  root     14662:                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     14663:                REG16(AX) = 0x01;
1.1.1.3   root     14664:                m_CF = 1;
1.1       root     14665:                break;
                   14666:        }
                   14667: }
                   14668: 
1.1.1.44  root     14669: inline void msdos_int_2fh_4dh()
                   14670: {
                   14671:        switch(REG8(AL)) {
                   14672:        case 0x00:
                   14673:                // KKCFUNC is not installed ???
                   14674:                break;
                   14675:        default:
                   14676: //             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));
                   14677:                REG16(AX) = 0x01; // invalid function
                   14678:                m_CF = 1;
                   14679:                break;
                   14680:        }
                   14681: }
                   14682: 
1.1       root     14683: inline void msdos_int_2fh_4fh()
                   14684: {
                   14685:        switch(REG8(AL)) {
                   14686:        case 0x00:
1.1.1.29  root     14687:                // BILING is installed
1.1.1.27  root     14688:                REG16(AX) = 0x0000;
                   14689:                REG8(DL) = 0x01;        // major version
                   14690:                REG8(DH) = 0x00;        // minor version
1.1       root     14691:                break;
                   14692:        case 0x01:
1.1.1.27  root     14693:                REG16(AX) = 0x0000;
1.1       root     14694:                REG16(BX) = active_code_page;
                   14695:                break;
                   14696:        default:
1.1.1.22  root     14697:                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));
                   14698:                REG16(AX) = 0x01;
                   14699:                m_CF = 1;
                   14700:                break;
                   14701:        }
                   14702: }
                   14703: 
                   14704: inline void msdos_int_2fh_55h()
                   14705: {
                   14706:        switch(REG8(AL)) {
                   14707:        case 0x00:
                   14708:        case 0x01:
                   14709: //             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));
                   14710:                break;
                   14711:        default:
                   14712:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     14713:                REG16(AX) = 0x01;
1.1.1.3   root     14714:                m_CF = 1;
1.1       root     14715:                break;
                   14716:        }
                   14717: }
                   14718: 
1.1.1.44  root     14719: inline void msdos_int_2fh_56h()
                   14720: {
                   14721:        switch(REG8(AL)) {
                   14722:        case 0x00:
                   14723:                // INTERLNK is not installed
                   14724:                break;
                   14725:        case 0x01:
                   14726:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14727: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14728: //                     REG8(AL) = 0x00;
                   14729: //             }
                   14730:                break;
                   14731:        default:
                   14732:                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));
                   14733:                REG16(AX) = 0x01;
                   14734:                m_CF = 1;
                   14735:                break;
                   14736:        }
                   14737: }
                   14738: 
1.1.1.24  root     14739: inline void msdos_int_2fh_adh()
                   14740: {
                   14741:        switch(REG8(AL)) {
                   14742:        case 0x00:
1.1.1.29  root     14743:                // DISPLAY.SYS is installed
1.1.1.24  root     14744:                REG8(AL) = 0xff;
                   14745:                REG16(BX) = 0x100; // ???
                   14746:                break;
                   14747:        case 0x01:
                   14748:                active_code_page = REG16(BX);
                   14749:                msdos_nls_tables_update();
                   14750:                REG16(AX) = 0x01;
                   14751:                break;
                   14752:        case 0x02:
                   14753:                REG16(BX) = active_code_page;
                   14754:                break;
                   14755:        case 0x03:
                   14756:                // FIXME
                   14757:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14758:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14759:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14760:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14761:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14762:                break;
                   14763:        case 0x80:
1.1.1.49  root     14764:                // KEYB.COM is not installed
                   14765:                break;
1.1.1.24  root     14766:        default:
                   14767:                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));
                   14768:                REG16(AX) = 0x01;
                   14769:                m_CF = 1;
                   14770:                break;
                   14771:        }
                   14772: }
                   14773: 
1.1       root     14774: inline void msdos_int_2fh_aeh()
                   14775: {
                   14776:        switch(REG8(AL)) {
                   14777:        case 0x00:
1.1.1.28  root     14778:                // FIXME: we need to check the given command line
                   14779:                REG8(AL) = 0x00; // the command should be executed as usual
                   14780: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14781:                break;
                   14782:        case 0x01:
                   14783:                {
                   14784:                        char command[MAX_PATH];
                   14785:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14786:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14787:                        
                   14788:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14789:                        param->env_seg = 0;
                   14790:                        param->cmd_line.w.l = 44;
                   14791:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14792:                        param->fcb1.w.l = 24;
                   14793:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14794:                        param->fcb2.w.l = 24;
                   14795:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14796:                        
                   14797:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14798:                        
                   14799:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14800:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14801:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14802:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14803:                        
1.1.1.28  root     14804:                        try {
                   14805:                                msdos_process_exec(command, param, 0);
                   14806:                        } catch(...) {
                   14807:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14808:                        }
                   14809:                }
                   14810:                break;
                   14811:        default:
1.1.1.22  root     14812:                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     14813:                REG16(AX) = 0x01;
1.1.1.3   root     14814:                m_CF = 1;
1.1       root     14815:                break;
                   14816:        }
                   14817: }
                   14818: 
1.1.1.34  root     14819: inline void msdos_int_2fh_b7h()
                   14820: {
                   14821:        switch(REG8(AL)) {
                   14822:        case 0x00:
                   14823:                // APPEND is not installed
                   14824: //             REG8(AL) = 0x00;
                   14825:                break;
1.1.1.44  root     14826:        case 0x06:
                   14827:                REG16(BX) = 0x0000;
                   14828:                break;
1.1.1.34  root     14829:        case 0x07:
1.1.1.43  root     14830:        case 0x11:
1.1.1.34  root     14831:                // COMMAND.COM calls this service without checking APPEND is installed
                   14832:                break;
                   14833:        default:
                   14834:                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));
                   14835:                REG16(AX) = 0x01;
                   14836:                m_CF = 1;
                   14837:                break;
                   14838:        }
                   14839: }
                   14840: 
1.1.1.24  root     14841: inline void msdos_int_33h_0000h()
                   14842: {
                   14843:        REG16(AX) = 0xffff; // hardware/driver installed
                   14844:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14845: }
                   14846: 
                   14847: inline void msdos_int_33h_0001h()
                   14848: {
1.1.1.34  root     14849:        if(mouse.hidden > 0) {
                   14850:                mouse.hidden--;
                   14851:        }
                   14852:        if(mouse.hidden == 0) {
1.1.1.24  root     14853:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14854:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14855:                }
1.1.1.59! root     14856:                pic[1].imr &= ~0x10; // enable irq12
1.1.1.24  root     14857:        }
                   14858: }
                   14859: 
                   14860: inline void msdos_int_33h_0002h()
                   14861: {
1.1.1.34  root     14862:        mouse.hidden++;
                   14863: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
1.1.1.59! root     14864:        pic[1].imr |= 0x10; // disable irq12
1.1.1.24  root     14865: }
                   14866: 
                   14867: inline void msdos_int_33h_0003h()
                   14868: {
1.1.1.34  root     14869: //     if(mouse.hidden > 0) {
                   14870:                update_console_input();
                   14871: //     }
1.1.1.24  root     14872:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14873:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14874:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14875: }
                   14876: 
                   14877: inline void msdos_int_33h_0004h()
                   14878: {
                   14879:        mouse.position.x = REG16(CX);
                   14880:        mouse.position.x = REG16(DX);
1.1.1.24  root     14881: }
                   14882: 
                   14883: inline void msdos_int_33h_0005h()
                   14884: {
1.1.1.34  root     14885: //     if(mouse.hidden > 0) {
                   14886:                update_console_input();
                   14887: //     }
1.1.1.24  root     14888:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14889:                int idx = REG16(BX);
1.1.1.34  root     14890:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14891:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14892:                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     14893:                mouse.buttons[idx].pressed_times = 0;
                   14894:        } else {
                   14895:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14896:        }
                   14897:        REG16(AX) = mouse.get_buttons();
                   14898: }
                   14899: 
                   14900: inline void msdos_int_33h_0006h()
                   14901: {
1.1.1.34  root     14902: //     if(mouse.hidden > 0) {
                   14903:                update_console_input();
                   14904: //     }
1.1.1.24  root     14905:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14906:                int idx = REG16(BX);
1.1.1.34  root     14907:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14908:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14909:                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     14910:                mouse.buttons[idx].released_times = 0;
                   14911:        } else {
                   14912:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14913:        }
                   14914:        REG16(AX) = mouse.get_buttons();
                   14915: }
                   14916: 
                   14917: inline void msdos_int_33h_0007h()
                   14918: {
                   14919:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14920:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14921: }
                   14922: 
                   14923: inline void msdos_int_33h_0008h()
                   14924: {
                   14925:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14926:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14927: }
                   14928: 
                   14929: inline void msdos_int_33h_0009h()
                   14930: {
                   14931:        mouse.hot_spot[0] = REG16(BX);
                   14932:        mouse.hot_spot[1] = REG16(CX);
                   14933: }
                   14934: 
1.1.1.49  root     14935: inline void msdos_int_33h_000ah()
                   14936: {
                   14937:        mouse.screen_mask = REG16(CX);
                   14938:        mouse.cursor_mask = REG16(DX);
                   14939: }
                   14940: 
1.1.1.24  root     14941: inline void msdos_int_33h_000bh()
                   14942: {
1.1.1.34  root     14943: //     if(mouse.hidden > 0) {
                   14944:                update_console_input();
                   14945: //     }
1.1.1.24  root     14946:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14947:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14948:        mouse.prev_position.x = mouse.position.x;
                   14949:        mouse.prev_position.y = mouse.position.y;
                   14950:        REG16(CX) = dx;
                   14951:        REG16(DX) = dy;
                   14952: }
                   14953: 
                   14954: inline void msdos_int_33h_000ch()
                   14955: {
                   14956:        mouse.call_mask = REG16(CX);
                   14957:        mouse.call_addr.w.l = REG16(DX);
                   14958:        mouse.call_addr.w.h = SREG(ES);
                   14959: }
                   14960: 
                   14961: inline void msdos_int_33h_000fh()
                   14962: {
                   14963:        mouse.mickey.x = REG16(CX);
                   14964:        mouse.mickey.y = REG16(DX);
                   14965: }
                   14966: 
                   14967: inline void msdos_int_33h_0011h()
                   14968: {
                   14969:        REG16(AX) = 0xffff;
                   14970:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14971: }
                   14972: 
                   14973: inline void msdos_int_33h_0014h()
                   14974: {
                   14975:        UINT16 old_mask = mouse.call_mask;
                   14976:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14977:        UINT16 old_seg = mouse.call_addr.w.h;
                   14978:        
                   14979:        mouse.call_mask = REG16(CX);
                   14980:        mouse.call_addr.w.l = REG16(DX);
                   14981:        mouse.call_addr.w.h = SREG(ES);
                   14982:        
                   14983:        REG16(CX) = old_mask;
                   14984:        REG16(DX) = old_ofs;
                   14985:        SREG(ES) = old_seg;
                   14986:        i386_load_segment_descriptor(ES);
                   14987: }
                   14988: 
                   14989: inline void msdos_int_33h_0015h()
                   14990: {
                   14991:        REG16(BX) = sizeof(mouse);
                   14992: }
                   14993: 
                   14994: inline void msdos_int_33h_0016h()
                   14995: {
                   14996:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14997: }
                   14998: 
                   14999: inline void msdos_int_33h_0017h()
                   15000: {
                   15001:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   15002: }
                   15003: 
1.1.1.43  root     15004: inline void msdos_int_33h_0018h()
                   15005: {
                   15006:        for(int i = 0; i < 8; i++) {
                   15007:                if(REG16(CX) & (1 << i)) {
                   15008:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   15009:                                // event handler already exists
                   15010:                                REG16(AX) = 0xffff;
                   15011:                                break;
                   15012:                        }
                   15013:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   15014:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   15015:                }
                   15016:        }
                   15017: }
                   15018: 
                   15019: inline void msdos_int_33h_0019h()
                   15020: {
                   15021:        UINT16 call_mask = REG16(CX);
                   15022:        
                   15023:        REG16(CX) = 0;
                   15024:        
                   15025:        for(int i = 0; i < 8; i++) {
                   15026:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   15027:                        for(int j = 0; j < 8; j++) {
                   15028:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   15029:                                        REG16(CX) |= (1 << j);
                   15030:                                }
                   15031:                        }
                   15032:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   15033:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   15034:                        break;
                   15035:                }
                   15036:        }
                   15037: }
                   15038: 
1.1.1.24  root     15039: inline void msdos_int_33h_001ah()
                   15040: {
                   15041:        mouse.sensitivity[0] = REG16(BX);
                   15042:        mouse.sensitivity[1] = REG16(CX);
                   15043:        mouse.sensitivity[2] = REG16(DX);
                   15044: }
                   15045: 
                   15046: inline void msdos_int_33h_001bh()
                   15047: {
                   15048:        REG16(BX) = mouse.sensitivity[0];
                   15049:        REG16(CX) = mouse.sensitivity[1];
                   15050:        REG16(DX) = mouse.sensitivity[2];
                   15051: }
                   15052: 
                   15053: inline void msdos_int_33h_001dh()
                   15054: {
                   15055:        mouse.display_page = REG16(BX);
                   15056: }
                   15057: 
                   15058: inline void msdos_int_33h_001eh()
                   15059: {
                   15060:        REG16(BX) = mouse.display_page;
                   15061: }
                   15062: 
1.1.1.34  root     15063: inline void msdos_int_33h_001fh()
                   15064: {
                   15065:        // from DOSBox
                   15066:        REG16(BX) = 0x0000;
                   15067:        SREG(ES) = 0x0000;
                   15068:        i386_load_segment_descriptor(ES);
                   15069:        mouse.enabled = false;
                   15070:        mouse.old_hidden = mouse.hidden;
                   15071:        mouse.hidden = 1;
                   15072: }
                   15073: 
                   15074: inline void msdos_int_33h_0020h()
                   15075: {
                   15076:        // from DOSBox
                   15077:        mouse.enabled = true;
                   15078:        mouse.hidden = mouse.old_hidden;
                   15079: }
                   15080: 
1.1.1.24  root     15081: inline void msdos_int_33h_0021h()
                   15082: {
                   15083:        REG16(AX) = 0xffff;
                   15084:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15085: }
                   15086: 
                   15087: inline void msdos_int_33h_0022h()
                   15088: {
                   15089:        mouse.language = REG16(BX);
                   15090: }
                   15091: 
                   15092: inline void msdos_int_33h_0023h()
                   15093: {
                   15094:        REG16(BX) = mouse.language;
                   15095: }
                   15096: 
                   15097: inline void msdos_int_33h_0024h()
                   15098: {
                   15099:        REG16(BX) = 0x0805; // V8.05
                   15100:        REG16(CX) = 0x0400; // PS/2
                   15101: }
                   15102: 
1.1.1.49  root     15103: inline void msdos_int_33h_0025h()
                   15104: {
                   15105:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   15106: }
                   15107: 
1.1.1.24  root     15108: inline void msdos_int_33h_0026h()
                   15109: {
                   15110:        REG16(BX) = 0x0000;
                   15111:        REG16(CX) = mouse.max_position.x;
                   15112:        REG16(DX) = mouse.max_position.y;
                   15113: }
                   15114: 
1.1.1.49  root     15115: inline void msdos_int_33h_0027h()
                   15116: {
                   15117: //     if(mouse.hidden > 0) {
                   15118:                update_console_input();
                   15119: //     }
                   15120:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15121:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15122:        mouse.prev_position.x = mouse.position.x;
                   15123:        mouse.prev_position.y = mouse.position.y;
                   15124:        REG16(AX) = mouse.screen_mask;
                   15125:        REG16(BX) = mouse.cursor_mask;
                   15126:        REG16(CX) = dx;
                   15127:        REG16(DX) = dy;
                   15128: }
                   15129: 
                   15130: inline void msdos_int_33h_0028h()
                   15131: {
                   15132:        if(REG16(CX) != 0) {
                   15133:                UINT8 tmp = REG8(AL);
                   15134:                REG8(AL) = REG8(CL);
                   15135:                pcbios_int_10h_00h();
                   15136:                REG8(AL) = tmp;
                   15137:        }
                   15138:        REG8(CL) = 0x00; // successful
                   15139: }
                   15140: 
                   15141: inline void msdos_int_33h_0029h()
                   15142: {
                   15143:        switch(REG16(CX)) {
                   15144:        case 0x0000:
                   15145:                REG16(CX) = 0x0003;
                   15146:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   15147:                break;
                   15148:        case 0x0003:
                   15149:                REG16(CX) = 0x0070;
                   15150:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15151:                break;
                   15152:        case 0x0070:
                   15153:                REG16(CX) = 0x0071;
                   15154:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15155:                break;
                   15156:        case 0x0071:
                   15157:                REG16(CX) = 0x0073;
                   15158:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   15159:                break;
                   15160:        default:
                   15161:                REG16(CX) = 0x0000;
                   15162:                break;
                   15163:        }
                   15164:        if(REG16(CX) != 0) {
                   15165:                SREG(DS) = (WORK_TOP >> 4);
                   15166:        } else {
                   15167:                SREG(DS) = 0x0000;
                   15168:        }
                   15169:        i386_load_segment_descriptor(DS);
                   15170:        REG16(DX) = 0x0000;
                   15171: }
                   15172: 
1.1.1.24  root     15173: inline void msdos_int_33h_002ah()
                   15174: {
1.1.1.34  root     15175:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     15176:        REG16(BX) = mouse.hot_spot[0];
                   15177:        REG16(CX) = mouse.hot_spot[1];
                   15178:        REG16(DX) = 4; // PS/2
                   15179: }
                   15180: 
                   15181: inline void msdos_int_33h_0031h()
                   15182: {
                   15183:        REG16(AX) = mouse.min_position.x;
                   15184:        REG16(BX) = mouse.min_position.y;
                   15185:        REG16(CX) = mouse.max_position.x;
                   15186:        REG16(DX) = mouse.max_position.y;
                   15187: }
                   15188: 
                   15189: inline void msdos_int_33h_0032h()
                   15190: {
                   15191:        REG16(AX) = 0;
1.1.1.49  root     15192:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     15193:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     15194:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     15195: //     REG16(AX) |= 0x1000; // 0028h
                   15196: //     REG16(AX) |= 0x0800; // 0029h
                   15197:        REG16(AX) |= 0x0400; // 002ah
                   15198: //     REG16(AX) |= 0x0200; // 002bh
                   15199: //     REG16(AX) |= 0x0100; // 002ch
                   15200: //     REG16(AX) |= 0x0080; // 002dh
                   15201: //     REG16(AX) |= 0x0040; // 002eh
                   15202:        REG16(AX) |= 0x0020; // 002fh
                   15203: //     REG16(AX) |= 0x0010; // 0030h
                   15204:        REG16(AX) |= 0x0008; // 0031h
                   15205:        REG16(AX) |= 0x0004; // 0032h
                   15206: //     REG16(AX) |= 0x0002; // 0033h
                   15207: //     REG16(AX) |= 0x0001; // 0034h
                   15208: }
                   15209: 
1.1.1.49  root     15210: inline void msdos_int_33h_004dh()
                   15211: {
                   15212:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   15213: }
                   15214: 
                   15215: inline void msdos_int_33h_006dh()
                   15216: {
                   15217:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   15218:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   15219: }
                   15220: 
1.1.1.19  root     15221: inline void msdos_int_67h_40h()
                   15222: {
                   15223:        if(!support_ems) {
                   15224:                REG8(AH) = 0x84;
                   15225:        } else {
                   15226:                REG8(AH) = 0x00;
                   15227:        }
                   15228: }
                   15229: 
                   15230: inline void msdos_int_67h_41h()
                   15231: {
                   15232:        if(!support_ems) {
                   15233:                REG8(AH) = 0x84;
                   15234:        } else {
                   15235:                REG8(AH) = 0x00;
                   15236:                REG16(BX) = EMS_TOP >> 4;
                   15237:        }
                   15238: }
                   15239: 
                   15240: inline void msdos_int_67h_42h()
                   15241: {
                   15242:        if(!support_ems) {
                   15243:                REG8(AH) = 0x84;
                   15244:        } else {
                   15245:                REG8(AH) = 0x00;
                   15246:                REG16(BX) = free_ems_pages;
                   15247:                REG16(DX) = MAX_EMS_PAGES;
                   15248:        }
                   15249: }
                   15250: 
                   15251: inline void msdos_int_67h_43h()
                   15252: {
                   15253:        if(!support_ems) {
                   15254:                REG8(AH) = 0x84;
                   15255:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15256:                REG8(AH) = 0x87;
                   15257:        } else if(REG16(BX) > free_ems_pages) {
                   15258:                REG8(AH) = 0x88;
                   15259:        } else if(REG16(BX) == 0) {
                   15260:                REG8(AH) = 0x89;
                   15261:        } else {
1.1.1.31  root     15262:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15263:                        if(!ems_handles[i].allocated) {
                   15264:                                ems_allocate_pages(i, REG16(BX));
                   15265:                                REG8(AH) = 0x00;
                   15266:                                REG16(DX) = i;
                   15267:                                return;
                   15268:                        }
                   15269:                }
                   15270:                REG8(AH) = 0x85;
                   15271:        }
                   15272: }
                   15273: 
                   15274: inline void msdos_int_67h_44h()
                   15275: {
                   15276:        if(!support_ems) {
                   15277:                REG8(AH) = 0x84;
1.1.1.31  root     15278:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15279:                REG8(AH) = 0x83;
                   15280:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15281:                REG8(AH) = 0x8a;
                   15282: //     } else if(!(REG8(AL) < 4)) {
                   15283: //             REG8(AH) = 0x8b;
                   15284:        } else if(REG16(BX) == 0xffff) {
                   15285:                ems_unmap_page(REG8(AL) & 3);
                   15286:                REG8(AH) = 0x00;
                   15287:        } else {
                   15288:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15289:                REG8(AH) = 0x00;
                   15290:        }
                   15291: }
                   15292: 
                   15293: inline void msdos_int_67h_45h()
                   15294: {
                   15295:        if(!support_ems) {
                   15296:                REG8(AH) = 0x84;
1.1.1.31  root     15297:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15298:                REG8(AH) = 0x83;
                   15299:        } else {
                   15300:                ems_release_pages(REG16(DX));
                   15301:                REG8(AH) = 0x00;
                   15302:        }
                   15303: }
                   15304: 
                   15305: inline void msdos_int_67h_46h()
                   15306: {
                   15307:        if(!support_ems) {
                   15308:                REG8(AH) = 0x84;
                   15309:        } else {
1.1.1.29  root     15310: //             REG16(AX) = 0x0032; // EMS 3.2
                   15311:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15312:        }
                   15313: }
                   15314: 
                   15315: inline void msdos_int_67h_47h()
                   15316: {
                   15317:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15318:        process_t *process = msdos_process_info_get(current_psp);
                   15319:        
                   15320:        if(!support_ems) {
                   15321:                REG8(AH) = 0x84;
1.1.1.31  root     15322: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15323: //             REG8(AH) = 0x83;
                   15324:        } else if(process->ems_pages_stored) {
                   15325:                REG8(AH) = 0x8d;
                   15326:        } else {
                   15327:                for(int i = 0; i < 4; i++) {
                   15328:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15329:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15330:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15331:                }
                   15332:                process->ems_pages_stored = true;
                   15333:                REG8(AH) = 0x00;
                   15334:        }
                   15335: }
                   15336: 
                   15337: inline void msdos_int_67h_48h()
                   15338: {
                   15339:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15340:        process_t *process = msdos_process_info_get(current_psp);
                   15341:        
                   15342:        if(!support_ems) {
                   15343:                REG8(AH) = 0x84;
1.1.1.31  root     15344: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15345: //             REG8(AH) = 0x83;
                   15346:        } else if(!process->ems_pages_stored) {
                   15347:                REG8(AH) = 0x8e;
                   15348:        } else {
                   15349:                for(int i = 0; i < 4; i++) {
                   15350:                        if(process->ems_pages[i].mapped) {
                   15351:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15352:                        } else {
                   15353:                                ems_unmap_page(i);
                   15354:                        }
                   15355:                }
                   15356:                process->ems_pages_stored = false;
                   15357:                REG8(AH) = 0x00;
                   15358:        }
                   15359: }
                   15360: 
                   15361: inline void msdos_int_67h_4bh()
                   15362: {
                   15363:        if(!support_ems) {
                   15364:                REG8(AH) = 0x84;
                   15365:        } else {
                   15366:                REG8(AH) = 0x00;
                   15367:                REG16(BX) = 0;
1.1.1.31  root     15368:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15369:                        if(ems_handles[i].allocated) {
                   15370:                                REG16(BX)++;
                   15371:                        }
                   15372:                }
                   15373:        }
                   15374: }
                   15375: 
                   15376: inline void msdos_int_67h_4ch()
                   15377: {
                   15378:        if(!support_ems) {
                   15379:                REG8(AH) = 0x84;
1.1.1.31  root     15380:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15381:                REG8(AH) = 0x83;
                   15382:        } else {
                   15383:                REG8(AH) = 0x00;
                   15384:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15385:        }
                   15386: }
                   15387: 
                   15388: inline void msdos_int_67h_4dh()
                   15389: {
                   15390:        if(!support_ems) {
                   15391:                REG8(AH) = 0x84;
                   15392:        } else {
                   15393:                REG8(AH) = 0x00;
                   15394:                REG16(BX) = 0;
1.1.1.31  root     15395:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15396:                        if(ems_handles[i].allocated) {
                   15397:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15398:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15399:                                REG16(BX)++;
                   15400:                        }
                   15401:                }
                   15402:        }
                   15403: }
                   15404: 
1.1.1.20  root     15405: inline void msdos_int_67h_4eh()
                   15406: {
                   15407:        if(!support_ems) {
                   15408:                REG8(AH) = 0x84;
                   15409:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15410:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15411:                        // save page map
                   15412:                        for(int i = 0; i < 4; i++) {
                   15413:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15414:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15415:                        }
                   15416:                }
                   15417:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15418:                        // restore page map
                   15419:                        for(int i = 0; i < 4; i++) {
                   15420:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15421:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15422:                                
1.1.1.31  root     15423:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15424:                                        ems_map_page(i, handle, page);
                   15425:                                } else {
                   15426:                                        ems_unmap_page(i);
                   15427:                                }
                   15428:                        }
                   15429:                }
                   15430:                REG8(AH) = 0x00;
                   15431:        } else if(REG8(AL) == 0x03) {
                   15432:                REG8(AH) = 0x00;
1.1.1.21  root     15433:                REG8(AL) = 4 * 4;
                   15434:        } else {
1.1.1.22  root     15435:                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     15436:                REG8(AH) = 0x8f;
                   15437:        }
                   15438: }
                   15439: 
                   15440: inline void msdos_int_67h_4fh()
                   15441: {
                   15442:        if(!support_ems) {
                   15443:                REG8(AH) = 0x84;
                   15444:        } else if(REG8(AL) == 0x00) {
                   15445:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15446:                
                   15447:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15448:                for(int i = 0; i < count; i++) {
                   15449:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15450:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15451:                        
                   15452: //                     if(!(physical < 4)) {
                   15453: //                             REG8(AH) = 0x8b;
                   15454: //                             return;
                   15455: //                     }
                   15456:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15457:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15458:                        *(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     15459:                }
                   15460:                REG8(AH) = 0x00;
                   15461:        } else if(REG8(AL) == 0x01) {
                   15462:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15463:                
                   15464:                for(int i = 0; i < count; i++) {
                   15465:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15466:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15467:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15468:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15469:                        
                   15470: //                     if(!(physical < 4)) {
                   15471: //                             REG8(AH) = 0x8b;
                   15472: //                             return;
                   15473: //                     } else
1.1.1.41  root     15474:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15475:                                ems_map_page(physical & 3, handle, logical);
                   15476:                        } else {
1.1.1.41  root     15477:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15478:                        }
                   15479:                }
                   15480:                REG8(AH) = 0x00;
                   15481:        } else if(REG8(AL) == 0x02) {
                   15482:                REG8(AH) = 0x00;
                   15483:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15484:        } else {
1.1.1.22  root     15485:                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     15486:                REG8(AH) = 0x8f;
                   15487:        }
                   15488: }
                   15489: 
                   15490: inline void msdos_int_67h_50h()
                   15491: {
                   15492:        if(!support_ems) {
                   15493:                REG8(AH) = 0x84;
1.1.1.31  root     15494:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15495:                REG8(AH) = 0x83;
                   15496:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15497:                for(int i = 0; i < REG16(CX); i++) {
                   15498:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15499:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15500:                        
                   15501:                        if(REG8(AL) == 0x01) {
                   15502:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15503:                        }
                   15504: //                     if(!(physical < 4)) {
                   15505: //                             REG8(AH) = 0x8b;
                   15506: //                             return;
                   15507: //                     } else
                   15508:                        if(logical == 0xffff) {
                   15509:                                ems_unmap_page(physical & 3);
                   15510:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15511:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15512:                        } else {
                   15513:                                REG8(AH) = 0x8a;
                   15514:                                return;
                   15515:                        }
                   15516:                }
                   15517:                REG8(AH) = 0x00;
                   15518:        } else {
1.1.1.22  root     15519:                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     15520:                REG8(AH) = 0x8f;
                   15521:        }
                   15522: }
                   15523: 
1.1.1.19  root     15524: inline void msdos_int_67h_51h()
                   15525: {
                   15526:        if(!support_ems) {
                   15527:                REG8(AH) = 0x84;
1.1.1.31  root     15528:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15529:                REG8(AH) = 0x83;
                   15530:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15531:                REG8(AH) = 0x87;
                   15532:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15533:                REG8(AH) = 0x88;
                   15534:        } else {
                   15535:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15536:                REG8(AH) = 0x00;
                   15537:        }
                   15538: }
                   15539: 
1.1.1.20  root     15540: inline void msdos_int_67h_52h()
                   15541: {
                   15542:        if(!support_ems) {
                   15543:                REG8(AH) = 0x84;
1.1.1.31  root     15544: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15545: //             REG8(AH) = 0x83;
1.1.1.20  root     15546:        } else if(REG8(AL) == 0x00) {
                   15547:                REG8(AL) = 0x00; // handle is volatile
                   15548:                REG8(AH) = 0x00;
                   15549:        } else if(REG8(AL) == 0x01) {
                   15550:                if(REG8(BL) == 0x00) {
                   15551:                        REG8(AH) = 0x00;
                   15552:                } else {
                   15553:                        REG8(AH) = 0x90; // undefined attribute type
                   15554:                }
                   15555:        } else if(REG8(AL) == 0x02) {
                   15556:                REG8(AL) = 0x00; // only volatile handles supported
                   15557:                REG8(AH) = 0x00;
                   15558:        } else {
1.1.1.22  root     15559:                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     15560:                REG8(AH) = 0x8f;
                   15561:        }
                   15562: }
                   15563: 
1.1.1.19  root     15564: inline void msdos_int_67h_53h()
                   15565: {
                   15566:        if(!support_ems) {
                   15567:                REG8(AH) = 0x84;
1.1.1.31  root     15568:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15569:                REG8(AH) = 0x83;
                   15570:        } else if(REG8(AL) == 0x00) {
                   15571:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15572:                REG8(AH) = 0x00;
                   15573:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15574:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15575:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15576:                                REG8(AH) = 0xa1;
                   15577:                                return;
                   15578:                        }
                   15579:                }
                   15580:                REG8(AH) = 0x00;
                   15581:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15582:        } else {
1.1.1.22  root     15583:                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     15584:                REG8(AH) = 0x8f;
1.1.1.19  root     15585:        }
                   15586: }
                   15587: 
                   15588: inline void msdos_int_67h_54h()
                   15589: {
                   15590:        if(!support_ems) {
                   15591:                REG8(AH) = 0x84;
                   15592:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15593:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15594:                        if(ems_handles[i].allocated) {
                   15595:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15596:                        } else {
                   15597:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15598:                        }
                   15599:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15600:                }
                   15601:                REG8(AH) = 0x00;
                   15602:                REG8(AL) = MAX_EMS_HANDLES;
                   15603:        } else if(REG8(AL) == 0x01) {
                   15604:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15605:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15606:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15607:                                REG8(AH) = 0x00;
                   15608:                                REG16(DX) = i;
                   15609:                                break;
                   15610:                        }
                   15611:                }
                   15612:        } else if(REG8(AL) == 0x02) {
                   15613:                REG8(AH) = 0x00;
                   15614:                REG16(BX) = MAX_EMS_HANDLES;
                   15615:        } else {
1.1.1.22  root     15616:                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     15617:                REG8(AH) = 0x8f;
                   15618:        }
                   15619: }
                   15620: 
1.1.1.49  root     15621: inline void msdos_int_67h_55h()
                   15622: {
                   15623:        if(!support_ems) {
                   15624:                REG8(AH) = 0x84;
                   15625:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15626:                REG8(AH) = 0x83;
                   15627:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15628:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15629:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15630:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15631:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15632:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15633:                
                   15634:                for(int i = 0; i < (int)entries; i++) {
                   15635:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15636:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15637:                        
                   15638:                        if(REG8(AL) == 0x01) {
                   15639:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15640:                        }
                   15641: //                     if(!(physical < 4)) {
                   15642: //                             REG8(AH) = 0x8b;
                   15643: //                             return;
                   15644: //                     } else
                   15645:                        if(logical == 0xffff) {
                   15646:                                ems_unmap_page(physical & 3);
                   15647:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15648:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15649:                        } else {
                   15650:                                REG8(AH) = 0x8a;
                   15651:                                return;
                   15652:                        }
                   15653:                }
                   15654:                i386_jmp_far(jump_seg, jump_ofs);
                   15655:                REG8(AH) = 0x00;
                   15656:        } else {
                   15657:                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));
                   15658:                REG8(AH) = 0x8f;
                   15659:        }
                   15660: }
                   15661: 
                   15662: inline void msdos_int_67h_56h()
                   15663: {
                   15664:        if(!support_ems) {
                   15665:                REG8(AH) = 0x84;
                   15666:        } else if(REG8(AL) == 0x02) {
                   15667:                REG16(BX) = (2 + 2) * 4;
                   15668:                REG8(AH) = 0x00;
                   15669:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15670:                REG8(AH) = 0x83;
                   15671:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15672:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15673:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15674:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15675:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15676:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15677: #if 0
                   15678:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15679:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15680:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15681: #endif
                   15682:                UINT16 handles[4], pages[4];
                   15683:                
                   15684:                // alter page map and call routine is at fffc:001f
                   15685:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15686:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15687:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15688:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15689:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15690:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15691:                } else {
                   15692:                        // invalid call addr :-(
                   15693:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15694:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15695:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15696:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15697:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15698:                }
                   15699:                // do call far (push cs/ip) in old mapping
                   15700:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15701:                
                   15702:                // get old mapping data
                   15703: #if 0
                   15704:                for(int i = 0; i < (int)old_entries; i++) {
                   15705:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15706:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15707:                        
                   15708:                        if(REG8(AL) == 0x01) {
                   15709:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15710:                        }
                   15711: //                     if(!(physical < 4)) {
                   15712: //                             REG8(AH) = 0x8b;
                   15713: //                             return;
                   15714: //                     } else
                   15715:                        if(logical == 0xffff) {
                   15716:                                ems_unmap_page(physical & 3);
                   15717:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15718:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15719:                        } else {
                   15720:                                REG8(AH) = 0x8a;
                   15721:                                return;
                   15722:                        }
                   15723:                }
                   15724: #endif
                   15725:                for(int i = 0; i < 4; i++) {
                   15726:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15727:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15728:                }
                   15729:                
                   15730:                // set new mapping
                   15731:                for(int i = 0; i < (int)new_entries; i++) {
                   15732:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   15733:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   15734:                        
                   15735:                        if(REG8(AL) == 0x01) {
                   15736:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15737:                        }
                   15738: //                     if(!(physical < 4)) {
                   15739: //                             REG8(AH) = 0x8b;
                   15740: //                             return;
                   15741: //                     } else
                   15742:                        if(logical == 0xffff) {
                   15743:                                ems_unmap_page(physical & 3);
                   15744:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15745:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15746:                        } else {
                   15747:                                REG8(AH) = 0x8a;
                   15748:                                return;
                   15749:                        }
                   15750:                }
                   15751:                
                   15752:                // push old mapping data in new mapping
                   15753:                for(int i = 0; i < 4; i++) {
                   15754:                        i386_push16(handles[i]);
                   15755:                        i386_push16(pages  [i]);
                   15756:                }
                   15757:                REG8(AH) = 0x00;
                   15758:        } else {
                   15759:                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));
                   15760:                REG8(AH) = 0x8f;
                   15761:        }
                   15762: }
                   15763: 
1.1.1.20  root     15764: inline void msdos_int_67h_57h_tmp()
                   15765: {
                   15766:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15767:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15768:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   15769:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   15770:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   15771:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   15772:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15773:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   15774:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   15775:        
1.1.1.32  root     15776:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     15777:        UINT32 src_addr, dest_addr;
                   15778:        UINT32 src_addr_max, dest_addr_max;
                   15779:        
                   15780:        if(src_type == 0) {
                   15781:                src_buffer = mem;
                   15782:                src_addr = (src_seg << 4) + src_ofs;
                   15783:                src_addr_max = MAX_MEM;
                   15784:        } else {
1.1.1.31  root     15785:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     15786:                        REG8(AH) = 0x83;
                   15787:                        return;
                   15788:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   15789:                        REG8(AH) = 0x8a;
                   15790:                        return;
                   15791:                }
1.1.1.32  root     15792:                if(ems_handles[src_handle].buffer != NULL) {
                   15793:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   15794:                }
1.1.1.20  root     15795:                src_addr = src_ofs;
1.1.1.32  root     15796:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     15797:        }
                   15798:        if(dest_type == 0) {
                   15799:                dest_buffer = mem;
                   15800:                dest_addr = (dest_seg << 4) + dest_ofs;
                   15801:                dest_addr_max = MAX_MEM;
                   15802:        } else {
1.1.1.31  root     15803:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     15804:                        REG8(AH) = 0x83;
                   15805:                        return;
                   15806:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   15807:                        REG8(AH) = 0x8a;
                   15808:                        return;
                   15809:                }
1.1.1.32  root     15810:                if(ems_handles[dest_handle].buffer != NULL) {
                   15811:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   15812:                }
1.1.1.20  root     15813:                dest_addr = dest_ofs;
1.1.1.32  root     15814:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     15815:        }
1.1.1.32  root     15816:        if(src_buffer != NULL && dest_buffer != NULL) {
                   15817:                for(int i = 0; i < copy_length; i++) {
                   15818:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15819:                                if(REG8(AL) == 0x00) {
                   15820:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15821:                                } else if(REG8(AL) == 0x01) {
                   15822:                                        UINT8 tmp = dest_buffer[dest_addr];
                   15823:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   15824:                                        src_buffer[src_addr++] = tmp;
                   15825:                                }
                   15826:                        } else {
                   15827:                                REG8(AH) = 0x93;
                   15828:                                return;
1.1.1.20  root     15829:                        }
                   15830:                }
1.1.1.32  root     15831:                REG8(AH) = 0x00;
                   15832:        } else {
                   15833:                REG8(AH) = 0x80;
1.1.1.20  root     15834:        }
                   15835: }
                   15836: 
                   15837: inline void msdos_int_67h_57h()
                   15838: {
                   15839:        if(!support_ems) {
                   15840:                REG8(AH) = 0x84;
                   15841:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15842:                struct {
                   15843:                        UINT16 handle;
                   15844:                        UINT16 page;
                   15845:                        bool mapped;
                   15846:                } tmp_pages[4];
                   15847:                
                   15848:                // unmap pages to copy memory data to ems buffer
                   15849:                for(int i = 0; i < 4; i++) {
                   15850:                        tmp_pages[i].handle = ems_pages[i].handle;
                   15851:                        tmp_pages[i].page   = ems_pages[i].page;
                   15852:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   15853:                        ems_unmap_page(i);
                   15854:                }
                   15855:                
                   15856:                // run move/exchange operation
                   15857:                msdos_int_67h_57h_tmp();
                   15858:                
                   15859:                // restore unmapped pages
                   15860:                for(int i = 0; i < 4; i++) {
                   15861:                        if(tmp_pages[i].mapped) {
                   15862:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   15863:                        }
                   15864:                }
                   15865:        } else {
1.1.1.22  root     15866:                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     15867:                REG8(AH) = 0x8f;
                   15868:        }
                   15869: }
                   15870: 
                   15871: inline void msdos_int_67h_58h()
                   15872: {
                   15873:        if(!support_ems) {
                   15874:                REG8(AH) = 0x84;
                   15875:        } else if(REG8(AL) == 0x00) {
                   15876:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     15877:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   15878:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     15879:                }
                   15880:                REG8(AH) = 0x00;
                   15881:                REG16(CX) = 4;
                   15882:        } else if(REG8(AL) == 0x01) {
                   15883:                REG8(AH) = 0x00;
                   15884:                REG16(CX) = 4;
                   15885:        } else {
1.1.1.22  root     15886:                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     15887:                REG8(AH) = 0x8f;
                   15888:        }
                   15889: }
                   15890: 
1.1.1.42  root     15891: inline void msdos_int_67h_59h()
                   15892: {
                   15893:        if(!support_ems) {
                   15894:                REG8(AH) = 0x84;
                   15895:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     15896:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   15897:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   15898:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   15899:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   15900:                REG8(AH) = 0x00;
                   15901: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     15902:        } else if(REG8(AL) == 0x01) {
                   15903:                REG8(AH) = 0x00;
                   15904:                REG16(BX) = free_ems_pages;
                   15905:                REG16(DX) = MAX_EMS_PAGES;
                   15906:        } else {
                   15907:                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));
                   15908:                REG8(AH) = 0x8f;
                   15909:        }
                   15910: }
                   15911: 
1.1.1.20  root     15912: inline void msdos_int_67h_5ah()
                   15913: {
                   15914:        if(!support_ems) {
1.1.1.19  root     15915:                REG8(AH) = 0x84;
1.1.1.20  root     15916:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15917:                REG8(AH) = 0x87;
                   15918:        } else if(REG16(BX) > free_ems_pages) {
                   15919:                REG8(AH) = 0x88;
                   15920: //     } else if(REG16(BX) == 0) {
                   15921: //             REG8(AH) = 0x89;
                   15922:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     15923:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     15924:                        if(!ems_handles[i].allocated) {
                   15925:                                ems_allocate_pages(i, REG16(BX));
                   15926:                                REG8(AH) = 0x00;
                   15927:                                REG16(DX) = i;
                   15928:                                return;
                   15929:                        }
                   15930:                }
                   15931:                REG8(AH) = 0x85;
                   15932:        } else {
1.1.1.22  root     15933:                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     15934:                REG8(AH) = 0x8f;
1.1.1.19  root     15935:        }
                   15936: }
                   15937: 
1.1.1.49  root     15938: inline void msdos_int_67h_5bh()
                   15939: {
                   15940:        static UINT8  stored_bl = 0x00;
                   15941:        static UINT16 stored_es = 0x0000;
                   15942:        static UINT16 stored_di = 0x0000;
                   15943:        
                   15944:        if(!support_ems) {
                   15945:                REG8(AH) = 0x84;
                   15946:        } else if(REG8(AL) == 0x00) {
                   15947:                if(stored_bl == 0x00) {
                   15948:                        if(!(stored_es == 0 && stored_di == 0)) {
                   15949:                                for(int i = 0; i < 4; i++) {
                   15950:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15951:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15952:                                }
                   15953:                        }
                   15954:                        SREG(ES) = stored_es;
                   15955:                        i386_load_segment_descriptor(ES);
                   15956:                        REG16(DI) = stored_di;
                   15957:                } else {
                   15958:                        REG8(BL) = stored_bl;
                   15959:                }
                   15960:                REG8(AH) = 0x00;
                   15961:        } else if(REG8(AL) == 0x01) {
                   15962:                if(REG8(BL) == 0x00) {
                   15963:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   15964:                                for(int i = 0; i < 4; i++) {
                   15965:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   15966:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   15967:                                        
                   15968:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   15969:                                                ems_map_page(i, handle, page);
                   15970:                                        } else {
                   15971:                                                ems_unmap_page(i);
                   15972:                                        }
                   15973:                                }
                   15974:                        }
                   15975:                }
                   15976:                stored_bl = REG8(BL);
                   15977:                stored_es = SREG(ES);
                   15978:                stored_di = REG16(DI);
                   15979:                REG8(AH) = 0x00;
                   15980:        } else if(REG8(AL) == 0x02) {
                   15981:                REG16(DX) = 4 * 4;
                   15982:                REG8(AH) = 0x00;
                   15983:        } else if(REG8(AL) == 0x03) {
                   15984:                REG8(BL) = 0x00; // not supported
                   15985:                REG8(AH) = 0x00;
                   15986:        } else if(REG8(AL) == 0x04) {
                   15987:                REG8(AH) = 0x00;
                   15988:        } else if(REG8(AL) == 0x05) {
                   15989:                REG8(BL) = 0x00; // not supported
                   15990:                REG8(AH) = 0x00;
                   15991:        } else {
                   15992:                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));
                   15993:                REG8(AH) = 0x8f;
                   15994:        }
                   15995: }
                   15996: 
1.1.1.43  root     15997: inline void msdos_int_67h_5dh()
                   15998: {
                   15999:        if(!support_ems) {
                   16000:                REG8(AH) = 0x84;
                   16001:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   16002:                REG8(AH) = 0xa4; // operating system denied access
                   16003:        } else {
                   16004:                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));
                   16005:                REG8(AH) = 0x8f;
                   16006:        }
                   16007: }
                   16008: 
1.1.1.49  root     16009: inline void msdos_int_67h_70h()
                   16010: {
                   16011:        if(!support_ems) {
                   16012:                REG8(AH) = 0x84;
                   16013:        } else if(REG8(AL) == 0x00) {
                   16014:                REG8(AL) = 0x00;
                   16015:                REG8(AH) = 0x00;
                   16016:        } else if(REG8(AL) == 0x01) {
                   16017:                REG8(AL) = 0x00;
                   16018: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   16019:                REG8(AH) = 0x00;
                   16020:        } else {
                   16021:                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));
                   16022:                REG8(AH) = 0x8f;
                   16023:        }
                   16024: }
                   16025: 
1.1.1.30  root     16026: inline void msdos_int_67h_deh()
                   16027: {
                   16028:        REG8(AH) = 0x84;
                   16029: }
                   16030: 
1.1.1.19  root     16031: #ifdef SUPPORT_XMS
                   16032: 
1.1.1.32  root     16033: void msdos_xms_init()
1.1.1.26  root     16034: {
1.1.1.30  root     16035:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16036:        emb_handle_top->address = EMB_TOP;
                   16037:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     16038:        xms_a20_local_enb_count = 0;
                   16039: }
                   16040: 
1.1.1.32  root     16041: void msdos_xms_finish()
                   16042: {
                   16043:        msdos_xms_release();
                   16044: }
                   16045: 
                   16046: void msdos_xms_release()
1.1.1.30  root     16047: {
                   16048:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   16049:                emb_handle_t *next_handle = emb_handle->next;
                   16050:                free(emb_handle);
                   16051:                emb_handle = next_handle;
                   16052:        }
                   16053: }
                   16054: 
                   16055: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   16056: {
                   16057:        if(handle != 0) {
                   16058:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16059:                        if(emb_handle->handle == handle) {
                   16060:                                return(emb_handle);
                   16061:                        }
                   16062:                }
                   16063:        }
                   16064:        return(NULL);
                   16065: }
                   16066: 
                   16067: int msdos_xms_get_unused_emb_handle_id()
                   16068: {
                   16069:        for(int handle = 1;; handle++) {
                   16070:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   16071:                        return(handle);
                   16072:                }
                   16073:        }
                   16074:        return(0);
                   16075: }
                   16076: 
                   16077: int msdos_xms_get_unused_emb_handle_count()
                   16078: {
                   16079:        int count = 64; //255;
                   16080:        
                   16081:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16082:                if(emb_handle->handle != 0) {
                   16083:                        if(--count == 1) {
                   16084:                                break;
                   16085:                        }
                   16086:                }
                   16087:        }
                   16088:        return(count);
                   16089: }
                   16090: 
                   16091: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   16092: {
                   16093:        if(emb_handle->size_kb > size_kb) {
                   16094:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16095:                
                   16096:                new_handle->address = emb_handle->address + size_kb * 1024;
                   16097:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   16098:                emb_handle->size_kb = size_kb;
                   16099:                
                   16100:                new_handle->prev = emb_handle;
                   16101:                new_handle->next = emb_handle->next;
                   16102:                if(emb_handle->next != NULL) {
                   16103:                        emb_handle->next->prev = new_handle;
                   16104:                }
                   16105:                emb_handle->next = new_handle;
                   16106:        }
                   16107: }
                   16108: 
                   16109: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   16110: {
                   16111:        emb_handle_t *next_handle = emb_handle->next;
                   16112:        
                   16113:        if(next_handle != NULL) {
                   16114:                emb_handle->size_kb += next_handle->size_kb;
                   16115:                
                   16116:                if(next_handle->next != NULL) {
                   16117:                        next_handle->next->prev = emb_handle;
                   16118:                }
                   16119:                emb_handle->next = next_handle->next;
                   16120:                free(next_handle);
                   16121:        }
                   16122: }
                   16123: 
                   16124: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   16125: {
                   16126:        emb_handle_t *target_handle = NULL;
                   16127:        
                   16128:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16129:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   16130:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   16131:                                target_handle = emb_handle;
                   16132:                        }
                   16133:                }
                   16134:        }
                   16135:        if(target_handle != NULL) {
                   16136:                if(target_handle->size_kb > size_kb) {
                   16137:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   16138:                }
                   16139: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16140:                return(target_handle);
                   16141:        }
                   16142:        return(NULL);
                   16143: }
                   16144: 
                   16145: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   16146: {
                   16147:        emb_handle_t *prev_handle = emb_handle->prev;
                   16148:        emb_handle_t *next_handle = emb_handle->next;
                   16149:        
                   16150:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   16151:                msdos_xms_combine_emb_handles(prev_handle);
                   16152:                emb_handle = prev_handle;
                   16153:        }
                   16154:        if(next_handle != NULL && next_handle->handle == 0) {
                   16155:                msdos_xms_combine_emb_handles(emb_handle);
                   16156:        }
                   16157:        emb_handle->handle = 0;
                   16158: }
                   16159: 
1.1.1.19  root     16160: inline void msdos_call_xms_00h()
                   16161: {
1.1.1.29  root     16162: #if defined(HAS_I386)
                   16163:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     16164: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     16165:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   16166: #else
                   16167:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   16168:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   16169: #endif
                   16170: //     REG16(DX) = 0x0000; // HMA does not exist
                   16171:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     16172: }
                   16173: 
                   16174: inline void msdos_call_xms_01h()
                   16175: {
1.1.1.29  root     16176:        if(REG8(AL) == 0x40) {
                   16177:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   16178:                // DX=KB free extended memory returned by last call of function 08h
                   16179:                REG16(AX) = 0x0000;
                   16180:                REG8(BL) = 0x91;
                   16181:                REG16(DX) = xms_dx_after_call_08h;
                   16182:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16183:                REG16(AX) = 0x0000;
                   16184:                REG8(BL) = 0x81; // Vdisk was detected
                   16185: #ifdef SUPPORT_HMA
                   16186:        } else if(is_hma_used_by_int_2fh) {
                   16187:                REG16(AX) = 0x0000;
                   16188:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16189:        } else if(is_hma_used_by_xms) {
                   16190:                REG16(AX) = 0x0000;
                   16191:                REG8(BL) = 0x91; // HMA is already in use
                   16192:        } else {
                   16193:                REG16(AX) = 0x0001;
                   16194:                is_hma_used_by_xms = true;
                   16195: #else
                   16196:        } else {
                   16197:                REG16(AX) = 0x0000;
                   16198:                REG8(BL) = 0x91; // HMA is already in use
                   16199: #endif
                   16200:        }
1.1.1.19  root     16201: }
                   16202: 
                   16203: inline void msdos_call_xms_02h()
                   16204: {
1.1.1.29  root     16205:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16206:                REG16(AX) = 0x0000;
                   16207:                REG8(BL) = 0x81; // Vdisk was detected
                   16208: #ifdef SUPPORT_HMA
                   16209:        } else if(is_hma_used_by_int_2fh) {
                   16210:                REG16(AX) = 0x0000;
                   16211:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16212:        } else if(!is_hma_used_by_xms) {
                   16213:                REG16(AX) = 0x0000;
                   16214:                REG8(BL) = 0x93; // HMA is not allocated
                   16215:        } else {
                   16216:                REG16(AX) = 0x0001;
                   16217:                is_hma_used_by_xms = false;
                   16218:                // restore first free mcb in high memory area
                   16219:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16220: #else
                   16221:        } else {
                   16222:                REG16(AX) = 0x0000;
                   16223:                REG8(BL) = 0x91; // HMA is already in use
                   16224: #endif
                   16225:        }
1.1.1.19  root     16226: }
                   16227: 
                   16228: inline void msdos_call_xms_03h()
                   16229: {
                   16230:        i386_set_a20_line(1);
                   16231:        REG16(AX) = 0x0001;
                   16232:        REG8(BL) = 0x00;
                   16233: }
                   16234: 
                   16235: inline void msdos_call_xms_04h()
                   16236: {
1.1.1.21  root     16237:        i386_set_a20_line(0);
                   16238:        REG16(AX) = 0x0001;
                   16239:        REG8(BL) = 0x00;
1.1.1.19  root     16240: }
                   16241: 
                   16242: inline void msdos_call_xms_05h()
                   16243: {
                   16244:        i386_set_a20_line(1);
                   16245:        REG16(AX) = 0x0001;
                   16246:        REG8(BL) = 0x00;
1.1.1.21  root     16247:        xms_a20_local_enb_count++;
1.1.1.19  root     16248: }
                   16249: 
                   16250: void msdos_call_xms_06h()
                   16251: {
1.1.1.21  root     16252:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16253:                if(--xms_a20_local_enb_count == 0) {
                   16254:                        i386_set_a20_line(0);
                   16255:                        REG16(AX) = 0x0001;
                   16256:                        REG8(BL) = 0x00;
                   16257:                } else {
                   16258:                        REG16(AX) = 0x0000;
                   16259:                        REG8(BL) = 0x94;
                   16260:                }
1.1.1.21  root     16261:        } else {
1.1.1.45  root     16262:                i386_set_a20_line(0);
1.1.1.21  root     16263:                REG16(AX) = 0x0001;
                   16264:                REG8(BL) = 0x00;
1.1.1.19  root     16265:        }
                   16266: }
                   16267: 
                   16268: inline void msdos_call_xms_07h()
                   16269: {
                   16270:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16271:        REG8(BL) = 0x00;
                   16272: }
                   16273: 
                   16274: inline void msdos_call_xms_08h()
                   16275: {
1.1.1.45  root     16276:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16277:        
1.1.1.30  root     16278:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16279:                if(emb_handle->handle == 0) {
1.1.1.45  root     16280:                        if(eax < emb_handle->size_kb) {
                   16281:                                eax = emb_handle->size_kb;
1.1.1.19  root     16282:                        }
1.1.1.45  root     16283:                        edx += emb_handle->size_kb;
1.1.1.19  root     16284:                }
                   16285:        }
1.1.1.45  root     16286:        if(eax > 65535) {
                   16287:                eax = 65535;
                   16288:        }
                   16289:        if(edx > 65535) {
                   16290:                edx = 65535;
                   16291:        }
                   16292:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16293:                REG8(BL) = 0xa0;
                   16294:        } else {
                   16295:                REG8(BL) = 0x00;
                   16296:        }
1.1.1.45  root     16297: #if defined(HAS_I386)
                   16298:        REG32(EAX) = eax;
                   16299:        REG32(EDX) = edx;
                   16300: #else
                   16301:        REG16(AX) = (UINT16)eax;
                   16302:        REG16(DX) = (UINT16)edx;
                   16303: #endif
1.1.1.29  root     16304:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16305: }
                   16306: 
1.1.1.30  root     16307: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16308: {
1.1.1.30  root     16309:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16310:        
                   16311:        if(emb_handle != NULL) {
                   16312:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16313:                
                   16314:                REG16(AX) = 0x0001;
                   16315:                REG16(DX) = emb_handle->handle;
                   16316:                REG8(BL) = 0x00;
                   16317:        } else {
                   16318:                REG16(AX) = REG16(DX) = 0x0000;
                   16319:                REG8(BL) = 0xa0;
1.1.1.19  root     16320:        }
1.1.1.30  root     16321: }
                   16322: 
                   16323: inline void msdos_call_xms_09h()
                   16324: {
                   16325:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16326: }
                   16327: 
                   16328: inline void msdos_call_xms_0ah()
                   16329: {
1.1.1.30  root     16330:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16331:        
                   16332:        if(emb_handle == NULL) {
1.1.1.19  root     16333:                REG16(AX) = 0x0000;
                   16334:                REG8(BL) = 0xa2;
1.1.1.45  root     16335: //     } else if(emb_handle->lock > 0) {
                   16336: //             REG16(AX) = 0x0000;
                   16337: //             REG8(BL) = 0xab;
1.1.1.19  root     16338:        } else {
1.1.1.30  root     16339:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16340:                
                   16341:                REG16(AX) = 0x0001;
                   16342:                REG8(BL) = 0x00;
                   16343:        }
                   16344: }
                   16345: 
                   16346: inline void msdos_call_xms_0bh()
                   16347: {
                   16348:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16349:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16350:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16351:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16352:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16353:        
                   16354:        UINT8 *src_buffer, *dest_buffer;
                   16355:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16356:        emb_handle_t *emb_handle;
1.1.1.19  root     16357:        
                   16358:        if(src_handle == 0) {
                   16359:                src_buffer = mem;
                   16360:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16361:                src_addr_max = MAX_MEM;
                   16362:        } else {
1.1.1.30  root     16363:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16364:                        REG16(AX) = 0x0000;
                   16365:                        REG8(BL) = 0xa3;
                   16366:                        return;
                   16367:                }
1.1.1.30  root     16368:                src_buffer = mem + emb_handle->address;
                   16369:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16370:        }
                   16371:        if(dest_handle == 0) {
                   16372:                dest_buffer = mem;
                   16373:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16374:                dest_addr_max = MAX_MEM;
                   16375:        } else {
1.1.1.30  root     16376:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16377:                        REG16(AX) = 0x0000;
                   16378:                        REG8(BL) = 0xa5;
                   16379:                        return;
                   16380:                }
1.1.1.30  root     16381:                dest_buffer = mem + emb_handle->address;
                   16382:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16383:        }
                   16384:        for(int i = 0; i < copy_length; i++) {
                   16385:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16386:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16387:                } else {
                   16388:                        break;
                   16389:                }
                   16390:        }
                   16391:        REG16(AX) = 0x0001;
                   16392:        REG8(BL) = 0x00;
                   16393: }
                   16394: 
                   16395: inline void msdos_call_xms_0ch()
                   16396: {
1.1.1.30  root     16397:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16398:        
                   16399:        if(emb_handle == NULL) {
1.1.1.19  root     16400:                REG16(AX) = 0x0000;
                   16401:                REG8(BL) = 0xa2;
                   16402:        } else {
1.1.1.45  root     16403:                if(emb_handle->lock < 255) {
                   16404:                        emb_handle->lock++;
                   16405:                }
1.1.1.19  root     16406:                REG16(AX) = 0x0001;
1.1.1.30  root     16407:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16408:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16409:        }
                   16410: }
                   16411: 
                   16412: inline void msdos_call_xms_0dh()
                   16413: {
1.1.1.30  root     16414:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16415:        
                   16416:        if(emb_handle == NULL) {
1.1.1.19  root     16417:                REG16(AX) = 0x0000;
                   16418:                REG8(BL) = 0xa2;
1.1.1.30  root     16419:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16420:                REG16(AX) = 0x0000;
                   16421:                REG8(BL) = 0xaa;
                   16422:        } else {
1.1.1.30  root     16423:                emb_handle->lock--;
1.1.1.19  root     16424:                REG16(AX) = 0x0001;
                   16425:                REG8(BL) = 0x00;
                   16426:        }
                   16427: }
                   16428: 
                   16429: inline void msdos_call_xms_0eh()
                   16430: {
1.1.1.30  root     16431:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16432:        
                   16433:        if(emb_handle == NULL) {
1.1.1.19  root     16434:                REG16(AX) = 0x0000;
                   16435:                REG8(BL) = 0xa2;
                   16436:        } else {
                   16437:                REG16(AX) = 0x0001;
1.1.1.30  root     16438:                REG8(BH) = emb_handle->lock;
                   16439:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16440:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16441:        }
                   16442: }
                   16443: 
1.1.1.30  root     16444: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16445: {
1.1.1.30  root     16446:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16447:        
                   16448:        if(emb_handle == NULL) {
1.1.1.19  root     16449:                REG16(AX) = 0x0000;
                   16450:                REG8(BL) = 0xa2;
1.1.1.30  root     16451:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16452:                REG16(AX) = 0x0000;
                   16453:                REG8(BL) = 0xab;
                   16454:        } else {
1.1.1.30  root     16455:                if(emb_handle->size_kb < size_kb) {
                   16456:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16457:                                msdos_xms_combine_emb_handles(emb_handle);
                   16458:                                if(emb_handle->size_kb > size_kb) {
                   16459:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16460:                                }
                   16461:                        } else {
                   16462:                                int old_handle = emb_handle->handle;
                   16463:                                int old_size_kb = emb_handle->size_kb;
                   16464:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16465:                                
                   16466:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16467:                                msdos_xms_free_emb_handle(emb_handle);
                   16468:                                
                   16469:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16470:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16471:                                }
                   16472:                                emb_handle->handle = old_handle;
                   16473:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16474:                                free(buffer);
                   16475:                        }
                   16476:                } else if(emb_handle->size_kb > size_kb) {
                   16477:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16478:                }
                   16479:                if(emb_handle->size_kb != size_kb) {
                   16480:                        REG16(AX) = 0x0000;
                   16481:                        REG8(BL) = 0xa0;
                   16482:                } else {
                   16483:                        REG16(AX) = 0x0001;
                   16484:                        REG8(BL) = 0x00;
                   16485:                }
1.1.1.19  root     16486:        }
                   16487: }
                   16488: 
1.1.1.30  root     16489: inline void msdos_call_xms_0fh()
                   16490: {
                   16491:        msdos_call_xms_0fh(REG16(BX));
                   16492: }
                   16493: 
1.1.1.19  root     16494: inline void msdos_call_xms_10h()
                   16495: {
                   16496:        int seg;
                   16497:        
                   16498:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16499:                REG16(AX) = 0x0001;
                   16500:                REG16(BX) = seg;
                   16501:        } else {
                   16502:                REG16(AX) = 0x0000;
                   16503:                REG8(BL) = 0xb0;
                   16504:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16505:        }
                   16506: }
                   16507: 
                   16508: inline void msdos_call_xms_11h()
                   16509: {
                   16510:        int mcb_seg = REG16(DX) - 1;
                   16511:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16512:        
                   16513:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16514:                msdos_mem_free(REG16(DX));
                   16515:                REG16(AX) = 0x0001;
                   16516:                REG8(BL) = 0x00;
                   16517:        } else {
                   16518:                REG16(AX) = 0x0000;
                   16519:                REG8(BL) = 0xb2;
                   16520:        }
                   16521: }
                   16522: 
                   16523: inline void msdos_call_xms_12h()
                   16524: {
                   16525:        int mcb_seg = REG16(DX) - 1;
                   16526:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16527:        int max_paragraphs;
                   16528:        
                   16529:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16530:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16531:                        REG16(AX) = 0x0001;
                   16532:                        REG8(BL) = 0x00;
                   16533:                } else {
                   16534:                        REG16(AX) = 0x0000;
                   16535:                        REG8(BL) = 0xb0;
                   16536:                        REG16(DX) = max_paragraphs;
                   16537:                }
                   16538:        } else {
                   16539:                REG16(AX) = 0x0000;
                   16540:                REG8(BL) = 0xb2;
                   16541:        }
                   16542: }
                   16543: 
1.1.1.29  root     16544: #if defined(HAS_I386)
                   16545: 
                   16546: inline void msdos_call_xms_88h()
                   16547: {
                   16548:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16549:        
1.1.1.30  root     16550:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16551:                if(emb_handle->handle == 0) {
                   16552:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16553:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16554:                        }
1.1.1.30  root     16555:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16556:                }
                   16557:        }
                   16558:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16559:                REG8(BL) = 0xa0;
                   16560:        } else {
                   16561:                REG8(BL) = 0x00;
                   16562:        }
                   16563:        REG32(ECX) = EMB_END - 1;
                   16564: }
                   16565: 
                   16566: inline void msdos_call_xms_89h()
                   16567: {
1.1.1.30  root     16568:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16569: }
                   16570: 
                   16571: inline void msdos_call_xms_8eh()
                   16572: {
1.1.1.30  root     16573:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16574:        
                   16575:        if(emb_handle == NULL) {
1.1.1.29  root     16576:                REG16(AX) = 0x0000;
                   16577:                REG8(BL) = 0xa2;
                   16578:        } else {
                   16579:                REG16(AX) = 0x0001;
1.1.1.30  root     16580:                REG8(BH) = emb_handle->lock;
                   16581:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16582:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16583:        }
                   16584: }
                   16585: 
                   16586: inline void msdos_call_xms_8fh()
                   16587: {
1.1.1.30  root     16588:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16589: }
                   16590: 
                   16591: #endif
1.1.1.19  root     16592: #endif
                   16593: 
1.1.1.26  root     16594: UINT16 msdos_get_equipment()
                   16595: {
                   16596:        static UINT16 equip = 0;
                   16597:        
                   16598:        if(equip == 0) {
                   16599: #ifdef SUPPORT_FPU
                   16600:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   16601: #endif
                   16602:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   16603:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   16604: //             equip |= (1 << 8);      // 0 if DMA installed
                   16605:                equip |= (2 << 9);      // number of serial ports
                   16606:                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     16607:                
                   16608:                // check only A: and B: if it is floppy drive
                   16609:                int n = 0;
                   16610:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     16611:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   16612:                                n++;
1.1.1.28  root     16613:                        }
                   16614:                }
                   16615:                if(n != 0) {
                   16616:                        equip |= (1 << 0);      // floppy disk(s) installed
                   16617:                        n--;
                   16618:                        equip |= (n << 6);      // number of floppies installed less 1
                   16619:                }
                   16620: //             if(joyGetNumDevs() != 0) {
                   16621: //                     equip |= (1 << 12);     // game port installed
                   16622: //             }
1.1.1.26  root     16623:        }
                   16624:        return(equip);
                   16625: }
                   16626: 
1.1       root     16627: void msdos_syscall(unsigned num)
                   16628: {
1.1.1.22  root     16629: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     16630:        if(num == 0x08 || num == 0x1c) {
                   16631:                // don't log the timer interrupts
1.1.1.45  root     16632: //             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     16633:        } else if(num == 0x30) {
                   16634:                // dummy interrupt for call 0005h (call near)
                   16635:                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.59! root     16636:        } else if(num == 0x65) {
1.1.1.22  root     16637:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     16638:                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.59! root     16639:        } else if(num == 0x66) {
1.1.1.22  root     16640:                // dummy interrupt for XMS (call far)
1.1.1.33  root     16641:                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.59! root     16642:        } else if(num >= 0x68 && num <= 0x6f) {
1.1.1.45  root     16643:                // dummy interrupt
1.1.1.22  root     16644:        } else {
1.1.1.33  root     16645:                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     16646:        }
                   16647: #endif
1.1.1.36  root     16648:        // update cursor position
                   16649:        if(cursor_moved) {
                   16650:                pcbios_update_cursor_position();
                   16651:                cursor_moved = false;
                   16652:        }
1.1.1.50  root     16653: #ifdef USE_SERVICE_THREAD
                   16654:        // this is called from dummy loop to wait until a serive that waits input is done
                   16655:        if(!in_service)
                   16656: #endif
1.1.1.33  root     16657:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     16658:        
1.1       root     16659:        switch(num) {
                   16660:        case 0x00:
1.1.1.28  root     16661:                try {
                   16662:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16663:                        error("division by zero\n");
                   16664:                } catch(...) {
                   16665:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   16666:                }
1.1       root     16667:                break;
                   16668:        case 0x04:
1.1.1.28  root     16669:                try {
                   16670:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16671:                        error("overflow\n");
                   16672:                } catch(...) {
                   16673:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   16674:                }
1.1       root     16675:                break;
                   16676:        case 0x06:
                   16677:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     16678:                if(!ignore_illegal_insn) {
1.1.1.28  root     16679:                        try {
                   16680:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16681:                                error("illegal instruction\n");
                   16682:                        } catch(...) {
                   16683:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   16684:                        }
1.1.1.14  root     16685:                } else {
                   16686: #if defined(HAS_I386)
1.1.1.39  root     16687:                        m_eip = m_int6h_skip_eip;
                   16688: #elif defined(HAS_I286)
                   16689:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     16690: #else
1.1.1.39  root     16691:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     16692: #endif
                   16693:                }
1.1       root     16694:                break;
1.1.1.33  root     16695:        case 0x09:
                   16696:                // ctrl-break is pressed
                   16697:                if(raise_int_1bh) {
                   16698: #if defined(HAS_I386)
                   16699:                        m_ext = 0; // not an external interrupt
                   16700:                        i386_trap(0x1b, 1, 0);
                   16701:                        m_ext = 1;
                   16702: #else
                   16703:                        PREFIX86(_interrupt)(0x1b);
                   16704: #endif
                   16705:                        raise_int_1bh = false;
                   16706:                }
1.1.1.8   root     16707:        case 0x08:
1.1.1.14  root     16708: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     16709:        case 0x0b:
                   16710:        case 0x0c:
                   16711:        case 0x0d:
                   16712:        case 0x0e:
                   16713:        case 0x0f:
                   16714:                // EOI
                   16715:                pic[0].isr &= ~(1 << (num - 0x08));
                   16716:                pic_update();
                   16717:                break;
1.1       root     16718:        case 0x10:
                   16719:                // PC BIOS - Video
1.1.1.14  root     16720:                if(!restore_console_on_exit) {
1.1.1.15  root     16721:                        change_console_size(scr_width, scr_height);
1.1       root     16722:                }
1.1.1.3   root     16723:                m_CF = 0;
1.1       root     16724:                switch(REG8(AH)) {
1.1.1.16  root     16725:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     16726:                case 0x01: pcbios_int_10h_01h(); break;
                   16727:                case 0x02: pcbios_int_10h_02h(); break;
                   16728:                case 0x03: pcbios_int_10h_03h(); break;
                   16729:                case 0x05: pcbios_int_10h_05h(); break;
                   16730:                case 0x06: pcbios_int_10h_06h(); break;
                   16731:                case 0x07: pcbios_int_10h_07h(); break;
                   16732:                case 0x08: pcbios_int_10h_08h(); break;
                   16733:                case 0x09: pcbios_int_10h_09h(); break;
                   16734:                case 0x0a: pcbios_int_10h_0ah(); break;
                   16735:                case 0x0b: break;
1.1.1.40  root     16736:                case 0x0c: pcbios_int_10h_0ch(); break;
                   16737:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     16738:                case 0x0e: pcbios_int_10h_0eh(); break;
                   16739:                case 0x0f: pcbios_int_10h_0fh(); break;
                   16740:                case 0x10: break;
1.1.1.14  root     16741:                case 0x11: pcbios_int_10h_11h(); break;
                   16742:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     16743:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     16744:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     16745:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     16746:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   16747:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     16748:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     16749:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   16750:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     16751:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     16752:                case 0x6f: break;
1.1.1.22  root     16753:                case 0x80: m_CF = 1; break; // unknown
                   16754:                case 0x81: m_CF = 1; break; // unknown
1.1       root     16755:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     16756:                case 0x83: pcbios_int_10h_83h(); break;
                   16757:                case 0x8b: break;
                   16758:                case 0x8c: m_CF = 1; break; // unknown
                   16759:                case 0x8d: m_CF = 1; break; // unknown
                   16760:                case 0x8e: m_CF = 1; break; // unknown
                   16761:                case 0x90: pcbios_int_10h_90h(); break;
                   16762:                case 0x91: pcbios_int_10h_91h(); break;
                   16763:                case 0x92: break;
                   16764:                case 0x93: break;
                   16765:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     16766:                case 0xfa: break; // ega register interface library is not installed
1.1       root     16767:                case 0xfe: pcbios_int_10h_feh(); break;
                   16768:                case 0xff: pcbios_int_10h_ffh(); break;
                   16769:                default:
1.1.1.22  root     16770:                        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));
                   16771:                        m_CF = 1;
1.1       root     16772:                        break;
                   16773:                }
                   16774:                break;
                   16775:        case 0x11:
                   16776:                // PC BIOS - Get Equipment List
1.1.1.26  root     16777:                REG16(AX) = msdos_get_equipment();
1.1       root     16778:                break;
                   16779:        case 0x12:
                   16780:                // PC BIOS - Get Memory Size
1.1.1.33  root     16781:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     16782:                break;
                   16783:        case 0x13:
1.1.1.42  root     16784:                // PC BIOS - Disk I/O
                   16785:                {
                   16786:                        static UINT8 last = 0x00;
                   16787:                        switch(REG8(AH)) {
                   16788:                        case 0x00: pcbios_int_13h_00h(); break;
                   16789:                        case 0x01: // get last status
                   16790:                                REG8(AH) = last;
                   16791:                                break;
                   16792:                        case 0x02: pcbios_int_13h_02h(); break;
                   16793:                        case 0x03: pcbios_int_13h_03h(); break;
                   16794:                        case 0x04: pcbios_int_13h_04h(); break;
                   16795:                        case 0x08: pcbios_int_13h_08h(); break;
                   16796:                        case 0x0a: pcbios_int_13h_02h(); break;
                   16797:                        case 0x0b: pcbios_int_13h_03h(); break;
                   16798:                        case 0x0d: pcbios_int_13h_00h(); break;
                   16799:                        case 0x10: pcbios_int_13h_10h(); break;
                   16800:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     16801:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     16802:                        case 0x05: // format
                   16803:                        case 0x06:
                   16804:                        case 0x07:
                   16805:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   16806:                                m_CF = 1;
                   16807:                                break;
                   16808:                        case 0x09:
                   16809:                        case 0x0c: // seek
                   16810:                        case 0x11: // recalib
                   16811:                        case 0x14:
                   16812:                        case 0x17:
                   16813:                                REG8(AH) = 0x00; // successful completion
                   16814:                                break;
1.1.1.43  root     16815:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   16816:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   16817:                                REG8(AH) = 0x01; // invalid function
                   16818:                                m_CF = 1;
                   16819:                                break;
1.1.1.42  root     16820:                        default:
                   16821:                                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));
                   16822:                                REG8(AH) = 0x01; // invalid function
                   16823:                                m_CF = 1;
                   16824:                                break;
                   16825:                        }
                   16826:                        last = REG8(AH);
                   16827:                }
1.1       root     16828:                break;
                   16829:        case 0x14:
                   16830:                // PC BIOS - Serial I/O
1.1.1.25  root     16831:                switch(REG8(AH)) {
                   16832:                case 0x00: pcbios_int_14h_00h(); break;
                   16833:                case 0x01: pcbios_int_14h_01h(); break;
                   16834:                case 0x02: pcbios_int_14h_02h(); break;
                   16835:                case 0x03: pcbios_int_14h_03h(); break;
                   16836:                case 0x04: pcbios_int_14h_04h(); break;
                   16837:                case 0x05: pcbios_int_14h_05h(); break;
                   16838:                default:
                   16839:                        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));
                   16840:                        break;
                   16841:                }
1.1       root     16842:                break;
                   16843:        case 0x15:
                   16844:                // PC BIOS
1.1.1.3   root     16845:                m_CF = 0;
1.1       root     16846:                switch(REG8(AH)) {
1.1.1.14  root     16847:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     16848:                case 0x23: pcbios_int_15h_23h(); break;
                   16849:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     16850:                case 0x41: break;
1.1       root     16851:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     16852:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     16853:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     16854:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     16855:                case 0x86: pcbios_int_15h_86h(); break;
                   16856:                case 0x87: pcbios_int_15h_87h(); break;
                   16857:                case 0x88: pcbios_int_15h_88h(); break;
                   16858:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     16859:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     16860:                case 0xc0: // PS/2 ???
1.1.1.54  root     16861: #ifndef EXT_BIOS_TOP
1.1.1.22  root     16862:                case 0xc1:
1.1.1.54  root     16863: #endif
1.1.1.30  root     16864:                case 0xc3: // PS50+ ???
                   16865:                case 0xc4:
1.1.1.22  root     16866:                        REG8(AH) = 0x86;
                   16867:                        m_CF = 1;
                   16868:                        break;
1.1.1.54  root     16869: #ifdef EXT_BIOS_TOP
                   16870:                case 0xc1: pcbios_int_15h_c1h(); break;
                   16871: #endif
                   16872:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     16873: #if defined(HAS_I386)
1.1       root     16874:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     16875: #endif
1.1       root     16876:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     16877:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     16878:                default:
1.1.1.22  root     16879:                        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));
                   16880:                        REG8(AH) = 0x86;
1.1.1.3   root     16881:                        m_CF = 1;
1.1       root     16882:                        break;
                   16883:                }
                   16884:                break;
                   16885:        case 0x16:
                   16886:                // PC BIOS - Keyboard
1.1.1.3   root     16887:                m_CF = 0;
1.1       root     16888:                switch(REG8(AH)) {
                   16889:                case 0x00: pcbios_int_16h_00h(); break;
                   16890:                case 0x01: pcbios_int_16h_01h(); break;
                   16891:                case 0x02: pcbios_int_16h_02h(); break;
                   16892:                case 0x03: pcbios_int_16h_03h(); break;
                   16893:                case 0x05: pcbios_int_16h_05h(); break;
                   16894:                case 0x10: pcbios_int_16h_00h(); break;
                   16895:                case 0x11: pcbios_int_16h_01h(); break;
                   16896:                case 0x12: pcbios_int_16h_12h(); break;
                   16897:                case 0x13: pcbios_int_16h_13h(); break;
                   16898:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     16899:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     16900:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     16901:                case 0xda: break; // unknown
1.1.1.43  root     16902:                case 0xdb: break; // unknown
1.1.1.22  root     16903:                case 0xff: break; // unknown
1.1       root     16904:                default:
1.1.1.22  root     16905:                        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     16906:                        break;
                   16907:                }
                   16908:                break;
                   16909:        case 0x17:
                   16910:                // PC BIOS - Printer
1.1.1.37  root     16911:                m_CF = 0;
                   16912:                switch(REG8(AH)) {
                   16913:                case 0x00: pcbios_int_17h_00h(); break;
                   16914:                case 0x01: pcbios_int_17h_01h(); break;
                   16915:                case 0x02: pcbios_int_17h_02h(); break;
                   16916:                case 0x03: pcbios_int_17h_03h(); break;
                   16917:                case 0x50: pcbios_int_17h_50h(); break;
                   16918:                case 0x51: pcbios_int_17h_51h(); break;
                   16919:                case 0x52: pcbios_int_17h_52h(); break;
                   16920:                case 0x84: pcbios_int_17h_84h(); break;
                   16921:                case 0x85: pcbios_int_17h_85h(); break;
                   16922:                default:
                   16923:                        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));
                   16924:                        break;
                   16925:                }
1.1       root     16926:                break;
                   16927:        case 0x1a:
                   16928:                // PC BIOS - Timer
1.1.1.3   root     16929:                m_CF = 0;
1.1       root     16930:                switch(REG8(AH)) {
                   16931:                case 0x00: pcbios_int_1ah_00h(); break;
                   16932:                case 0x01: break;
                   16933:                case 0x02: pcbios_int_1ah_02h(); break;
                   16934:                case 0x03: break;
                   16935:                case 0x04: pcbios_int_1ah_04h(); break;
                   16936:                case 0x05: break;
                   16937:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   16938:                case 0x0b: break;
1.1.1.14  root     16939:                case 0x35: break; // Word Perfect Third Party Interface?
                   16940:                case 0x36: break; // Word Perfect Third Party Interface
                   16941:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     16942:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     16943:                case 0xb1: break; // PCI BIOS v2.0c+
                   16944:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     16945:                default:
1.1.1.22  root     16946:                        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     16947:                        break;
                   16948:                }
                   16949:                break;
1.1.1.33  root     16950:        case 0x1b:
                   16951:                mem[0x471] = 0x00;
                   16952:                break;
1.1       root     16953:        case 0x20:
1.1.1.28  root     16954:                try {
                   16955:                        msdos_process_terminate(SREG(CS), retval, 1);
                   16956:                } catch(...) {
                   16957:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   16958:                }
1.1       root     16959:                break;
1.1.1.49  root     16960:        case 0x30:
1.1.1.46  root     16961:                // dummy interrupt for case map routine pointed in the country info
                   16962: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   16963: //                     REG8(AL) = 0x00;
                   16964: //                     break;
                   16965: //             }
1.1       root     16966:        case 0x21:
                   16967:                // MS-DOS System Call
1.1.1.3   root     16968:                m_CF = 0;
1.1.1.28  root     16969:                try {
1.1.1.46  root     16970:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     16971:                        case 0x00: msdos_int_21h_00h(); break;
                   16972:                        case 0x01: msdos_int_21h_01h(); break;
                   16973:                        case 0x02: msdos_int_21h_02h(); break;
                   16974:                        case 0x03: msdos_int_21h_03h(); break;
                   16975:                        case 0x04: msdos_int_21h_04h(); break;
                   16976:                        case 0x05: msdos_int_21h_05h(); break;
                   16977:                        case 0x06: msdos_int_21h_06h(); break;
                   16978:                        case 0x07: msdos_int_21h_07h(); break;
                   16979:                        case 0x08: msdos_int_21h_08h(); break;
                   16980:                        case 0x09: msdos_int_21h_09h(); break;
                   16981:                        case 0x0a: msdos_int_21h_0ah(); break;
                   16982:                        case 0x0b: msdos_int_21h_0bh(); break;
                   16983:                        case 0x0c: msdos_int_21h_0ch(); break;
                   16984:                        case 0x0d: msdos_int_21h_0dh(); break;
                   16985:                        case 0x0e: msdos_int_21h_0eh(); break;
                   16986:                        case 0x0f: msdos_int_21h_0fh(); break;
                   16987:                        case 0x10: msdos_int_21h_10h(); break;
                   16988:                        case 0x11: msdos_int_21h_11h(); break;
                   16989:                        case 0x12: msdos_int_21h_12h(); break;
                   16990:                        case 0x13: msdos_int_21h_13h(); break;
                   16991:                        case 0x14: msdos_int_21h_14h(); break;
                   16992:                        case 0x15: msdos_int_21h_15h(); break;
                   16993:                        case 0x16: msdos_int_21h_16h(); break;
                   16994:                        case 0x17: msdos_int_21h_17h(); break;
                   16995:                        case 0x18: msdos_int_21h_18h(); break;
                   16996:                        case 0x19: msdos_int_21h_19h(); break;
                   16997:                        case 0x1a: msdos_int_21h_1ah(); break;
                   16998:                        case 0x1b: msdos_int_21h_1bh(); break;
                   16999:                        case 0x1c: msdos_int_21h_1ch(); break;
                   17000:                        case 0x1d: msdos_int_21h_1dh(); break;
                   17001:                        case 0x1e: msdos_int_21h_1eh(); break;
                   17002:                        case 0x1f: msdos_int_21h_1fh(); break;
                   17003:                        case 0x20: msdos_int_21h_20h(); break;
                   17004:                        case 0x21: msdos_int_21h_21h(); break;
                   17005:                        case 0x22: msdos_int_21h_22h(); break;
                   17006:                        case 0x23: msdos_int_21h_23h(); break;
                   17007:                        case 0x24: msdos_int_21h_24h(); break;
                   17008:                        case 0x25: msdos_int_21h_25h(); break;
                   17009:                        case 0x26: msdos_int_21h_26h(); break;
                   17010:                        case 0x27: msdos_int_21h_27h(); break;
                   17011:                        case 0x28: msdos_int_21h_28h(); break;
                   17012:                        case 0x29: msdos_int_21h_29h(); break;
                   17013:                        case 0x2a: msdos_int_21h_2ah(); break;
                   17014:                        case 0x2b: msdos_int_21h_2bh(); break;
                   17015:                        case 0x2c: msdos_int_21h_2ch(); break;
                   17016:                        case 0x2d: msdos_int_21h_2dh(); break;
                   17017:                        case 0x2e: msdos_int_21h_2eh(); break;
                   17018:                        case 0x2f: msdos_int_21h_2fh(); break;
                   17019:                        case 0x30: msdos_int_21h_30h(); break;
                   17020:                        case 0x31: msdos_int_21h_31h(); break;
                   17021:                        case 0x32: msdos_int_21h_32h(); break;
                   17022:                        case 0x33: msdos_int_21h_33h(); break;
                   17023:                        case 0x34: msdos_int_21h_34h(); break;
                   17024:                        case 0x35: msdos_int_21h_35h(); break;
                   17025:                        case 0x36: msdos_int_21h_36h(); break;
                   17026:                        case 0x37: msdos_int_21h_37h(); break;
                   17027:                        case 0x38: msdos_int_21h_38h(); break;
                   17028:                        case 0x39: msdos_int_21h_39h(0); break;
                   17029:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   17030:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   17031:                        case 0x3c: msdos_int_21h_3ch(); break;
                   17032:                        case 0x3d: msdos_int_21h_3dh(); break;
                   17033:                        case 0x3e: msdos_int_21h_3eh(); break;
                   17034:                        case 0x3f: msdos_int_21h_3fh(); break;
                   17035:                        case 0x40: msdos_int_21h_40h(); break;
                   17036:                        case 0x41: msdos_int_21h_41h(0); break;
                   17037:                        case 0x42: msdos_int_21h_42h(); break;
                   17038:                        case 0x43: msdos_int_21h_43h(0); break;
                   17039:                        case 0x44: msdos_int_21h_44h(); break;
                   17040:                        case 0x45: msdos_int_21h_45h(); break;
                   17041:                        case 0x46: msdos_int_21h_46h(); break;
                   17042:                        case 0x47: msdos_int_21h_47h(0); break;
                   17043:                        case 0x48: msdos_int_21h_48h(); break;
                   17044:                        case 0x49: msdos_int_21h_49h(); break;
                   17045:                        case 0x4a: msdos_int_21h_4ah(); break;
                   17046:                        case 0x4b: msdos_int_21h_4bh(); break;
                   17047:                        case 0x4c: msdos_int_21h_4ch(); break;
                   17048:                        case 0x4d: msdos_int_21h_4dh(); break;
                   17049:                        case 0x4e: msdos_int_21h_4eh(); break;
                   17050:                        case 0x4f: msdos_int_21h_4fh(); break;
                   17051:                        case 0x50: msdos_int_21h_50h(); break;
                   17052:                        case 0x51: msdos_int_21h_51h(); break;
                   17053:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     17054:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     17055:                        case 0x54: msdos_int_21h_54h(); break;
                   17056:                        case 0x55: msdos_int_21h_55h(); break;
                   17057:                        case 0x56: msdos_int_21h_56h(0); break;
                   17058:                        case 0x57: msdos_int_21h_57h(); break;
                   17059:                        case 0x58: msdos_int_21h_58h(); break;
                   17060:                        case 0x59: msdos_int_21h_59h(); break;
                   17061:                        case 0x5a: msdos_int_21h_5ah(); break;
                   17062:                        case 0x5b: msdos_int_21h_5bh(); break;
                   17063:                        case 0x5c: msdos_int_21h_5ch(); break;
                   17064:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     17065:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     17066:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     17067:                        case 0x60: msdos_int_21h_60h(0); break;
                   17068:                        case 0x61: msdos_int_21h_61h(); break;
                   17069:                        case 0x62: msdos_int_21h_62h(); break;
                   17070:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     17071:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     17072:                        case 0x65: msdos_int_21h_65h(); break;
                   17073:                        case 0x66: msdos_int_21h_66h(); break;
                   17074:                        case 0x67: msdos_int_21h_67h(); break;
                   17075:                        case 0x68: msdos_int_21h_68h(); break;
                   17076:                        case 0x69: msdos_int_21h_69h(); break;
                   17077:                        case 0x6a: msdos_int_21h_6ah(); break;
                   17078:                        case 0x6b: msdos_int_21h_6bh(); break;
                   17079:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     17080:                        case 0x6d: // Find First ROM Program
                   17081:                        case 0x6e: // Find Next ROM Program
                   17082:                        case 0x6f: // Get/Set ROM Scan Start Address
                   17083:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   17084:                                break;
1.1.1.43  root     17085:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     17086:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     17087:                                switch(REG8(AL)) {
                   17088:                                case 0x0d: msdos_int_21h_710dh(); break;
                   17089:                                case 0x39: msdos_int_21h_39h(1); break;
                   17090:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   17091:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     17092:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     17093:                                case 0x43: msdos_int_21h_43h(1); break;
                   17094:                                case 0x47: msdos_int_21h_47h(1); break;
                   17095:                                case 0x4e: msdos_int_21h_714eh(); break;
                   17096:                                case 0x4f: msdos_int_21h_714fh(); break;
                   17097:                                case 0x56: msdos_int_21h_56h(1); break;
                   17098:                                case 0x60: msdos_int_21h_60h(1); break;
                   17099:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   17100:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   17101:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   17102:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   17103:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   17104:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     17105:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     17106:                                case 0xaa: msdos_int_21h_71aah(); break;
                   17107:                                default:
                   17108:                                        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));
                   17109:                                        REG16(AX) = 0x7100;
                   17110:                                        m_CF = 1;
                   17111:                                        break;
                   17112:                                }
                   17113:                                break;
1.1.1.48  root     17114:                        case 0x72: // Windows95 beta - LFN FindClose
                   17115: //                             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));
                   17116:                                REG16(AX) = 0x7200;
                   17117:                                m_CF = 1;
                   17118:                                break;
                   17119:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     17120:                                switch(REG8(AL)) {
                   17121:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     17122:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     17123:                                case 0x02: msdos_int_21h_7302h(); break;
                   17124:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     17125:                                // 0x04: Set DPB to Use for Formatting
                   17126:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     17127:                                default:
                   17128:                                        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));
                   17129:                                        REG16(AX) = 0x7300;
                   17130:                                        m_CF = 1;
                   17131:                                        break;
                   17132:                                }
1.1       root     17133:                                break;
1.1.1.30  root     17134:                        case 0xdb: msdos_int_21h_dbh(); break;
                   17135:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     17136:                        default:
1.1.1.22  root     17137:                                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     17138:                                REG16(AX) = 0x01;
1.1.1.3   root     17139:                                m_CF = 1;
1.1       root     17140:                                break;
                   17141:                        }
1.1.1.28  root     17142:                } catch(int error) {
                   17143:                        REG16(AX) = error;
                   17144:                        m_CF = 1;
                   17145:                } catch(...) {
                   17146:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     17147:                        m_CF = 1;
1.1       root     17148:                }
1.1.1.3   root     17149:                if(m_CF) {
1.1.1.23  root     17150:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     17151:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     17152:                        sda->extended_error_code = REG16(AX);
                   17153:                        switch(sda->extended_error_code) {
                   17154:                        case  4: // Too many open files
                   17155:                        case  8: // Insufficient memory
                   17156:                                sda->error_class = 1; // Out of resource
                   17157:                                break;
                   17158:                        case  5: // Access denied
                   17159:                                sda->error_class = 3; // Authorization
                   17160:                                break;
                   17161:                        case  7: // Memory control block destroyed
                   17162:                                sda->error_class = 4; // Internal
                   17163:                                break;
                   17164:                        case  2: // File not found
                   17165:                        case  3: // Path not found
                   17166:                        case 15: // Invaid drive specified
                   17167:                        case 18: // No more files
                   17168:                                sda->error_class = 8; // Not found
                   17169:                                break;
                   17170:                        case 32: // Sharing violation
                   17171:                        case 33: // Lock violation
                   17172:                                sda->error_class = 10; // Locked
                   17173:                                break;
                   17174: //                     case 16: // Removal of current directory attempted
                   17175:                        case 19: // Attempted write on protected disk
                   17176:                        case 21: // Drive not ready
                   17177: //                     case 29: // Write failure
                   17178: //                     case 30: // Read failure
                   17179: //                     case 82: // Cannot create subdirectory
                   17180:                                sda->error_class = 11; // Media
                   17181:                                break;
                   17182:                        case 80: // File already exists
                   17183:                                sda->error_class = 12; // Already exist
                   17184:                                break;
                   17185:                        default:
                   17186:                                sda->error_class = 13; // Unknown
                   17187:                                break;
                   17188:                        }
                   17189:                        sda->suggested_action = 1; // Retry
                   17190:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     17191:                }
1.1.1.33  root     17192:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     17193:                        // raise int 23h
                   17194: #if defined(HAS_I386)
                   17195:                        m_ext = 0; // not an external interrupt
                   17196:                        i386_trap(0x23, 1, 0);
                   17197:                        m_ext = 1;
                   17198: #else
                   17199:                        PREFIX86(_interrupt)(0x23);
                   17200: #endif
                   17201:                }
1.1       root     17202:                break;
                   17203:        case 0x22:
                   17204:                fatalerror("int 22h (terminate address)\n");
                   17205:        case 0x23:
1.1.1.28  root     17206:                try {
                   17207:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   17208:                } catch(...) {
                   17209:                        fatalerror("failed to terminate the current process by int 23h\n");
                   17210:                }
1.1       root     17211:                break;
                   17212:        case 0x24:
1.1.1.32  root     17213: /*
1.1.1.28  root     17214:                try {
                   17215:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17216:                } catch(...) {
                   17217:                        fatalerror("failed to terminate the current process by int 24h\n");
                   17218:                }
1.1.1.32  root     17219: */
                   17220:                msdos_int_24h();
1.1       root     17221:                break;
                   17222:        case 0x25:
                   17223:                msdos_int_25h();
                   17224:                break;
                   17225:        case 0x26:
                   17226:                msdos_int_26h();
                   17227:                break;
                   17228:        case 0x27:
1.1.1.28  root     17229:                try {
                   17230:                        msdos_int_27h();
                   17231:                } catch(...) {
                   17232:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   17233:                }
1.1       root     17234:                break;
                   17235:        case 0x28:
                   17236:                Sleep(10);
1.1.1.35  root     17237:                REQUEST_HARDWRE_UPDATE();
1.1       root     17238:                break;
                   17239:        case 0x29:
                   17240:                msdos_int_29h();
                   17241:                break;
                   17242:        case 0x2e:
                   17243:                msdos_int_2eh();
                   17244:                break;
                   17245:        case 0x2f:
                   17246:                // multiplex interrupt
                   17247:                switch(REG8(AH)) {
1.1.1.22  root     17248:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     17249:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     17250:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     17251:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17252:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17253:                case 0x14: msdos_int_2fh_14h(); break;
                   17254:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17255:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17256:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17257:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17258:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17259:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17260:                case 0x46: msdos_int_2fh_46h(); break;
                   17261:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17262:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17263:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17264:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17265:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17266:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17267:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17268:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17269:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17270:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17271:                default:
1.1.1.30  root     17272:                        switch(REG8(AL)) {
                   17273:                        case 0x00:
                   17274:                                // This is not installed
                   17275: //                             REG8(AL) = 0x00;
                   17276:                                break;
1.1.1.33  root     17277:                        case 0x01:
1.1.1.42  root     17278:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17279:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17280:                                        break;
                   17281:                                }
1.1.1.33  root     17282:                                // Banyan VINES v4+ is not installed
                   17283:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17284:                                        break;
                   17285:                                }
1.1.1.42  root     17286:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17287:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17288:                                        break;
                   17289:                                }
1.1.1.30  root     17290:                        default:
1.1.1.42  root     17291:                                // NORTON UTILITIES 5.0+
                   17292:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17293:                                        break;
                   17294:                                }
1.1.1.30  root     17295:                                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     17296:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17297:                                m_CF = 1;
                   17298:                                break;
                   17299:                        }
                   17300:                        break;
1.1       root     17301:                }
                   17302:                break;
1.1.1.24  root     17303:        case 0x33:
                   17304:                switch(REG8(AH)) {
                   17305:                case 0x00:
                   17306:                        // Mouse
1.1.1.49  root     17307:                        switch(REG16(AX)) {
                   17308:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17309:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17310:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17311:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17312:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17313:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17314:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17315:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17316:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17317:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17318:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17319:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17320:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17321:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17322:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17323:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17324:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17325:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17326:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17327:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17328:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17329:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17330:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17331:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17332:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17333:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17334:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17335:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17336:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17337:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17338:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17339:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17340:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17341:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17342:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17343:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17344:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17345:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17346:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17347:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17348:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17349:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17350:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17351:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17352:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17353:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17354:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17355:                        case 0x002f: break; // Mouse Hardware Reset
                   17356:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17357:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17358:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17359:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17360:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17361:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17362:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17363:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17364:                        default:
                   17365:                                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));
                   17366:                                break;
                   17367:                        }
                   17368:                        break;
                   17369:                default:
                   17370:                        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));
                   17371:                        break;
                   17372:                }
                   17373:                break;
1.1.1.59! root     17374:        case 0x65:
1.1.1.19  root     17375:                // dummy interrupt for EMS (int 67h)
                   17376:                switch(REG8(AH)) {
                   17377:                case 0x40: msdos_int_67h_40h(); break;
                   17378:                case 0x41: msdos_int_67h_41h(); break;
                   17379:                case 0x42: msdos_int_67h_42h(); break;
                   17380:                case 0x43: msdos_int_67h_43h(); break;
                   17381:                case 0x44: msdos_int_67h_44h(); break;
                   17382:                case 0x45: msdos_int_67h_45h(); break;
                   17383:                case 0x46: msdos_int_67h_46h(); break;
                   17384:                case 0x47: msdos_int_67h_47h(); break;
                   17385:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17386:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17387:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17388:                case 0x4b: msdos_int_67h_4bh(); break;
                   17389:                case 0x4c: msdos_int_67h_4ch(); break;
                   17390:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17391:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17392:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17393:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17394:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17395:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17396:                case 0x53: msdos_int_67h_53h(); break;
                   17397:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17398:                case 0x55: msdos_int_67h_55h(); break;
                   17399:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17400:                case 0x57: msdos_int_67h_57h(); break;
                   17401:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17402:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17403:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17404:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17405:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17406:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17407:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17408:                // 0xde: VCPI
1.1.1.30  root     17409:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17410:                default:
1.1.1.22  root     17411:                        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     17412:                        REG8(AH) = 0x84;
                   17413:                        break;
                   17414:                }
                   17415:                break;
                   17416: #ifdef SUPPORT_XMS
1.1.1.59! root     17417:        case 0x66:
1.1.1.19  root     17418:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17419:                try {
                   17420:                        switch(REG8(AH)) {
                   17421:                        case 0x00: msdos_call_xms_00h(); break;
                   17422:                        case 0x01: msdos_call_xms_01h(); break;
                   17423:                        case 0x02: msdos_call_xms_02h(); break;
                   17424:                        case 0x03: msdos_call_xms_03h(); break;
                   17425:                        case 0x04: msdos_call_xms_04h(); break;
                   17426:                        case 0x05: msdos_call_xms_05h(); break;
                   17427:                        case 0x06: msdos_call_xms_06h(); break;
                   17428:                        case 0x07: msdos_call_xms_07h(); break;
                   17429:                        case 0x08: msdos_call_xms_08h(); break;
                   17430:                        case 0x09: msdos_call_xms_09h(); break;
                   17431:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17432:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17433:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17434:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17435:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17436:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17437:                        case 0x10: msdos_call_xms_10h(); break;
                   17438:                        case 0x11: msdos_call_xms_11h(); break;
                   17439:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17440: #if defined(HAS_I386)
                   17441:                        case 0x88: msdos_call_xms_88h(); break;
                   17442:                        case 0x89: msdos_call_xms_89h(); break;
                   17443:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17444:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17445: #endif
1.1.1.28  root     17446:                        default:
                   17447:                                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));
                   17448:                                REG16(AX) = 0x0000;
                   17449:                                REG8(BL) = 0x80; // function not implemented
                   17450:                                break;
                   17451:                        }
                   17452:                } catch(...) {
1.1.1.19  root     17453:                        REG16(AX) = 0x0000;
1.1.1.28  root     17454:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17455:                }
                   17456:                break;
                   17457: #endif
1.1.1.59! root     17458: /*
        !          17459:        case 0x67:
        !          17460:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 65h
        !          17461:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
        !          17462:                break;
        !          17463: */
        !          17464:        case 0x69:
1.1.1.24  root     17465:                // irq12 (mouse)
                   17466:                mouse_push_ax = REG16(AX);
                   17467:                mouse_push_bx = REG16(BX);
                   17468:                mouse_push_cx = REG16(CX);
                   17469:                mouse_push_dx = REG16(DX);
                   17470:                mouse_push_si = REG16(SI);
                   17471:                mouse_push_di = REG16(DI);
                   17472:                
1.1.1.43  root     17473:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17474:                        REG16(AX) = mouse.status_irq;
                   17475:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17476:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17477:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17478:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17479:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17480:                        
1.1.1.49  root     17481:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17482:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17483:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17484:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17485:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.59! root     17486:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
        !          17487:                        mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17488:                        break;
1.1.1.24  root     17489:                }
1.1.1.43  root     17490:                for(int i = 0; i < 8; i++) {
                   17491:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17492:                                REG16(AX) = mouse.status_irq_alt;
                   17493:                                REG16(BX) = mouse.get_buttons();
                   17494:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17495:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17496:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17497:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17498:                                
1.1.1.49  root     17499:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17500:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17501:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17502:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17503:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.59! root     17504:                                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
        !          17505:                                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17506:                                break;
                   17507:                        }
                   17508:                }
1.1.1.59! root     17509:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     17510:                        UINT16 data_1st, data_2nd, data_3rd;
                   17511:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   17512:                        i386_push16(data_1st);
                   17513:                        i386_push16(data_2nd);
                   17514:                        i386_push16(data_3rd);
                   17515:                        i386_push16(0x0000);
                   17516:                        
                   17517:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17518:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
                   17519:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
                   17520:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
                   17521:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
1.1.1.59! root     17522:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
        !          17523:                        mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.54  root     17524:                        break;
                   17525:                }
1.1.1.43  root     17526:                // invalid call addr :-(
1.1.1.49  root     17527:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17528:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17529:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17530:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17531:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.59! root     17532:                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
        !          17533:                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.24  root     17534:                break;
1.1.1.59! root     17535:        case 0x6a:
        !          17536:                // end of ps/2 mouse bios
        !          17537:                i386_pop16();
        !          17538:                i386_pop16();
        !          17539:                i386_pop16();
        !          17540:                i386_pop16();
1.1.1.24  root     17541:        case 0x6b:
                   17542:                // end of irq12 (mouse)
                   17543:                REG16(AX) = mouse_push_ax;
                   17544:                REG16(BX) = mouse_push_bx;
                   17545:                REG16(CX) = mouse_push_cx;
                   17546:                REG16(DX) = mouse_push_dx;
                   17547:                REG16(SI) = mouse_push_si;
                   17548:                REG16(DI) = mouse_push_di;
                   17549:                
                   17550:                // EOI
                   17551:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17552:                        pic[0].isr &= ~(1 << 2); // master
                   17553:                }
                   17554:                pic_update();
                   17555:                break;
                   17556:        case 0x6c:
1.1.1.19  root     17557:                // dummy interrupt for case map routine pointed in the country info
                   17558:                if(REG8(AL) >= 0x80) {
                   17559:                        char tmp[2] = {0};
                   17560:                        tmp[0] = REG8(AL);
                   17561:                        my_strupr(tmp);
                   17562:                        REG8(AL) = tmp[0];
                   17563:                }
                   17564:                break;
1.1.1.27  root     17565:        case 0x6d:
                   17566:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17567:                REG8(AL) = 0x86; // not supported
                   17568:                m_CF = 1;
                   17569:                break;
1.1.1.32  root     17570:        case 0x6e:
                   17571:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17572:                {
                   17573:                        UINT16 code = REG16(AX);
                   17574:                        if(code & 0xf0) {
                   17575:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17576:                        }
                   17577:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17578:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17579:                                        const char *message = NULL;
                   17580:                                        if(active_code_page == 932) {
                   17581:                                                message = param_error_table[i].message_japanese;
                   17582:                                        }
                   17583:                                        if(message == NULL) {
                   17584:                                                message = param_error_table[i].message_english;
                   17585:                                        }
                   17586:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   17587:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   17588:                                        
                   17589:                                        SREG(ES) = WORK_TOP >> 4;
                   17590:                                        i386_load_segment_descriptor(ES);
                   17591:                                        REG16(DI) = 0x0000;
                   17592:                                        break;
                   17593:                                }
                   17594:                        }
                   17595:                }
                   17596:                break;
1.1.1.49  root     17597:        case 0x6f:
                   17598:                // dummy interrupt for end of alter page map and call
                   17599:                {
                   17600:                        UINT16 handles[4], pages[4];
                   17601:                        
                   17602:                        // pop old mapping data in new mapping
                   17603:                        for(int i = 0; i < 4; i++) {
                   17604:                                pages  [3 - i] = i386_pop16();
                   17605:                                handles[3 - i] = i386_pop16();
                   17606:                        }
                   17607:                        
                   17608:                        // restore old mapping
                   17609:                        for(int i = 0; i < 4; i++) {
                   17610:                                UINT16 handle = handles[i];
                   17611:                                UINT16 page   = pages  [i];
                   17612:                                
                   17613:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   17614:                                        ems_map_page(i, handle, page);
                   17615:                                } else {
                   17616:                                        ems_unmap_page(i);
                   17617:                                }
                   17618:                        }
                   17619:                        // do ret_far (pop cs/ip) in old mapping
                   17620:                }
                   17621:                break;
1.1.1.8   root     17622:        case 0x70:
                   17623:        case 0x71:
                   17624:        case 0x72:
                   17625:        case 0x73:
                   17626:        case 0x74:
                   17627:        case 0x75:
                   17628:        case 0x76:
                   17629:        case 0x77:
                   17630:                // EOI
                   17631:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   17632:                        pic[0].isr &= ~(1 << 2); // master
                   17633:                }
                   17634:                pic_update();
                   17635:                break;
1.1       root     17636:        default:
1.1.1.22  root     17637: //             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     17638:                break;
                   17639:        }
                   17640:        
                   17641:        // update cursor position
                   17642:        if(cursor_moved) {
1.1.1.36  root     17643:                pcbios_update_cursor_position();
1.1       root     17644:                cursor_moved = false;
                   17645:        }
                   17646: }
                   17647: 
                   17648: // init
                   17649: 
                   17650: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   17651: {
                   17652:        // init file handler
                   17653:        memset(file_handler, 0, sizeof(file_handler));
                   17654:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   17655:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   17656:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     17657: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17658:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     17659: #else
                   17660:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   17661: #endif
                   17662:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     17663:        }
1.1.1.21  root     17664:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     17665: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   17666:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     17667:        }
1.1       root     17668:        _dup2(0, DUP_STDIN);
                   17669:        _dup2(1, DUP_STDOUT);
                   17670:        _dup2(2, DUP_STDERR);
1.1.1.21  root     17671:        _dup2(3, DUP_STDAUX);
                   17672:        _dup2(4, DUP_STDPRN);
1.1       root     17673:        
1.1.1.24  root     17674:        // init mouse
                   17675:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     17676:        mouse.enabled = true;   // from DOSBox
                   17677:        mouse.hidden = 1;       // hidden in default ???
                   17678:        mouse.old_hidden = 1;   // from DOSBox
                   17679:        mouse.max_position.x = 8 * (scr_width  - 1);
                   17680:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     17681:        mouse.mickey.x = 8;
                   17682:        mouse.mickey.y = 16;
                   17683:        
1.1.1.26  root     17684: #ifdef SUPPORT_XMS
                   17685:        // init xms
                   17686:        msdos_xms_init();
                   17687: #endif
                   17688:        
1.1       root     17689:        // init process
                   17690:        memset(process, 0, sizeof(process));
                   17691:        
1.1.1.13  root     17692:        // init dtainfo
                   17693:        msdos_dta_info_init();
                   17694:        
1.1       root     17695:        // init memory
                   17696:        memset(mem, 0, sizeof(mem));
                   17697:        
                   17698:        // bios data area
1.1.1.23  root     17699:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     17700:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   17701:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.58  root     17702: //     CONSOLE_FONT_INFO cfi;
1.1.1.57  root     17703: //     GetCurrentConsoleFont(hStdout, FALSE, &cfi);
1.1.1.58  root     17704:        int font_width = 10, font_height = 18; // default in english mode
                   17705:        get_console_font_size(hStdout, &font_width, &font_height);
1.1.1.14  root     17706:        
                   17707:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   17708:        text_vram_top_address = TEXT_VRAM_TOP;
                   17709:        text_vram_end_address = text_vram_top_address + regen;
                   17710:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   17711:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     17712:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     17713:        
                   17714:        if(regen > 0x4000) {
                   17715:                regen = 0x8000;
                   17716:                vram_pages = 1;
                   17717:        } else if(regen > 0x2000) {
                   17718:                regen = 0x4000;
                   17719:                vram_pages = 2;
                   17720:        } else if(regen > 0x1000) {
                   17721:                regen = 0x2000;
                   17722:                vram_pages = 4;
                   17723:        } else {
                   17724:                regen = 0x1000;
                   17725:                vram_pages = 8;
                   17726:        }
1.1       root     17727:        
1.1.1.25  root     17728:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   17729:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     17730:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   17731:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     17732:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     17733:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   17734:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     17735: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17736:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     17737: #endif
1.1.1.26  root     17738:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     17739:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     17740:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   17741:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     17742:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     17743:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   17744:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     17745:        *(UINT16 *)(mem + 0x44e) = 0;
                   17746:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     17747:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     17748:        *(UINT8  *)(mem + 0x460) = 7;
                   17749:        *(UINT8  *)(mem + 0x461) = 7;
                   17750:        *(UINT8  *)(mem + 0x462) = 0;
                   17751:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     17752:        *(UINT8  *)(mem + 0x465) = 0x09;
                   17753:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     17754:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     17755:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   17756:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     17757:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
1.1.1.58  root     17758:        *(UINT16 *)(mem + 0x485) = font_height;
1.1.1.14  root     17759:        *(UINT8  *)(mem + 0x487) = 0x60;
                   17760:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     17761: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17762:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     17763: #endif
1.1.1.14  root     17764:        
                   17765:        // initial screen
                   17766:        SMALL_RECT rect;
                   17767:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   17768:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   17769:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   17770:                for(int x = 0; x < scr_width; x++) {
                   17771:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   17772:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   17773:                }
                   17774:        }
1.1       root     17775:        
1.1.1.19  root     17776:        // init mcb
1.1       root     17777:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     17778:        
                   17779:        // iret table
                   17780:        // note: int 2eh vector should address the routine in command.com,
                   17781:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   17782:        // so move iret table into allocated memory block
                   17783:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.58  root     17784:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, (IRET_SIZE + 5 * 128) >> 4);
1.1.1.19  root     17785:        IRET_TOP = seg << 4;
1.1.1.58  root     17786:        seg += (IRET_SIZE + 5 * 128) >> 4;
1.1.1.25  root     17787:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     17788:        
1.1.1.58  root     17789:        // note: SO1 checks int 21h vector and if it aims iret (cfh)
                   17790:        // it is recognized SO1 is not running on MS-DOS environment
                   17791:        for(int i = 0; i < 128; i++) {
                   17792:                // jmp far (IRET_TOP >> 4):(interrupt number)
                   17793:                *(UINT8  *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 0) = 0xea;
                   17794:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 1) = i;
                   17795:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 3) = IRET_TOP >> 4;
                   17796:        }
                   17797:        
1.1.1.19  root     17798:        // dummy xms/ems device
1.1.1.33  root     17799:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     17800:        XMS_TOP = seg << 4;
                   17801:        seg += XMS_SIZE >> 4;
                   17802:        
                   17803:        // environment
1.1.1.33  root     17804:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     17805:        int env_seg = seg;
                   17806:        int ofs = 0;
1.1.1.32  root     17807:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   17808:        char comspec_added = 0;
1.1.1.33  root     17809:        char lastdrive_added = 0;
1.1.1.32  root     17810:        char env_msdos_path[ENV_SIZE] = {0};
                   17811:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     17812:        char prompt_added = 0;
1.1.1.32  root     17813:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     17814:        char tz_added = 0;
1.1.1.45  root     17815:        const char *path, *short_path;
1.1.1.32  root     17816:        
                   17817:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   17818:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17819:                        strcpy(env_append, short_path);
                   17820:                }
                   17821:        }
                   17822:        if((path = getenv("APPEND")) != NULL) {
                   17823:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17824:                        if(env_append[0] != '\0') {
                   17825:                                strcat(env_append, ";");
                   17826:                        }
                   17827:                        strcat(env_append, short_path);
                   17828:                }
                   17829:        }
                   17830:        
                   17831:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   17832:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17833:                        strcpy(comspec_path, short_path);
                   17834:                }
                   17835:        }
                   17836:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   17837:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17838:                        strcpy(comspec_path, short_path);
                   17839:                }
                   17840:        }
1.1       root     17841:        
1.1.1.28  root     17842:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     17843:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17844:                        strcpy(env_msdos_path, short_path);
                   17845:                        strcpy(env_path, short_path);
1.1.1.14  root     17846:                }
                   17847:        }
1.1.1.28  root     17848:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     17849:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17850:                        if(env_path[0] != '\0') {
                   17851:                                strcat(env_path, ";");
                   17852:                        }
                   17853:                        strcat(env_path, short_path);
1.1.1.9   root     17854:                }
                   17855:        }
1.1.1.32  root     17856:        
                   17857:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   17858:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     17859:        }
1.1.1.32  root     17860:        for(int i = 0; i < 4; i++) {
                   17861:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   17862:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   17863:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17864:                                strcpy(env_temp, short_path);
                   17865:                                break;
                   17866:                        }
                   17867:                }
1.1.1.24  root     17868:        }
1.1.1.32  root     17869:        
1.1.1.9   root     17870:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     17871:                // lower to upper
1.1.1.28  root     17872:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     17873:                strcpy(tmp, *p);
                   17874:                for(int i = 0;; i++) {
                   17875:                        if(tmp[i] == '=') {
                   17876:                                tmp[i] = '\0';
                   17877:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     17878:                                my_strupr(name);
1.1       root     17879:                                tmp[i] = '=';
                   17880:                                break;
                   17881:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     17882:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     17883:                        }
                   17884:                }
1.1.1.33  root     17885:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   17886:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   17887:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     17888:                        // ignore non standard environments
                   17889:                } else {
1.1.1.33  root     17890:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     17891:                                if(env_append[0] != '\0') {
                   17892:                                        sprintf(tmp, "APPEND=%s", env_append);
                   17893:                                } else {
                   17894:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   17895:                                }
                   17896:                                append_added = 1;
                   17897:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     17898:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     17899:                                comspec_added = 1;
1.1.1.33  root     17900:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   17901:                                char *env = getenv("MSDOS_LASTDRIVE");
                   17902:                                if(env != NULL) {
                   17903:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   17904:                                }
                   17905:                                lastdrive_added = 1;
                   17906:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     17907:                                if(env_msdos_path[0] != '\0') {
                   17908:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   17909:                                } else {
                   17910:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   17911:                                }
1.1.1.33  root     17912:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     17913:                                if(env_path[0] != '\0') {
                   17914:                                        sprintf(tmp, "PATH=%s", env_path);
                   17915:                                } else {
                   17916:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   17917:                                }
1.1.1.32  root     17918:                                path_added = 1;
1.1.1.33  root     17919:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   17920:                                prompt_added = 1;
1.1.1.28  root     17921:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   17922:                                if(env_temp[0] != '\0') {
                   17923:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   17924:                                } else {
                   17925:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   17926:                                }
1.1.1.32  root     17927:                                temp_added = 1;
1.1.1.33  root     17928:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     17929:                                if(env_temp[0] != '\0') {
                   17930:                                        sprintf(tmp, "TMP=%s", env_temp);
                   17931:                                } else {
                   17932:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     17933:                                }
1.1.1.32  root     17934:                                tmp_added = 1;
1.1.1.33  root     17935:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   17936:                                char *env = getenv("MSDOS_TZ");
                   17937:                                if(env != NULL) {
                   17938:                                        sprintf(tmp, "TZ=%s", env);
                   17939:                                }
                   17940:                                tz_added = 1;
1.1       root     17941:                        }
                   17942:                        int len = strlen(tmp);
1.1.1.14  root     17943:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     17944:                                fatalerror("too many environments\n");
                   17945:                        }
                   17946:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   17947:                        ofs += len + 1;
                   17948:                }
                   17949:        }
1.1.1.32  root     17950:        if(!append_added && env_append[0] != '\0') {
                   17951:                #define SET_ENV(name, value) { \
                   17952:                        char tmp[ENV_SIZE]; \
                   17953:                        sprintf(tmp, "%s=%s", name, value); \
                   17954:                        int len = strlen(tmp); \
                   17955:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   17956:                                fatalerror("too many environments\n"); \
                   17957:                        } \
                   17958:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   17959:                        ofs += len + 1; \
                   17960:                }
                   17961:                SET_ENV("APPEND", env_append);
                   17962:        }
                   17963:        if(!comspec_added) {
                   17964:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   17965:        }
1.1.1.33  root     17966:        if(!lastdrive_added) {
                   17967:                SET_ENV("LASTDRIVE", "Z");
                   17968:        }
1.1.1.32  root     17969:        if(!path_added) {
                   17970:                SET_ENV("PATH", env_path);
                   17971:        }
1.1.1.33  root     17972:        if(!prompt_added) {
                   17973:                SET_ENV("PROMPT", "$P$G");
                   17974:        }
1.1.1.32  root     17975:        if(!temp_added) {
                   17976:                SET_ENV("TEMP", env_temp);
                   17977:        }
                   17978:        if(!tmp_added) {
                   17979:                SET_ENV("TMP", env_temp);
                   17980:        }
1.1.1.33  root     17981:        if(!tz_added) {
                   17982:                TIME_ZONE_INFORMATION tzi;
                   17983:                HKEY hKey, hSubKey;
                   17984:                char tzi_std_name[64];
                   17985:                char tz_std[8] = "GMT";
                   17986:                char tz_dlt[8] = "GST";
                   17987:                char tz_value[32];
                   17988:                
                   17989:                // timezone name from GetTimeZoneInformation may not be english
                   17990:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   17991:                setlocale(LC_CTYPE, "");
                   17992:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   17993:                
                   17994:                // get english timezone name from registry
                   17995:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   17996:                        for(DWORD i = 0; !tz_added; i++) {
                   17997:                                char reg_name[256], sub_key[1024], std_name[256];
                   17998:                                DWORD size;
                   17999:                                FILETIME ftTime;
                   18000:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   18001:                                
                   18002:                                if(result == ERROR_SUCCESS) {
                   18003:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   18004:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   18005:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   18006:                                                        // search english timezone name from table
1.1.1.37  root     18007:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     18008:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   18009:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   18010:                                                                                if(tz_table[j].std != NULL) {
                   18011:                                                                                        strcpy(tz_std, tz_table[j].std);
                   18012:                                                                                }
                   18013:                                                                                if(tz_table[j].dlt != NULL) {
                   18014:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   18015:                                                                                }
                   18016:                                                                                tz_added = 1;
                   18017:                                                                                break;
                   18018:                                                                        }
                   18019:                                                                }
                   18020:                                                        }
                   18021:                                                }
                   18022:                                                RegCloseKey(hSubKey);
                   18023:                                        }
                   18024:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   18025:                                        break;
                   18026:                                }
                   18027:                        }
                   18028:                        RegCloseKey(hKey);
                   18029:                }
                   18030:                if((tzi.Bias % 60) != 0) {
                   18031:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   18032:                } else {
                   18033:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   18034:                }
                   18035:                if(daylight) {
                   18036:                        strcat(tz_value, tz_dlt);
                   18037:                }
                   18038:                SET_ENV("TZ", tz_value);
                   18039:        }
1.1       root     18040:        seg += (ENV_SIZE >> 4);
                   18041:        
                   18042:        // psp
1.1.1.33  root     18043:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     18044:        current_psp = seg;
1.1.1.35  root     18045:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   18046:        psp->parent_psp = current_psp;
1.1       root     18047:        seg += (PSP_SIZE >> 4);
                   18048:        
1.1.1.19  root     18049:        // first free mcb in conventional memory
1.1.1.33  root     18050:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     18051:        first_mcb = seg;
                   18052:        
1.1.1.19  root     18053:        // dummy mcb to link to umb
1.1.1.33  root     18054: #if 0
1.1.1.39  root     18055:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     18056: #else
1.1.1.39  root     18057:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     18058: #endif
1.1.1.19  root     18059:        
                   18060:        // first free mcb in upper memory block
1.1.1.8   root     18061:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     18062:        
1.1.1.29  root     18063: #ifdef SUPPORT_HMA
                   18064:        // first free mcb in high memory area
                   18065:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   18066: #endif
                   18067:        
1.1.1.26  root     18068:        // interrupt vector
1.1.1.58  root     18069:        for(int i = 0; i < 256; i++) {
                   18070:                // 00-07: CPU exception handler
                   18071:                // 08-0F: IRQ 0-7
                   18072:                // 10-1F: PC BIOS
                   18073:                // 20-3F: MS-DOS system call
                   18074:                // 70-77: IRQ 8-15
                   18075:                *(UINT16 *)(mem + 4 * i + 0) = (i <= 0x3f || (i >= 0x70 && i <= 0x77)) ? (IRET_SIZE + 5 * i) : i;
1.1.1.26  root     18076:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   18077:        }
1.1.1.49  root     18078:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   18079:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18080:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   18081:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   18082:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   18083:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     18084:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   18085:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18086:        
1.1.1.29  root     18087:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     18088:        static const struct {
                   18089:                UINT16 attributes;
                   18090:                char *dev_name;
                   18091:        } dummy_devices[] = {
                   18092:                {0x8013, "CON     "},
                   18093:                {0x8000, "AUX     "},
                   18094:                {0xa0c0, "PRN     "},
                   18095:                {0x8008, "CLOCK$  "},
                   18096:                {0x8000, "COM1    "},
                   18097:                {0xa0c0, "LPT1    "},
                   18098:                {0xa0c0, "LPT2    "},
                   18099:                {0xa0c0, "LPT3    "},
                   18100:                {0x8000, "COM2    "},
                   18101:                {0x8000, "COM3    "},
                   18102:                {0x8000, "COM4    "},
1.1.1.30  root     18103: //             {0xc000, "CONFIG$ "},
                   18104:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     18105:        };
                   18106:        static const UINT8 dummy_device_routine[] = {
                   18107:                // from NUL device of Windows 98 SE
                   18108:                // or word ptr ES:[BX+03],0100
                   18109:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   18110:                // retf
                   18111:                0xcb,
                   18112:        };
1.1.1.29  root     18113:        device_t *last = NULL;
1.1.1.32  root     18114:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     18115:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     18116:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   18117:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18118:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     18119:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   18120:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     18121:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     18122:                last = device;
                   18123:        }
                   18124:        if(last != NULL) {
                   18125:                last->next_driver.w.l = 0;
                   18126:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     18127:        }
1.1.1.29  root     18128:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     18129:        
1.1.1.25  root     18130:        // dos info
                   18131:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   18132:        dos_info->magic_word = 1;
                   18133:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   18134:        dos_info->first_dpb.w.l = 0;
                   18135:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   18136:        dos_info->first_sft.w.l = 0;
                   18137:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     18138:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     18139:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18140:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     18141:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   18142:        dos_info->max_sector_len = 512;
                   18143:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   18144:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   18145:        dos_info->cds.w.l = 0;
                   18146:        dos_info->cds.w.h = CDS_TOP >> 4;
                   18147:        dos_info->fcb_table.w.l = 0;
                   18148:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   18149:        dos_info->last_drive = 'Z' - 'A' + 1;
                   18150:        dos_info->buffers_x = 20;
                   18151:        dos_info->buffers_y = 0;
                   18152:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     18153:        dos_info->nul_device.next_driver.w.l = 22;
                   18154:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     18155:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     18156:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   18157:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18158:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   18159:        dos_info->disk_buf_heads.w.l = 0;
                   18160:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     18161:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     18162:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   18163:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     18164:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     18165:        
                   18166:        char *env;
                   18167:        if((env = getenv("LASTDRIVE")) != NULL) {
                   18168:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18169:                        dos_info->last_drive = env[0] - 'A' + 1;
                   18170:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18171:                        dos_info->last_drive = env[0] - 'a' + 1;
                   18172:                }
                   18173:        }
                   18174:        if((env = getenv("windir")) != NULL) {
                   18175:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18176:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   18177:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18178:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   18179:                }
                   18180:        }
                   18181: #if defined(HAS_I386)
                   18182:        dos_info->i386_or_later = 1;
                   18183: #else
                   18184:        dos_info->i386_or_later = 0;
                   18185: #endif
                   18186:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   18187:        
1.1.1.27  root     18188:        // ems (int 67h) and xms
1.1.1.25  root     18189:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   18190:        xms_device->next_driver.w.l = 0xffff;
                   18191:        xms_device->next_driver.w.h = 0xffff;
                   18192:        xms_device->attributes = 0xc000;
1.1.1.29  root     18193:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   18194:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18195:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   18196:        
1.1.1.59! root     18197:        mem[XMS_TOP + 0x12] = 0xcd;     // int 65h (dummy)
        !          18198:        mem[XMS_TOP + 0x13] = 0x65;
1.1.1.26  root     18199:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     18200: #ifdef SUPPORT_XMS
                   18201:        if(support_xms) {
1.1.1.59! root     18202:                mem[XMS_TOP + 0x15] = 0xcd;     // int 66h (dummy)
        !          18203:                mem[XMS_TOP + 0x16] = 0x66;
1.1.1.26  root     18204:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     18205:        } else
                   18206: #endif
1.1.1.26  root     18207:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     18208:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     18209:        
1.1.1.26  root     18210:        // irq12 routine (mouse)
1.1.1.59! root     18211:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 69h (dummy)
        !          18212:        mem[DUMMY_TOP + 0x01] = 0x69;
1.1.1.49  root     18213:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   18214:        mem[DUMMY_TOP + 0x03] = 0xff;
                   18215:        mem[DUMMY_TOP + 0x04] = 0xff;
                   18216:        mem[DUMMY_TOP + 0x05] = 0xff;
                   18217:        mem[DUMMY_TOP + 0x06] = 0xff;
1.1.1.59! root     18218: //     mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
        !          18219: //     mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.49  root     18220:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   18221:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   18222:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     18223:        
1.1.1.27  root     18224:        // case map routine
1.1.1.49  root     18225:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   18226:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   18227:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     18228:        
                   18229:        // font read routine
1.1.1.49  root     18230:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   18231:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   18232:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     18233:        
1.1.1.32  root     18234:        // error message read routine
1.1.1.49  root     18235:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   18236:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   18237:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     18238:        
1.1.1.35  root     18239:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     18240:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   18241:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   18242:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   18243:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   18244:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     18245:        
1.1.1.50  root     18246:        // irq0 routine (system timer)
1.1.1.49  root     18247:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   18248:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   18249:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   18250:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   18251:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   18252:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   18253:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   18254:        
                   18255:        // alter page map and call routine
                   18256:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   18257:        mem[DUMMY_TOP + 0x20] = 0xff;
                   18258:        mem[DUMMY_TOP + 0x21] = 0xff;
                   18259:        mem[DUMMY_TOP + 0x22] = 0xff;
                   18260:        mem[DUMMY_TOP + 0x23] = 0xff;
                   18261:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   18262:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   18263:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     18264:        
1.1.1.50  root     18265:        // call int 29h routine
                   18266:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   18267:        mem[DUMMY_TOP + 0x28] = 0x29;
                   18268:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   18269:        
1.1.1.26  root     18270:        // boot routine
1.1.1.59! root     18271:        mem[0xffff0 + 0x00] = 0xf4;     // halt to exit MS-DOS Player
        !          18272: #if 1
        !          18273:        mem[0xffff0 + 0x05] = '0';      // rom date (same as DOSBox)
        !          18274:        mem[0xffff0 + 0x06] = '1';
        !          18275:        mem[0xffff0 + 0x07] = '/';
        !          18276:        mem[0xffff0 + 0x08] = '0';
        !          18277:        mem[0xffff0 + 0x09] = '1';
        !          18278:        mem[0xffff0 + 0x0a] = '/';
        !          18279:        mem[0xffff0 + 0x0b] = '9';
        !          18280:        mem[0xffff0 + 0x0c] = '2';
        !          18281:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
        !          18282:        mem[0xffff0 + 0x0f] = 0x55;     // signature
        !          18283: #else
        !          18284:        mem[0xffff0 + 0x05] = '0';      // rom date (same as Windows 98 SE)
1.1.1.49  root     18285:        mem[0xffff0 + 0x06] = '2';
                   18286:        mem[0xffff0 + 0x07] = '/';
                   18287:        mem[0xffff0 + 0x08] = '2';
                   18288:        mem[0xffff0 + 0x09] = '2';
                   18289:        mem[0xffff0 + 0x0a] = '/';
                   18290:        mem[0xffff0 + 0x0b] = '0';
                   18291:        mem[0xffff0 + 0x0c] = '6';
1.1.1.59! root     18292:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
1.1.1.49  root     18293:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.59! root     18294: #endif
1.1.1.24  root     18295:        
1.1       root     18296:        // param block
                   18297:        // + 0: param block (22bytes)
                   18298:        // +24: fcb1/2 (20bytes)
                   18299:        // +44: command tail (128bytes)
                   18300:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18301:        param->env_seg = 0;
                   18302:        param->cmd_line.w.l = 44;
                   18303:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18304:        param->fcb1.w.l = 24;
                   18305:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18306:        param->fcb2.w.l = 24;
                   18307:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18308:        
                   18309:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18310:        
                   18311:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18312:        if(argc > 1) {
                   18313:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18314:                for(int i = 2; i < argc; i++) {
                   18315:                        char tmp[128];
                   18316:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18317:                        strcpy(cmd_line->cmd, tmp);
                   18318:                }
                   18319:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18320:        } else {
                   18321:                cmd_line->len = 0;
                   18322:        }
                   18323:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18324:        
                   18325:        // system file table
1.1.1.21  root     18326:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18327:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18328:        
1.1.1.19  root     18329:        // disk buffer header (from DOSBox)
                   18330:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18331:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18332:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18333:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18334:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18335:        
1.1       root     18336:        // fcb table
                   18337:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18338:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18339:        
1.1.1.41  root     18340:        // drive parameter block
1.1.1.42  root     18341:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18342:                // may be a floppy drive
1.1.1.44  root     18343:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18344:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18345:                cds->drive_attrib = 0x4000;     // physical drive
                   18346:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18347:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18348:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18349:                cds->bs_offset = 2;
                   18350:                
1.1.1.41  root     18351:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18352:                dpb->drive_num = i;
                   18353:                dpb->unit_num = i;
1.1.1.43  root     18354:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18355:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18356:        }
                   18357:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18358:                msdos_cds_update(i);
1.1.1.42  root     18359:                UINT16 seg, ofs;
                   18360:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18361:        }
                   18362:        
1.1.1.17  root     18363:        // nls stuff
                   18364:        msdos_nls_tables_init();
1.1       root     18365:        
                   18366:        // execute command
1.1.1.28  root     18367:        try {
1.1.1.52  root     18368:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18369:                        fatalerror("'%s' not found\n", argv[0]);
                   18370:                }
                   18371:        } catch(...) {
                   18372:                // we should not reach here :-(
                   18373:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18374:        }
                   18375:        retval = 0;
                   18376:        return(0);
                   18377: }
                   18378: 
                   18379: #define remove_std_file(path) { \
                   18380:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18381:        if(fd != -1) { \
                   18382:                _lseek(fd, 0, SEEK_END); \
                   18383:                int size = _tell(fd); \
                   18384:                _close(fd); \
                   18385:                if(size == 0) { \
                   18386:                        remove(path); \
                   18387:                } \
                   18388:        } \
                   18389: }
                   18390: 
                   18391: void msdos_finish()
                   18392: {
                   18393:        for(int i = 0; i < MAX_FILES; i++) {
                   18394:                if(file_handler[i].valid) {
                   18395:                        _close(i);
                   18396:                }
                   18397:        }
1.1.1.21  root     18398: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18399:        remove_std_file("stdaux.txt");
1.1.1.21  root     18400: #endif
1.1.1.30  root     18401: #ifdef SUPPORT_XMS
                   18402:        msdos_xms_finish();
                   18403: #endif
1.1       root     18404:        msdos_dbcs_table_finish();
                   18405: }
                   18406: 
                   18407: /* ----------------------------------------------------------------------------
                   18408:        PC/AT hardware emulation
                   18409: ---------------------------------------------------------------------------- */
                   18410: 
                   18411: void hardware_init()
                   18412: {
1.1.1.3   root     18413:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18414:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18415:        m_IF = 1;
1.1.1.3   root     18416: #if defined(HAS_I386)
1.1       root     18417:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18418:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18419: #endif
                   18420:        i386_set_a20_line(0);
1.1.1.14  root     18421:        
1.1.1.19  root     18422:        ems_init();
1.1.1.25  root     18423:        dma_init();
1.1       root     18424:        pic_init();
1.1.1.25  root     18425:        pio_init();
1.1.1.8   root     18426: #ifdef PIT_ALWAYS_RUNNING
                   18427:        pit_init();
                   18428: #else
1.1       root     18429:        pit_active = 0;
                   18430: #endif
1.1.1.25  root     18431:        sio_init();
1.1.1.8   root     18432:        cmos_init();
                   18433:        kbd_init();
1.1       root     18434: }
                   18435: 
1.1.1.10  root     18436: void hardware_finish()
                   18437: {
                   18438: #if defined(HAS_I386)
                   18439:        vtlb_free(m_vtlb);
                   18440: #endif
1.1.1.19  root     18441:        ems_finish();
1.1.1.37  root     18442:        pio_finish();
1.1.1.25  root     18443:        sio_finish();
1.1.1.10  root     18444: }
                   18445: 
1.1.1.28  root     18446: void hardware_release()
                   18447: {
                   18448:        // release hardware resources when this program will be terminated abnormally
                   18449: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18450:        if(fp_debug_log != NULL) {
                   18451:                fclose(fp_debug_log);
                   18452:                fp_debug_log = NULL;
1.1.1.28  root     18453:        }
                   18454: #endif
                   18455: #if defined(HAS_I386)
                   18456:        vtlb_free(m_vtlb);
                   18457: #endif
                   18458:        ems_release();
1.1.1.37  root     18459:        pio_release();
1.1.1.28  root     18460:        sio_release();
                   18461: }
                   18462: 
1.1       root     18463: void hardware_run()
                   18464: {
1.1.1.22  root     18465: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18466:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18467:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18468: #endif
1.1.1.51  root     18469: #ifdef USE_DEBUGGER
                   18470:        m_int_num = -1;
                   18471: #endif
1.1.1.54  root     18472:        while(!m_exit) {
1.1.1.50  root     18473:                hardware_run_cpu();
1.1       root     18474:        }
1.1.1.22  root     18475: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18476:        if(fp_debug_log != NULL) {
                   18477:                fclose(fp_debug_log);
                   18478:                fp_debug_log = NULL;
1.1.1.28  root     18479:        }
1.1.1.22  root     18480: #endif
1.1       root     18481: }
                   18482: 
1.1.1.50  root     18483: inline void hardware_run_cpu()
                   18484: {
                   18485: #if defined(HAS_I386)
                   18486:        CPU_EXECUTE_CALL(i386);
                   18487:        if(m_eip != m_prev_eip) {
                   18488:                idle_ops++;
                   18489:        }
                   18490: #else
                   18491:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18492:        if(m_pc != m_prevpc) {
                   18493:                idle_ops++;
                   18494:        }
                   18495: #endif
                   18496: #ifdef USE_DEBUGGER
                   18497:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18498:        if(m_int_num >= 0) {
                   18499:                unsigned num = (unsigned)m_int_num;
                   18500:                m_int_num = -1;
                   18501:                msdos_syscall(num);
                   18502:        }
                   18503: #endif
                   18504:        if(++update_ops == UPDATE_OPS) {
                   18505:                update_ops = 0;
                   18506:                hardware_update();
                   18507:        }
                   18508: }
                   18509: 
1.1       root     18510: void hardware_update()
                   18511: {
1.1.1.8   root     18512:        static UINT32 prev_time = 0;
                   18513:        UINT32 cur_time = timeGetTime();
                   18514:        
                   18515:        if(prev_time != cur_time) {
                   18516:                // update pit and raise irq0
                   18517: #ifndef PIT_ALWAYS_RUNNING
                   18518:                if(pit_active)
                   18519: #endif
                   18520:                {
                   18521:                        if(pit_run(0, cur_time)) {
                   18522:                                pic_req(0, 0, 1);
                   18523:                        }
                   18524:                        pit_run(1, cur_time);
                   18525:                        pit_run(2, cur_time);
                   18526:                }
1.1.1.24  root     18527:                
1.1.1.25  root     18528:                // update sio and raise irq4/3
1.1.1.29  root     18529:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18530:                        sio_update(c);
                   18531:                }
                   18532:                
1.1.1.24  root     18533:                // update keyboard and mouse
1.1.1.14  root     18534:                static UINT32 prev_tick = 0;
                   18535:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     18536:                
1.1.1.14  root     18537:                if(prev_tick != cur_tick) {
                   18538:                        // update keyboard flags
                   18539:                        UINT8 state;
1.1.1.24  root     18540:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   18541:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   18542:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   18543:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   18544:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   18545:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   18546:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   18547:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     18548:                        mem[0x417] = state;
                   18549:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   18550:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   18551:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   18552:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     18553: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   18554: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     18555:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   18556:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   18557:                        mem[0x418] = state;
                   18558:                        
1.1.1.24  root     18559:                        // update console input if needed
1.1.1.34  root     18560:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     18561:                                update_console_input();
                   18562:                        }
1.1.1.57  root     18563:                        if(!(kbd_status & 1)) {
                   18564:                                if(key_buf_data != NULL) {
                   18565: #ifdef USE_SERVICE_THREAD
                   18566:                                        EnterCriticalSection(&key_buf_crit_sect);
                   18567: #endif
                   18568:                                        if(!key_buf_data->empty()) {
                   18569:                                                kbd_data = key_buf_data->read();
                   18570:                                                kbd_status |= 1;
                   18571:                                                key_changed = true;
                   18572:                                        }
                   18573: #ifdef USE_SERVICE_THREAD
                   18574:                                        LeaveCriticalSection(&key_buf_crit_sect);
                   18575: #endif
                   18576:                                }
                   18577:                        }
1.1.1.24  root     18578:                        
1.1.1.57  root     18579:                        // raise irq1 if key is pressed/released or key buffer is not empty
1.1.1.56  root     18580:                        if(!key_changed) {
1.1.1.55  root     18581: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18582:                                EnterCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18583: #endif
1.1.1.57  root     18584:                                if(!pcbios_is_key_buffer_empty()) {
                   18585: /*
                   18586:                                        if(!(kbd_status & 1)) {
                   18587:                                                UINT16 head = *(UINT16 *)(mem + 0x41a);
                   18588:                                                UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   18589:                                                if(head != tail) {
                   18590:                                                        int key_char = mem[0x400 + (head++)];
                   18591:                                                        int key_scan = mem[0x400 + (head++)];
                   18592:                                                        kbd_data = key_char ? key_char : key_scan;
                   18593:                                                        kbd_status |= 1;
                   18594:                                                }
                   18595:                                        }
                   18596: */
                   18597:                                        key_changed = true;
                   18598:                                }
1.1.1.55  root     18599: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18600:                                LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18601: #endif
1.1.1.56  root     18602:                        }
                   18603:                        if(key_changed) {
1.1.1.8   root     18604:                                pic_req(0, 1, 1);
1.1.1.56  root     18605:                                key_changed = false;
1.1.1.24  root     18606:                        }
                   18607:                        
                   18608:                        // raise irq12 if mouse status is changed
1.1.1.59! root     18609:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     18610:                                mouse.status_irq = 0; // ???
                   18611:                                mouse.status_irq_alt = 0; // ???
                   18612:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
                   18613:                                mouse.status = 0;
                   18614:                                pic_req(1, 4, 1);
1.1.1.59! root     18615:                        } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     18616:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   18617:                                mouse.status_irq_alt = 0; // ???
1.1.1.54  root     18618:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     18619:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     18620:                                pic_req(1, 4, 1);
                   18621:                        } else {
                   18622:                                for(int i = 0; i < 8; i++) {
                   18623:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   18624:                                                mouse.status_irq = 0; // ???
                   18625:                                                mouse.status_irq_alt = 0;
1.1.1.54  root     18626:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     18627:                                                for(int j = 0; j < 8; j++) {
                   18628:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   18629:                                                                mouse.status_irq_alt |=  (1 << j);
                   18630:                                                                mouse.status_alt     &= ~(1 << j);
                   18631:                                                        }
                   18632:                                                }
                   18633:                                                pic_req(1, 4, 1);
                   18634:                                                break;
                   18635:                                        }
                   18636:                                }
1.1.1.8   root     18637:                        }
1.1.1.24  root     18638:                        
1.1.1.58  root     18639:                        // update cursor position by crtc
                   18640:                        if(crtc_changed[14] != 0 || crtc_changed[15] != 0) {
1.1.1.59! root     18641:                                if(cursor_moved) {
        !          18642:                                        pcbios_update_cursor_position();
        !          18643:                                        cursor_moved = false;
        !          18644:                                }
1.1.1.58  root     18645:                                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   18646:                                int position = crtc_regs[14] * 256 + crtc_regs[15];
                   18647:                                int width = *(UINT16 *)(mem + 0x44a);
                   18648:                                COORD co;
                   18649:                                co.X = position % width;
                   18650:                                co.Y = position / width + scr_top;
                   18651:                                SetConsoleCursorPosition(hStdout, co);
                   18652:                                
                   18653:                                crtc_changed[14] = crtc_changed[15] = 0;
1.1.1.59! root     18654:                                cursor_moved_by_crtc = true;
1.1.1.58  root     18655:                        }
1.1.1.59! root     18656:                        
        !          18657:                        // update cursor info
        !          18658:                        if(is_cursor_blink_off()) {
        !          18659:                                if(!(ci_old.dwSize == ci_new.dwSize && ci_old.bVisible == ci_new.bVisible)) {
        !          18660:                                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
        !          18661:                                        SetConsoleCursorInfo(hStdout, &ci_new);
        !          18662:                                }
        !          18663:                        } else {
        !          18664:                                if(!(ci_old.dwSize == ci_new.dwSize)) {
        !          18665:                                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
        !          18666:                                        ci_new.bVisible = TRUE;
        !          18667:                                        SetConsoleCursorInfo(hStdout, &ci_new);
        !          18668:                                }
        !          18669:                        }
        !          18670:                        ci_old = ci_new;
        !          18671:                        
1.1.1.14  root     18672:                        prev_tick = cur_tick;
1.1.1.8   root     18673:                }
1.1.1.24  root     18674:                
1.1.1.19  root     18675:                // update daily timer counter
                   18676:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     18677:                
1.1.1.8   root     18678:                prev_time = cur_time;
1.1       root     18679:        }
                   18680: }
                   18681: 
1.1.1.19  root     18682: // ems
                   18683: 
                   18684: void ems_init()
                   18685: {
                   18686:        memset(ems_handles, 0, sizeof(ems_handles));
                   18687:        memset(ems_pages, 0, sizeof(ems_pages));
                   18688:        free_ems_pages = MAX_EMS_PAGES;
                   18689: }
                   18690: 
                   18691: void ems_finish()
                   18692: {
1.1.1.28  root     18693:        ems_release();
                   18694: }
                   18695: 
                   18696: void ems_release()
                   18697: {
1.1.1.31  root     18698:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     18699:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     18700:                        free(ems_handles[i].buffer);
                   18701:                        ems_handles[i].buffer = NULL;
                   18702:                }
                   18703:        }
                   18704: }
                   18705: 
                   18706: void ems_allocate_pages(int handle, int pages)
                   18707: {
                   18708:        if(pages > 0) {
                   18709:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18710:        } else {
                   18711:                ems_handles[handle].buffer = NULL;
                   18712:        }
                   18713:        ems_handles[handle].pages = pages;
                   18714:        ems_handles[handle].allocated = true;
                   18715:        free_ems_pages -= pages;
                   18716: }
                   18717: 
                   18718: void ems_reallocate_pages(int handle, int pages)
                   18719: {
                   18720:        if(ems_handles[handle].allocated) {
                   18721:                if(ems_handles[handle].pages != pages) {
                   18722:                        UINT8 *new_buffer = NULL;
                   18723:                        
                   18724:                        if(pages > 0) {
                   18725:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18726:                        }
1.1.1.32  root     18727:                        if(ems_handles[handle].buffer != NULL) {
                   18728:                                if(new_buffer != NULL) {
1.1.1.19  root     18729:                                        if(pages > ems_handles[handle].pages) {
                   18730:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   18731:                                        } else {
                   18732:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   18733:                                        }
                   18734:                                }
                   18735:                                free(ems_handles[handle].buffer);
                   18736:                                ems_handles[handle].buffer = NULL;
                   18737:                        }
                   18738:                        free_ems_pages += ems_handles[handle].pages;
                   18739:                        
                   18740:                        ems_handles[handle].buffer = new_buffer;
                   18741:                        ems_handles[handle].pages = pages;
                   18742:                        free_ems_pages -= pages;
                   18743:                }
                   18744:        } else {
                   18745:                ems_allocate_pages(handle, pages);
                   18746:        }
                   18747: }
                   18748: 
                   18749: void ems_release_pages(int handle)
                   18750: {
                   18751:        if(ems_handles[handle].allocated) {
1.1.1.32  root     18752:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     18753:                        free(ems_handles[handle].buffer);
                   18754:                        ems_handles[handle].buffer = NULL;
                   18755:                }
                   18756:                free_ems_pages += ems_handles[handle].pages;
                   18757:                ems_handles[handle].allocated = false;
                   18758:        }
                   18759: }
                   18760: 
                   18761: void ems_map_page(int physical, int handle, int logical)
                   18762: {
                   18763:        if(ems_pages[physical].mapped) {
                   18764:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   18765:                        return;
                   18766:                }
                   18767:                ems_unmap_page(physical);
                   18768:        }
1.1.1.32  root     18769:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18770:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   18771:        }
                   18772:        ems_pages[physical].handle = handle;
                   18773:        ems_pages[physical].page = logical;
                   18774:        ems_pages[physical].mapped = true;
                   18775: }
                   18776: 
                   18777: void ems_unmap_page(int physical)
                   18778: {
                   18779:        if(ems_pages[physical].mapped) {
                   18780:                int handle = ems_pages[physical].handle;
                   18781:                int logical = ems_pages[physical].page;
                   18782:                
1.1.1.32  root     18783:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18784:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   18785:                }
                   18786:                ems_pages[physical].mapped = false;
                   18787:        }
                   18788: }
                   18789: 
1.1.1.25  root     18790: // dma
1.1       root     18791: 
1.1.1.25  root     18792: void dma_init()
1.1       root     18793: {
1.1.1.26  root     18794:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     18795:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     18796: //             for(int ch = 0; ch < 4; ch++) {
                   18797: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   18798: //             }
1.1.1.25  root     18799:                dma_reset(c);
                   18800:        }
1.1       root     18801: }
                   18802: 
1.1.1.25  root     18803: void dma_reset(int c)
1.1       root     18804: {
1.1.1.25  root     18805:        dma[c].low_high = false;
                   18806:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   18807:        dma[c].mask = 0xff;
                   18808: }
                   18809: 
                   18810: void dma_write(int c, UINT32 addr, UINT8 data)
                   18811: {
                   18812:        int ch = (addr >> 1) & 3;
                   18813:        UINT8 bit = 1 << (data & 3);
                   18814:        
                   18815:        switch(addr & 0x0f) {
                   18816:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18817:                if(dma[c].low_high) {
                   18818:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     18819:                } else {
1.1.1.25  root     18820:                        dma[c].ch[ch].bareg.b.l = data;
                   18821:                }
                   18822:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18823:                dma[c].low_high = !dma[c].low_high;
                   18824:                break;
                   18825:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18826:                if(dma[c].low_high) {
                   18827:                        dma[c].ch[ch].bcreg.b.h = data;
                   18828:                } else {
                   18829:                        dma[c].ch[ch].bcreg.b.l = data;
                   18830:                }
                   18831:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18832:                dma[c].low_high = !dma[c].low_high;
                   18833:                break;
                   18834:        case 0x08:
                   18835:                // command register
                   18836:                dma[c].cmd = data;
                   18837:                break;
                   18838:        case 0x09:
                   18839:                // dma[c].request register
                   18840:                if(data & 4) {
                   18841:                        if(!(dma[c].req & bit)) {
                   18842:                                dma[c].req |= bit;
                   18843: //                             dma_run(c, ch);
                   18844:                        }
                   18845:                } else {
                   18846:                        dma[c].req &= ~bit;
                   18847:                }
                   18848:                break;
                   18849:        case 0x0a:
                   18850:                // single mask register
                   18851:                if(data & 4) {
                   18852:                        dma[c].mask |= bit;
                   18853:                } else {
                   18854:                        dma[c].mask &= ~bit;
                   18855:                }
                   18856:                break;
                   18857:        case 0x0b:
                   18858:                // mode register
                   18859:                dma[c].ch[data & 3].mode = data;
                   18860:                break;
                   18861:        case 0x0c:
                   18862:                dma[c].low_high = false;
                   18863:                break;
                   18864:        case 0x0d:
                   18865:                // clear master
                   18866:                dma_reset(c);
                   18867:                break;
                   18868:        case 0x0e:
                   18869:                // clear mask register
                   18870:                dma[c].mask = 0;
                   18871:                break;
                   18872:        case 0x0f:
                   18873:                // all mask register
                   18874:                dma[c].mask = data & 0x0f;
                   18875:                break;
                   18876:        }
                   18877: }
                   18878: 
                   18879: UINT8 dma_read(int c, UINT32 addr)
                   18880: {
                   18881:        int ch = (addr >> 1) & 3;
                   18882:        UINT8 val = 0xff;
                   18883:        
                   18884:        switch(addr & 0x0f) {
                   18885:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18886:                if(dma[c].low_high) {
                   18887:                        val = dma[c].ch[ch].areg.b.h;
                   18888:                } else {
                   18889:                        val = dma[c].ch[ch].areg.b.l;
                   18890:                }
                   18891:                dma[c].low_high = !dma[c].low_high;
                   18892:                return(val);
                   18893:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18894:                if(dma[c].low_high) {
                   18895:                        val = dma[c].ch[ch].creg.b.h;
                   18896:                } else {
                   18897:                        val = dma[c].ch[ch].creg.b.l;
                   18898:                }
                   18899:                dma[c].low_high = !dma[c].low_high;
                   18900:                return(val);
                   18901:        case 0x08:
                   18902:                // status register
                   18903:                val = (dma[c].req << 4) | dma[c].tc;
                   18904:                dma[c].tc = 0;
                   18905:                return(val);
                   18906:        case 0x0d:
1.1.1.26  root     18907:                // temporary register (intel 82374 does not support)
1.1.1.25  root     18908:                return(dma[c].tmp & 0xff);
1.1.1.26  root     18909:        case 0x0f:
                   18910:                // mask register (intel 82374 does support)
                   18911:                return(dma[c].mask);
1.1.1.25  root     18912:        }
                   18913:        return(0xff);
                   18914: }
                   18915: 
                   18916: void dma_page_write(int c, int ch, UINT8 data)
                   18917: {
                   18918:        dma[c].ch[ch].pagereg = data;
                   18919: }
                   18920: 
                   18921: UINT8 dma_page_read(int c, int ch)
                   18922: {
                   18923:        return(dma[c].ch[ch].pagereg);
                   18924: }
                   18925: 
                   18926: void dma_run(int c, int ch)
                   18927: {
                   18928:        UINT8 bit = 1 << ch;
                   18929:        
                   18930:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   18931:                // execute dma
                   18932:                while(dma[c].req & bit) {
                   18933:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   18934:                                // memory -> memory
                   18935:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   18936:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   18937:                                
                   18938:                                if(c == 0) {
                   18939:                                        dma[c].tmp = read_byte(saddr);
                   18940:                                        write_byte(daddr, dma[c].tmp);
                   18941:                                } else {
                   18942:                                        dma[c].tmp = read_word(saddr << 1);
                   18943:                                        write_word(daddr << 1, dma[c].tmp);
                   18944:                                }
                   18945:                                if(!(dma[c].cmd & 0x02)) {
                   18946:                                        if(dma[c].ch[0].mode & 0x20) {
                   18947:                                                dma[c].ch[0].areg.w--;
                   18948:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   18949:                                                        dma[c].ch[0].pagereg--;
                   18950:                                                }
                   18951:                                        } else {
                   18952:                                                dma[c].ch[0].areg.w++;
                   18953:                                                if(dma[c].ch[0].areg.w == 0) {
                   18954:                                                        dma[c].ch[0].pagereg++;
                   18955:                                                }
                   18956:                                        }
                   18957:                                }
                   18958:                                if(dma[c].ch[1].mode & 0x20) {
                   18959:                                        dma[c].ch[1].areg.w--;
                   18960:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   18961:                                                dma[c].ch[1].pagereg--;
                   18962:                                        }
                   18963:                                } else {
                   18964:                                        dma[c].ch[1].areg.w++;
                   18965:                                        if(dma[c].ch[1].areg.w == 0) {
                   18966:                                                dma[c].ch[1].pagereg++;
                   18967:                                        }
                   18968:                                }
                   18969:                                
                   18970:                                // check dma condition
                   18971:                                if(dma[c].ch[0].creg.w-- == 0) {
                   18972:                                        if(dma[c].ch[0].mode & 0x10) {
                   18973:                                                // self initialize
                   18974:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   18975:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   18976:                                        } else {
                   18977: //                                             dma[c].mask |= bit;
                   18978:                                        }
                   18979:                                }
                   18980:                                if(dma[c].ch[1].creg.w-- == 0) {
                   18981:                                        // terminal count
                   18982:                                        if(dma[c].ch[1].mode & 0x10) {
                   18983:                                                // self initialize
                   18984:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   18985:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   18986:                                        } else {
                   18987:                                                dma[c].mask |= bit;
                   18988:                                        }
                   18989:                                        dma[c].req &= ~bit;
                   18990:                                        dma[c].tc |= bit;
                   18991:                                }
                   18992:                        } else {
                   18993:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   18994:                                
                   18995:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   18996:                                        // verify
                   18997:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   18998:                                        // io -> memory
                   18999:                                        if(c == 0) {
                   19000:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   19001:                                                write_byte(addr, dma[c].tmp);
                   19002:                                        } else {
                   19003:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   19004:                                                write_word(addr << 1, dma[c].tmp);
                   19005:                                        }
                   19006:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   19007:                                        // memory -> io
                   19008:                                        if(c == 0) {
                   19009:                                                dma[c].tmp = read_byte(addr);
                   19010:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   19011:                                        } else {
                   19012:                                                dma[c].tmp = read_word(addr << 1);
                   19013:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   19014:                                        }
                   19015:                                }
                   19016:                                if(dma[c].ch[ch].mode & 0x20) {
                   19017:                                        dma[c].ch[ch].areg.w--;
                   19018:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   19019:                                                dma[c].ch[ch].pagereg--;
                   19020:                                        }
                   19021:                                } else {
                   19022:                                        dma[c].ch[ch].areg.w++;
                   19023:                                        if(dma[c].ch[ch].areg.w == 0) {
                   19024:                                                dma[c].ch[ch].pagereg++;
                   19025:                                        }
                   19026:                                }
                   19027:                                
                   19028:                                // check dma condition
                   19029:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   19030:                                        // terminal count
                   19031:                                        if(dma[c].ch[ch].mode & 0x10) {
                   19032:                                                // self initialize
                   19033:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19034:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19035:                                        } else {
                   19036:                                                dma[c].mask |= bit;
                   19037:                                        }
                   19038:                                        dma[c].req &= ~bit;
                   19039:                                        dma[c].tc |= bit;
                   19040:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   19041:                                        // single mode
                   19042:                                        break;
                   19043:                                }
                   19044:                        }
                   19045:                }
                   19046:        }
                   19047: }
                   19048: 
                   19049: // pic
                   19050: 
                   19051: void pic_init()
                   19052: {
                   19053:        memset(pic, 0, sizeof(pic));
                   19054:        pic[0].imr = pic[1].imr = 0xff;
                   19055:        
                   19056:        // from bochs bios
                   19057:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   19058:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   19059:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   19060:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   19061:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   19062:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   19063:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   19064:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   19065:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   19066: }
                   19067: 
                   19068: void pic_write(int c, UINT32 addr, UINT8 data)
                   19069: {
                   19070:        if(addr & 1) {
                   19071:                if(pic[c].icw2_r) {
                   19072:                        // icw2
                   19073:                        pic[c].icw2 = data;
                   19074:                        pic[c].icw2_r = 0;
                   19075:                } else if(pic[c].icw3_r) {
                   19076:                        // icw3
                   19077:                        pic[c].icw3 = data;
                   19078:                        pic[c].icw3_r = 0;
                   19079:                } else if(pic[c].icw4_r) {
                   19080:                        // icw4
                   19081:                        pic[c].icw4 = data;
                   19082:                        pic[c].icw4_r = 0;
                   19083:                } else {
                   19084:                        // ocw1
1.1       root     19085:                        pic[c].imr = data;
                   19086:                }
                   19087:        } else {
                   19088:                if(data & 0x10) {
                   19089:                        // icw1
                   19090:                        pic[c].icw1 = data;
                   19091:                        pic[c].icw2_r = 1;
                   19092:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   19093:                        pic[c].icw4_r = data & 1;
                   19094:                        pic[c].irr = 0;
                   19095:                        pic[c].isr = 0;
                   19096:                        pic[c].imr = 0;
                   19097:                        pic[c].prio = 0;
                   19098:                        if(!(pic[c].icw1 & 1)) {
                   19099:                                pic[c].icw4 = 0;
                   19100:                        }
                   19101:                        pic[c].ocw3 = 0;
                   19102:                } else if(data & 8) {
                   19103:                        // ocw3
                   19104:                        if(!(data & 2)) {
                   19105:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   19106:                        }
                   19107:                        if(!(data & 0x40)) {
                   19108:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   19109:                        }
                   19110:                        pic[c].ocw3 = data;
                   19111:                } else {
                   19112:                        // ocw2
                   19113:                        int level = 0;
                   19114:                        if(data & 0x40) {
                   19115:                                level = data & 7;
                   19116:                        } else {
                   19117:                                if(!pic[c].isr) {
                   19118:                                        return;
                   19119:                                }
                   19120:                                level = pic[c].prio;
                   19121:                                while(!(pic[c].isr & (1 << level))) {
                   19122:                                        level = (level + 1) & 7;
                   19123:                                }
                   19124:                        }
                   19125:                        if(data & 0x80) {
                   19126:                                pic[c].prio = (level + 1) & 7;
                   19127:                        }
                   19128:                        if(data & 0x20) {
                   19129:                                pic[c].isr &= ~(1 << level);
                   19130:                        }
                   19131:                }
                   19132:        }
                   19133:        pic_update();
                   19134: }
                   19135: 
                   19136: UINT8 pic_read(int c, UINT32 addr)
                   19137: {
                   19138:        if(addr & 1) {
                   19139:                return(pic[c].imr);
                   19140:        } else {
                   19141:                // polling mode is not supported...
                   19142:                //if(pic[c].ocw3 & 4) {
                   19143:                //      return ???;
                   19144:                //}
                   19145:                if(pic[c].ocw3 & 1) {
                   19146:                        return(pic[c].isr);
                   19147:                } else {
                   19148:                        return(pic[c].irr);
                   19149:                }
                   19150:        }
                   19151: }
                   19152: 
                   19153: void pic_req(int c, int level, int signal)
                   19154: {
                   19155:        if(signal) {
                   19156:                pic[c].irr |= (1 << level);
                   19157:        } else {
                   19158:                pic[c].irr &= ~(1 << level);
                   19159:        }
                   19160:        pic_update();
                   19161: }
                   19162: 
                   19163: int pic_ack()
                   19164: {
                   19165:        // ack (INTA=L)
                   19166:        pic[pic_req_chip].isr |= pic_req_bit;
                   19167:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   19168:        if(pic_req_chip > 0) {
                   19169:                // update isr and irr of master
                   19170:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   19171:                pic[pic_req_chip - 1].isr |= slave;
                   19172:                pic[pic_req_chip - 1].irr &= ~slave;
                   19173:        }
                   19174:        //if(pic[pic_req_chip].icw4 & 1) {
                   19175:                // 8086 mode
                   19176:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   19177:        //} else {
                   19178:        //      // 8080 mode
                   19179:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   19180:        //      if(pic[pic_req_chip].icw1 & 4) {
                   19181:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   19182:        //      } else {
                   19183:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   19184:        //      }
                   19185:        //      vector = 0xcd | (addr << 8);
                   19186:        //}
                   19187:        if(pic[pic_req_chip].icw4 & 2) {
                   19188:                // auto eoi
                   19189:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   19190:        }
                   19191:        return(vector);
                   19192: }
                   19193: 
                   19194: void pic_update()
                   19195: {
                   19196:        for(int c = 0; c < 2; c++) {
                   19197:                UINT8 irr = pic[c].irr;
                   19198:                if(c + 1 < 2) {
                   19199:                        // this is master
                   19200:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   19201:                                // request from slave
                   19202:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   19203:                        }
                   19204:                }
                   19205:                irr &= (~pic[c].imr);
                   19206:                if(!irr) {
                   19207:                        break;
                   19208:                }
                   19209:                if(!(pic[c].ocw3 & 0x20)) {
                   19210:                        irr |= pic[c].isr;
                   19211:                }
                   19212:                int level = pic[c].prio;
                   19213:                UINT8 bit = 1 << level;
                   19214:                while(!(irr & bit)) {
                   19215:                        level = (level + 1) & 7;
                   19216:                        bit = 1 << level;
                   19217:                }
                   19218:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   19219:                        // check slave
                   19220:                        continue;
                   19221:                }
                   19222:                if(pic[c].isr & bit) {
                   19223:                        break;
                   19224:                }
                   19225:                // interrupt request
                   19226:                pic_req_chip = c;
                   19227:                pic_req_level = level;
                   19228:                pic_req_bit = bit;
1.1.1.3   root     19229:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     19230:                return;
                   19231:        }
1.1.1.3   root     19232:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     19233: }
1.1       root     19234: 
1.1.1.25  root     19235: // pio
                   19236: 
                   19237: void pio_init()
                   19238: {
1.1.1.38  root     19239: //     bool conv_mode = (GetConsoleCP() == 932);
                   19240:        
1.1.1.26  root     19241:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     19242:        
1.1.1.25  root     19243:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     19244:                pio[c].stat = 0xdf;
1.1.1.25  root     19245:                pio[c].ctrl = 0x0c;
1.1.1.38  root     19246: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     19247:        }
                   19248: }
                   19249: 
1.1.1.37  root     19250: void pio_finish()
                   19251: {
                   19252:        pio_release();
                   19253: }
                   19254: 
                   19255: void pio_release()
                   19256: {
                   19257:        for(int c = 0; c < 2; c++) {
                   19258:                if(pio[c].fp != NULL) {
1.1.1.38  root     19259:                        if(pio[c].jis_mode) {
                   19260:                                fputc(0x1c, pio[c].fp);
                   19261:                                fputc(0x2e, pio[c].fp);
                   19262:                        }
1.1.1.37  root     19263:                        fclose(pio[c].fp);
                   19264:                        pio[c].fp = NULL;
                   19265:                }
                   19266:        }
                   19267: }
                   19268: 
1.1.1.25  root     19269: void pio_write(int c, UINT32 addr, UINT8 data)
                   19270: {
                   19271:        switch(addr & 3) {
                   19272:        case 0:
                   19273:                pio[c].data = data;
                   19274:                break;
                   19275:        case 2:
1.1.1.37  root     19276:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   19277:                        // strobe H -> L
                   19278:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   19279:                                // auto feed
                   19280:                                printer_out(c, 0x0d);
                   19281:                                printer_out(c, 0x0a);
                   19282:                        } else {
                   19283:                                printer_out(c, pio[c].data);
                   19284:                        }
                   19285:                        pio[c].stat &= ~0x40; // set ack
                   19286:                }
1.1.1.25  root     19287:                pio[c].ctrl = data;
                   19288:                break;
                   19289:        }
                   19290: }
                   19291: 
                   19292: UINT8 pio_read(int c, UINT32 addr)
                   19293: {
                   19294:        switch(addr & 3) {
                   19295:        case 0:
1.1.1.37  root     19296:                if(pio[c].ctrl & 0x20) {
                   19297:                        // input mode
                   19298:                        return(0xff);
                   19299:                }
1.1.1.25  root     19300:                return(pio[c].data);
                   19301:        case 1:
1.1.1.37  root     19302:                {
                   19303:                        UINT8 stat = pio[c].stat;
                   19304:                        pio[c].stat |= 0x40; // clear ack
                   19305:                        return(stat);
                   19306:                }
1.1.1.25  root     19307:        case 2:
                   19308:                return(pio[c].ctrl);
                   19309:        }
                   19310:        return(0xff);
                   19311: }
                   19312: 
1.1.1.37  root     19313: void printer_out(int c, UINT8 data)
                   19314: {
                   19315:        SYSTEMTIME time;
1.1.1.38  root     19316:        bool jis_mode = false;
1.1.1.37  root     19317:        
                   19318:        GetLocalTime(&time);
                   19319:        
                   19320:        if(pio[c].fp != NULL) {
                   19321:                // if at least 1000ms passed from last written, close the current file
                   19322:                FILETIME ftime1;
                   19323:                FILETIME ftime2;
                   19324:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   19325:                SystemTimeToFileTime(&time, &ftime2);
                   19326:                INT64 *time1 = (INT64 *)&ftime1;
                   19327:                INT64 *time2 = (INT64 *)&ftime2;
                   19328:                INT64 msec = (*time2 - *time1) / 10000;
                   19329:                
                   19330:                if(msec >= 1000) {
1.1.1.38  root     19331:                        if(pio[c].jis_mode) {
                   19332:                                fputc(0x1c, pio[c].fp);
                   19333:                                fputc(0x2e, pio[c].fp);
                   19334:                                jis_mode = true;
                   19335:                        }
1.1.1.37  root     19336:                        fclose(pio[c].fp);
                   19337:                        pio[c].fp = NULL;
                   19338:                }
                   19339:        }
                   19340:        if(pio[c].fp == NULL) {
                   19341:                // create a new file in the temp folder
                   19342:                char file_name[MAX_PATH];
                   19343:                
                   19344:                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);
                   19345:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   19346:                        strcat(pio[c].path, file_name);
                   19347:                } else {
                   19348:                        strcpy(pio[c].path, file_name);
                   19349:                }
1.1.1.38  root     19350:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     19351:        }
                   19352:        if(pio[c].fp != NULL) {
1.1.1.38  root     19353:                if(jis_mode) {
                   19354:                        fputc(0x1c, pio[c].fp);
                   19355:                        fputc(0x26, pio[c].fp);
                   19356:                }
1.1.1.37  root     19357:                fputc(data, pio[c].fp);
1.1.1.38  root     19358:                
                   19359:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   19360:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   19361:                        UINT8 buffer[4];
                   19362:                        fseek(pio[c].fp, 0, SEEK_SET);
                   19363:                        fread(buffer, 4, 1, pio[c].fp);
                   19364:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   19365:                                fclose(pio[c].fp);
                   19366:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   19367:                        }
                   19368:                }
1.1.1.37  root     19369:                pio[c].time = time;
                   19370:        }
                   19371: }
                   19372: 
1.1       root     19373: // pit
                   19374: 
1.1.1.22  root     19375: #define PIT_FREQ 1193182ULL
1.1       root     19376: #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)
                   19377: 
                   19378: void pit_init()
                   19379: {
1.1.1.8   root     19380:        memset(pit, 0, sizeof(pit));
1.1       root     19381:        for(int ch = 0; ch < 3; ch++) {
                   19382:                pit[ch].count = 0x10000;
                   19383:                pit[ch].ctrl_reg = 0x34;
                   19384:                pit[ch].mode = 3;
                   19385:        }
                   19386:        
                   19387:        // from bochs bios
                   19388:        pit_write(3, 0x34);
                   19389:        pit_write(0, 0x00);
                   19390:        pit_write(0, 0x00);
                   19391: }
                   19392: 
                   19393: void pit_write(int ch, UINT8 val)
                   19394: {
1.1.1.8   root     19395: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19396:        if(!pit_active) {
                   19397:                pit_active = 1;
                   19398:                pit_init();
                   19399:        }
1.1.1.8   root     19400: #endif
1.1       root     19401:        switch(ch) {
                   19402:        case 0:
                   19403:        case 1:
                   19404:        case 2:
                   19405:                // write count register
                   19406:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19407:                        if(pit[ch].ctrl_reg & 0x10) {
                   19408:                                pit[ch].low_write = 1;
                   19409:                        }
                   19410:                        if(pit[ch].ctrl_reg & 0x20) {
                   19411:                                pit[ch].high_write = 1;
                   19412:                        }
                   19413:                }
                   19414:                if(pit[ch].low_write) {
                   19415:                        pit[ch].count_reg = val;
                   19416:                        pit[ch].low_write = 0;
                   19417:                } else if(pit[ch].high_write) {
                   19418:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19419:                                pit[ch].count_reg = val << 8;
                   19420:                        } else {
                   19421:                                pit[ch].count_reg |= val << 8;
                   19422:                        }
                   19423:                        pit[ch].high_write = 0;
                   19424:                }
                   19425:                // start count
1.1.1.8   root     19426:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19427:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19428:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19429:                                pit[ch].prev_time = timeGetTime();
                   19430:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19431:                        }
                   19432:                }
                   19433:                break;
                   19434:        case 3: // ctrl reg
                   19435:                if((val & 0xc0) == 0xc0) {
                   19436:                        // i8254 read-back command
                   19437:                        for(ch = 0; ch < 3; ch++) {
                   19438:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19439:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19440:                                        pit[ch].status_latched = 1;
                   19441:                                }
                   19442:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19443:                                        pit_latch_count(ch);
                   19444:                                }
                   19445:                        }
                   19446:                        break;
                   19447:                }
                   19448:                ch = (val >> 6) & 3;
                   19449:                if(val & 0x30) {
1.1.1.35  root     19450:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19451:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19452:                        pit[ch].count_latched = 0;
                   19453:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19454:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19455:                        pit[ch].ctrl_reg = val;
                   19456:                        // stop count
1.1.1.8   root     19457:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19458:                        pit[ch].count_reg = 0;
                   19459:                } else if(!pit[ch].count_latched) {
                   19460:                        pit_latch_count(ch);
                   19461:                }
                   19462:                break;
                   19463:        }
                   19464: }
                   19465: 
                   19466: UINT8 pit_read(int ch)
                   19467: {
1.1.1.8   root     19468: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19469:        if(!pit_active) {
                   19470:                pit_active = 1;
                   19471:                pit_init();
                   19472:        }
1.1.1.8   root     19473: #endif
1.1       root     19474:        switch(ch) {
                   19475:        case 0:
                   19476:        case 1:
                   19477:        case 2:
                   19478:                if(pit[ch].status_latched) {
                   19479:                        pit[ch].status_latched = 0;
                   19480:                        return(pit[ch].status);
                   19481:                }
                   19482:                // if not latched, through current count
                   19483:                if(!pit[ch].count_latched) {
                   19484:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19485:                                pit_latch_count(ch);
                   19486:                        }
                   19487:                }
                   19488:                // return latched count
                   19489:                if(pit[ch].low_read) {
                   19490:                        pit[ch].low_read = 0;
                   19491:                        if(!pit[ch].high_read) {
                   19492:                                pit[ch].count_latched = 0;
                   19493:                        }
                   19494:                        return(pit[ch].latch & 0xff);
                   19495:                } else if(pit[ch].high_read) {
                   19496:                        pit[ch].high_read = 0;
                   19497:                        pit[ch].count_latched = 0;
                   19498:                        return((pit[ch].latch >> 8) & 0xff);
                   19499:                }
                   19500:        }
                   19501:        return(0xff);
                   19502: }
                   19503: 
1.1.1.8   root     19504: int pit_run(int ch, UINT32 cur_time)
1.1       root     19505: {
1.1.1.8   root     19506:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19507:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19508:                pit[ch].prev_time = pit[ch].expired_time;
                   19509:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19510:                if(cur_time >= pit[ch].expired_time) {
                   19511:                        pit[ch].prev_time = cur_time;
                   19512:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19513:                }
1.1.1.8   root     19514:                return(1);
1.1       root     19515:        }
1.1.1.8   root     19516:        return(0);
1.1       root     19517: }
                   19518: 
                   19519: void pit_latch_count(int ch)
                   19520: {
1.1.1.8   root     19521:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19522:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19523:                pit_run(ch, cur_time);
                   19524:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19525:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19526:                
                   19527:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19528:                        // decrement counter in 1msec period
                   19529:                        if(pit[ch].next_latch == 0) {
                   19530:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   19531:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19532:                        }
                   19533:                        if(pit[ch].latch > pit[ch].next_latch) {
                   19534:                                pit[ch].latch--;
                   19535:                        }
                   19536:                } else {
                   19537:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   19538:                        pit[ch].next_latch = 0;
                   19539:                }
1.1.1.8   root     19540:        } else {
                   19541:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     19542:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     19543:        }
                   19544:        pit[ch].count_latched = 1;
                   19545:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   19546:                // lower byte
                   19547:                pit[ch].low_read = 1;
                   19548:                pit[ch].high_read = 0;
                   19549:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19550:                // upper byte
                   19551:                pit[ch].low_read = 0;
                   19552:                pit[ch].high_read = 1;
                   19553:        } else {
                   19554:                // lower -> upper
1.1.1.14  root     19555:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     19556:        }
                   19557: }
                   19558: 
1.1.1.8   root     19559: int pit_get_expired_time(int ch)
1.1       root     19560: {
1.1.1.22  root     19561:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   19562:        UINT64 val = pit[ch].accum >> 10;
                   19563:        pit[ch].accum -= val << 10;
                   19564:        return((val != 0) ? val : 1);
1.1.1.8   root     19565: }
                   19566: 
1.1.1.25  root     19567: // sio
                   19568: 
                   19569: void sio_init()
                   19570: {
1.1.1.26  root     19571:        memset(sio, 0, sizeof(sio));
                   19572:        memset(sio_mt, 0, sizeof(sio_mt));
                   19573:        
1.1.1.29  root     19574:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19575:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19576:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19577:                
                   19578:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   19579:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     19580:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   19581:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     19582:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   19583:                sio[c].irq_identify = 0x01;     // no pending irq
                   19584:                
                   19585:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   19586:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   19587:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   19588:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   19589:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   19590:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   19591:                
1.1.1.26  root     19592:                if(sio_port_number[c] != 0) {
1.1.1.25  root     19593:                        sio[c].channel = c;
                   19594:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   19595:                }
                   19596:        }
                   19597: }
                   19598: 
                   19599: void sio_finish()
                   19600: {
1.1.1.29  root     19601:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19602:                if(sio_mt[c].hThread != NULL) {
                   19603:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   19604:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     19605:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     19606:                }
                   19607:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   19608:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   19609:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   19610:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   19611:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   19612:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     19613:        }
                   19614:        sio_release();
                   19615: }
                   19616: 
                   19617: void sio_release()
                   19618: {
1.1.1.29  root     19619:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     19620:                // sio_thread() may access the resources :-(
1.1.1.32  root     19621:                bool running = (sio_mt[c].hThread != NULL);
                   19622:                
                   19623:                if(running) {
                   19624:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   19625:                }
                   19626:                if(sio[c].send_buffer != NULL) {
                   19627:                        sio[c].send_buffer->release();
                   19628:                        delete sio[c].send_buffer;
                   19629:                        sio[c].send_buffer = NULL;
                   19630:                }
                   19631:                if(running) {
                   19632:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19633:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   19634:                }
                   19635:                if(sio[c].recv_buffer != NULL) {
                   19636:                        sio[c].recv_buffer->release();
                   19637:                        delete sio[c].recv_buffer;
                   19638:                        sio[c].recv_buffer = NULL;
                   19639:                }
                   19640:                if(running) {
                   19641:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     19642:                }
1.1.1.25  root     19643:        }
                   19644: }
                   19645: 
                   19646: void sio_write(int c, UINT32 addr, UINT8 data)
                   19647: {
                   19648:        switch(addr & 7) {
                   19649:        case 0:
                   19650:                if(sio[c].selector & 0x80) {
                   19651:                        if(sio[c].divisor.b.l != data) {
                   19652:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19653:                                sio[c].divisor.b.l = data;
                   19654:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19655:                        }
                   19656:                } else {
                   19657:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19658:                        if(sio[c].send_buffer != NULL) {
                   19659:                                sio[c].send_buffer->write(data);
                   19660:                        }
1.1.1.25  root     19661:                        // transmitter holding/shift registers are not empty
                   19662:                        sio[c].line_stat_buf &= ~0x60;
                   19663:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19664:                        
                   19665:                        if(sio[c].irq_enable & 0x02) {
                   19666:                                sio_update_irq(c);
                   19667:                        }
                   19668:                }
                   19669:                break;
                   19670:        case 1:
                   19671:                if(sio[c].selector & 0x80) {
                   19672:                        if(sio[c].divisor.b.h != data) {
                   19673:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19674:                                sio[c].divisor.b.h = data;
                   19675:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19676:                        }
                   19677:                } else {
                   19678:                        if(sio[c].irq_enable != data) {
                   19679:                                sio[c].irq_enable = data;
                   19680:                                sio_update_irq(c);
                   19681:                        }
                   19682:                }
                   19683:                break;
                   19684:        case 3:
                   19685:                {
                   19686:                        UINT8 line_ctrl = data & 0x3f;
                   19687:                        bool set_brk = ((data & 0x40) != 0);
                   19688:                        
                   19689:                        if(sio[c].line_ctrl != line_ctrl) {
                   19690:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19691:                                sio[c].line_ctrl = line_ctrl;
                   19692:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19693:                        }
                   19694:                        if(sio[c].set_brk != set_brk) {
                   19695:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   19696:                                sio[c].set_brk = set_brk;
                   19697:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19698:                        }
                   19699:                }
                   19700:                sio[c].selector = data;
                   19701:                break;
                   19702:        case 4:
                   19703:                {
                   19704:                        bool set_dtr = ((data & 0x01) != 0);
                   19705:                        bool set_rts = ((data & 0x02) != 0);
                   19706:                        
                   19707:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     19708: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     19709:                                sio[c].set_dtr = set_dtr;
                   19710:                                sio[c].set_rts = set_rts;
1.1.1.26  root     19711: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19712:                                
                   19713:                                bool state_changed = false;
                   19714:                                
                   19715:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19716:                                if(set_dtr) {
                   19717:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   19718:                                } else {
                   19719:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   19720:                                }
                   19721:                                if(set_rts) {
                   19722:                                        sio[c].modem_stat |= 0x10;      // cts on
                   19723:                                } else {
                   19724:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   19725:                                }
                   19726:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   19727:                                        if(!(sio[c].modem_stat & 0x02)) {
                   19728:                                                if(sio[c].irq_enable & 0x08) {
                   19729:                                                        state_changed = true;
                   19730:                                                }
                   19731:                                                sio[c].modem_stat |= 0x02;
                   19732:                                        }
                   19733:                                }
                   19734:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   19735:                                        if(!(sio[c].modem_stat & 0x01)) {
                   19736:                                                if(sio[c].irq_enable & 0x08) {
                   19737:                                                        state_changed = true;
                   19738:                                                }
                   19739:                                                sio[c].modem_stat |= 0x01;
                   19740:                                        }
                   19741:                                }
                   19742:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19743:                                
                   19744:                                if(state_changed) {
                   19745:                                        sio_update_irq(c);
                   19746:                                }
1.1.1.25  root     19747:                        }
                   19748:                }
                   19749:                sio[c].modem_ctrl = data;
                   19750:                break;
                   19751:        case 7:
                   19752:                sio[c].scratch = data;
                   19753:                break;
                   19754:        }
                   19755: }
                   19756: 
                   19757: UINT8 sio_read(int c, UINT32 addr)
                   19758: {
                   19759:        switch(addr & 7) {
                   19760:        case 0:
                   19761:                if(sio[c].selector & 0x80) {
                   19762:                        return(sio[c].divisor.b.l);
                   19763:                } else {
                   19764:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19765:                        UINT8 data = 0;
                   19766:                        if(sio[c].recv_buffer != NULL) {
                   19767:                                data = sio[c].recv_buffer->read();
                   19768:                        }
1.1.1.25  root     19769:                        // data is not ready
                   19770:                        sio[c].line_stat_buf &= ~0x01;
                   19771:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19772:                        
                   19773:                        if(sio[c].irq_enable & 0x01) {
                   19774:                                sio_update_irq(c);
                   19775:                        }
                   19776:                        return(data);
                   19777:                }
                   19778:        case 1:
                   19779:                if(sio[c].selector & 0x80) {
                   19780:                        return(sio[c].divisor.b.h);
                   19781:                } else {
                   19782:                        return(sio[c].irq_enable);
                   19783:                }
                   19784:        case 2:
                   19785:                return(sio[c].irq_identify);
                   19786:        case 3:
                   19787:                return(sio[c].selector);
                   19788:        case 4:
                   19789:                return(sio[c].modem_ctrl);
                   19790:        case 5:
                   19791:                {
                   19792:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   19793:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   19794:                        sio[c].line_stat_err = 0x00;
                   19795:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19796:                        
                   19797:                        bool state_changed = false;
                   19798:                        
                   19799:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19800:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19801:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19802:                                        // transmitter holding register will be empty first
                   19803:                                        if(sio[c].irq_enable & 0x02) {
                   19804:                                                state_changed = true;
                   19805:                                        }
                   19806:                                        sio[c].line_stat_buf |= 0x20;
                   19807:                                }
                   19808:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19809:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19810:                                // transmitter shift register will be empty later
                   19811:                                sio[c].line_stat_buf |= 0x40;
                   19812:                        }
                   19813:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   19814:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19815:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19816:                                        // data is ready
                   19817:                                        if(sio[c].irq_enable & 0x01) {
                   19818:                                                state_changed = true;
                   19819:                                        }
                   19820:                                        sio[c].line_stat_buf |= 0x01;
                   19821:                                }
                   19822:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19823:                        }
                   19824:                        if(state_changed) {
                   19825:                                sio_update_irq(c);
                   19826:                        }
                   19827:                        return(val);
                   19828:                }
                   19829:        case 6:
                   19830:                {
                   19831:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   19832:                        UINT8 val = sio[c].modem_stat;
                   19833:                        sio[c].modem_stat &= 0xf0;
                   19834:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   19835:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19836:                        
                   19837:                        if(sio[c].modem_ctrl & 0x10) {
                   19838:                                // loop-back
                   19839:                                val &= 0x0f;
                   19840:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   19841:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   19842:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   19843:                        }
                   19844:                        return(val);
                   19845:                }
                   19846:        case 7:
                   19847:                return(sio[c].scratch);
                   19848:        }
                   19849:        return(0xff);
                   19850: }
                   19851: 
                   19852: void sio_update(int c)
                   19853: {
                   19854:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19855:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19856:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19857:                        // transmitter holding/shift registers will be empty
                   19858:                        sio[c].line_stat_buf |= 0x60;
                   19859:                }
                   19860:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19861:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19862:                // transmitter shift register will be empty
                   19863:                sio[c].line_stat_buf |= 0x40;
                   19864:        }
                   19865:        if(!(sio[c].line_stat_buf & 0x01)) {
                   19866:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19867:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19868:                        // data is ready
                   19869:                        sio[c].line_stat_buf |= 0x01;
                   19870:                }
                   19871:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19872:        }
                   19873:        sio_update_irq(c);
                   19874: }
                   19875: 
                   19876: void sio_update_irq(int c)
                   19877: {
                   19878:        int level = -1;
                   19879:        
                   19880:        if(sio[c].irq_enable & 0x08) {
                   19881:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19882:                if((sio[c].modem_stat & 0x0f) != 0) {
                   19883:                        level = 0;
                   19884:                }
                   19885:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19886:        }
                   19887:        if(sio[c].irq_enable & 0x02) {
                   19888:                if(sio[c].line_stat_buf & 0x20) {
                   19889:                        level = 1;
                   19890:                }
                   19891:        }
                   19892:        if(sio[c].irq_enable & 0x01) {
                   19893:                if(sio[c].line_stat_buf & 0x01) {
                   19894:                        level = 2;
                   19895:                }
                   19896:        }
                   19897:        if(sio[c].irq_enable & 0x04) {
                   19898:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   19899:                if(sio[c].line_stat_err != 0) {
                   19900:                        level = 3;
                   19901:                }
                   19902:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19903:        }
1.1.1.29  root     19904:        
                   19905:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     19906:        if(level != -1) {
                   19907:                sio[c].irq_identify = level << 1;
1.1.1.29  root     19908:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     19909:        } else {
                   19910:                sio[c].irq_identify = 1;
1.1.1.29  root     19911:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     19912:        }
                   19913: }
                   19914: 
                   19915: DWORD WINAPI sio_thread(void *lpx)
                   19916: {
                   19917:        volatile sio_t *p = (sio_t *)lpx;
                   19918:        sio_mt_t *q = &sio_mt[p->channel];
                   19919:        
                   19920:        char name[] = "COM1";
1.1.1.26  root     19921:        name[3] = '0' + sio_port_number[p->channel];
                   19922:        HANDLE hComm = NULL;
                   19923:        COMMPROP commProp;
                   19924:        DCB dcb;
                   19925:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   19926:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   19927:        
                   19928:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   19929:                if(GetCommProperties(hComm, &commProp)) {
                   19930:                        dwSettableBaud = commProp.dwSettableBaud;
                   19931:                }
1.1.1.25  root     19932:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     19933: //             EscapeCommFunction(hComm, SETRTS);
                   19934: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     19935:                
1.1.1.54  root     19936:                while(!m_exit) {
1.1.1.25  root     19937:                        // setup comm port
                   19938:                        bool comm_state_changed = false;
                   19939:                        
                   19940:                        EnterCriticalSection(&q->csLineCtrl);
                   19941:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   19942:                                p->prev_divisor = p->divisor.w;
                   19943:                                p->prev_line_ctrl = p->line_ctrl;
                   19944:                                comm_state_changed = true;
                   19945:                        }
                   19946:                        LeaveCriticalSection(&q->csLineCtrl);
                   19947:                        
                   19948:                        if(comm_state_changed) {
1.1.1.26  root     19949:                                if(GetCommState(hComm, &dcb)) {
                   19950: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   19951:                                        DWORD baud = 115200 / p->prev_divisor;
                   19952:                                        dcb.BaudRate = 9600; // default
                   19953:                                        
                   19954:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   19955:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   19956:                                        // 134.5bps is not supported ???
                   19957: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   19958:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   19959:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   19960:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   19961:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   19962:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   19963:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   19964:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   19965:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   19966:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   19967: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   19968: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   19969: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   19970:                                        
                   19971:                                        switch(p->prev_line_ctrl & 0x03) {
                   19972:                                        case 0x00: dcb.ByteSize = 5; break;
                   19973:                                        case 0x01: dcb.ByteSize = 6; break;
                   19974:                                        case 0x02: dcb.ByteSize = 7; break;
                   19975:                                        case 0x03: dcb.ByteSize = 8; break;
                   19976:                                        }
                   19977:                                        switch(p->prev_line_ctrl & 0x04) {
                   19978:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   19979:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   19980:                                        }
                   19981:                                        switch(p->prev_line_ctrl & 0x38) {
                   19982:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   19983:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   19984:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   19985:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   19986:                                        default:   dcb.Parity = NOPARITY;    break;
                   19987:                                        }
                   19988:                                        dcb.fBinary = TRUE;
                   19989:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   19990:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   19991:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   19992:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   19993:                                        dcb.fTXContinueOnXoff = TRUE;
                   19994:                                        dcb.fOutX = dcb.fInX = FALSE;
                   19995:                                        dcb.fErrorChar = FALSE;
                   19996:                                        dcb.fNull = FALSE;
                   19997:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   19998:                                        dcb.fAbortOnError = FALSE;
                   19999:                                        
                   20000:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     20001:                                }
                   20002:                                
                   20003:                                // check again to apply all comm state changes
                   20004:                                Sleep(10);
                   20005:                                continue;
                   20006:                        }
                   20007:                        
                   20008:                        // set comm pins
                   20009:                        bool change_brk = false;
1.1.1.26  root     20010: //                     bool change_rts = false;
                   20011: //                     bool change_dtr = false;
1.1.1.25  root     20012:                        
                   20013:                        EnterCriticalSection(&q->csModemCtrl);
                   20014:                        if(p->prev_set_brk != p->set_brk) {
                   20015:                                p->prev_set_brk = p->set_brk;
                   20016:                                change_brk = true;
                   20017:                        }
1.1.1.26  root     20018: //                     if(p->prev_set_rts != p->set_rts) {
                   20019: //                             p->prev_set_rts = p->set_rts;
                   20020: //                             change_rts = true;
                   20021: //                     }
                   20022: //                     if(p->prev_set_dtr != p->set_dtr) {
                   20023: //                             p->prev_set_dtr = p->set_dtr;
                   20024: //                             change_dtr = true;
                   20025: //                     }
1.1.1.25  root     20026:                        LeaveCriticalSection(&q->csModemCtrl);
                   20027:                        
                   20028:                        if(change_brk) {
1.1.1.26  root     20029:                                static UINT32 clear_time = 0;
                   20030:                                if(p->prev_set_brk) {
                   20031:                                        EscapeCommFunction(hComm, SETBREAK);
                   20032:                                        clear_time = timeGetTime() + 200;
                   20033:                                } else {
                   20034:                                        // keep break for at least 200msec
                   20035:                                        UINT32 cur_time = timeGetTime();
                   20036:                                        if(clear_time > cur_time) {
                   20037:                                                Sleep(clear_time - cur_time);
                   20038:                                        }
                   20039:                                        EscapeCommFunction(hComm, CLRBREAK);
                   20040:                                }
1.1.1.25  root     20041:                        }
1.1.1.26  root     20042: //                     if(change_rts) {
                   20043: //                             if(p->prev_set_rts) {
                   20044: //                                     EscapeCommFunction(hComm, SETRTS);
                   20045: //                             } else {
                   20046: //                                     EscapeCommFunction(hComm, CLRRTS);
                   20047: //                             }
                   20048: //                     }
                   20049: //                     if(change_dtr) {
                   20050: //                             if(p->prev_set_dtr) {
                   20051: //                                     EscapeCommFunction(hComm, SETDTR);
                   20052: //                             } else {
                   20053: //                                     EscapeCommFunction(hComm, CLRDTR);
                   20054: //                             }
                   20055: //                     }
1.1.1.25  root     20056:                        
                   20057:                        // get comm pins
                   20058:                        DWORD dwModemStat = 0;
                   20059:                        
                   20060:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   20061:                                EnterCriticalSection(&q->csModemStat);
                   20062:                                if(dwModemStat & MS_RLSD_ON) {
                   20063:                                        p->modem_stat |= 0x80;
                   20064:                                } else {
                   20065:                                        p->modem_stat &= ~0x80;
                   20066:                                }
                   20067:                                if(dwModemStat & MS_RING_ON) {
                   20068:                                        p->modem_stat |= 0x40;
                   20069:                                } else {
                   20070:                                        p->modem_stat &= ~0x40;
                   20071:                                }
1.1.1.26  root     20072: //                             if(dwModemStat & MS_DSR_ON) {
                   20073: //                                     p->modem_stat |= 0x20;
                   20074: //                             } else {
                   20075: //                                     p->modem_stat &= ~0x20;
                   20076: //                             }
                   20077: //                             if(dwModemStat & MS_CTS_ON) {
                   20078: //                                     p->modem_stat |= 0x10;
                   20079: //                             } else {
                   20080: //                                     p->modem_stat &= ~0x10;
                   20081: //                             }
1.1.1.25  root     20082:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   20083:                                        p->modem_stat |= 0x08;
                   20084:                                }
                   20085:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   20086:                                        p->modem_stat |= 0x04;
                   20087:                                }
1.1.1.26  root     20088: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   20089: //                                     p->modem_stat |= 0x02;
                   20090: //                             }
                   20091: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   20092: //                                     p->modem_stat |= 0x01;
                   20093: //                             }
1.1.1.25  root     20094:                                LeaveCriticalSection(&q->csModemStat);
                   20095:                        }
                   20096:                        
                   20097:                        // send data
                   20098:                        DWORD dwSend = 0;
                   20099:                        
                   20100:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     20101:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     20102:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   20103:                        }
                   20104:                        LeaveCriticalSection(&q->csSendData);
                   20105:                        
                   20106:                        if(dwSend != 0) {
                   20107:                                DWORD dwWritten = 0;
                   20108:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   20109:                        }
                   20110:                        
                   20111:                        // get line status and recv data
                   20112:                        DWORD dwLineStat = 0;
                   20113:                        COMSTAT comStat;
                   20114:                        
                   20115:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   20116:                                EnterCriticalSection(&q->csLineStat);
                   20117:                                if(dwLineStat & CE_BREAK) {
                   20118:                                        p->line_stat_err |= 0x10;
                   20119:                                }
                   20120:                                if(dwLineStat & CE_FRAME) {
                   20121:                                        p->line_stat_err |= 0x08;
                   20122:                                }
                   20123:                                if(dwLineStat & CE_RXPARITY) {
                   20124:                                        p->line_stat_err |= 0x04;
                   20125:                                }
                   20126:                                if(dwLineStat & CE_OVERRUN) {
                   20127:                                        p->line_stat_err |= 0x02;
                   20128:                                }
                   20129:                                LeaveCriticalSection(&q->csLineStat);
                   20130:                                
                   20131:                                if(comStat.cbInQue != 0) {
                   20132:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20133:                                        DWORD dwRecv = 0;
                   20134:                                        if(p->recv_buffer != NULL) {
                   20135:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   20136:                                        }
1.1.1.25  root     20137:                                        LeaveCriticalSection(&q->csRecvData);
                   20138:                                        
                   20139:                                        if(dwRecv != 0) {
                   20140:                                                DWORD dwRead = 0;
                   20141:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   20142:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20143:                                                        if(p->recv_buffer != NULL) {
                   20144:                                                                for(int i = 0; i < dwRead; i++) {
                   20145:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   20146:                                                                }
1.1.1.25  root     20147:                                                        }
                   20148:                                                        LeaveCriticalSection(&q->csRecvData);
                   20149:                                                }
                   20150:                                        }
                   20151:                                }
                   20152:                        }
                   20153:                        Sleep(10);
                   20154:                }
                   20155:                CloseHandle(hComm);
                   20156:        }
                   20157:        return 0;
                   20158: }
                   20159: 
1.1.1.8   root     20160: // cmos
                   20161: 
                   20162: void cmos_init()
                   20163: {
                   20164:        memset(cmos, 0, sizeof(cmos));
                   20165:        cmos_addr = 0;
1.1       root     20166:        
1.1.1.8   root     20167:        // from DOSBox
                   20168:        cmos_write(0x0a, 0x26);
                   20169:        cmos_write(0x0b, 0x02);
                   20170:        cmos_write(0x0d, 0x80);
1.1       root     20171: }
                   20172: 
1.1.1.8   root     20173: void cmos_write(int addr, UINT8 val)
1.1       root     20174: {
1.1.1.8   root     20175:        cmos[addr & 0x7f] = val;
                   20176: }
                   20177: 
                   20178: #define CMOS_GET_TIME() { \
                   20179:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   20180:        if(prev_sec != cur_sec) { \
                   20181:                GetLocalTime(&time); \
                   20182:                prev_sec = cur_sec; \
                   20183:        } \
1.1       root     20184: }
1.1.1.8   root     20185: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     20186: 
1.1.1.8   root     20187: UINT8 cmos_read(int addr)
1.1       root     20188: {
1.1.1.8   root     20189:        static SYSTEMTIME time;
                   20190:        static UINT32 prev_sec = 0;
1.1       root     20191:        
1.1.1.8   root     20192:        switch(addr & 0x7f) {
                   20193:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   20194:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   20195:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   20196:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   20197:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   20198:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   20199:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   20200: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   20201:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   20202:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   20203:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   20204:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20205:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20206:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20207:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20208:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     20209:        }
1.1.1.8   root     20210:        return(cmos[addr & 0x7f]);
1.1       root     20211: }
                   20212: 
1.1.1.7   root     20213: // kbd (a20)
                   20214: 
                   20215: void kbd_init()
                   20216: {
1.1.1.8   root     20217:        kbd_data = kbd_command = 0;
1.1.1.7   root     20218:        kbd_status = 0x18;
                   20219: }
                   20220: 
                   20221: UINT8 kbd_read_data()
                   20222: {
1.1.1.57  root     20223:        UINT8 data = kbd_data;
                   20224:        kbd_data = 0;
1.1.1.8   root     20225:        kbd_status &= ~1;
1.1.1.57  root     20226:        return(data);
1.1.1.7   root     20227: }
                   20228: 
                   20229: void kbd_write_data(UINT8 val)
                   20230: {
                   20231:        switch(kbd_command) {
                   20232:        case 0xd1:
                   20233:                i386_set_a20_line((val >> 1) & 1);
                   20234:                break;
                   20235:        }
                   20236:        kbd_command = 0;
1.1.1.8   root     20237:        kbd_status &= ~8;
1.1.1.7   root     20238: }
                   20239: 
                   20240: UINT8 kbd_read_status()
                   20241: {
                   20242:        return(kbd_status);
                   20243: }
                   20244: 
                   20245: void kbd_write_command(UINT8 val)
                   20246: {
                   20247:        switch(val) {
                   20248:        case 0xd0:
                   20249:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     20250:                kbd_status |= 1;
1.1.1.7   root     20251:                break;
                   20252:        case 0xdd:
                   20253:                i386_set_a20_line(0);
                   20254:                break;
                   20255:        case 0xdf:
                   20256:                i386_set_a20_line(1);
                   20257:                break;
1.1.1.26  root     20258:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   20259:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     20260:                if(!(val & 1)) {
1.1.1.8   root     20261:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     20262:                                // reset pic
                   20263:                                pic_init();
                   20264:                                pic[0].irr = pic[1].irr = 0x00;
                   20265:                                pic[0].imr = pic[1].imr = 0xff;
                   20266:                        }
                   20267:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     20268:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   20269:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   20270:                        i386_jmp_far(selector, address);
1.1.1.7   root     20271:                }
                   20272:                i386_set_a20_line((val >> 1) & 1);
                   20273:                break;
                   20274:        }
                   20275:        kbd_command = val;
1.1.1.8   root     20276:        kbd_status |= 8;
1.1.1.7   root     20277: }
                   20278: 
1.1.1.9   root     20279: // vga
                   20280: 
                   20281: UINT8 vga_read_status()
                   20282: {
                   20283:        // 60hz
                   20284:        static const int period[3] = {16, 17, 17};
                   20285:        static int index = 0;
                   20286:        UINT32 time = timeGetTime() % period[index];
                   20287:        
                   20288:        index = (index + 1) % 3;
1.1.1.14  root     20289:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     20290: }
                   20291: 
1.1       root     20292: // i/o bus
                   20293: 
1.1.1.29  root     20294: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   20295: //#define SW1US_PATCH
                   20296: 
1.1.1.25  root     20297: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     20298: #ifdef USE_DEBUGGER
1.1.1.25  root     20299: {
1.1.1.33  root     20300:        if(now_debugging) {
                   20301:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20302:                        if(in_break_point.table[i].status == 1) {
                   20303:                                if(addr == in_break_point.table[i].addr) {
                   20304:                                        in_break_point.hit = i + 1;
                   20305:                                        now_suspended = true;
                   20306:                                        break;
                   20307:                                }
                   20308:                        }
                   20309:                }
1.1.1.25  root     20310:        }
1.1.1.33  root     20311:        return(debugger_read_io_byte(addr));
1.1.1.25  root     20312: }
1.1.1.33  root     20313: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     20314: #endif
1.1       root     20315: {
1.1.1.33  root     20316:        UINT8 val = 0xff;
                   20317:        
1.1       root     20318:        switch(addr) {
1.1.1.29  root     20319: #ifdef SW1US_PATCH
                   20320:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20321:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     20322:                val = sio_read(0, addr - 1);
                   20323:                break;
1.1.1.29  root     20324: #else
1.1.1.25  root     20325:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20326:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     20327:                val = dma_read(0, addr);
                   20328:                break;
1.1.1.29  root     20329: #endif
1.1.1.25  root     20330:        case 0x20: case 0x21:
1.1.1.33  root     20331:                val = pic_read(0, addr);
                   20332:                break;
1.1.1.25  root     20333:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     20334:                val = pit_read(addr & 0x03);
                   20335:                break;
1.1.1.7   root     20336:        case 0x60:
1.1.1.33  root     20337:                val = kbd_read_data();
                   20338:                break;
1.1.1.9   root     20339:        case 0x61:
1.1.1.33  root     20340:                val = system_port;
                   20341:                break;
1.1.1.7   root     20342:        case 0x64:
1.1.1.33  root     20343:                val = kbd_read_status();
                   20344:                break;
1.1       root     20345:        case 0x71:
1.1.1.33  root     20346:                val = cmos_read(cmos_addr);
                   20347:                break;
1.1.1.25  root     20348:        case 0x81:
1.1.1.33  root     20349:                val = dma_page_read(0, 2);
                   20350:                break;
1.1.1.25  root     20351:        case 0x82:
1.1.1.33  root     20352:                val = dma_page_read(0, 3);
                   20353:                break;
1.1.1.25  root     20354:        case 0x83:
1.1.1.33  root     20355:                val = dma_page_read(0, 1);
                   20356:                break;
1.1.1.25  root     20357:        case 0x87:
1.1.1.33  root     20358:                val = dma_page_read(0, 0);
                   20359:                break;
1.1.1.25  root     20360:        case 0x89:
1.1.1.33  root     20361:                val = dma_page_read(1, 2);
                   20362:                break;
1.1.1.25  root     20363:        case 0x8a:
1.1.1.33  root     20364:                val = dma_page_read(1, 3);
                   20365:                break;
1.1.1.25  root     20366:        case 0x8b:
1.1.1.33  root     20367:                val = dma_page_read(1, 1);
                   20368:                break;
1.1.1.25  root     20369:        case 0x8f:
1.1.1.33  root     20370:                val = dma_page_read(1, 0);
                   20371:                break;
1.1       root     20372:        case 0x92:
1.1.1.33  root     20373:                val = (m_a20_mask >> 19) & 2;
                   20374:                break;
1.1.1.25  root     20375:        case 0xa0: case 0xa1:
1.1.1.33  root     20376:                val = pic_read(1, addr);
                   20377:                break;
1.1.1.25  root     20378:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20379:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20380:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20381:                break;
1.1.1.37  root     20382:        case 0x278: case 0x279: case 0x27a:
                   20383:                val = pio_read(1, addr);
                   20384:                break;
1.1.1.29  root     20385:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20386:                val = sio_read(3, addr);
                   20387:                break;
1.1.1.25  root     20388:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20389:                val = sio_read(1, addr);
                   20390:                break;
1.1.1.25  root     20391:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20392:                val = pio_read(0, addr);
                   20393:                break;
1.1.1.25  root     20394:        case 0x3ba: case 0x3da:
1.1.1.33  root     20395:                val = vga_read_status();
                   20396:                break;
1.1.1.37  root     20397:        case 0x3bc: case 0x3bd: case 0x3be:
                   20398:                val = pio_read(2, addr);
                   20399:                break;
1.1.1.58  root     20400:        case 0x3d5:
                   20401:                if(crtc_addr < 16) {
                   20402:                        val = crtc_regs[crtc_addr];
                   20403:                }
                   20404:                break;
1.1.1.29  root     20405:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20406:                val = sio_read(2, addr);
                   20407:                break;
1.1.1.25  root     20408:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20409:                val = sio_read(0, addr);
                   20410:                break;
1.1       root     20411:        default:
1.1.1.33  root     20412: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20413:                break;
                   20414:        }
1.1.1.33  root     20415: #ifdef ENABLE_DEBUG_IOPORT
                   20416:        if(fp_debug_log != NULL) {
                   20417:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20418:        }
                   20419: #endif
                   20420:        return(val);
1.1       root     20421: }
                   20422: 
                   20423: UINT16 read_io_word(offs_t addr)
                   20424: {
                   20425:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20426: }
                   20427: 
1.1.1.33  root     20428: #ifdef USE_DEBUGGER
                   20429: UINT16 debugger_read_io_word(offs_t addr)
                   20430: {
                   20431:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20432: }
                   20433: #endif
                   20434: 
1.1       root     20435: UINT32 read_io_dword(offs_t addr)
                   20436: {
                   20437:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20438: }
                   20439: 
1.1.1.33  root     20440: #ifdef USE_DEBUGGER
                   20441: UINT32 debugger_read_io_dword(offs_t addr)
                   20442: {
                   20443:        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));
                   20444: }
                   20445: #endif
                   20446: 
1.1       root     20447: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20448: #ifdef USE_DEBUGGER
                   20449: {
                   20450:        if(now_debugging) {
                   20451:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20452:                        if(out_break_point.table[i].status == 1) {
                   20453:                                if(addr == out_break_point.table[i].addr) {
                   20454:                                        out_break_point.hit = i + 1;
                   20455:                                        now_suspended = true;
                   20456:                                        break;
                   20457:                                }
                   20458:                        }
                   20459:                }
                   20460:        }
                   20461:        debugger_write_io_byte(addr, val);
                   20462: }
                   20463: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20464: #endif
1.1       root     20465: {
1.1.1.25  root     20466: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20467:        if(fp_debug_log != NULL) {
1.1.1.43  root     20468: #ifdef USE_SERVICE_THREAD
                   20469:                if(addr != 0xf7)
                   20470: #endif
1.1.1.33  root     20471:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20472:        }
                   20473: #endif
1.1       root     20474:        switch(addr) {
1.1.1.29  root     20475: #ifdef SW1US_PATCH
                   20476:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20477:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20478:                sio_write(0, addr - 1, val);
                   20479:                break;
                   20480: #else
1.1.1.25  root     20481:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20482:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20483:                dma_write(0, addr, val);
                   20484:                break;
1.1.1.29  root     20485: #endif
1.1.1.25  root     20486:        case 0x20: case 0x21:
1.1       root     20487:                pic_write(0, addr, val);
                   20488:                break;
1.1.1.25  root     20489:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20490:                pit_write(addr & 0x03, val);
                   20491:                break;
1.1.1.7   root     20492:        case 0x60:
                   20493:                kbd_write_data(val);
                   20494:                break;
1.1.1.9   root     20495:        case 0x61:
                   20496:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20497:                        // beep on
                   20498: //                     MessageBeep(-1);
                   20499:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20500:                        // beep off
                   20501:                }
                   20502:                system_port = val;
                   20503:                break;
1.1       root     20504:        case 0x64:
1.1.1.7   root     20505:                kbd_write_command(val);
1.1       root     20506:                break;
                   20507:        case 0x70:
                   20508:                cmos_addr = val;
                   20509:                break;
                   20510:        case 0x71:
1.1.1.8   root     20511:                cmos_write(cmos_addr, val);
1.1       root     20512:                break;
1.1.1.25  root     20513:        case 0x81:
                   20514:                dma_page_write(0, 2, val);
                   20515:        case 0x82:
                   20516:                dma_page_write(0, 3, val);
                   20517:        case 0x83:
                   20518:                dma_page_write(0, 1, val);
                   20519:        case 0x87:
                   20520:                dma_page_write(0, 0, val);
                   20521:        case 0x89:
                   20522:                dma_page_write(1, 2, val);
                   20523:        case 0x8a:
                   20524:                dma_page_write(1, 3, val);
                   20525:        case 0x8b:
                   20526:                dma_page_write(1, 1, val);
                   20527:        case 0x8f:
                   20528:                dma_page_write(1, 0, val);
1.1       root     20529:        case 0x92:
1.1.1.7   root     20530:                i386_set_a20_line((val >> 1) & 1);
1.1       root     20531:                break;
1.1.1.25  root     20532:        case 0xa0: case 0xa1:
1.1       root     20533:                pic_write(1, addr, val);
                   20534:                break;
1.1.1.25  root     20535:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20536:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     20537:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     20538:                break;
1.1.1.35  root     20539: #ifdef USE_SERVICE_THREAD
                   20540:        case 0xf7:
                   20541:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     20542:                if(in_service && cursor_moved) {
                   20543:                        // update cursor position before service is done
                   20544:                        pcbios_update_cursor_position();
                   20545:                        cursor_moved = false;
                   20546:                }
1.1.1.35  root     20547:                finish_service_loop();
                   20548:                break;
                   20549: #endif
1.1.1.37  root     20550:        case 0x278: case 0x279: case 0x27a:
                   20551:                pio_write(1, addr, val);
                   20552:                break;
1.1.1.29  root     20553:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   20554:                sio_write(3, addr, val);
                   20555:                break;
1.1.1.25  root     20556:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   20557:                sio_write(1, addr, val);
                   20558:                break;
                   20559:        case 0x378: case 0x379: case 0x37a:
                   20560:                pio_write(0, addr, val);
                   20561:                break;
1.1.1.37  root     20562:        case 0x3bc: case 0x3bd: case 0x3be:
                   20563:                pio_write(2, addr, val);
                   20564:                break;
1.1.1.58  root     20565:        case 0x3d4:
                   20566:                crtc_addr = val;
                   20567:                break;
                   20568:        case 0x3d5:
                   20569:                if(crtc_addr < 16) {
                   20570:                        if(crtc_regs[crtc_addr] != val) {
                   20571:                                crtc_regs[crtc_addr] = val;
                   20572:                                crtc_changed[crtc_addr] = 1;
                   20573:                        }
                   20574:                }
                   20575:                break;
1.1.1.29  root     20576:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   20577:                sio_write(2, addr, val);
                   20578:                break;
1.1.1.25  root     20579:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   20580:                sio_write(0, addr, val);
                   20581:                break;
1.1       root     20582:        default:
1.1.1.33  root     20583: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     20584:                break;
                   20585:        }
                   20586: }
                   20587: 
                   20588: void write_io_word(offs_t addr, UINT16 val)
                   20589: {
                   20590:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20591:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20592: }
                   20593: 
1.1.1.33  root     20594: #ifdef USE_DEBUGGER
                   20595: void debugger_write_io_word(offs_t addr, UINT16 val)
                   20596: {
                   20597:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   20598:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   20599: }
                   20600: #endif
                   20601: 
1.1       root     20602: void write_io_dword(offs_t addr, UINT32 val)
                   20603: {
                   20604:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20605:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20606:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20607:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20608: }
1.1.1.33  root     20609: 
                   20610: #ifdef USE_DEBUGGER
                   20611: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   20612: {
                   20613:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   20614:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   20615:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   20616:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   20617: }
                   20618: #endif

unix.superglobalmegacorp.com

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