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

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.60  root       84: #ifdef _MBCS
                     85: inline char *my_strchr(char *str, int chr)
                     86: {
                     87:        return (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr));
                     88: }
                     89: inline const char *my_strchr(const char *str, int chr)
                     90: {
                     91:        return (const char *)_mbschr((const unsigned char *)(str), (unsigned int)(chr));
                     92: }
                     93: inline char *my_strrchr(char *str, int chr)
                     94: {
                     95:        return (char *)_mbsrchr((unsigned char *)(str), (unsigned int)(chr));
                     96: }
                     97: inline const char *my_strrchr(const char *str, int chr)
                     98: {
                     99:        return (const char *)_mbsrchr((const unsigned char *)(str), (unsigned int)(chr));
                    100: }
                    101: inline char *my_strtok(char *tok, const char *del)
                    102: {
                    103:        return (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del));
                    104: }
                    105: inline char *my_strupr(char *str)
                    106: {
                    107:        return (char *)_mbsupr((unsigned char *)(str));
                    108: }
                    109: #else
                    110: #define my_strchr(str, chr) strchr((str), (chr))
                    111: #define my_strrchr(str, chr) strrchr((str), (chr))
                    112: #define my_strtok(tok, del) strtok((tok), (del))
                    113: #define my_strupr(str) _strupr((str))
                    114: #endif
1.1.1.32  root      115: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1       root      116: 
1.1.1.12  root      117: #if defined(__MINGW32__)
                    118: extern "C" int _CRT_glob = 0;
                    119: #endif
                    120: 
                    121: /*
                    122:        kludge for "more-standardized" C++
                    123: */
                    124: #if !defined(_MSC_VER)
                    125: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
                    126: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
                    127: #define min(a,b) kludge_min(a,b)
                    128: #define max(a,b) kludge_max(a,b)
1.1.1.14  root      129: #elif _MSC_VER >= 1400
                    130: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
                    131: {
                    132: }
                    133: #endif
                    134: 
1.1.1.35  root      135: #define USE_VRAM_THREAD
1.1.1.14  root      136: 
1.1.1.35  root      137: #ifdef USE_VRAM_THREAD
1.1.1.14  root      138: static CRITICAL_SECTION vram_crit_sect;
                    139: #else
                    140: #define vram_flush()
1.1.1.12  root      141: #endif
                    142: 
1.1.1.14  root      143: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
                    144: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
                    145: 
                    146: void change_console_size(int width, int height);
                    147: void clear_scr_buffer(WORD attr);
                    148: 
                    149: static UINT32 vram_length_char = 0, vram_length_attr = 0;
                    150: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
                    151: static COORD vram_coord_char, vram_coord_attr;
                    152: 
1.1.1.28  root      153: char temp_file_path[MAX_PATH];
                    154: bool temp_file_created = false;
                    155: 
1.1.1.14  root      156: bool ignore_illegal_insn = false;
                    157: bool limit_max_memory = false;
                    158: bool no_windows = false;
                    159: bool stay_busy = false;
1.1.1.19  root      160: bool support_ems = false;
                    161: #ifdef SUPPORT_XMS
                    162: bool support_xms = false;
                    163: #endif
1.1.1.29  root      164: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14  root      165: 
1.1.1.62  root      166: BOOL is_winxp_or_later;
1.1.1.54  root      167: BOOL is_xp_64_or_later;
1.1.1.14  root      168: BOOL is_vista_or_later;
                    169: 
1.1.1.35  root      170: #define UPDATE_OPS 16384
                    171: #define REQUEST_HARDWRE_UPDATE() { \
                    172:        update_ops = UPDATE_OPS - 1; \
                    173: }
                    174: UINT32 update_ops = 0;
                    175: UINT32 idle_ops = 0;
                    176: 
1.1.1.54  root      177: inline BOOL is_sse2_ready()
                    178: {
                    179:        static int result = -1;
                    180:        int cpu_info[4];
                    181:        
                    182:        if(result == -1) {
                    183:                result = 0;
                    184:                __cpuid(cpu_info, 0);
                    185:                if(cpu_info[0] >= 1){
                    186:                        __cpuid(cpu_info, 1);
                    187:                        if(cpu_info[3] & (1 << 26)) {
                    188:                                result = 1;
                    189:                        }
                    190:                }
                    191:        }
                    192:        return(result == 1);
                    193: }
                    194: 
1.1.1.14  root      195: inline void maybe_idle()
                    196: {
                    197:        // if it appears to be in a tight loop, assume waiting for input
                    198:        // allow for one updated video character, for a spinning cursor
1.1.1.35  root      199:        if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.54  root      200:                if(is_sse2_ready()) {
                    201:                        _mm_pause();    // SSE2 pause
                    202:                } else if(is_xp_64_or_later) {
                    203:                        Sleep(0);       // switch to other thread that is ready to run, without checking priority
                    204:                } else {
                    205:                        Sleep(1);
                    206:                        REQUEST_HARDWRE_UPDATE();
                    207:                }
1.1.1.14  root      208:        }
1.1.1.35  root      209:        idle_ops = 0;
1.1.1.14  root      210: }
1.1.1.12  root      211: 
1.1       root      212: /* ----------------------------------------------------------------------------
1.1.1.3   root      213:        MAME i86/i386
1.1       root      214: ---------------------------------------------------------------------------- */
                    215: 
1.1.1.10  root      216: #ifndef __BIG_ENDIAN__
1.1       root      217: #define LSB_FIRST
1.1.1.10  root      218: #endif
1.1       root      219: 
                    220: #ifndef INLINE
                    221: #define INLINE inline
                    222: #endif
                    223: #define U64(v) UINT64(v)
                    224: 
                    225: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    226: #define logerror(...)
                    227: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    228: #define popmessage(...)
                    229: 
                    230: /*****************************************************************************/
1.1.1.10  root      231: /* src/emu/devcpu.h */
                    232: 
                    233: // CPU interface functions
                    234: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    235: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    236: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    237: 
                    238: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    239: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    240: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    241: 
                    242: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    243: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    244: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    245: 
                    246: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    247: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    248: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    249: 
                    250: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    251: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    252: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    253: 
1.1.1.14  root      254: #define CPU_MODEL_STR(name)                    #name
                    255: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    256: 
1.1.1.10  root      257: /*****************************************************************************/
                    258: /* src/emu/didisasm.h */
                    259: 
                    260: // Disassembler constants
                    261: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    262: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    263: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    264: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    265: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    266: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    267: 
                    268: /*****************************************************************************/
1.1       root      269: /* src/emu/diexec.h */
                    270: 
                    271: // I/O line states
                    272: enum line_state
                    273: {
                    274:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    275:        ASSERT_LINE,                            // assert an interrupt immediately
                    276:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    277:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    278: };
                    279: 
                    280: // I/O line definitions
                    281: enum
                    282: {
                    283:        INPUT_LINE_IRQ = 0,
                    284:        INPUT_LINE_NMI
                    285: };
                    286: 
                    287: /*****************************************************************************/
1.1.1.10  root      288: /* src/emu/dimemory.h */
1.1       root      289: 
1.1.1.10  root      290: // Translation intentions
                    291: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    292: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    293: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    294: 
                    295: const int TRANSLATE_READ            = 0;        // translate for read
                    296: const int TRANSLATE_WRITE           = 1;        // translate for write
                    297: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    298: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    299: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    300: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    301: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    302: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    303: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      304: 
1.1.1.10  root      305: /*****************************************************************************/
                    306: /* src/emu/emucore.h */
1.1       root      307: 
1.1.1.10  root      308: // constants for expression endianness
                    309: enum endianness_t
                    310: {
                    311:        ENDIANNESS_LITTLE,
                    312:        ENDIANNESS_BIG
                    313: };
1.1       root      314: 
1.1.1.10  root      315: // declare native endianness to be one or the other
                    316: #ifdef LSB_FIRST
                    317: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    318: #else
                    319: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    320: #endif
                    321: 
                    322: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    323: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    324: 
                    325: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    326: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    327: 
                    328: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    329: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      330: 
                    331: /*****************************************************************************/
                    332: /* src/emu/memory.h */
                    333: 
1.1.1.10  root      334: // address spaces
                    335: enum address_spacenum
                    336: {
                    337:        AS_0,                           // first address space
                    338:        AS_1,                           // second address space
                    339:        AS_2,                           // third address space
                    340:        AS_3,                           // fourth address space
                    341:        ADDRESS_SPACES,                 // maximum number of address spaces
                    342: 
                    343:        // alternate address space names for common use
                    344:        AS_PROGRAM = AS_0,              // program address space
                    345:        AS_DATA = AS_1,                 // data address space
                    346:        AS_IO = AS_2                    // I/O address space
                    347: };
                    348: 
1.1       root      349: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      350: //typedef UINT32       offs_t;
1.1       root      351: 
                    352: // read accessors
                    353: UINT8 read_byte(offs_t byteaddress)
1.1.1.33  root      354: #ifdef USE_DEBUGGER
                    355: {
                    356:        if(now_debugging) {
                    357:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    358:                        if(rd_break_point.table[i].status == 1) {
                    359:                                if(byteaddress == rd_break_point.table[i].addr) {
                    360:                                        rd_break_point.hit = i + 1;
                    361:                                        now_suspended = true;
                    362:                                        break;
                    363:                                }
                    364:                        }
                    365:                }
                    366:        }
                    367:        return(debugger_read_byte(byteaddress));
                    368: }
                    369: UINT8 debugger_read_byte(offs_t byteaddress)
                    370: #endif
1.1       root      371: {
1.1.1.4   root      372: #if defined(HAS_I386)
1.1       root      373:        if(byteaddress < MAX_MEM) {
                    374:                return mem[byteaddress];
1.1.1.3   root      375: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    376: //             return read_byte(byteaddress & 0xfffff);
1.1       root      377:        }
                    378:        return 0;
1.1.1.4   root      379: #else
                    380:        return mem[byteaddress];
                    381: #endif
1.1       root      382: }
                    383: 
                    384: UINT16 read_word(offs_t byteaddress)
1.1.1.33  root      385: #ifdef USE_DEBUGGER
                    386: {
                    387:        if(now_debugging) {
                    388:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    389:                        if(rd_break_point.table[i].status == 1) {
                    390:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
                    391:                                        rd_break_point.hit = i + 1;
                    392:                                        now_suspended = true;
                    393:                                        break;
                    394:                                }
                    395:                        }
                    396:                }
                    397:        }
                    398:        return(debugger_read_word(byteaddress));
                    399: }
                    400: UINT16 debugger_read_word(offs_t byteaddress)
                    401: #endif
1.1       root      402: {
1.1.1.14  root      403:        if(byteaddress == 0x41c) {
                    404:                // pointer to first free slot in keyboard buffer
1.1.1.35  root      405:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                    406: #ifdef USE_SERVICE_THREAD
                    407:                        EnterCriticalSection(&key_buf_crit_sect);
                    408: #endif
1.1.1.55  root      409:                        bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root      410: #ifdef USE_SERVICE_THREAD
                    411:                        LeaveCriticalSection(&key_buf_crit_sect);
                    412: #endif
1.1.1.51  root      413:                        if(empty) maybe_idle();
1.1.1.14  root      414:                }
                    415:        }
1.1.1.4   root      416: #if defined(HAS_I386)
1.1       root      417:        if(byteaddress < MAX_MEM - 1) {
                    418:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      419: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    420: //             return read_word(byteaddress & 0xfffff);
1.1       root      421:        }
                    422:        return 0;
1.1.1.4   root      423: #else
                    424:        return *(UINT16 *)(mem + byteaddress);
                    425: #endif
1.1       root      426: }
                    427: 
                    428: UINT32 read_dword(offs_t byteaddress)
1.1.1.33  root      429: #ifdef USE_DEBUGGER
                    430: {
                    431:        if(now_debugging) {
                    432:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    433:                        if(rd_break_point.table[i].status == 1) {
                    434:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
                    435:                                        rd_break_point.hit = i + 1;
                    436:                                        now_suspended = true;
                    437:                                        break;
                    438:                                }
                    439:                        }
                    440:                }
                    441:        }
                    442:        return(debugger_read_dword(byteaddress));
                    443: }
                    444: UINT32 debugger_read_dword(offs_t byteaddress)
                    445: #endif
1.1       root      446: {
1.1.1.4   root      447: #if defined(HAS_I386)
1.1       root      448:        if(byteaddress < MAX_MEM - 3) {
                    449:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      450: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    451: //             return read_dword(byteaddress & 0xfffff);
1.1       root      452:        }
                    453:        return 0;
1.1.1.4   root      454: #else
                    455:        return *(UINT32 *)(mem + byteaddress);
                    456: #endif
1.1       root      457: }
                    458: 
                    459: // write accessors
1.1.1.35  root      460: #ifdef USE_VRAM_THREAD
1.1.1.14  root      461: void vram_flush_char()
                    462: {
                    463:        if(vram_length_char != 0) {
                    464:                DWORD num;
1.1.1.60  root      465:                WriteConsoleOutputCharacterA(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      466:                vram_length_char = vram_last_length_char = 0;
                    467:        }
                    468: }
                    469: 
                    470: void vram_flush_attr()
                    471: {
                    472:        if(vram_length_attr != 0) {
                    473:                DWORD num;
1.1.1.23  root      474:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      475:                vram_length_attr = vram_last_length_attr = 0;
                    476:        }
                    477: }
                    478: 
                    479: void vram_flush()
                    480: {
                    481:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    482:                EnterCriticalSection(&vram_crit_sect);
                    483:                vram_flush_char();
                    484:                vram_flush_attr();
                    485:                LeaveCriticalSection(&vram_crit_sect);
                    486:        }
                    487: }
                    488: #endif
                    489: 
                    490: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      491: {
1.1.1.35  root      492: #ifdef USE_VRAM_THREAD
1.1.1.14  root      493:        static offs_t first_offset_char, last_offset_char;
                    494:        
                    495:        if(vram_length_char != 0) {
                    496:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    497:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    498:                        return;
                    499:                }
                    500:                if(offset != last_offset_char + 2) {
                    501:                        vram_flush_char();
                    502:                }
                    503:        }
                    504:        if(vram_length_char == 0) {
                    505:                first_offset_char = offset;
                    506:                vram_coord_char.X = (offset >> 1) % scr_width;
                    507:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    508:        }
                    509:        scr_char[vram_length_char++] = data;
                    510:        last_offset_char = offset;
                    511: #else
1.1.1.8   root      512:        COORD co;
                    513:        DWORD num;
                    514:        
1.1.1.14  root      515:        co.X = (offset >> 1) % scr_width;
                    516:        co.Y = (offset >> 1) / scr_width;
                    517:        scr_char[0] = data;
1.1.1.60  root      518:        WriteConsoleOutputCharacterA(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      519: #endif
                    520: }
                    521: 
                    522: void write_text_vram_attr(offs_t offset, UINT8 data)
                    523: {
1.1.1.35  root      524: #ifdef USE_VRAM_THREAD
1.1.1.14  root      525:        static offs_t first_offset_attr, last_offset_attr;
                    526:        
                    527:        if(vram_length_attr != 0) {
                    528:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    529:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    530:                        return;
                    531:                }
                    532:                if(offset != last_offset_attr + 2) {
                    533:                        vram_flush_attr();
                    534:                }
                    535:        }
                    536:        if(vram_length_attr == 0) {
                    537:                first_offset_attr = offset;
                    538:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    539:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    540:        }
                    541:        scr_attr[vram_length_attr++] = data;
                    542:        last_offset_attr = offset;
                    543: #else
                    544:        COORD co;
                    545:        DWORD num;
1.1.1.8   root      546:        
1.1.1.14  root      547:        co.X = (offset >> 1) % scr_width;
                    548:        co.Y = (offset >> 1) / scr_width;
                    549:        scr_attr[0] = data;
1.1.1.23  root      550:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      551: #endif
                    552: }
                    553: 
                    554: void write_text_vram_byte(offs_t offset, UINT8 data)
                    555: {
1.1.1.35  root      556: #ifdef USE_VRAM_THREAD
1.1.1.14  root      557:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      558: #endif
1.1.1.8   root      559:        if(offset & 1) {
1.1.1.14  root      560:                write_text_vram_attr(offset, data);
1.1.1.8   root      561:        } else {
1.1.1.14  root      562:                write_text_vram_char(offset, data);
1.1.1.8   root      563:        }
1.1.1.35  root      564: #ifdef USE_VRAM_THREAD
1.1.1.14  root      565:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      566: #endif
1.1.1.8   root      567: }
                    568: 
                    569: void write_text_vram_word(offs_t offset, UINT16 data)
                    570: {
1.1.1.35  root      571: #ifdef USE_VRAM_THREAD
1.1.1.14  root      572:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      573: #endif
1.1.1.8   root      574:        if(offset & 1) {
1.1.1.14  root      575:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    576:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      577:        } else {
1.1.1.14  root      578:                write_text_vram_char(offset    , (data     ) & 0xff);
                    579:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      580:        }
1.1.1.35  root      581: #ifdef USE_VRAM_THREAD
1.1.1.14  root      582:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      583: #endif
1.1.1.8   root      584: }
                    585: 
                    586: void write_text_vram_dword(offs_t offset, UINT32 data)
                    587: {
1.1.1.35  root      588: #ifdef USE_VRAM_THREAD
1.1.1.14  root      589:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      590: #endif
1.1.1.8   root      591:        if(offset & 1) {
1.1.1.14  root      592:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    593:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    594:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    595:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    596:        } else {
                    597:                write_text_vram_char(offset    , (data      ) & 0xff);
                    598:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    599:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    600:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      601:        }
1.1.1.35  root      602: #ifdef USE_VRAM_THREAD
1.1.1.14  root      603:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      604: #endif
1.1.1.8   root      605: }
                    606: 
1.1       root      607: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      608: #ifdef USE_DEBUGGER
                    609: {
                    610:        if(now_debugging) {
                    611:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    612:                        if(wr_break_point.table[i].status == 1) {
                    613:                                if(byteaddress == wr_break_point.table[i].addr) {
                    614:                                        wr_break_point.hit = i + 1;
                    615:                                        now_suspended = true;
                    616:                                        break;
                    617:                                }
                    618:                        }
                    619:                }
                    620:        }
                    621:        debugger_write_byte(byteaddress, data);
                    622: }
                    623: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    624: #endif
1.1       root      625: {
1.1.1.8   root      626:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      627:                mem[byteaddress] = data;
1.1.1.8   root      628:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      629:                if(!restore_console_on_exit) {
                    630:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      631:                }
1.1.1.8   root      632:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    633:                mem[byteaddress] = data;
                    634:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    635:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    636:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      637:                }
                    638:                mem[byteaddress] = data;
1.1.1.4   root      639: #if defined(HAS_I386)
1.1.1.3   root      640:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      641: #else
                    642:        } else {
                    643: #endif
1.1.1.3   root      644:                mem[byteaddress] = data;
1.1       root      645:        }
                    646: }
                    647: 
                    648: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      649: #ifdef USE_DEBUGGER
                    650: {
                    651:        if(now_debugging) {
                    652:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    653:                        if(wr_break_point.table[i].status == 1) {
                    654:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    655:                                        wr_break_point.hit = i + 1;
                    656:                                        now_suspended = true;
                    657:                                        break;
                    658:                                }
                    659:                        }
                    660:                }
                    661:        }
                    662:        debugger_write_word(byteaddress, data);
                    663: }
                    664: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    665: #endif
1.1       root      666: {
1.1.1.8   root      667:        if(byteaddress < MEMORY_END) {
1.1.1.51  root      668:                if(byteaddress == cursor_position_address) {
                    669:                        if(*(UINT16 *)(mem + byteaddress) != data) {
                    670:                                COORD co;
                    671:                                co.X = data & 0xff;
                    672:                                co.Y = (data >> 8) + scr_top;
                    673:                                SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.59  root      674:                                cursor_moved = false;
                    675:                                cursor_moved_by_crtc = false;
1.1.1.51  root      676:                        }
1.1.1.14  root      677:                }
1.1.1.3   root      678:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      679:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      680:                if(!restore_console_on_exit) {
                    681:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      682:                }
1.1.1.8   root      683:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    684:                *(UINT16 *)(mem + byteaddress) = data;
                    685:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    686:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    687:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      688:                }
                    689:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      690: #if defined(HAS_I386)
1.1.1.3   root      691:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      692: #else
                    693:        } else {
                    694: #endif
1.1.1.3   root      695:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      696:        }
                    697: }
                    698: 
                    699: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      700: #ifdef USE_DEBUGGER
                    701: {
                    702:        if(now_debugging) {
                    703:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    704:                        if(wr_break_point.table[i].status == 1) {
                    705:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    706:                                        wr_break_point.hit = i + 1;
                    707:                                        now_suspended = true;
                    708:                                        break;
                    709:                                }
                    710:                        }
                    711:                }
                    712:        }
                    713:        debugger_write_dword(byteaddress, data);
                    714: }
                    715: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    716: #endif
1.1       root      717: {
1.1.1.8   root      718:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      719:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      720:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      721:                if(!restore_console_on_exit) {
                    722:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      723:                }
1.1.1.8   root      724:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    725:                *(UINT32 *)(mem + byteaddress) = data;
                    726:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    727:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    728:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      729:                }
                    730:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      731: #if defined(HAS_I386)
1.1.1.3   root      732:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      733: #else
                    734:        } else {
                    735: #endif
1.1.1.3   root      736:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      737:        }
                    738: }
                    739: 
                    740: #define read_decrypted_byte read_byte
                    741: #define read_decrypted_word read_word
                    742: #define read_decrypted_dword read_dword
                    743: 
1.1.1.3   root      744: #define read_raw_byte read_byte
                    745: #define write_raw_byte write_byte
                    746: 
                    747: #define read_word_unaligned read_word
                    748: #define write_word_unaligned write_word
                    749: 
                    750: #define read_io_word_unaligned read_io_word
                    751: #define write_io_word_unaligned write_io_word
                    752: 
1.1       root      753: UINT8 read_io_byte(offs_t byteaddress);
                    754: UINT16 read_io_word(offs_t byteaddress);
                    755: UINT32 read_io_dword(offs_t byteaddress);
                    756: 
                    757: void write_io_byte(offs_t byteaddress, UINT8 data);
                    758: void write_io_word(offs_t byteaddress, UINT16 data);
                    759: void write_io_dword(offs_t byteaddress, UINT32 data);
                    760: 
                    761: /*****************************************************************************/
                    762: /* src/osd/osdcomm.h */
                    763: 
                    764: /* Highly useful macro for compile-time knowledge of an array size */
                    765: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    766: 
1.1.1.54  root      767: // flag to exit MS-DOS Player
                    768: // this is set when the first process is terminated and jump to FFFF:0000 HALT
                    769: int m_exit = 0;
                    770: 
1.1.1.3   root      771: #if defined(HAS_I386)
1.1.1.10  root      772:        static CPU_TRANSLATE(i386);
                    773:        #include "mame/lib/softfloat/softfloat.c"
                    774:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      775:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      776: #elif defined(HAS_I286)
1.1.1.10  root      777:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      778: #else
1.1.1.10  root      779:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      780: #endif
1.1.1.33  root      781: #ifdef USE_DEBUGGER
1.1.1.10  root      782:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      783: #endif
                    784: 
1.1.1.3   root      785: #if defined(HAS_I386)
                    786:        #define SREG(x)                         m_sreg[x].selector
                    787:        #define SREG_BASE(x)                    m_sreg[x].base
                    788:        int cpu_type, cpu_step;
1.1.1.64! root      789:        #define i386_get_flags()                get_flags()
        !           790:        #define i386_set_flags(x)               set_flags(x)
1.1.1.3   root      791: #else
                    792:        #define REG8(x)                         m_regs.b[x]
                    793:        #define REG16(x)                        m_regs.w[x]
                    794:        #define SREG(x)                         m_sregs[x]
                    795:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      796:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      797:        #define m_CF                            m_CarryVal
                    798:        #define m_a20_mask                      AMASK
                    799:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
1.1.1.64! root      800:        void i386_sreg_load(UINT16 selector, UINT8 reg, bool *fault)
        !           801:        {
        !           802: #if defined(HAS_I286)
        !           803:                i80286_data_descriptor(reg, selector);
        !           804: #else
        !           805:                m_sregs[reg] = selector;
        !           806:                m_base[reg] = SegBase(reg);
        !           807: #endif
        !           808:        }
        !           809:        #define i386_get_flags()                CompressFlags()
        !           810:        #define i386_set_flags(x)               ExpandFlags(x)
1.1.1.3   root      811:        #if defined(HAS_I286)
                    812:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    813:        #else
                    814:                #define i386_set_a20_line(x)
                    815:        #endif
                    816:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    817: #endif
1.1       root      818: 
                    819: void i386_jmp_far(UINT16 selector, UINT32 address)
                    820: {
1.1.1.3   root      821: #if defined(HAS_I386)
1.1       root      822:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      823:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      824:        } else {
1.1.1.3   root      825:                SREG(CS) = selector;
                    826:                m_performed_intersegment_jump = 1;
                    827:                i386_load_segment_descriptor(CS);
                    828:                m_eip = address;
                    829:                CHANGE_PC(m_eip);
1.1       root      830:        }
1.1.1.3   root      831: #elif defined(HAS_I286)
                    832:        i80286_code_descriptor(selector, address, 1);
                    833: #else
                    834:        SREG(CS) = selector;
                    835:        i386_load_segment_descriptor(CS);
                    836:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    837: #endif
1.1       root      838: }
                    839: 
1.1.1.24  root      840: void i386_call_far(UINT16 selector, UINT32 address)
                    841: {
                    842: #if defined(HAS_I386)
                    843:        if(PROTECTED_MODE && !V8086_MODE) {
                    844:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    845:        } else {
                    846:                PUSH16(SREG(CS));
                    847:                PUSH16(m_eip);
                    848:                SREG(CS) = selector;
                    849:                m_performed_intersegment_jump = 1;
                    850:                i386_load_segment_descriptor(CS);
                    851:                m_eip = address;
                    852:                CHANGE_PC(m_eip);
                    853:        }
                    854: #else
                    855:        UINT16 ip = m_pc - SREG_BASE(CS);
                    856:        UINT16 cs = SREG(CS);
                    857: #if defined(HAS_I286)
                    858:        i80286_code_descriptor(selector, address, 2);
                    859: #else
                    860:        SREG(CS) = selector;
                    861:        i386_load_segment_descriptor(CS);
                    862:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    863: #endif
                    864:        PUSH(cs);
                    865:        PUSH(ip);
                    866:        CHANGE_PC(m_pc);
                    867: #endif
                    868: }
1.1.1.49  root      869: 
                    870: void i386_push16(UINT16 value)
                    871: {
                    872: #if defined(HAS_I386)
                    873:        PUSH16(value);
                    874: #else
                    875:        PUSH(value);
1.1.1.35  root      876: #endif
1.1.1.49  root      877: }
                    878: 
                    879: UINT16 i386_pop16()
                    880: {
                    881: #if defined(HAS_I386)
                    882:        return POP16();
                    883: #else
                    884:        UINT16 value;
                    885:        POP(value);
                    886:        return value;
                    887: #endif
                    888: }
1.1.1.24  root      889: 
1.1.1.29  root      890: UINT16 i386_read_stack()
                    891: {
                    892: #if defined(HAS_I386)
                    893:        UINT32 ea, new_esp;
                    894:        if( STACK_32BIT ) {
                    895:                new_esp = REG32(ESP) + 2;
                    896:                ea = i386_translate(SS, new_esp - 2, 0);
                    897:        } else {
                    898:                new_esp = REG16(SP) + 2;
                    899:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    900:        }
                    901:        return READ16(ea);
                    902: #else
                    903:        UINT16 sp = m_regs.w[SP] + 2;
                    904:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    905: #endif
                    906: }
                    907: 
1.1.1.53  root      908: void i386_write_stack(UINT16 value)
                    909: {
                    910: #if defined(HAS_I386)
                    911:        UINT32 ea, new_esp;
                    912:        if( STACK_32BIT ) {
                    913:                new_esp = REG32(ESP) + 2;
                    914:                ea = i386_translate(SS, new_esp - 2, 0);
                    915:        } else {
                    916:                new_esp = REG16(SP) + 2;
                    917:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    918:        }
                    919:        WRITE16(ea, value);
                    920: #else
                    921:        UINT16 sp = m_regs.w[SP] + 2;
                    922:        WriteWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK), value);
                    923: #endif
                    924: }
                    925: 
1.1       root      926: /* ----------------------------------------------------------------------------
1.1.1.33  root      927:        debugger
                    928: ---------------------------------------------------------------------------- */
                    929: 
                    930: #ifdef USE_DEBUGGER
                    931: #define TELNET_BLUE      0x0004 // text color contains blue.
                    932: #define TELNET_GREEN     0x0002 // text color contains green.
                    933: #define TELNET_RED       0x0001 // text color contains red.
                    934: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    935: 
                    936: int svr_socket = 0;
                    937: int cli_socket = 0;
                    938: 
1.1.1.55  root      939: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
1.1.1.33  root      940: 
                    941: void debugger_init()
                    942: {
                    943:        now_debugging = false;
                    944:        now_going = false;
                    945:        now_suspended = false;
                    946:        force_suspend = false;
                    947:        
                    948:        memset(&break_point, 0, sizeof(break_point_t));
                    949:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    950:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    951:        memset(&in_break_point, 0, sizeof(break_point_t));
                    952:        memset(&out_break_point, 0, sizeof(break_point_t));
                    953:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    954: }
                    955: 
1.1.1.45  root      956: void telnet_send(const char *string)
1.1.1.33  root      957: {
                    958:        char buffer[8192], *ptr;
                    959:        strcpy(buffer, string);
                    960:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    961:                char tmp[8192];
                    962:                *ptr = '\0';
                    963:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    964:                strcpy(buffer, tmp);
                    965:        }
                    966:        
                    967:        int len = strlen(buffer), res;
                    968:        ptr = buffer;
                    969:        while(len > 0) {
                    970:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    971:                        len -= res;
                    972:                        ptr += res;
                    973:                }
                    974:        }
                    975: }
                    976: 
                    977: void telnet_command(const char *format, ...)
                    978: {
                    979:        char buffer[1024];
                    980:        va_list ap;
                    981:        va_start(ap, format);
                    982:        vsprintf(buffer, format, ap);
                    983:        va_end(ap);
                    984:        
                    985:        telnet_send(buffer);
                    986: }
                    987: 
                    988: void telnet_printf(const char *format, ...)
                    989: {
                    990:        char buffer[1024];
                    991:        va_list ap;
                    992:        va_start(ap, format);
                    993:        vsprintf(buffer, format, ap);
                    994:        va_end(ap);
                    995:        
                    996:        if(fp_debugger != NULL) {
                    997:                fprintf(fp_debugger, "%s", buffer);
                    998:        }
                    999:        telnet_send(buffer);
                   1000: }
                   1001: 
                   1002: bool telnet_gets(char *str, int n)
                   1003: {
                   1004:        char buffer[1024];
                   1005:        int ptr = 0;
                   1006:        
                   1007:        telnet_command("\033[12l"); // local echo on
                   1008:        telnet_command("\033[2l");  // key unlock
                   1009:        
1.1.1.54  root     1010:        while(!m_exit) {
1.1.1.33  root     1011:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1012:                
                   1013:                if(len > 0 && buffer[0] != 0xff) {
                   1014:                        for(int i = 0; i < len; i++) {
                   1015:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1016:                                        str[ptr] = 0;
                   1017:                                        telnet_command("\033[2h");  // key lock
                   1018:                                        telnet_command("\033[12h"); // local echo off
1.1.1.54  root     1019:                                        return(!m_exit);
1.1.1.33  root     1020:                                } else if(buffer[i] == 0x08) {
                   1021:                                        if(ptr > 0) {
                   1022:                                                telnet_command("\033[0K"); // erase from cursor position
                   1023:                                                ptr--;
                   1024:                                        } else {
                   1025:                                                telnet_command("\033[1C"); // move cursor forward
                   1026:                                        }
                   1027:                                } else if(ptr < n - 1) {
1.1.1.37  root     1028:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root     1029:                                                str[ptr++] = buffer[i];
                   1030:                                        }
                   1031:                                } else {
                   1032:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                   1033:                                }
                   1034:                        }
                   1035:                } else if(len == -1) {
                   1036:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1037:                                return(false);
                   1038:                        }
                   1039:                } else if(len == 0) {
                   1040:                        return(false);
                   1041:                }
                   1042:                Sleep(10);
                   1043:        }
1.1.1.54  root     1044:        return(!m_exit);
1.1.1.33  root     1045: }
                   1046: 
                   1047: bool telnet_kbhit()
                   1048: {
                   1049:        char buffer[1024];
                   1050:        
1.1.1.54  root     1051:        if(!m_exit) {
1.1.1.33  root     1052:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1053:                
                   1054:                if(len > 0) {
                   1055:                        for(int i = 0; i < len; i++) {
                   1056:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1057:                                        return(true);
                   1058:                                }
                   1059:                        }
                   1060:                } else if(len == 0) {
                   1061:                        return(true); // disconnected
                   1062:                }
                   1063:        }
                   1064:        return(false);
                   1065: }
                   1066: 
                   1067: bool telnet_disconnected()
                   1068: {
                   1069:        char buffer[1024];
                   1070:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1071:        
                   1072:        if(len == 0) {
                   1073:                return(true);
                   1074:        } else if(len == -1) {
                   1075:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1076:                        return(true);
                   1077:                }
                   1078:        }
                   1079:        return(false);
                   1080: }
                   1081: 
                   1082: void telnet_set_color(int color)
                   1083: {
                   1084:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                   1085: }
                   1086: 
                   1087: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                   1088: {
                   1089: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                   1090:        UINT8 ops[16];
                   1091:        for(int i = 0; i < 16; i++) {
                   1092:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                   1093:        }
                   1094:        UINT8 *oprom = ops;
                   1095:        
                   1096: #if defined(HAS_I386)
                   1097:        if(m_operand_size) {
                   1098:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                   1099:        } else
                   1100: #endif
                   1101:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1102: }
                   1103: 
                   1104: void debugger_regs_info(char *buffer)
                   1105: {
1.1.1.64! root     1106:        UINT32 flags = i386_get_flags();
        !          1107:        
1.1.1.33  root     1108: #if defined(HAS_I386)
                   1109:        if(m_operand_size) {
                   1110:                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",
                   1111:                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),
                   1112:                PROTECTED_MODE ? "PE" : "--",
                   1113:                (flags & 0x40000) ? 'A' : '-',
                   1114:                (flags & 0x20000) ? 'V' : '-',
                   1115:                (flags & 0x10000) ? 'R' : '-',
                   1116:                (flags & 0x04000) ? 'N' : '-',
                   1117:                (flags & 0x02000) ? '1' : '0',
                   1118:                (flags & 0x01000) ? '1' : '0',
                   1119:                (flags & 0x00800) ? 'O' : '-',
                   1120:                (flags & 0x00400) ? 'D' : '-',
                   1121:                (flags & 0x00200) ? 'I' : '-',
                   1122:                (flags & 0x00100) ? 'T' : '-',
                   1123:                (flags & 0x00080) ? 'S' : '-',
                   1124:                (flags & 0x00040) ? 'Z' : '-',
                   1125:                (flags & 0x00010) ? 'A' : '-',
                   1126:                (flags & 0x00004) ? 'P' : '-',
                   1127:                (flags & 0x00001) ? 'C' : '-');
                   1128:        } else {
                   1129: #endif
                   1130:                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",
                   1131:                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),
                   1132: #if defined(HAS_I386)
                   1133:                PROTECTED_MODE ? "PE" : "--",
                   1134: #else
                   1135:                "--",
                   1136: #endif
                   1137:                (flags & 0x40000) ? 'A' : '-',
                   1138:                (flags & 0x20000) ? 'V' : '-',
                   1139:                (flags & 0x10000) ? 'R' : '-',
                   1140:                (flags & 0x04000) ? 'N' : '-',
                   1141:                (flags & 0x02000) ? '1' : '0',
                   1142:                (flags & 0x01000) ? '1' : '0',
                   1143:                (flags & 0x00800) ? 'O' : '-',
                   1144:                (flags & 0x00400) ? 'D' : '-',
                   1145:                (flags & 0x00200) ? 'I' : '-',
                   1146:                (flags & 0x00100) ? 'T' : '-',
                   1147:                (flags & 0x00080) ? 'S' : '-',
                   1148:                (flags & 0x00040) ? 'Z' : '-',
                   1149:                (flags & 0x00010) ? 'A' : '-',
                   1150:                (flags & 0x00004) ? 'P' : '-',
                   1151:                (flags & 0x00001) ? 'C' : '-');
                   1152: #if defined(HAS_I386)
                   1153:        }
                   1154: #endif
                   1155: }
                   1156: 
                   1157: void debugger_process_info(char *buffer)
                   1158: {
                   1159:        UINT16 psp_seg = current_psp;
                   1160:        process_t *process;
                   1161:        bool check[0x10000] = {0};
                   1162:        
                   1163:        buffer[0] = '\0';
                   1164:        
                   1165:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1166:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1167:                char *file = process->module_path, *s;
                   1168:                char tmp[8192];
                   1169:                
                   1170:                while((s = strstr(file, "\\")) != NULL) {
                   1171:                        file = s + 1;
                   1172:                }
                   1173:                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));
                   1174:                strcat(tmp, buffer);
                   1175:                strcpy(buffer, tmp);
                   1176:                
                   1177:                check[psp_seg] = true;
                   1178:                psp_seg = psp->parent_psp;
                   1179:        }
                   1180: }
                   1181: 
                   1182: UINT32 debugger_get_val(const char *str)
                   1183: {
                   1184:        char tmp[1024];
                   1185:        
                   1186:        if(str == NULL || strlen(str) == 0) {
                   1187:                return(0);
                   1188:        }
                   1189:        strcpy(tmp, str);
                   1190:        
                   1191:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1192:                // ank
                   1193:                return(tmp[1] & 0xff);
                   1194:        } else if(tmp[0] == '%') {
                   1195:                // decimal
                   1196:                return(strtoul(tmp + 1, NULL, 10));
                   1197:        }
                   1198:        return(strtoul(tmp, NULL, 16));
                   1199: }
                   1200: 
                   1201: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1202: {
                   1203:        char tmp[1024], *s;
                   1204:        
                   1205:        if(str == NULL || strlen(str) == 0) {
                   1206:                return(val);
                   1207:        }
                   1208:        strcpy(tmp, str);
                   1209:        
                   1210:        if((s = strstr(tmp, ":")) != NULL) {
                   1211:                // 0000:0000
                   1212:                *s = '\0';
                   1213:                return(debugger_get_val(tmp));
                   1214:        }
                   1215:        return(val);
                   1216: }
                   1217: 
                   1218: UINT32 debugger_get_ofs(const char *str)
                   1219: {
                   1220:        char tmp[1024], *s;
                   1221:        
                   1222:        if(str == NULL || strlen(str) == 0) {
                   1223:                return(0);
                   1224:        }
                   1225:        strcpy(tmp, str);
                   1226:        
                   1227:        if((s = strstr(tmp, ":")) != NULL) {
                   1228:                // 0000:0000
                   1229:                return(debugger_get_val(s + 1));
                   1230:        }
                   1231:        return(debugger_get_val(tmp));
                   1232: }
                   1233: 
                   1234: void debugger_main()
                   1235: {
                   1236:        telnet_command("\033[20h"); // cr-lf
                   1237:        
                   1238:        force_suspend = true;
                   1239:        now_going = false;
                   1240:        now_debugging = true;
                   1241:        Sleep(100);
                   1242:        
1.1.1.54  root     1243:        if(!m_exit && !now_suspended) {
1.1.1.33  root     1244:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1245:                telnet_printf("waiting until cpu is suspended...\n");
                   1246:        }
1.1.1.54  root     1247:        while(!m_exit && !now_suspended) {
1.1.1.33  root     1248:                if(telnet_disconnected()) {
                   1249:                        break;
                   1250:                }
                   1251:                Sleep(10);
                   1252:        }
                   1253:        
                   1254:        char buffer[8192];
                   1255:        
                   1256:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1257:        debugger_process_info(buffer);
                   1258:        telnet_printf("%s", buffer);
                   1259:        debugger_regs_info(buffer);
                   1260:        telnet_printf("%s", buffer);
                   1261:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1262:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1263:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1264:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1265:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1266:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1267:        
                   1268:        #define MAX_COMMAND_LEN 64
                   1269:        
                   1270:        char command[MAX_COMMAND_LEN + 1];
                   1271:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1272:        
                   1273:        UINT32 data_seg = SREG(DS);
                   1274:        UINT32 data_ofs = 0;
                   1275:        UINT32 dasm_seg = SREG(CS);
                   1276:        UINT32 dasm_ofs = m_eip;
                   1277:        
1.1.1.54  root     1278:        while(!m_exit) {
1.1.1.33  root     1279:                telnet_printf("- ");
                   1280:                command[0] = '\0';
                   1281:                
                   1282:                if(fi_debugger != NULL) {
                   1283:                        while(command[0] == '\0') {
                   1284:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1285:                                        break;
                   1286:                                }
                   1287:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1288:                                        command[strlen(command) - 1] = '\0';
                   1289:                                }
                   1290:                        }
                   1291:                        if(command[0] != '\0') {
                   1292:                                telnet_command("%s\n", command);
                   1293:                        }
                   1294:                }
                   1295:                if(command[0] == '\0') {
                   1296:                        if(!telnet_gets(command, sizeof(command))) {
                   1297:                                break;
                   1298:                        }
                   1299:                }
                   1300:                if(command[0] == '\0') {
                   1301:                        strcpy(command, prev_command);
                   1302:                } else {
                   1303:                        strcpy(prev_command, command);
                   1304:                }
                   1305:                if(fp_debugger != NULL) {
                   1306:                        fprintf(fp_debugger, "%s\n", command);
                   1307:                }
                   1308:                
1.1.1.54  root     1309:                if(!m_exit && command[0] != 0) {
1.1.1.33  root     1310:                        char *params[32], *token = NULL;
                   1311:                        int num = 0;
                   1312:                        
                   1313:                        if((token = strtok(command, " ")) != NULL) {
                   1314:                                params[num++] = token;
                   1315:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1316:                                        params[num++] = token;
                   1317:                                }
                   1318:                        }
                   1319:                        if(stricmp(params[0], "D") == 0) {
                   1320:                                if(num <= 3) {
                   1321:                                        if(num >= 2) {
                   1322:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1323:                                                data_ofs = debugger_get_ofs(params[1]);
                   1324:                                        }
                   1325:                                        UINT32 end_seg = data_seg;
                   1326:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1327:                                        if(num == 3) {
                   1328:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1329:                                                end_ofs = debugger_get_ofs(params[2]);
                   1330:                                        }
                   1331:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1332:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1333: //                                     bool is_sjis = false;
1.1.1.33  root     1334:                                        
                   1335:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1336:                                                if((addr & 0x0f) == 0) {
                   1337:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1338:                                                                data_seg += 0x1000;
                   1339:                                                                data_ofs -= 0x10000;
                   1340:                                                        }
                   1341:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1342:                                                        memset(buffer, 0, sizeof(buffer));
                   1343:                                                }
                   1344:                                                if(addr < start_addr || addr > end_addr) {
                   1345:                                                        telnet_printf("   ");
                   1346:                                                        buffer[addr & 0x0f] = ' ';
                   1347:                                                } else {
                   1348:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1349:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1350: //                                                     if(is_sjis) {
1.1.1.33  root     1351: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1352: //                                                             is_sjis = false;
1.1.1.33  root     1353: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1354: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1355: //                                                             is_sjis = true;
1.1.1.33  root     1356: //                                                     } else
                   1357:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1358:                                                                buffer[addr & 0x0f] = data;
                   1359:                                                        } else {
                   1360:                                                                buffer[addr & 0x0f] = '.';
                   1361:                                                        }
                   1362:                                                }
                   1363:                                                if((addr & 0x0f) == 0x0f) {
                   1364:                                                        telnet_printf("  %s\n", buffer);
                   1365:                                                }
                   1366:                                        }
                   1367:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1368:                                                data_seg += 0x1000;
                   1369:                                                data_ofs -= 0x10000;
                   1370:                                        }
                   1371:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1372:                                } else {
                   1373:                                        telnet_printf("invalid parameter number\n");
                   1374:                                }
                   1375:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1376:                                if(num >= 3) {
                   1377:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1378:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1379:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1380:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1381:                                        }
                   1382:                                } else {
                   1383:                                        telnet_printf("invalid parameter number\n");
                   1384:                                }
                   1385:                        } else if(stricmp(params[0], "EW") == 0) {
                   1386:                                if(num >= 3) {
                   1387:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1388:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1389:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1390:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1391:                                        }
                   1392:                                } else {
                   1393:                                        telnet_printf("invalid parameter number\n");
                   1394:                                }
                   1395:                        } else if(stricmp(params[0], "ED") == 0) {
                   1396:                                if(num >= 3) {
                   1397:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1398:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1399:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1400:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1401:                                        }
                   1402:                                } else {
                   1403:                                        telnet_printf("invalid parameter number\n");
                   1404:                                }
                   1405:                        } else if(stricmp(params[0], "EA") == 0) {
                   1406:                                if(num >= 3) {
                   1407:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1408:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1409:                                        strcpy(buffer, prev_command);
                   1410:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1411:                                                int len = strlen(token);
                   1412:                                                for(int i = 0; i < len; i++) {
                   1413:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1414:                                                }
                   1415:                                        } else {
                   1416:                                                telnet_printf("invalid parameter\n");
                   1417:                                        }
                   1418:                                } else {
                   1419:                                        telnet_printf("invalid parameter number\n");
                   1420:                                }
                   1421:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1422:                                if(num == 2) {
                   1423:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1424:                                } else {
                   1425:                                        telnet_printf("invalid parameter number\n");
                   1426:                                }
                   1427:                        } else if(stricmp(params[0], "IW") == 0) {
                   1428:                                if(num == 2) {
                   1429:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1430:                                } else {
                   1431:                                        telnet_printf("invalid parameter number\n");
                   1432:                                }
                   1433:                        } else if(stricmp(params[0], "ID") == 0) {
                   1434:                                if(num == 2) {
                   1435:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1436:                                } else {
                   1437:                                        telnet_printf("invalid parameter number\n");
                   1438:                                }
                   1439:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1440:                                if(num == 3) {
                   1441:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1442:                                } else {
                   1443:                                        telnet_printf("invalid parameter number\n");
                   1444:                                }
                   1445:                        } else if(stricmp(params[0], "OW") == 0) {
                   1446:                                if(num == 3) {
                   1447:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1448:                                } else {
                   1449:                                        telnet_printf("invalid parameter number\n");
                   1450:                                }
                   1451:                        } else if(stricmp(params[0], "OD") == 0) {
                   1452:                                if(num == 3) {
                   1453:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1454:                                } else {
                   1455:                                        telnet_printf("invalid parameter number\n");
                   1456:                                }
                   1457:                        } else if(stricmp(params[0], "R") == 0) {
                   1458:                                if(num == 1) {
                   1459:                                        debugger_regs_info(buffer);
                   1460:                                        telnet_printf("%s", buffer);
                   1461:                                } else if(num == 3) {
                   1462: #if defined(HAS_I386)
                   1463:                                        if(stricmp(params[1], "EAX") == 0) {
                   1464:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1465:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1466:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1467:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1468:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1469:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1470:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1471:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1472:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1473:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1474:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1475:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1476:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1477:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1478:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1479:                                        } else
                   1480: #endif
                   1481:                                        if(stricmp(params[1], "AX") == 0) {
                   1482:                                                REG16(AX) = debugger_get_val(params[2]);
                   1483:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1484:                                                REG16(BX) = debugger_get_val(params[2]);
                   1485:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1486:                                                REG16(CX) = debugger_get_val(params[2]);
                   1487:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1488:                                                REG16(DX) = debugger_get_val(params[2]);
                   1489:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1490:                                                REG16(SP) = debugger_get_val(params[2]);
                   1491:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1492:                                                REG16(BP) = debugger_get_val(params[2]);
                   1493:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1494:                                                REG16(SI) = debugger_get_val(params[2]);
                   1495:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1496:                                                REG16(DI) = debugger_get_val(params[2]);
                   1497:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1498: #if defined(HAS_I386)
                   1499:                                                if(m_operand_size) {
                   1500:                                                        m_eip = debugger_get_val(params[2]);
                   1501:                                                } else {
                   1502:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1503:                                                }
                   1504:                                                CHANGE_PC(m_eip);
                   1505: #else
                   1506:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1507:                                                CHANGE_PC(m_pc);
                   1508: #endif
                   1509:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1510:                                                REG8(AL) = debugger_get_val(params[2]);
                   1511:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1512:                                                REG8(AH) = debugger_get_val(params[2]);
                   1513:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1514:                                                REG8(BL) = debugger_get_val(params[2]);
                   1515:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1516:                                                REG8(BH) = debugger_get_val(params[2]);
                   1517:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1518:                                                REG8(CL) = debugger_get_val(params[2]);
                   1519:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1520:                                                REG8(CH) = debugger_get_val(params[2]);
                   1521:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1522:                                                REG8(DL) = debugger_get_val(params[2]);
                   1523:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1524:                                                REG8(DH) = debugger_get_val(params[2]);
                   1525:                                        } else {
                   1526:                                                telnet_printf("unknown register %s\n", params[1]);
                   1527:                                        }
                   1528:                                } else {
                   1529:                                        telnet_printf("invalid parameter number\n");
                   1530:                                }
1.1.1.60  root     1531:                        } else if(stricmp(params[0], "S") == 0) {
1.1.1.33  root     1532:                                if(num >= 4) {
                   1533:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1534:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1535:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1536:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1537:                                        UINT8 list[32];
                   1538:                                        
                   1539:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1540:                                                list[j] = debugger_get_val(params[i]);
                   1541:                                        }
                   1542:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1543:                                                bool found = true;
                   1544:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1545:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1546:                                                                found = false;
                   1547:                                                                break;
                   1548:                                                        }
                   1549:                                                }
                   1550:                                                if(found) {
                   1551:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1552:                                                }
                   1553:                                                if((cur_ofs += 1) > 0xffff) {
                   1554:                                                        cur_seg += 0x1000;
                   1555:                                                        cur_ofs -= 0x10000;
                   1556:                                                }
                   1557:                                        }
                   1558:                                } else {
                   1559:                                        telnet_printf("invalid parameter number\n");
                   1560:                                }
                   1561:                        } else if(stricmp(params[0], "U") == 0) {
                   1562:                                if(num <= 3) {
                   1563:                                        if(num >= 2) {
                   1564:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1565:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1566:                                        }
                   1567:                                        if(num == 3) {
                   1568:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1569:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1570:                                                
                   1571:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1572:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1573:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1574:                                                        for(int i = 0; i < len; i++) {
                   1575:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1576:                                                        }
                   1577:                                                        for(int i = len; i < 8; i++) {
                   1578:                                                                telnet_printf("  ");
                   1579:                                                        }
                   1580:                                                        telnet_printf("  %s\n", buffer);
                   1581:                                                        if((dasm_ofs += len) > 0xffff) {
                   1582:                                                                dasm_seg += 0x1000;
                   1583:                                                                dasm_ofs -= 0x10000;
                   1584:                                                        }
                   1585:                                                }
                   1586:                                        } else {
                   1587:                                                for(int i = 0; i < 16; i++) {
                   1588:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1589:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1590:                                                        for(int i = 0; i < len; i++) {
                   1591:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1592:                                                        }
                   1593:                                                        for(int i = len; i < 8; i++) {
                   1594:                                                                telnet_printf("  ");
                   1595:                                                        }
                   1596:                                                        telnet_printf("  %s\n", buffer);
                   1597:                                                        if((dasm_ofs += len) > 0xffff) {
                   1598:                                                                dasm_seg += 0x1000;
                   1599:                                                                dasm_ofs -= 0x10000;
                   1600:                                                        }
                   1601:                                                }
                   1602:                                        }
                   1603:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1604:                                } else {
                   1605:                                        telnet_printf("invalid parameter number\n");
                   1606:                                }
                   1607:                        } else if(stricmp(params[0], "H") == 0) {
                   1608:                                if(num == 3) {
                   1609:                                        UINT32 l = debugger_get_val(params[1]);
                   1610:                                        UINT32 r = debugger_get_val(params[2]);
                   1611:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1612:                                } else {
                   1613:                                        telnet_printf("invalid parameter number\n");
                   1614:                                }
                   1615:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1616:                                break_point_t *break_point_ptr;
                   1617:                                #define GET_BREAK_POINT_PTR() { \
1.1.1.58  root     1618:                                        if(params[0][0] == 'R' || params[0][0] == 'r') { \
1.1.1.33  root     1619:                                                break_point_ptr = &rd_break_point; \
1.1.1.58  root     1620:                                        } else if(params[0][0] == 'W' || params[0][0] == 'w') { \
1.1.1.33  root     1621:                                                break_point_ptr = &wr_break_point; \
1.1.1.58  root     1622:                                        } else if(params[0][0] == 'I' || params[0][0] == 'i') { \
1.1.1.33  root     1623:                                                break_point_ptr = &in_break_point; \
1.1.1.58  root     1624:                                        } else if(params[0][0] == 'O' || params[0][0] == 'o') { \
1.1.1.33  root     1625:                                                break_point_ptr = &out_break_point; \
                   1626:                                        } else { \
                   1627:                                                break_point_ptr = &break_point; \
                   1628:                                        } \
                   1629:                                }
                   1630:                                GET_BREAK_POINT_PTR();
                   1631:                                if(num == 2) {
1.1.1.58  root     1632:                                        UINT32 seg = 0;
                   1633:                                        if(params[0][0] == 'R' || params[0][0] == 'r' || params[0][0] == 'W' || params[0][0] == 'w') {
                   1634:                                                seg = debugger_get_seg(params[1], data_seg);
                   1635:                                        } else {
                   1636:                                                seg = debugger_get_seg(params[1], SREG(CS));
                   1637:                                        }
1.1.1.33  root     1638:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1639:                                        bool found = false;
                   1640:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1641:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1642:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1643:                                                        break_point_ptr->table[i].seg = seg;
                   1644:                                                        break_point_ptr->table[i].ofs = ofs;
                   1645:                                                        break_point_ptr->table[i].status = 1;
                   1646:                                                        found = true;
                   1647:                                                }
                   1648:                                        }
                   1649:                                        if(!found) {
                   1650:                                                telnet_printf("too many break points\n");
                   1651:                                        }
                   1652:                                } else {
                   1653:                                        telnet_printf("invalid parameter number\n");
                   1654:                                }
                   1655:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1656:                                break_point_t *break_point_ptr;
                   1657:                                GET_BREAK_POINT_PTR();
                   1658:                                if(num == 2) {
                   1659:                                        UINT32 addr = debugger_get_val(params[1]);
                   1660:                                        bool found = false;
                   1661:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1662:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1663:                                                        break_point_ptr->table[i].addr = addr;
                   1664:                                                        break_point_ptr->table[i].status = 1;
                   1665:                                                        found = true;
                   1666:                                                }
                   1667:                                        }
                   1668:                                        if(!found) {
                   1669:                                                telnet_printf("too many break points\n");
                   1670:                                        }
                   1671:                                } else {
                   1672:                                        telnet_printf("invalid parameter number\n");
                   1673:                                }
                   1674:                        } 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) {
                   1675:                                break_point_t *break_point_ptr;
                   1676:                                GET_BREAK_POINT_PTR();
                   1677:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1678:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1679:                                } else if(num >= 2) {
                   1680:                                        for(int i = 1; i < num; i++) {
                   1681:                                                int index = debugger_get_val(params[i]);
                   1682:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1683:                                                        telnet_printf("invalid index %x\n", index);
                   1684:                                                } else {
                   1685:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1686:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1687:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1688:                                                        break_point_ptr->table[index - 1].status = 0;
                   1689:                                                }
                   1690:                                        }
                   1691:                                } else {
                   1692:                                        telnet_printf("invalid parameter number\n");
                   1693:                                }
                   1694:                        } 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 ||
                   1695:                                  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) {
                   1696:                                break_point_t *break_point_ptr;
                   1697:                                GET_BREAK_POINT_PTR();
                   1698:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1699:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1700:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1701:                                                if(break_point_ptr->table[i].status != 0) {
                   1702:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1703:                                                }
                   1704:                                        }
                   1705:                                } else if(num >= 2) {
                   1706:                                        for(int i = 1; i < num; i++) {
                   1707:                                                int index = debugger_get_val(params[i]);
                   1708:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1709:                                                        telnet_printf("invalid index %x\n", index);
                   1710:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1711:                                                        telnet_printf("break point %x is null\n", index);
                   1712:                                                } else {
                   1713:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1714:                                                }
                   1715:                                        }
                   1716:                                } else {
                   1717:                                        telnet_printf("invalid parameter number\n");
                   1718:                                }
                   1719:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1720:                                break_point_t *break_point_ptr;
                   1721:                                GET_BREAK_POINT_PTR();
                   1722:                                if(num == 1) {
                   1723:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1724:                                                if(break_point_ptr->table[i].status) {
                   1725:                                                        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);
                   1726:                                                }
                   1727:                                        }
                   1728:                                } else {
                   1729:                                        telnet_printf("invalid parameter number\n");
                   1730:                                }
                   1731:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1732:                                break_point_t *break_point_ptr;
                   1733:                                GET_BREAK_POINT_PTR();
                   1734:                                if(num == 1) {
                   1735:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1736:                                                if(break_point_ptr->table[i].status) {
                   1737:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1738:                                                }
                   1739:                                        }
                   1740:                                } else {
                   1741:                                        telnet_printf("invalid parameter number\n");
                   1742:                                }
                   1743:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1744:                                if(num >= 2 && num <= 4) {
                   1745:                                        int int_num = debugger_get_val(params[1]);
                   1746:                                        UINT8 ah = 0, ah_registered = 0;
                   1747:                                        UINT8 al = 0, al_registered = 0;
                   1748:                                        if(num >= 3) {
                   1749:                                                ah = debugger_get_val(params[2]);
                   1750:                                                ah_registered = 1;
                   1751:                                        }
                   1752:                                        if(num == 4) {
                   1753:                                                al = debugger_get_val(params[3]);
                   1754:                                                al_registered = 1;
                   1755:                                        }
                   1756:                                        bool found = false;
                   1757:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1758:                                                if(int_break_point.table[i].status == 0 || (
                   1759:                                                   int_break_point.table[i].int_num == int_num &&
                   1760:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1761:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1762:                                                        int_break_point.table[i].int_num = int_num;
                   1763:                                                        int_break_point.table[i].ah = ah;
                   1764:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1765:                                                        int_break_point.table[i].al = al;
                   1766:                                                        int_break_point.table[i].al_registered = al_registered;
                   1767:                                                        int_break_point.table[i].status = 1;
                   1768:                                                        found = true;
                   1769:                                                }
                   1770:                                        }
                   1771:                                        if(!found) {
                   1772:                                                telnet_printf("too many break points\n");
                   1773:                                        }
                   1774:                                } else {
                   1775:                                        telnet_printf("invalid parameter number\n");
                   1776:                                }
                   1777:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1778:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1779:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1780:                                } else if(num >= 2) {
                   1781:                                        for(int i = 1; i < num; i++) {
                   1782:                                                int index = debugger_get_val(params[i]);
                   1783:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1784:                                                        telnet_printf("invalid index %x\n", index);
                   1785:                                                } else {
                   1786:                                                        int_break_point.table[index - 1].int_num = 0;
                   1787:                                                        int_break_point.table[index - 1].ah = 0;
                   1788:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1789:                                                        int_break_point.table[index - 1].al = 0;
                   1790:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1791:                                                        int_break_point.table[index - 1].status = 0;
                   1792:                                                }
                   1793:                                        }
                   1794:                                } else {
                   1795:                                        telnet_printf("invalid parameter number\n");
                   1796:                                }
                   1797:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1798:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1799:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1800:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1801:                                                if(int_break_point.table[i].status != 0) {
                   1802:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1803:                                                }
                   1804:                                        }
                   1805:                                } else if(num >= 2) {
                   1806:                                        for(int i = 1; i < num; i++) {
                   1807:                                                int index = debugger_get_val(params[i]);
                   1808:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1809:                                                        telnet_printf("invalid index %x\n", index);
                   1810:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1811:                                                        telnet_printf("break point %x is null\n", index);
                   1812:                                                } else {
                   1813:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1814:                                                }
                   1815:                                        }
                   1816:                                } else {
                   1817:                                        telnet_printf("invalid parameter number\n");
                   1818:                                }
                   1819:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1820:                                if(num == 1) {
                   1821:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1822:                                                if(int_break_point.table[i].status) {
                   1823:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1824:                                                        if(int_break_point.table[i].ah_registered) {
                   1825:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1826:                                                        }
                   1827:                                                        if(int_break_point.table[i].al_registered) {
                   1828:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1829:                                                        }
                   1830:                                                        telnet_printf("\n");
                   1831:                                                }
                   1832:                                        }
                   1833:                                } else {
                   1834:                                        telnet_printf("invalid parameter number\n");
                   1835:                                }
                   1836:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1837:                                if(num == 1 || num == 2) {
                   1838:                                        break_point_t break_point_stored;
                   1839:                                        bool break_points_stored = false;
                   1840:                                        
                   1841:                                        if(stricmp(params[0], "P") == 0) {
                   1842:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1843:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1844:                                                break_points_stored = true;
                   1845:                                                
                   1846:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1847:                                                break_point.table[0].status = 1;
                   1848:                                        } else if(num >= 2) {
                   1849:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1850:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1851:                                                break_points_stored = true;
                   1852:                                                
                   1853:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1854:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1855:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1856:                                                break_point.table[0].seg = seg;
                   1857:                                                break_point.table[0].ofs = ofs;
                   1858:                                                break_point.table[0].status = 1;
                   1859:                                        }
                   1860:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1861:                                        now_going = true;
                   1862:                                        now_suspended = false;
                   1863:                                        
                   1864:                                        telnet_command("\033[2l"); // key unlock
1.1.1.54  root     1865:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1866:                                                if(telnet_kbhit()) {
                   1867:                                                        break;
                   1868:                                                }
                   1869:                                                Sleep(10);
                   1870:                                        }
                   1871:                                        now_going = false;
                   1872:                                        telnet_command("\033[2h"); // key lock
                   1873:                                        
                   1874:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1875:                                                Sleep(100);
1.1.1.54  root     1876:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1877:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1878:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1879:                                                }
                   1880:                                        }
1.1.1.54  root     1881:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1882:                                                if(telnet_disconnected()) {
                   1883:                                                        break;
                   1884:                                                }
                   1885:                                                Sleep(10);
                   1886:                                        }
                   1887:                                        dasm_seg = SREG(CS);
                   1888:                                        dasm_ofs = m_eip;
                   1889:                                        
                   1890:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1891:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1892:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1893:                                        
                   1894:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1895:                                        debugger_regs_info(buffer);
                   1896:                                        telnet_printf("%s", buffer);
                   1897:                                        
                   1898:                                        if(break_point.hit) {
                   1899:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1900:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1901:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1902:                                                }
                   1903:                                        } else if(rd_break_point.hit) {
                   1904:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1905:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1906:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1907:                                                m_prev_cs, m_prev_eip);
                   1908:                                        } else if(wr_break_point.hit) {
                   1909:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1910:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1911:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1912:                                                m_prev_cs, m_prev_eip);
                   1913:                                        } else if(in_break_point.hit) {
                   1914:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1915:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1916:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1917:                                                m_prev_cs, m_prev_eip);
                   1918:                                        } else if(out_break_point.hit) {
                   1919:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1920:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1921:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1922:                                                m_prev_cs, m_prev_eip);
                   1923:                                        } else if(int_break_point.hit) {
                   1924:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1925:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1926:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1927:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1928:                                                }
                   1929:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1930:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1931:                                                }
                   1932:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1933:                                        } else {
                   1934:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1935:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1936:                                        }
                   1937:                                        if(break_points_stored) {
                   1938:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1939:                                        }
                   1940:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1941:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1942:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1943:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1944:                                } else {
                   1945:                                        telnet_printf("invalid parameter number\n");
                   1946:                                }
                   1947:                        } else if(stricmp(params[0], "T") == 0) {
                   1948:                                if(num == 1 || num == 2) {
                   1949:                                        int steps = 1;
                   1950:                                        if(num >= 2) {
                   1951:                                                steps = debugger_get_val(params[1]);
                   1952:                                        }
                   1953:                                        
                   1954:                                        telnet_command("\033[2l"); // key unlock
                   1955:                                        while(steps-- > 0) {
                   1956:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1957:                                                now_going = false;
                   1958:                                                now_suspended = false;
                   1959:                                                
1.1.1.54  root     1960:                                                while(!m_exit && !now_suspended) {
1.1.1.33  root     1961:                                                        if(telnet_disconnected()) {
                   1962:                                                                break;
                   1963:                                                        }
                   1964:                                                        Sleep(10);
                   1965:                                                }
                   1966:                                                dasm_seg = SREG(CS);
                   1967:                                                dasm_ofs = m_eip;
                   1968:                                                
                   1969:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1970:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1971:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1972:                                                
                   1973:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1974:                                                debugger_regs_info(buffer);
                   1975:                                                telnet_printf("%s", buffer);
                   1976:                                                
                   1977:                                                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()) {
                   1978:                                                        break;
                   1979:                                                }
                   1980:                                        }
                   1981:                                        telnet_command("\033[2h"); // key lock
                   1982:                                        
                   1983:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1984:                                                Sleep(100);
1.1.1.54  root     1985:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1986:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1987:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1988:                                                }
                   1989:                                        }
1.1.1.54  root     1990:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1991:                                                if(telnet_disconnected()) {
                   1992:                                                        break;
                   1993:                                                }
                   1994:                                                Sleep(10);
                   1995:                                        }
                   1996:                                        if(break_point.hit) {
                   1997:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1998:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1999:                                        } else if(rd_break_point.hit) {
                   2000:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2001:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   2002:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   2003:                                                m_prev_cs, m_prev_eip);
                   2004:                                        } else if(wr_break_point.hit) {
                   2005:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2006:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   2007:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   2008:                                                m_prev_cs, m_prev_eip);
                   2009:                                        } else if(in_break_point.hit) {
                   2010:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2011:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   2012:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   2013:                                                m_prev_cs, m_prev_eip);
                   2014:                                        } else if(out_break_point.hit) {
                   2015:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2016:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   2017:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   2018:                                                m_prev_cs, m_prev_eip);
                   2019:                                        } else if(int_break_point.hit) {
                   2020:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2021:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   2022:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   2023:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   2024:                                                }
                   2025:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   2026:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   2027:                                                }
                   2028:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   2029:                                        } else if(steps > 0) {
                   2030:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2031:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   2032:                                        }
                   2033:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   2034:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   2035:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   2036:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   2037:                                } else {
                   2038:                                        telnet_printf("invalid parameter number\n");
                   2039:                                }
                   2040:                        } else if(stricmp(params[0], "Q") == 0) {
                   2041:                                break;
                   2042:                        } else if(stricmp(params[0], "X") == 0) {
                   2043:                                debugger_process_info(buffer);
                   2044:                                telnet_printf("%s", buffer);
                   2045:                        } else if(stricmp(params[0], ">") == 0) {
                   2046:                                if(num == 2) {
                   2047:                                        if(fp_debugger != NULL) {
                   2048:                                                fclose(fp_debugger);
                   2049:                                                fp_debugger = NULL;
                   2050:                                        }
                   2051:                                        fp_debugger = fopen(params[1], "w");
                   2052:                                } else {
                   2053:                                        telnet_printf("invalid parameter number\n");
                   2054:                                }
                   2055:                        } else if(stricmp(params[0], "<") == 0) {
                   2056:                                if(num == 2) {
                   2057:                                        if(fi_debugger != NULL) {
                   2058:                                                fclose(fi_debugger);
                   2059:                                                fi_debugger = NULL;
                   2060:                                        }
                   2061:                                        fi_debugger = fopen(params[1], "r");
                   2062:                                } else {
                   2063:                                        telnet_printf("invalid parameter number\n");
                   2064:                                }
                   2065:                        } else if(stricmp(params[0], "?") == 0) {
                   2066:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   2067:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   2068:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   2069:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   2070:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   2071:                                
                   2072:                                telnet_printf("R - show registers\n");
                   2073:                                telnet_printf("R <reg> <value> - edit register\n");
                   2074:                                telnet_printf("S <start> <end> <list> - search\n");
                   2075:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   2076:                                
                   2077:                                telnet_printf("H <value> <value> - hexadd\n");
                   2078:                                
                   2079:                                telnet_printf("BP <address> - set breakpoint\n");
                   2080:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   2081:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   2082:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   2083:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   2084:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   2085:                                
                   2086:                                telnet_printf("G - go (press enter key to break)\n");
                   2087:                                telnet_printf("G <address> - go and break at address\n");
                   2088:                                telnet_printf("P - trace one opcode (step over)\n");
                   2089:                                telnet_printf("T [<count>] - trace (step in)\n");
                   2090:                                telnet_printf("Q - quit\n");
                   2091:                                telnet_printf("X - show dos process info\n");
                   2092:                                
                   2093:                                telnet_printf("> <filename> - output logfile\n");
                   2094:                                telnet_printf("< <filename> - input commands from file\n");
                   2095:                                
                   2096:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   2097:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   2098:                        } else {
                   2099:                                telnet_printf("unknown command %s\n", params[0]);
                   2100:                        }
                   2101:                }
                   2102:        }
                   2103:        if(fp_debugger != NULL) {
                   2104:                fclose(fp_debugger);
                   2105:                fp_debugger = NULL;
                   2106:        }
                   2107:        if(fi_debugger != NULL) {
                   2108:                fclose(fi_debugger);
                   2109:                fi_debugger = NULL;
                   2110:        }
                   2111:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2112:        closesocket(cli_socket);
                   2113: }
                   2114: 
                   2115: const char *debugger_get_ttermpro_path()
                   2116: {
                   2117:        static char path[MAX_PATH] = {0};
                   2118:        
                   2119:        if(getenv("ProgramFiles")) {
                   2120:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2121:        }
                   2122:        return(path);
                   2123: }
                   2124: 
                   2125: const char *debugger_get_ttermpro_x86_path()
                   2126: {
                   2127:        static char path[MAX_PATH] = {0};
                   2128:        
                   2129:        if(getenv("ProgramFiles(x86)")) {
                   2130:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2131:        }
                   2132:        return(path);
                   2133: }
                   2134: 
                   2135: const char *debugger_get_putty_path()
                   2136: {
                   2137:        static char path[MAX_PATH] = {0};
                   2138:        
                   2139:        if(getenv("ProgramFiles")) {
                   2140:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2141:        }
                   2142:        return(path);
                   2143: }
                   2144: 
                   2145: const char *debugger_get_putty_x86_path()
                   2146: {
                   2147:        static char path[MAX_PATH] = {0};
                   2148:        
                   2149:        if(getenv("ProgramFiles(x86)")) {
                   2150:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2151:        }
                   2152:        return(path);
                   2153: }
                   2154: 
                   2155: const char *debugger_get_telnet_path()
                   2156: {
                   2157:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2158:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2159:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2160:        // and 64bit version of telnet.exe will be installed in System32.
                   2161:        static char path[MAX_PATH] = {0};
                   2162:        
                   2163:        if(getenv("windir") != NULL) {
                   2164:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2165:        }
                   2166:        return(path);
                   2167: }
                   2168: 
                   2169: DWORD WINAPI debugger_thread(LPVOID)
                   2170: {
                   2171:        WSADATA was_data;
                   2172:        struct sockaddr_in svr_addr;
                   2173:        struct sockaddr_in cli_addr;
                   2174:        int cli_addr_len = sizeof(cli_addr);
                   2175:        int port = 23;
                   2176:        int bind_stat = SOCKET_ERROR;
                   2177:        struct timeval timeout;
                   2178:        
                   2179:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2180:        
                   2181:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2182:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2183:                svr_addr.sin_family = AF_INET;
                   2184:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2185:                
1.1.1.54  root     2186:                while(!m_exit && port < 10000) {
1.1.1.33  root     2187:                        svr_addr.sin_port = htons(port);
                   2188:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2189:                                break;
                   2190:                        } else {
                   2191:                                port = (port == 23) ? 9000 : (port + 1);
                   2192:                        }
                   2193:                }
                   2194:                if(bind_stat == 0) {
                   2195:                        timeout.tv_sec = 1;
                   2196:                        timeout.tv_usec = 0;
1.1.1.45  root     2197:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2198:                        
                   2199:                        listen(svr_socket, 1);
                   2200:                        
                   2201:                        char command[MAX_PATH] = {0};
1.1.1.60  root     2202:                        STARTUPINFOA si;
1.1.1.33  root     2203:                        PROCESS_INFORMATION pi;
                   2204:                        
                   2205:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2206:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2207:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2208:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2209:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2210:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2211:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2212:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2213:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2214:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2215:                        }
                   2216:                        if(command[0] != '\0') {
1.1.1.60  root     2217:                                memset(&si, 0, sizeof(STARTUPINFOA));
1.1.1.33  root     2218:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
1.1.1.60  root     2219:                                CreateProcessA(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
1.1.1.33  root     2220:                        }
                   2221:                        
1.1.1.54  root     2222:                        while(!m_exit) {
1.1.1.33  root     2223:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2224:                                        u_long val = 1;
                   2225:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2226:                                        debugger_main();
                   2227:                                }
                   2228:                        }
                   2229:                }
                   2230:        }
                   2231:        WSACleanup();
                   2232:        return(0);
                   2233: }
                   2234: #endif
                   2235: 
                   2236: /* ----------------------------------------------------------------------------
1.1       root     2237:        main
                   2238: ---------------------------------------------------------------------------- */
                   2239: 
1.1.1.28  root     2240: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2241: {
                   2242:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2243:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2244: #ifdef USE_SERVICE_THREAD
                   2245:                        EnterCriticalSection(&key_buf_crit_sect);
                   2246: #endif
1.1.1.51  root     2247:                        pcbios_clear_key_buffer();
1.1.1.35  root     2248: #ifdef USE_SERVICE_THREAD
                   2249:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2250: #endif
1.1.1.33  root     2251:                }
                   2252: //             key_code = key_recv = 0;
1.1.1.28  root     2253:                return TRUE;
                   2254:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2255:                return TRUE;
                   2256:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2257:                // this program will be terminated abnormally, do minimum end process
                   2258:                exit_handler();
                   2259:                exit(1);
                   2260:        }
                   2261:        return FALSE;
                   2262: }
                   2263: 
                   2264: void exit_handler()
                   2265: {
                   2266:        if(temp_file_created) {
1.1.1.60  root     2267:                DeleteFileA(temp_file_path);
1.1.1.28  root     2268:                temp_file_created = false;
                   2269:        }
                   2270:        if(key_buf_char != NULL) {
                   2271:                key_buf_char->release();
                   2272:                delete key_buf_char;
                   2273:                key_buf_char = NULL;
                   2274:        }
                   2275:        if(key_buf_scan != NULL) {
                   2276:                key_buf_scan->release();
                   2277:                delete key_buf_scan;
                   2278:                key_buf_scan = NULL;
                   2279:        }
1.1.1.57  root     2280:        if(key_buf_data != NULL) {
                   2281:                key_buf_data->release();
                   2282:                delete key_buf_data;
                   2283:                key_buf_data = NULL;
                   2284:        }
1.1.1.32  root     2285: #ifdef SUPPORT_XMS
                   2286:        msdos_xms_release();
                   2287: #endif
1.1.1.28  root     2288:        hardware_release();
                   2289: }
                   2290: 
1.1.1.35  root     2291: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2292: DWORD WINAPI vram_thread(LPVOID)
                   2293: {
1.1.1.54  root     2294:        while(!m_exit) {
1.1.1.28  root     2295:                EnterCriticalSection(&vram_crit_sect);
                   2296:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2297:                        vram_flush_char();
                   2298:                }
                   2299:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2300:                        vram_flush_attr();
                   2301:                }
                   2302:                vram_last_length_char = vram_length_char;
                   2303:                vram_last_length_attr = vram_length_attr;
                   2304:                LeaveCriticalSection(&vram_crit_sect);
                   2305:                // this is about half the maximum keyboard repeat rate - any
                   2306:                // lower tends to be jerky, any higher misses updates
                   2307:                Sleep(15);
                   2308:        }
                   2309:        return 0;
                   2310: }
                   2311: #endif
                   2312: 
1.1.1.45  root     2313: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2314: {
                   2315:        UINT8 header[0x400];
                   2316:        
                   2317:        long position = ftell(fp);
                   2318:        fseek(fp, 0, SEEK_SET);
                   2319:        fread(header, sizeof(header), 1, fp);
                   2320:        fseek(fp, position, SEEK_SET);
                   2321:        
                   2322:        try {
                   2323:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2324:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2325:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2326:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2327:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2328:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2329:                
                   2330:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2331:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2332:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2333:                                return(sectionHeader->PointerToRawData);
                   2334:                        }
                   2335:                }
                   2336:        } catch(...) {
                   2337:        }
                   2338:        return(0);
                   2339: }
                   2340: 
1.1.1.10  root     2341: bool is_started_from_command_prompt()
                   2342: {
1.1.1.58  root     2343:        bool result = false;
1.1.1.60  root     2344:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.58  root     2345:        
1.1.1.18  root     2346:        if(hLibrary) {
                   2347:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2348:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2349:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
1.1.1.58  root     2350:                if(lpfnGetConsoleProcessList) { // Windows XP or later
1.1.1.18  root     2351:                        DWORD pl;
1.1.1.58  root     2352:                        result = (lpfnGetConsoleProcessList(&pl, 1) > 1);
1.1.1.18  root     2353:                        FreeLibrary(hLibrary);
1.1.1.58  root     2354:                        return(result);
1.1.1.18  root     2355:                }
                   2356:                FreeLibrary(hLibrary);
                   2357:        }
                   2358:        
                   2359:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2360:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2361:                DWORD dwParentProcessID = 0;
                   2362:                PROCESSENTRY32 pe32;
                   2363:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2364:                if(Process32First(hSnapshot, &pe32)) {
                   2365:                        do {
                   2366:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2367:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2368:                                        break;
                   2369:                                }
                   2370:                        } while(Process32Next(hSnapshot, &pe32));
                   2371:                }
                   2372:                CloseHandle(hSnapshot);
                   2373:                if(dwParentProcessID != 0) {
                   2374:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2375:                        if(hProcess != NULL) {
                   2376:                                HMODULE hMod;
                   2377:                                DWORD cbNeeded;
                   2378:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2379:                                        char module_name[MAX_PATH];
1.1.1.60  root     2380:                                        if(GetModuleBaseNameA(hProcess, hMod, module_name, sizeof(module_name))) {
1.1.1.58  root     2381:                                                result = (_strnicmp(module_name, "cmd.exe", 7) == 0);
1.1.1.18  root     2382:                                        }
                   2383:                                }
                   2384:                                CloseHandle(hProcess);
                   2385:                        }
                   2386:                }
                   2387:        }
1.1.1.58  root     2388:        return(result);
1.1.1.14  root     2389: }
                   2390: 
                   2391: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2392: {
1.1.1.60  root     2393:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.14  root     2394:        
1.1.1.60  root     2395:        if(hLibrary) {
                   2396:                typedef ULONGLONG (WINAPI* VerSetConditionMaskFunction)(ULONGLONG, DWORD, BYTE);
                   2397:                typedef BOOL(WINAPI* VerifyVersionInfoFunction)(LPOSVERSIONINFOEXA, DWORD, DWORDLONG);
                   2398:                
                   2399:                VerSetConditionMaskFunction lpfnVerSetConditionMask = reinterpret_cast<VerSetConditionMaskFunction>(::GetProcAddress(hLibrary, "VerSetConditionMask"));
                   2400:                VerifyVersionInfoFunction lpfnVerifyVersionInfo = reinterpret_cast<VerifyVersionInfoFunction>(::GetProcAddress(hLibrary, "VerifyVersionInfoA"));
                   2401:                
                   2402:                if(lpfnVerSetConditionMask && lpfnVerifyVersionInfo) { // Windows 2000 or later
                   2403:                        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
                   2404:                        OSVERSIONINFOEXA osvi;
                   2405:                        DWORDLONG dwlConditionMask = 0;
                   2406:                        int op = VER_GREATER_EQUAL;
                   2407:                        
                   2408:                        // Initialize the OSVERSIONINFOEXA structure.
                   2409:                        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEXA));
                   2410:                        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXA);
                   2411:                        osvi.dwMajorVersion = dwMajorVersion;
                   2412:                        osvi.dwMinorVersion = dwMinorVersion;
                   2413:                        osvi.wServicePackMajor = wServicePackMajor;
                   2414:                        osvi.wServicePackMinor = wServicePackMinor;
                   2415:                        
                   2416:                         // Initialize the condition mask.
                   2417:                        #define MY_VER_SET_CONDITION(_m_,_t_,_c_) ((_m_)=lpfnVerSetConditionMask((_m_),(_t_),(_c_)))
                   2418:                        
                   2419:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2420:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2421:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2422:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2423:                        
                   2424:                        // Perform the test.
                   2425:                        BOOL result = lpfnVerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2426:                        FreeLibrary(hLibrary);
                   2427:                        return(result);
                   2428:                }
                   2429:                FreeLibrary(hLibrary);
                   2430:        }
                   2431:        
                   2432:        OSVERSIONINFOA osvi;
                   2433:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
                   2434:        
                   2435:        if(GetVersionExA((LPOSVERSIONINFOA)&osvi)) {
                   2436:                if(osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) {
                   2437:                        return(false);
                   2438:                } else if(osvi.dwMajorVersion > dwMajorVersion) {
                   2439:                        return(true);
                   2440:                } else if(osvi.dwMajorVersion < dwMajorVersion) {
                   2441:                        return(false);
                   2442:                } else if(osvi.dwMinorVersion > dwMinorVersion) {
                   2443:                        return(true);
                   2444:                } else if(osvi.dwMinorVersion < dwMinorVersion) {
                   2445:                        return(false);
                   2446:                }
                   2447:                // FIXME: check wServicePackMajor and wServicePackMinor :-(
                   2448:                return(true);
                   2449:        }
                   2450:        return(false);
1.1.1.14  root     2451: }
                   2452: 
1.1.1.61  root     2453: HWND get_console_window_handle()
1.1.1.58  root     2454: {
1.1.1.61  root     2455:        static HWND hwndFound = 0;
                   2456:        
                   2457:        if(hwndFound == 0) {
                   2458:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   2459:                char pszNewWindowTitle[1024];
                   2460:                char pszOldWindowTitle[1024];
                   2461:                
                   2462:                GetConsoleTitleA(pszOldWindowTitle, 1024);
                   2463:                wsprintfA(pszNewWindowTitle, "%d/%d", GetTickCount(), GetCurrentProcessId());
                   2464:                SetConsoleTitleA(pszNewWindowTitle);
                   2465:                Sleep(100);
                   2466:                hwndFound = FindWindowA(NULL, pszNewWindowTitle);
                   2467:                SetConsoleTitleA(pszOldWindowTitle);
                   2468:        }
                   2469:        return hwndFound;
                   2470: }
                   2471: 
                   2472: HDC get_console_window_device_context()
                   2473: {
                   2474:        return GetDC(get_console_window_handle());
                   2475: }
                   2476: 
                   2477: bool get_console_font_size(int *width, int *height)
                   2478: {
                   2479:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.58  root     2480:        bool result = false;
                   2481:        
1.1.1.62  root     2482:        if(is_winxp_or_later) {
                   2483:                HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
                   2484:                if(hLibrary) {
                   2485:                        typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
                   2486:                        GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
                   2487:                        if(lpfnGetCurrentConsoleFont) { // Windows XP or later
                   2488:                                CONSOLE_FONT_INFO fi;
                   2489:                                if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi) != 0) {
                   2490:                                        *width  = fi.dwFontSize.X;
                   2491:                                        *height = fi.dwFontSize.Y;
                   2492:                                        result = true;
                   2493:                                }
1.1.1.58  root     2494:                        }
1.1.1.62  root     2495:                        FreeLibrary(hLibrary);
                   2496:                }
                   2497:        } else {
                   2498:                CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2499:                RECT rect;
                   2500:                if(GetConsoleScreenBufferInfo(hStdout, &csbi) && GetClientRect(get_console_window_handle(), &rect)) {
                   2501:                        int cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
                   2502:                        int rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2503:                        *width  = rect.right / cols;
                   2504:                        *height = rect.bottom / rows;
                   2505:                        result = true;
1.1.1.58  root     2506:                }
                   2507:        }
                   2508:        return(result);
                   2509: }
                   2510: 
1.1.1.61  root     2511: bool set_console_font_size(int width, int height)
1.1.1.56  root     2512: {
                   2513:        // http://d.hatena.ne.jp/aharisu/20090427/1240852598
1.1.1.61  root     2514:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.56  root     2515:        bool result = false;
1.1.1.60  root     2516:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.56  root     2517:        
                   2518:        if(hLibrary) {
1.1.1.62  root     2519:                CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2520:                RECT rect;
                   2521:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   2522:                int cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
                   2523:                int rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2524:                
1.1.1.56  root     2525:                typedef BOOL (WINAPI* GetConsoleFontInfoFunction)(HANDLE, BOOL, DWORD, PCONSOLE_FONT_INFO);
                   2526:                typedef DWORD (WINAPI* GetNumberOfConsoleFontsFunction)(VOID);
1.1.1.60  root     2527:                typedef COORD (WINAPI* GetConsoleFontSizeFunction)(HANDLE, DWORD);
1.1.1.56  root     2528:                typedef BOOL (WINAPI* SetConsoleFontFunction)(HANDLE, DWORD);
                   2529:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
1.1.1.61  root     2530:                typedef BOOL (WINAPI* GetCurrentConsoleFontExFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
                   2531:                typedef BOOL (WINAPI* SetCurrentConsoleFontExFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
1.1.1.56  root     2532:                
                   2533:                GetConsoleFontInfoFunction lpfnGetConsoleFontInfo = reinterpret_cast<GetConsoleFontInfoFunction>(::GetProcAddress(hLibrary, "GetConsoleFontInfo"));
                   2534:                GetNumberOfConsoleFontsFunction lpfnGetNumberOfConsoleFonts = reinterpret_cast<GetNumberOfConsoleFontsFunction>(::GetProcAddress(hLibrary, "GetNumberOfConsoleFonts"));
1.1.1.60  root     2535:                GetConsoleFontSizeFunction lpfnGetConsoleFontSize = reinterpret_cast<GetConsoleFontSizeFunction>(::GetProcAddress(hLibrary, "GetConsoleFontSize"));
1.1.1.56  root     2536:                SetConsoleFontFunction lpfnSetConsoleFont = reinterpret_cast<SetConsoleFontFunction>(::GetProcAddress(hLibrary, "SetConsoleFont"));
                   2537:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
1.1.1.61  root     2538:                GetCurrentConsoleFontExFunction lpfnGetCurrentConsoleFontEx = reinterpret_cast<GetCurrentConsoleFontExFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFontEx"));
                   2539:                SetCurrentConsoleFontExFunction lpfnSetCurrentConsoleFontEx = reinterpret_cast<SetCurrentConsoleFontExFunction>(::GetProcAddress(hLibrary, "SetCurrentConsoleFontEx"));
1.1.1.56  root     2540:                
1.1.1.62  root     2541:                if(lpfnGetConsoleFontInfo && lpfnGetNumberOfConsoleFonts && lpfnSetConsoleFont) { // Windows 2000 or later
1.1.1.56  root     2542:                        DWORD dwFontNum = lpfnGetNumberOfConsoleFonts();
1.1.1.61  root     2543:                        if(dwFontNum) {
                   2544:                                CONSOLE_FONT_INFO* fonts = (CONSOLE_FONT_INFO*)malloc(sizeof(CONSOLE_FONT_INFO) * dwFontNum);
                   2545:                                lpfnGetConsoleFontInfo(hStdout, FALSE, dwFontNum, fonts);
                   2546:                                for(int i = 0; i < dwFontNum; i++) {
                   2547:                                        fonts[i].dwFontSize = lpfnGetConsoleFontSize(hStdout, fonts[i].nFont);
                   2548:                                        if(fonts[i].dwFontSize.X == width && fonts[i].dwFontSize.Y == height) {
1.1.1.62  root     2549:                                                if(lpfnSetConsoleFont(hStdout, fonts[i].nFont)) {
                   2550:                                                        if(is_winxp_or_later && lpfnGetCurrentConsoleFont) { // Windows XP or later
                   2551:                                                                CONSOLE_FONT_INFO fi;
                   2552:                                                                if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi)) {
                   2553:                                                                        if(fonts[i].dwFontSize.X == fi.dwFontSize.X && fonts[i].dwFontSize.Y == fi.dwFontSize.Y) {
                   2554:                                                                                result = true;
                   2555:                                                                                break;
                   2556:                                                                        }
                   2557:                                                                }
                   2558:                                                        } else {
                   2559:                                                                Sleep(10);
                   2560:                                                                if(GetClientRect(get_console_window_handle(), &rect)) {
                   2561:                                                                        if(fonts[i].dwFontSize.X * cols == rect.right && fonts[i].dwFontSize.Y * rows == rect.bottom) {
                   2562:                                                                                result = true;
                   2563:                                                                                break;
                   2564:                                                                        }
                   2565:                                                                }
1.1.1.58  root     2566:                                                        }
                   2567:                                                }
1.1.1.61  root     2568:                                        }
                   2569:                                }
                   2570:                                free(fonts);
                   2571:                        } else if(lpfnGetCurrentConsoleFontEx && lpfnSetCurrentConsoleFontEx) {
                   2572:                                // for Windows10 enhanced command prompt
                   2573:                                CONSOLE_FONT_INFOEX fi_old, fi_new;
                   2574:                                fi_old.cbSize = sizeof(CONSOLE_FONT_INFOEX);
                   2575:                                if(lpfnGetCurrentConsoleFontEx(hStdout, FALSE, &fi_old)) {
                   2576:                                        fi_new = fi_old;
                   2577:                                        fi_new.dwFontSize.X = width;
                   2578:                                        fi_new.dwFontSize.Y = height;
                   2579:                                        if(lpfnSetCurrentConsoleFontEx(hStdout, FALSE, &fi_new)) {
                   2580:                                                lpfnGetCurrentConsoleFontEx(hStdout, FALSE, &fi_new);
                   2581:                                                if(fi_new.dwFontSize.X == width && fi_new.dwFontSize.Y == height) {
                   2582:                                                        result = true;
                   2583:                                                } else {
                   2584:                                                        lpfnSetCurrentConsoleFontEx(hStdout, FALSE, &fi_old);
1.1.1.58  root     2585:                                                }
                   2586:                                        }
1.1.1.57  root     2587:                                }
1.1.1.56  root     2588:                        }
                   2589:                }
                   2590:                FreeLibrary(hLibrary);
                   2591:        }
                   2592:        return(result);
                   2593: }
                   2594: 
1.1.1.59  root     2595: bool is_cursor_blink_off()
                   2596: {
                   2597:        static int result = -1;
                   2598:        HKEY hKey;
                   2599:        char chData[64];
                   2600:        DWORD dwSize = sizeof(chData);
                   2601:        
                   2602:        if(result == -1) {
                   2603:                result = 0;
1.1.1.60  root     2604:                if(RegOpenKeyExA(HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   2605:                        if(RegQueryValueExA(hKey, "CursorBlinkRate", NULL, NULL, (LPBYTE)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.59  root     2606:                                if(strncmp(chData, "-1", 2) == 0) {
                   2607:                                        result = 1;
                   2608:                                }
                   2609:                        }
                   2610:                        RegCloseKey(hKey);
                   2611:                }
                   2612:        }
                   2613:        return(result != 0);
                   2614: }
                   2615: 
1.1.1.27  root     2616: void get_sio_port_numbers()
                   2617: {
                   2618:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2619:        HDEVINFO hDevInfo = 0;
                   2620:        HKEY hKey = 0;
1.1.1.60  root     2621:        if((hDevInfo = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
1.1.1.27  root     2622:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2623:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2624:                                char chData[256];
                   2625:                                DWORD dwType = 0;
                   2626:                                DWORD dwSize = sizeof(chData);
                   2627:                                int port_number = 0;
                   2628:                                
1.1.1.60  root     2629:                                if(RegQueryValueExA(hKey, "PortName", NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.27  root     2630:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2631:                                                port_number = atoi(chData + 3);
                   2632:                                        }
                   2633:                                }
                   2634:                                RegCloseKey(hKey);
                   2635:                                
1.1.1.29  root     2636:                                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     2637:                                        continue;
                   2638:                                }
                   2639:                                if(sio_port_number[0] == 0) {
                   2640:                                        sio_port_number[0] = port_number;
                   2641:                                } else if(sio_port_number[1] == 0) {
                   2642:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2643:                                } else if(sio_port_number[2] == 0) {
                   2644:                                        sio_port_number[2] = port_number;
                   2645:                                } else if(sio_port_number[3] == 0) {
                   2646:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2647:                                }
1.1.1.29  root     2648:                                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     2649:                                        break;
                   2650:                                }
                   2651:                        }
                   2652:                }
                   2653:        }
                   2654: }
                   2655: 
1.1.1.28  root     2656: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2657: 
1.1       root     2658: int main(int argc, char *argv[], char *envp[])
                   2659: {
1.1.1.9   root     2660:        int arg_offset = 0;
                   2661:        int standard_env = 0;
1.1.1.14  root     2662:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2663:        bool get_console_info_success = false;
1.1.1.56  root     2664:        bool get_console_font_success = false;
1.1.1.28  root     2665:        bool screen_size_changed = false;
                   2666:        
1.1.1.60  root     2667:        char path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2668:        GetModuleFileNameA(NULL, path, MAX_PATH);
                   2669:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1       root     2670:        
1.1.1.27  root     2671:        char dummy_argv_0[] = "msdos.exe";
                   2672:        char dummy_argv_1[MAX_PATH];
                   2673:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2674:        char new_exec_file[MAX_PATH];
                   2675:        bool convert_cmd_file = false;
1.1.1.28  root     2676:        unsigned int code_page = 0;
1.1.1.27  root     2677:        
                   2678:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2679:                // check if command file is embedded to this execution file
                   2680:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2681:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2682:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2683:                if(offset != 0) {
1.1.1.30  root     2684:                        UINT8 buffer[16];
1.1.1.28  root     2685:                        fseek(fp, offset, SEEK_SET);
                   2686:                        fread(buffer, sizeof(buffer), 1, fp);
                   2687:                        
                   2688:                        // restore flags
                   2689:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2690:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2691:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2692:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2693:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2694:                        if((buffer[0] & 0x20) != 0) {
                   2695:                                get_sio_port_numbers();
                   2696:                        }
                   2697:                        if((buffer[0] & 0x40) != 0) {
                   2698:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2699:                                support_ems = true;
1.1.1.30  root     2700:                        }
1.1.1.27  root     2701: #ifdef SUPPORT_XMS
1.1.1.30  root     2702:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2703:                                support_xms = true;
                   2704:                        }
1.1.1.30  root     2705: #endif
1.1.1.28  root     2706:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2707:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2708:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2709:                        }
                   2710:                        if(buffer[5] != 0) {
1.1.1.30  root     2711:                                dos_major_version = buffer[5];
                   2712:                                dos_minor_version = buffer[6];
                   2713:                        }
                   2714:                        if(buffer[7] != 0) {
                   2715:                                win_major_version = buffer[7];
                   2716:                                win_minor_version = buffer[8];
1.1.1.28  root     2717:                        }
1.1.1.30  root     2718:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2719:                                SetConsoleCP(code_page);
                   2720:                                SetConsoleOutputCP(code_page);
                   2721:                        }
1.1.1.30  root     2722:                        int name_len = buffer[11];
                   2723:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2724:                        
                   2725:                        // restore command file name
                   2726:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2727:                        fread(dummy_argv_1, name_len, 1, fp);
                   2728:                        
                   2729:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2730:                                // if original command file exists, create a temporary file name
1.1.1.60  root     2731:                                if(GetTempFileNameA(".", "DOS", 0, dummy_argv_1) != 0) {
1.1.1.28  root     2732:                                        // create a temporary command file in the current director
1.1.1.60  root     2733:                                        DeleteFileA(dummy_argv_1);
1.1.1.27  root     2734:                                } else {
1.1.1.28  root     2735:                                        // create a temporary command file in the temporary folder
1.1.1.60  root     2736:                                        GetTempPathA(MAX_PATH, path);
                   2737:                                        if(GetTempFileNameA(path, "DOS", 0, dummy_argv_1) != 0) {
                   2738:                                                DeleteFileA(dummy_argv_1);
1.1.1.28  root     2739:                                        } else {
                   2740:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2741:                                        }
1.1.1.27  root     2742:                                }
1.1.1.28  root     2743:                                // check the command file type
                   2744:                                fread(buffer, 2, 1, fp);
                   2745:                                fseek(fp, -2, SEEK_CUR);
                   2746:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2747:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2748:                                } else {
                   2749:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2750:                                }
                   2751:                        }
1.1.1.28  root     2752:                        
                   2753:                        // restore command file
                   2754:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2755:                        for(int i = 0; i < file_len; i++) {
                   2756:                                fputc(fgetc(fp), fo);
                   2757:                        }
                   2758:                        fclose(fo);
                   2759:                        
1.1.1.60  root     2760:                        GetFullPathNameA(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
1.1.1.28  root     2761:                        temp_file_created = true;
1.1.1.60  root     2762:                        SetFileAttributesA(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
1.1.1.28  root     2763:                        
                   2764:                        // adjust argc/argv
                   2765:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2766:                                dummy_argv[i + 1] = argv[i];
                   2767:                        }
                   2768:                        argc++;
                   2769:                        argv = dummy_argv;
1.1.1.27  root     2770:                }
                   2771:                fclose(fp);
                   2772:        }
1.1.1.9   root     2773:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2774:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2775:                        stay_busy = true;
                   2776:                        arg_offset++;
1.1.1.27  root     2777:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2778:                        if(argv[i][2] != '\0') {
                   2779:                                strcpy(new_exec_file, &argv[i][2]);
                   2780:                        } else {
                   2781:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2782:                        }
                   2783:                        convert_cmd_file = true;
                   2784:                        arg_offset++;
1.1.1.28  root     2785:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2786:                        if(IS_NUMERIC(argv[i][2])) {
                   2787:                                code_page = atoi(&argv[i][2]);
                   2788:                        } else {
                   2789:                                code_page = GetConsoleCP();
                   2790:                        }
                   2791:                        arg_offset++;
1.1.1.25  root     2792:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2793:                        no_windows = true;
                   2794:                        arg_offset++;
                   2795:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2796:                        standard_env = 1;
                   2797:                        arg_offset++;
1.1.1.14  root     2798:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2799:                        ignore_illegal_insn = true;
                   2800:                        arg_offset++;
                   2801:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2802:                        limit_max_memory = true;
                   2803:                        arg_offset++;
                   2804:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51  root     2805:                        int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
                   2806:                        if(result == 1) {
                   2807:                                buf_width = 0;
                   2808:                        } else if(result != 2) {
1.1.1.17  root     2809:                                buf_width = buf_height = 0;
                   2810:                        }
                   2811:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2812:                                buf_width = 80;
                   2813:                        }
                   2814:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2815:                                buf_height = 25;
                   2816:                        }
1.1.1.14  root     2817:                        arg_offset++;
1.1.1.25  root     2818:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2819:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2820:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2821:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2822:                                        sio_port_number[1] = atoi(p1 + 1);
                   2823:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2824:                                                sio_port_number[2] = atoi(p2 + 1);
                   2825:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2826:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2827:                                                }
                   2828:                                        }
1.1.1.25  root     2829:                                }
1.1.1.29  root     2830:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2831:                        }
1.1.1.29  root     2832:                        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     2833:                                get_sio_port_numbers();
1.1.1.25  root     2834:                        }
                   2835:                        arg_offset++;
1.1.1.9   root     2836:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2837:                        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     2838:                                dos_major_version = argv[i][2] - '0';
                   2839:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2840:                        }
                   2841:                        arg_offset++;
                   2842:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2843:                        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]))) {
                   2844:                                win_major_version = argv[i][2] - '0';
                   2845:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2846:                        }
                   2847:                        arg_offset++;
1.1.1.25  root     2848:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2849:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2850:                        support_ems = true;
                   2851: #ifdef SUPPORT_XMS
                   2852:                        support_xms = true;
                   2853: #endif
                   2854:                        arg_offset++;
1.1.1.9   root     2855:                } else {
                   2856:                        break;
                   2857:                }
                   2858:        }
                   2859:        if(argc < 2 + arg_offset) {
1.1       root     2860: #ifdef _WIN64
1.1.1.14  root     2861:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2862: #else
1.1.1.14  root     2863:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2864: #endif
1.1.1.25  root     2865:                fprintf(stderr,
1.1.1.28  root     2866:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2867:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2868:                        "\n"
                   2869:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2870: #ifdef _WIN64
1.1.1.27  root     2871:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2872: #else
1.1.1.27  root     2873:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2874: #endif
1.1.1.28  root     2875:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2876:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2877:                        "\t-e\tuse a reduced environment block\n"
                   2878:                        "\t-i\tignore invalid instructions\n"
                   2879:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2880:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2881:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2882:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2883:                        "\t-w\tset the Windows version\n"
1.1.1.63  root     2884: #if defined(SUPPORT_VCPI)
                   2885:                        "\t-x\tenable LIM EMS, VCPI, and XMS\n"
                   2886: #elif defined(SUPPORT_XMS)
                   2887:                        "\t-x\tenable LIM EMS and XMS\n"
1.1.1.19  root     2888: #else
1.1.1.28  root     2889:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2890: #endif
                   2891:                );
1.1.1.10  root     2892:                
                   2893:                if(!is_started_from_command_prompt()) {
                   2894:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2895:                        while(!_kbhit()) {
                   2896:                                Sleep(10);
                   2897:                        }
                   2898:                }
1.1.1.20  root     2899: #ifdef _DEBUG
                   2900:                _CrtDumpMemoryLeaks();
                   2901: #endif
1.1       root     2902:                return(EXIT_FAILURE);
                   2903:        }
1.1.1.27  root     2904:        if(convert_cmd_file) {
                   2905:                retval = EXIT_FAILURE;
1.1.1.28  root     2906:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2907:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2908:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2909:                        
1.1.1.28  root     2910:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2911:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2912:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2913:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2914:                        } else {
1.1.1.28  root     2915:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2916:                                if(offset != 0) {
                   2917:                                        UINT8 buffer[14];
                   2918:                                        fseek(fp, offset, SEEK_SET);
                   2919:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2920:                                        memset(path, 0, sizeof(path));
                   2921:                                        fread(path, buffer[9], 1, fp);
                   2922:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2923:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2924:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2925:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2926:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2927:                                } else {
                   2928:                                        // read pe header of msdos.exe
                   2929:                                        UINT8 header[0x400];
                   2930:                                        fseek(fp, 0, SEEK_SET);
                   2931:                                        fread(header, sizeof(header), 1, fp);
                   2932:                                        
                   2933:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2934:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2935:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2936:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2937:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2938:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2939:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2940:                                        
                   2941:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2942:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2943:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2944:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2945:                                        if(dwExtraLastSectionBytes != 0) {
                   2946:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2947:                                                dwLastSectionSize += dwRemain;
                   2948:                                        }
                   2949:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2950:                                        
                   2951:                                        // store msdos.exe
                   2952:                                        fseek(fp, 0, SEEK_SET);
                   2953:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2954:                                                if((data = fgetc(fp)) != EOF) {
                   2955:                                                        fputc(data, fo);
                   2956:                                                } else {
                   2957:                                                        // we should not reach here :-(
                   2958:                                                        fputc(0, fo);
                   2959:                                                }
                   2960:                                        }
                   2961:                                        
                   2962:                                        // store options
                   2963:                                        UINT8 flags = 0;
                   2964:                                        if(stay_busy) {
                   2965:                                                flags |= 0x01;
                   2966:                                        }
                   2967:                                        if(no_windows) {
                   2968:                                                flags |= 0x02;
                   2969:                                        }
                   2970:                                        if(standard_env) {
                   2971:                                                flags |= 0x04;
                   2972:                                        }
                   2973:                                        if(ignore_illegal_insn) {
                   2974:                                                flags |= 0x08;
                   2975:                                        }
                   2976:                                        if(limit_max_memory) {
                   2977:                                                flags |= 0x10;
                   2978:                                        }
1.1.1.29  root     2979:                                        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     2980:                                                flags |= 0x20;
                   2981:                                        }
                   2982:                                        if(support_ems) {
                   2983:                                                flags |= 0x40;
                   2984:                                        }
1.1.1.30  root     2985: #ifdef SUPPORT_XMS
                   2986:                                        if(support_xms) {
                   2987:                                                flags |= 0x80;
                   2988:                                        }
                   2989: #endif
1.1.1.28  root     2990:                                        
                   2991:                                        fputc(flags, fo);
                   2992:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2993:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2994:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2995:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2996:                                        fputc(dos_major_version, fo);
                   2997:                                        fputc(dos_minor_version, fo);
                   2998:                                        fputc(win_major_version, fo);
                   2999:                                        fputc(win_minor_version, fo);
1.1.1.28  root     3000:                                        fputc((code_page >> 0) & 0xff, fo);
                   3001:                                        fputc((code_page >> 8) & 0xff, fo);
                   3002:                                        
                   3003:                                        // store command file info
1.1.1.60  root     3004:                                        GetFullPathNameA(argv[arg_offset + 1], MAX_PATH, full, &name);
1.1.1.28  root     3005:                                        int name_len = strlen(name);
                   3006:                                        fseek(fs, 0, SEEK_END);
                   3007:                                        long file_size = ftell(fs);
                   3008:                                        
                   3009:                                        fputc(name_len, fo);
                   3010:                                        fputc((file_size >>  0) & 0xff, fo);
                   3011:                                        fputc((file_size >>  8) & 0xff, fo);
                   3012:                                        fputc((file_size >> 16) & 0xff, fo);
                   3013:                                        fputc((file_size >> 24) & 0xff, fo);
                   3014:                                        fwrite(name, name_len, 1, fo);
                   3015:                                        
                   3016:                                        // store command file
                   3017:                                        fseek(fs, 0, SEEK_SET);
                   3018:                                        for(int i = 0; i < file_size; i++) {
                   3019:                                                if((data = fgetc(fs)) != EOF) {
                   3020:                                                        fputc(data, fo);
                   3021:                                                } else {
                   3022:                                                        // we should not reach here :-(
                   3023:                                                        fputc(0, fo);
                   3024:                                                }
                   3025:                                        }
                   3026:                                        
                   3027:                                        // store padding data and update pe header
1.1.1.29  root     3028:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   3029:                                        coffHeader->NumberOfSections++;
                   3030:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   3031:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   3032:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   3033:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   3034:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     3035:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   3036:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   3037:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     3038:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     3039:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   3040:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     3041:                                                        if(i < 2) {
                   3042:                                                                fputc(padding[i & 15], fo);
                   3043:                                                        } else {
                   3044:                                                                fputc(padding[(i - 2) & 15], fo);
                   3045:                                                        }
1.1.1.28  root     3046:                                                }
                   3047:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   3048:                                        }
                   3049:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   3050:                                        
                   3051:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   3052:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   3053:                                        if(dwExtraNewSectionBytes != 0) {
                   3054:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   3055:                                                dwNewSectionSize += dwRemain;
                   3056:                                        }
                   3057:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   3058:                                        
                   3059:                                        fseek(fo, 0, SEEK_SET);
                   3060:                                        fwrite(header, sizeof(header), 1, fo);
                   3061:                                        
                   3062:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   3063:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     3064:                                }
                   3065:                        }
                   3066:                        if(fp != NULL) {
                   3067:                                fclose(fp);
                   3068:                        }
                   3069:                        if(fs != NULL) {
                   3070:                                fclose(fs);
                   3071:                        }
                   3072:                        if(fo != NULL) {
                   3073:                                fclose(fo);
                   3074:                        }
                   3075:                }
                   3076: #ifdef _DEBUG
                   3077:                _CrtDumpMemoryLeaks();
                   3078: #endif
                   3079:                return(retval);
                   3080:        }
1.1       root     3081:        
1.1.1.62  root     3082:        is_winxp_or_later = is_greater_windows_version(5, 1, 0, 0);
1.1.1.54  root     3083:        is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14  root     3084:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   3085:        
1.1.1.23  root     3086:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     3087:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     3088:        CONSOLE_CURSOR_INFO ci;
1.1.1.61  root     3089:        UINT input_cp = GetConsoleCP();
                   3090:        UINT output_cp = GetConsoleOutputCP();
                   3091:        int multibyte_cp = _getmbcp();
1.1.1.23  root     3092:        
1.1.1.28  root     3093:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     3094:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.59  root     3095:        ci_old = ci_new = ci;
1.1.1.24  root     3096:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1.1.61  root     3097:        get_console_font_success = get_console_font_size(&font_width, &font_height);
1.1       root     3098:        
1.1.1.14  root     3099:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   3100:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   3101:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3102:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     3103:                }
                   3104:        }
1.1.1.28  root     3105:        if(get_console_info_success) {
1.1.1.12  root     3106:                scr_width = csbi.dwSize.X;
1.1.1.14  root     3107:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   3108:                
1.1.1.28  root     3109:                // v-text shadow buffer size must be lesser than 0x7fd0
                   3110:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     3111:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   3112:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   3113:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     3114:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     3115:                                scr_width = 80;
                   3116:                                scr_height = 25;
                   3117:                        }
1.1.1.28  root     3118:                        screen_size_changed = true;
1.1.1.14  root     3119:                }
1.1.1.12  root     3120:        } else {
                   3121:                // for a proof (not a console)
                   3122:                scr_width = 80;
                   3123:                scr_height = 25;
                   3124:        }
1.1.1.14  root     3125:        scr_buf_size.X = scr_width;
                   3126:        scr_buf_size.Y = scr_height;
                   3127:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   3128:        scr_top = csbi.srWindow.Top;
1.1       root     3129:        cursor_moved = false;
1.1.1.59  root     3130:        cursor_moved_by_crtc = false;
1.1       root     3131:        
1.1.1.54  root     3132:        SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
                   3133:        
1.1.1.35  root     3134: #ifdef USE_SERVICE_THREAD
                   3135:        InitializeCriticalSection(&input_crit_sect);
                   3136:        InitializeCriticalSection(&key_buf_crit_sect);
                   3137:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     3138:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     3139: #endif
1.1.1.50  root     3140:        
1.1.1.25  root     3141:        key_buf_char = new FIFO(256);
                   3142:        key_buf_scan = new FIFO(256);
1.1.1.57  root     3143:        key_buf_data = new FIFO(256);
1.1       root     3144:        
                   3145:        hardware_init();
                   3146:        
1.1.1.33  root     3147: #ifdef USE_DEBUGGER
                   3148:        debugger_init();
                   3149: #endif
                   3150:        
1.1.1.9   root     3151:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     3152:                retval = EXIT_FAILURE;
                   3153:        } else {
1.1.1.27  root     3154: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     3155:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   3156: #endif
                   3157:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   3158:                
1.1.1.28  root     3159:                if(screen_size_changed) {
1.1.1.24  root     3160:                        change_console_size(scr_width, scr_height);
                   3161:                }
1.1.1.8   root     3162:                TIMECAPS caps;
                   3163:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   3164:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     3165: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3166:                InitializeCriticalSection(&vram_crit_sect);
                   3167:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   3168: #endif
1.1.1.33  root     3169: #ifdef USE_DEBUGGER
                   3170:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   3171:                // wait until telnet client starts and connects to me
                   3172:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   3173:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   3174:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   3175:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   3176:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   3177:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   3178:                                Sleep(100);
                   3179:                        }
                   3180:                }
                   3181: #endif
1.1       root     3182:                hardware_run();
1.1.1.35  root     3183: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3184:                vram_flush();
                   3185:                DeleteCriticalSection(&vram_crit_sect);
                   3186: #endif
1.1.1.24  root     3187:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     3188:                
1.1.1.24  root     3189:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.61  root     3190:                hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   3191:                
                   3192:                // restore console settings
                   3193:                _setmbcp(multibyte_cp);
                   3194:                SetConsoleCP(input_cp);
                   3195:                SetConsoleOutputCP(multibyte_cp);
                   3196:                
1.1.1.28  root     3197:                if(get_console_info_success) {
1.1.1.12  root     3198:                        if(restore_console_on_exit) {
1.1.1.14  root     3199:                                // window can't be bigger than buffer,
                   3200:                                // buffer can't be smaller than window,
                   3201:                                // so make a tiny window,
                   3202:                                // set the required buffer,
                   3203:                                // then set the required window
1.1.1.61  root     3204:                                CONSOLE_SCREEN_BUFFER_INFO cur_csbi;
1.1.1.14  root     3205:                                SMALL_RECT rect;
1.1.1.61  root     3206:                                GetConsoleScreenBufferInfo(hStdout, &cur_csbi);
                   3207:                                int min_width  = min(cur_csbi.srWindow.Right - cur_csbi.srWindow.Left + 1, csbi.srWindow.Right - csbi.srWindow.Left + 1);
                   3208:                                int min_height = min(cur_csbi.srWindow.Bottom - cur_csbi.srWindow.Top + 1, csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
                   3209:                                
                   3210:                                SET_RECT(rect, 0, cur_csbi.srWindow.Top, min_width - 1, cur_csbi.srWindow.Top + min_height - 1);
1.1.1.14  root     3211:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     3212:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     3213:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.61  root     3214:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3215:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3216:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3217:                                }
                   3218:                        }
                   3219:                }
                   3220:                if(get_console_font_success) {
                   3221:                        set_console_font_size(font_width, font_height);
                   3222:                }
                   3223:                if(get_console_info_success) {
                   3224:                        if(restore_console_on_exit) {
                   3225:                                SMALL_RECT rect;
                   3226:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
                   3227:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
                   3228:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3229:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3230:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3231:                                }
1.1.1.12  root     3232:                        }
1.1.1.14  root     3233:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   3234:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     3235:                }
1.1.1.64! root     3236:                if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
        !          3237:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
        !          3238:                } else {
        !          3239:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
        !          3240:                }
1.1.1.24  root     3241:                
1.1       root     3242:                msdos_finish();
1.1.1.14  root     3243:                
                   3244:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     3245:        }
1.1.1.35  root     3246:        if(temp_file_created) {
1.1.1.60  root     3247:                DeleteFileA(temp_file_path);
1.1.1.35  root     3248:                temp_file_created = false;
                   3249:        }
1.1.1.10  root     3250:        hardware_finish();
                   3251:        
1.1.1.28  root     3252:        if(key_buf_char != NULL) {
                   3253:                key_buf_char->release();
                   3254:                delete key_buf_char;
                   3255:                key_buf_char = NULL;
                   3256:        }
                   3257:        if(key_buf_scan != NULL) {
                   3258:                key_buf_scan->release();
                   3259:                delete key_buf_scan;
                   3260:                key_buf_scan = NULL;
                   3261:        }
1.1.1.57  root     3262:        if(key_buf_data != NULL) {
                   3263:                key_buf_data->release();
                   3264:                delete key_buf_data;
                   3265:                key_buf_data = NULL;
                   3266:        }
1.1.1.35  root     3267: #ifdef USE_SERVICE_THREAD
                   3268:        DeleteCriticalSection(&input_crit_sect);
                   3269:        DeleteCriticalSection(&key_buf_crit_sect);
                   3270:        DeleteCriticalSection(&putch_crit_sect);
                   3271: #endif
1.1.1.20  root     3272: #ifdef _DEBUG
                   3273:        _CrtDumpMemoryLeaks();
                   3274: #endif
1.1       root     3275:        return(retval);
                   3276: }
                   3277: 
1.1.1.20  root     3278: /* ----------------------------------------------------------------------------
                   3279:        console
                   3280: ---------------------------------------------------------------------------- */
                   3281: 
1.1.1.14  root     3282: void change_console_size(int width, int height)
1.1.1.12  root     3283: {
1.1.1.23  root     3284:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3285:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3286:        SMALL_RECT rect;
                   3287:        COORD co;
                   3288:        
                   3289:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     3290:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   3291:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
1.1.1.60  root     3292:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
1.1.1.14  root     3293:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3294:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3295:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   3296:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
1.1.1.60  root     3297:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3298:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3299:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     3300:                }
                   3301:        }
1.1.1.14  root     3302:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     3303:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     3304:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     3305:                SetConsoleCursorPosition(hStdout, co);
                   3306:                cursor_moved = true;
1.1.1.59  root     3307:                cursor_moved_by_crtc = false;
1.1.1.12  root     3308:        }
1.1.1.14  root     3309:        
                   3310:        // window can't be bigger than buffer,
                   3311:        // buffer can't be smaller than window,
                   3312:        // so make a tiny window,
                   3313:        // set the required buffer,
                   3314:        // then set the required window
1.1.1.61  root     3315:        int min_width  = min(csbi.srWindow.Right - csbi.srWindow.Left + 1, width);
                   3316:        int min_height = min(csbi.srWindow.Bottom - csbi.srWindow.Top + 1, height);
                   3317:        
                   3318:        SET_RECT(rect, 0, csbi.srWindow.Top, min_width - 1, csbi.srWindow.Top + min_height - 1);
1.1.1.12  root     3319:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     3320:        co.X = width;
                   3321:        co.Y = height;
1.1.1.12  root     3322:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     3323:        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.61  root     3324:        if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3325:                SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3326:                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3327:        }
1.1.1.14  root     3328:        
                   3329:        scr_width = scr_buf_size.X = width;
                   3330:        scr_height = scr_buf_size.Y = height;
                   3331:        scr_top = 0;
                   3332:        
                   3333:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3334:        
                   3335:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     3336:        text_vram_end_address = text_vram_top_address + regen;
                   3337:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   3338:        
1.1.1.14  root     3339:        if(regen > 0x4000) {
                   3340:                regen = 0x8000;
                   3341:                vram_pages = 1;
                   3342:        } else if(regen > 0x2000) {
                   3343:                regen = 0x4000;
                   3344:                vram_pages = 2;
                   3345:        } else if(regen > 0x1000) {
                   3346:                regen = 0x2000;
                   3347:                vram_pages = 4;
                   3348:        } else {
                   3349:                regen = 0x1000;
                   3350:                vram_pages = 8;
                   3351:        }
1.1.1.15  root     3352:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3353:        *(UINT16 *)(mem + 0x44c) = regen;
                   3354:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3355:        
1.1.1.24  root     3356:        mouse.min_position.x = 0;
                   3357:        mouse.min_position.y = 0;
1.1.1.34  root     3358:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3359:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3360:        
1.1.1.15  root     3361:        restore_console_on_exit = true;
1.1.1.14  root     3362: }
                   3363: 
                   3364: void clear_scr_buffer(WORD attr)
                   3365: {
                   3366:        for(int y = 0; y < scr_height; y++) {
                   3367:                for(int x = 0; x < scr_width; x++) {
                   3368:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3369:                        SCR_BUF(y,x).Attributes = attr;
                   3370:                }
                   3371:        }
1.1.1.12  root     3372: }
                   3373: 
1.1.1.24  root     3374: bool update_console_input()
1.1       root     3375: {
1.1.1.35  root     3376: #ifdef USE_SERVICE_THREAD
                   3377:        EnterCriticalSection(&input_crit_sect);
                   3378: #endif
1.1.1.23  root     3379:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3380:        DWORD dwNumberOfEvents = 0;
1.1       root     3381:        DWORD dwRead;
                   3382:        INPUT_RECORD ir[16];
1.1.1.24  root     3383:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3384:        bool result = false;
1.1       root     3385:        
1.1.1.8   root     3386:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3387:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3388:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3389:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.59  root     3390:                                        if(ir[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
                   3391:                                                if(mouse.hidden == 0 || (mouse.call_addr_ps2.dw && mouse.enabled_ps2)) {
                   3392:                                                        // NOTE: if restore_console_on_exit, console is not scrolled
                   3393:                                                        if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3394:                                                                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3395:                                                        }
                   3396:                                                        // FIXME: character size is always 8x8 ???
                   3397:                                                        int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3398:                                                        int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3399:                                                        
                   3400:                                                        if(mouse.position.x != x || mouse.position.y != y) {
                   3401:                                                                mouse.position.x = x;
                   3402:                                                                mouse.position.y = y;
                   3403:                                                                mouse.status |= 1;
                   3404:                                                                mouse.status_alt |= 1;
                   3405:                                                        }
1.1.1.34  root     3406:                                                }
1.1.1.59  root     3407:                                        } else if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3408:                                                for(int j = 0; j < MAX_MOUSE_BUTTONS; j++) {
1.1.1.34  root     3409:                                                        static const DWORD bits[] = {
                   3410:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3411:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3412:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3413:                                                        };
1.1.1.59  root     3414:                                                        bool prev_status = mouse.buttons[j].status;
                   3415:                                                        mouse.buttons[j].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[j]) != 0);
1.1.1.34  root     3416:                                                        
1.1.1.59  root     3417:                                                        if(!prev_status && mouse.buttons[j].status) {
                   3418:                                                                mouse.buttons[j].pressed_times++;
                   3419:                                                                mouse.buttons[j].pressed_position.x = mouse.position.x;
                   3420:                                                                mouse.buttons[j].pressed_position.y = mouse.position.x;
                   3421:                                                                if(j < 2) {
                   3422:                                                                        mouse.status_alt |= 2 << (j * 2);
1.1.1.43  root     3423:                                                                }
1.1.1.59  root     3424:                                                                mouse.status |= 2 << (j * 2);
                   3425:                                                        } else if(prev_status && !mouse.buttons[j].status) {
                   3426:                                                                mouse.buttons[j].released_times++;
                   3427:                                                                mouse.buttons[j].released_position.x = mouse.position.x;
                   3428:                                                                mouse.buttons[j].released_position.y = mouse.position.x;
                   3429:                                                                if(j < 2) {
                   3430:                                                                        mouse.status_alt |= 4 << (j * 2);
1.1.1.43  root     3431:                                                                }
1.1.1.59  root     3432:                                                                mouse.status |= 4 << (j * 2);
1.1.1.14  root     3433:                                                        }
                   3434:                                                }
                   3435:                                        }
1.1.1.24  root     3436:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3437:                                        // update keyboard flags in bios data area
1.1.1.35  root     3438:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3439:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3440:                                        } else {
1.1.1.35  root     3441:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3442:                                        }
1.1.1.35  root     3443:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3444:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3445:                                        } else {
1.1.1.35  root     3446:                                                mem[0x417] &= ~0x20;
                   3447:                                        }
                   3448:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3449:                                                mem[0x417] |= 0x10;
                   3450:                                        } else {
                   3451:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3452:                                        }
                   3453:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3454:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3455:                                                        mouse.status_alt |= 0x80;
                   3456:                                                }
1.1.1.33  root     3457:                                                mem[0x417] |= 0x08;
                   3458:                                        } else {
                   3459:                                                mem[0x417] &= ~0x08;
                   3460:                                        }
1.1.1.35  root     3461:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3462:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3463:                                                        mouse.status_alt |= 0x40;
                   3464:                                                }
1.1.1.35  root     3465:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3466:                                        } else {
1.1.1.35  root     3467:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3468:                                        }
                   3469:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3470:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3471:                                                        mouse.status_alt |= 0x20;
                   3472:                                                }
1.1.1.33  root     3473:                                                if(!(mem[0x417] & 0x03)) {
                   3474:                                                        mem[0x417] |= 0x02; // left shift
                   3475:                                                }
                   3476:                                        } else {
                   3477:                                                mem[0x417] &= ~0x03;
                   3478:                                        }
1.1.1.35  root     3479:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3480:                                                mem[0x418] |= 0x02;
                   3481:                                        } else {
                   3482:                                                mem[0x418] &= ~0x02;
                   3483:                                        }
                   3484:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3485:                                                mem[0x418] |= 0x01;
                   3486:                                        } else {
                   3487:                                                mem[0x418] &= ~0x01;
                   3488:                                        }
1.1.1.33  root     3489:                                        
1.1.1.28  root     3490:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.57  root     3491: //                                     kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
                   3492: //                                     kbd_status |= 1;
                   3493:                                        UINT8 tmp_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3494:                                        
                   3495:                                        // update dos key buffer
                   3496:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3497:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3498:                                        UINT8 scn_old = scn;
1.1.1.33  root     3499:                                        
                   3500:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3501:                                                // make
1.1.1.57  root     3502:                                                tmp_data &= 0x7f;
1.1.1.24  root     3503:                                                
1.1.1.33  root     3504:                                                if(chr == 0x00) {
1.1.1.24  root     3505:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3506:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3507:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3508:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3509:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3510:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3511:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3512:                                                                } else if(scn == 0x35) {
                   3513:                                                                        scn = 0xa4;             // keypad /
                   3514:                                                                }
                   3515:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3516:                                                                if(scn == 0x07) {
                   3517:                                                                        chr = 0x1e;     // Ctrl+^
                   3518:                                                                } else if(scn == 0x0c) {
                   3519:                                                                        chr = 0x1f;     // Ctrl+_
                   3520:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3521:                                                                        static const UINT8 ctrl_map[] = {
                   3522:                                                                                0x95,   // keypad /
                   3523:                                                                                0,
                   3524:                                                                                0x96,   // keypad *
                   3525:                                                                                0, 0, 0,
                   3526:                                                                                0x5e,   // F1
                   3527:                                                                                0x5f,   // F2
                   3528:                                                                                0x60,   // F3
                   3529:                                                                                0x61,   // F4
                   3530:                                                                                0x62,   // F5
                   3531:                                                                                0x63,   // F6
                   3532:                                                                                0x64,   // F7
                   3533:                                                                                0x65,   // F8
                   3534:                                                                                0x66,   // F9
                   3535:                                                                                0x67,   // F10
                   3536:                                                                                0,
                   3537:                                                                                0,
                   3538:                                                                                0x77,   // Home
                   3539:                                                                                0x8d,   // Up
                   3540:                                                                                0x84,   // PgUp
                   3541:                                                                                0x8e,   // keypad -
                   3542:                                                                                0x73,   // Left
                   3543:                                                                                0x8f,   // keypad center
                   3544:                                                                                0x74,   // Right
                   3545:                                                                                0x90,   // keyapd +
                   3546:                                                                                0x75,   // End
                   3547:                                                                                0x91,   // Down
                   3548:                                                                                0x76,   // PgDn
                   3549:                                                                                0x92,   // Insert
                   3550:                                                                                0x93,   // Delete
                   3551:                                                                                0, 0, 0,
                   3552:                                                                                0x89,   // F11
                   3553:                                                                                0x8a,   // F12
                   3554:                                                                        };
                   3555:                                                                        scn = ctrl_map[scn - 0x35];
                   3556:                                                                }
                   3557:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3558:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3559:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3560:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3561:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3562:                                                                }
                   3563:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3564:                                                                scn += 0x85 - 0x57;
                   3565:                                                        }
                   3566:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3567:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3568:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3569: #ifdef USE_SERVICE_THREAD
                   3570:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3571: #endif
1.1.1.32  root     3572:                                                                        if(chr == 0) {
1.1.1.51  root     3573:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3574:                                                                        }
1.1.1.51  root     3575:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3576: #ifdef USE_SERVICE_THREAD
                   3577:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3578: #endif
1.1.1.24  root     3579:                                                                }
                   3580:                                                        }
                   3581:                                                } else {
                   3582:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3583:                                                                chr = 0;
                   3584:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3585:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3586:                                                                }
                   3587:                                                        }
1.1.1.32  root     3588:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3589: #ifdef USE_SERVICE_THREAD
                   3590:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3591: #endif
1.1.1.51  root     3592:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3593: #ifdef USE_SERVICE_THREAD
                   3594:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3595: #endif
1.1.1.32  root     3596:                                                        }
1.1.1.24  root     3597:                                                }
1.1.1.57  root     3598:                                        } else {
                   3599:                                                if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3600:                                                        // ctrl-break, ctrl-c
                   3601:                                                        if(scn == 0x46) {
                   3602:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3603: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3604:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3605: #endif
1.1.1.57  root     3606:                                                                        pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3607: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3608:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3609: #endif
1.1.1.57  root     3610:                                                                }
                   3611:                                                                ctrl_break_pressed = true;
                   3612:                                                                mem[0x471] = 0x80;
                   3613:                                                                raise_int_1bh = true;
                   3614:                                                        } else {
                   3615:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3616: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3617:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3618: #endif
1.1.1.57  root     3619:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3620: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3621:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3622: #endif
1.1.1.57  root     3623:                                                                }
                   3624:                                                                ctrl_c_pressed = (scn == 0x2e);
1.1.1.33  root     3625:                                                        }
                   3626:                                                }
                   3627:                                                // break
1.1.1.57  root     3628:                                                tmp_data |= 0x80;
                   3629:                                        }
                   3630:                                        if(!(kbd_status & 1)) {
                   3631:                                                kbd_data = tmp_data;
                   3632:                                                kbd_status |= 1;
                   3633:                                        } else {
                   3634:                                                if(key_buf_data != NULL) {
                   3635: #ifdef USE_SERVICE_THREAD
                   3636:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3637: #endif
                   3638:                                                        key_buf_data->write(tmp_data);
                   3639: #ifdef USE_SERVICE_THREAD
                   3640:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3641: #endif
                   3642:                                                }
1.1       root     3643:                                        }
1.1.1.24  root     3644:                                        result = key_changed = true;
1.1.1.36  root     3645:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3646:                                        cursor_moved = true;
1.1       root     3647:                                }
                   3648:                        }
                   3649:                }
                   3650:        }
1.1.1.35  root     3651: #ifdef USE_SERVICE_THREAD
                   3652:        LeaveCriticalSection(&input_crit_sect);
                   3653: #endif
1.1.1.24  root     3654:        return(result);
1.1.1.8   root     3655: }
                   3656: 
1.1.1.14  root     3657: bool update_key_buffer()
1.1.1.8   root     3658: {
1.1.1.35  root     3659:        if(update_console_input()) {
                   3660:                return(true);
                   3661:        }
                   3662:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3663: #ifdef USE_SERVICE_THREAD
                   3664:                EnterCriticalSection(&key_buf_crit_sect);
                   3665: #endif
1.1.1.55  root     3666:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     3667: #ifdef USE_SERVICE_THREAD
                   3668:                LeaveCriticalSection(&key_buf_crit_sect);
                   3669: #endif
                   3670:                if(!empty) return(true);
                   3671:        }
                   3672:        return(false);
1.1.1.8   root     3673: }
                   3674: 
1.1.1.20  root     3675: /* ----------------------------------------------------------------------------
                   3676:        MS-DOS virtual machine
                   3677: ---------------------------------------------------------------------------- */
                   3678: 
1.1.1.32  root     3679: static const struct {
1.1.1.33  root     3680:        char *name;
                   3681:        DWORD lcid;
                   3682:        char *std;
                   3683:        char *dlt;
                   3684: } tz_table[] = {
                   3685:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3686: //     0       GMT             Greenwich Mean Time             GMT0
                   3687:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3688:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3689:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3690:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3691: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3692:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3693:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3694: //     3       BST             Brazil Standard Time            BST3
                   3695:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3696:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3697:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3698: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3699:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3700: //     3       GST             Greenland Standard Time         GST3
                   3701:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3702: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3703:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3704: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3705:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3706: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3707:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3708:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3709: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3710:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3711:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3712:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3713: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3714:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3715: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3716:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3717: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3718:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3719: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3720:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3721:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3722:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3723: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3724:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3725: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3726:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3727:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3728:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3729: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3730:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3731:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3732: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3733: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3734:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3735: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3736:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3737:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3738: //     11      SST             Samoa Standard Time             SST11
                   3739:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3740: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3741:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3742: //     -10     GST             Guam Standard Time              GST-10
                   3743:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3744: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3745:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3746:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3747:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3748: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3749:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3750:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3751: //     -9      JST             Japan Standard Time             JST-9
                   3752:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3753: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3754:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3755:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3756: //     -8      HKT             Hong Kong Time                  HKT-8
                   3757:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3758: //     -8      CCT             China Coast Time                CCT-8
                   3759:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3760:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3761: //     -8      SST             Singapore Standard Time         SST-8
                   3762:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3763: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3764:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3765:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3766: //     -7:30   JT              Java Standard Time              JST-7:30
                   3767: //     -7      NST             North Sumatra Time              NST-7
                   3768:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3769: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3770:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3771: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3772:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3773: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3774:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3775:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3776: //     -2      EET             Eastern Europe Time             EET-2
                   3777:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3778:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3779:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3780:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3781: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3782:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3783: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3784: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3785: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3786: //     -1      CET     CES     Central European Time           CET-1CES
                   3787:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3788:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3789:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3790:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3791: //     -1      WAT             West African Time               WAT-1
                   3792:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3793:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3794: //     0       UTC             Universal Coordinated Time      UTC0
                   3795:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3796:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3797:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3798:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3799:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3800:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3801: };
                   3802: 
1.1.1.53  root     3803: // FIXME: consider to build on non-Japanese environment :-(
                   3804: // message_japanese string must be in shift-jis
                   3805: 
1.1.1.33  root     3806: static const struct {
1.1.1.32  root     3807:        UINT16 code;
                   3808:        char *message_english;
                   3809:        char *message_japanese;
                   3810: } standard_error_table[] = {
                   3811:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3812:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3813:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3814:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3815:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3816:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3817:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3818:        {0x08,  "Insufficient memory", "������������܂���."},
                   3819:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3820:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3821:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3822:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3823:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3824:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3825:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3826:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3827:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3828:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3829:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3830:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3831:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3832:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3833:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3834:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3835:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3836:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3837:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3838:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3839:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3840:        {0x1F,  "General failure", "�G���[�ł�."},
                   3841:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3842:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3843:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3844:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3845:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3846:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3847:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3848:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3849: /*
                   3850:        {0x32,  "Network request not supported", NULL},
                   3851:        {0x33,  "Remote computer not listening", NULL},
                   3852:        {0x34,  "Duplicate name on network", NULL},
                   3853:        {0x35,  "Network name not found", NULL},
                   3854:        {0x36,  "Network busy", NULL},
                   3855:        {0x37,  "Network device no longer exists", NULL},
                   3856:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3857:        {0x39,  "Network adapter hardware error", NULL},
                   3858:        {0x3A,  "Incorrect response from network", NULL},
                   3859:        {0x3B,  "Unexpected network error", NULL},
                   3860:        {0x3C,  "Incompatible remote adapter", NULL},
                   3861:        {0x3D,  "Print queue full", NULL},
                   3862:        {0x3E,  "Queue not full", NULL},
                   3863:        {0x3F,  "Not enough space to print file", NULL},
                   3864:        {0x40,  "Network name was deleted", NULL},
                   3865:        {0x41,  "Network: Access denied", NULL},
                   3866:        {0x42,  "Network device type incorrect", NULL},
                   3867:        {0x43,  "Network name not found", NULL},
                   3868:        {0x44,  "Network name limit exceeded", NULL},
                   3869:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3870:        {0x46,  "Temporarily paused", NULL},
                   3871:        {0x47,  "Network request not accepted", NULL},
                   3872:        {0x48,  "Network print/disk redirection paused", NULL},
                   3873:        {0x49,  "Network software not installed", NULL},
                   3874:        {0x4A,  "Unexpected adapter close", NULL},
                   3875: */
                   3876:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3877:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3878:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3879:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3880:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3881:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3882:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3883:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3884:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3885:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53  root     3886: #ifdef SUPPORT_MSCDEX
1.1.1.32  root     3887:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3888:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3889:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3890:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3891:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
1.1.1.53  root     3892: #endif
1.1.1.32  root     3893:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3894:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3895:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3896:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3897:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3898:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3899: };
                   3900: 
                   3901: static const struct {
                   3902:        UINT16 code;
                   3903:        char *message_english;
                   3904:        char *message_japanese;
                   3905: } param_error_table[] = {
                   3906:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3907:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3908:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3909:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3910:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3911:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3912:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3913:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3914:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3915:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3916:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3917: };
                   3918: 
                   3919: static const struct {
                   3920:        UINT16 code;
                   3921:        char *message_english;
                   3922:        char *message_japanese;
                   3923: } critical_error_table[] = {
                   3924:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3925:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3926:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3927:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3928:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3929:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3930:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3931:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3932:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3933:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3934:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3935:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3936:        {0x0C,  "General failure", "�G���[�ł�."},
                   3937:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3938:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3939:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3940:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3941:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3942:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3943:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3944:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3945:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3946: };
                   3947: 
1.1.1.20  root     3948: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3949: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3950: void msdos_putch(UINT8 data);
1.1.1.50  root     3951: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3952: #ifdef USE_SERVICE_THREAD
                   3953: void msdos_putch_tmp(UINT8 data);
                   3954: #endif
1.1.1.45  root     3955: const char *msdos_short_path(const char *path);
1.1.1.44  root     3956: bool msdos_is_valid_drive(int drv);
                   3957: bool msdos_is_removable_drive(int drv);
                   3958: bool msdos_is_cdrom_drive(int drv);
                   3959: bool msdos_is_remote_drive(int drv);
                   3960: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3961: 
1.1       root     3962: // process info
                   3963: 
                   3964: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3965: {
                   3966:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3967:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3968:                        memset(&process[i], 0, sizeof(process_t));
                   3969:                        process[i].psp = psp_seg;
                   3970:                        return(&process[i]);
                   3971:                }
                   3972:        }
                   3973:        fatalerror("too many processes\n");
                   3974:        return(NULL);
                   3975: }
                   3976: 
1.1.1.52  root     3977: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3978: {
                   3979:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3980:                if(process[i].psp == psp_seg) {
                   3981:                        return(&process[i]);
                   3982:                }
                   3983:        }
1.1.1.33  root     3984:        if(show_error) {
                   3985:                fatalerror("invalid psp address\n");
                   3986:        }
1.1       root     3987:        return(NULL);
                   3988: }
                   3989: 
1.1.1.23  root     3990: void msdos_sda_update(int psp_seg)
                   3991: {
                   3992:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3993:        
                   3994:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3995:                if(process[i].psp == psp_seg) {
                   3996:                        sda->switchar = process[i].switchar;
                   3997:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3998:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3999:                        sda->current_psp = process[i].psp;
                   4000:                        break;
                   4001:                }
                   4002:        }
                   4003:        sda->malloc_strategy = malloc_strategy;
                   4004:        sda->return_code = retval;
                   4005:        sda->current_drive = _getdrive();
                   4006: }
                   4007: 
1.1.1.13  root     4008: // dta info
                   4009: 
                   4010: void msdos_dta_info_init()
                   4011: {
1.1.1.14  root     4012:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     4013:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   4014:        }
                   4015: }
                   4016: 
                   4017: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   4018: {
                   4019:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     4020:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   4021:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   4022:                        if(free_dta == NULL) {
1.1.1.13  root     4023:                                free_dta = &dtalist[i];
                   4024:                        }
1.1.1.14  root     4025:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     4026:                        return(&dtalist[i]);
                   4027:                }
                   4028:        }
1.1.1.14  root     4029:        if(free_dta) {
1.1.1.13  root     4030:                free_dta->psp = psp_seg;
                   4031:                free_dta->dta = dta_laddr;
                   4032:                return(free_dta);
                   4033:        }
                   4034:        fatalerror("too many dta\n");
                   4035:        return(NULL);
                   4036: }
                   4037: 
                   4038: void msdos_dta_info_free(UINT16 psp_seg)
                   4039: {
1.1.1.14  root     4040:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   4041:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     4042:                        FindClose(dtalist[i].find_handle);
                   4043:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   4044:                }
                   4045:        }
                   4046: }
                   4047: 
1.1       root     4048: void msdos_cds_update(int drv)
                   4049: {
1.1.1.44  root     4050:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     4051:        
1.1.1.44  root     4052:        memset(cds, 0, 88);
                   4053:        
                   4054:        if(msdos_is_valid_drive(drv)) {
                   4055:                char path[MAX_PATH];
                   4056:                if(msdos_is_remote_drive(drv)) {
                   4057:                        cds->drive_attrib = 0xc000;     // network drive
                   4058:                } else if(msdos_is_subst_drive(drv)) {
                   4059:                        cds->drive_attrib = 0x5000;     // subst drive
                   4060:                } else {
                   4061:                        cds->drive_attrib = 0x4000;     // physical drive
                   4062:                }
                   4063:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   4064:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   4065:                }
                   4066:        }
                   4067:        if(cds->path_name[0] == '\0') {
                   4068:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   4069:        }
                   4070:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   4071:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   4072:        cds->word_1 = cds->word_2 = 0xffff;
                   4073:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   4074:        cds->bs_offset = 2;
                   4075: }
                   4076: 
1.1.1.45  root     4077: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     4078: {
                   4079:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   4080:        
                   4081:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     4082: }
                   4083: 
1.1.1.17  root     4084: // nls information tables
                   4085: 
                   4086: // uppercase table (func 6502h)
                   4087: void msdos_upper_table_update()
                   4088: {
                   4089:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4090:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4091:                UINT8 c[4];
1.1.1.33  root     4092:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4093:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     4094:                c[0] = 0x80 + i;
                   4095:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   4096:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4097:        }
                   4098: }
                   4099: 
1.1.1.23  root     4100: // lowercase table (func 6503h)
                   4101: void msdos_lower_table_update()
                   4102: {
                   4103:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   4104:        for(unsigned i = 0; i < 0x80; ++i) {
                   4105:                UINT8 c[4];
1.1.1.33  root     4106:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4107:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     4108:                c[0] = 0x80 + i;
                   4109:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   4110:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4111:        }
                   4112: }
                   4113: 
1.1.1.17  root     4114: // filename uppercase table (func 6504h)
                   4115: void msdos_filename_upper_table_init()
                   4116: {
                   4117:        // depended on (file)system, not on active codepage
                   4118:        // temporary solution: just filling data
                   4119:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4120:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4121:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   4122:        }
                   4123: }
                   4124: 
                   4125: // filaname terminator table (func 6505h)
                   4126: void msdos_filename_terminator_table_init()
                   4127: {
                   4128:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   4129:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   4130:        
                   4131:        data[2] = 1;            // marker? (permissible character value)
                   4132:        data[3] = 0x00;         // 00h...FFh
                   4133:        data[4] = 0xff;
                   4134:        data[5] = 0;            // marker? (excluded character)
                   4135:        data[6] = 0x00;         // 00h...20h
                   4136:        data[7] = 0x20;
                   4137:        data[8] = 2;            // marker? (illegal characters for filename)
                   4138:        data[9] = (UINT8)strlen(illegal_chars);
                   4139:        memcpy(data + 10, illegal_chars, data[9]);
                   4140:        
                   4141:        // total length
                   4142:        *(UINT16 *)data = (10 - 2) + data[9];
                   4143: }
                   4144: 
                   4145: // collating table (func 6506h)
                   4146: void msdos_collating_table_update()
                   4147: {
                   4148:        // temporary solution: just filling data
                   4149:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     4150:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     4151:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   4152:        }
                   4153: }
                   4154: 
1.1       root     4155: // dbcs
                   4156: 
                   4157: void msdos_dbcs_table_update()
                   4158: {
                   4159:        UINT8 dbcs_data[DBCS_SIZE];
                   4160:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   4161:        
                   4162:        CPINFO info;
                   4163:        GetCPInfo(active_code_page, &info);
                   4164:        
                   4165:        if(info.MaxCharSize != 1) {
                   4166:                for(int i = 0;; i += 2) {
                   4167:                        UINT8 lo = info.LeadByte[i + 0];
                   4168:                        UINT8 hi = info.LeadByte[i + 1];
                   4169:                        dbcs_data[2 + i + 0] = lo;
                   4170:                        dbcs_data[2 + i + 1] = hi;
                   4171:                        if(lo == 0 && hi == 0) {
                   4172:                                dbcs_data[0] = i + 2;
                   4173:                                break;
                   4174:                        }
                   4175:                }
                   4176:        } else {
                   4177:                dbcs_data[0] = 2;       // ???
                   4178:        }
                   4179:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   4180: }
                   4181: 
1.1.1.17  root     4182: void msdos_dbcs_table_finish()
                   4183: {
1.1.1.32  root     4184:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     4185:                _setmbcp(system_code_page);
                   4186:        }
1.1.1.32  root     4187:        if(console_code_page != GetConsoleCP()) {
                   4188:                SetConsoleCP(console_code_page);
                   4189:                SetConsoleOutputCP(console_code_page);
                   4190:        }
1.1.1.17  root     4191: }
                   4192: 
                   4193: void msdos_nls_tables_init()
1.1       root     4194: {
1.1.1.32  root     4195:        active_code_page = console_code_page = GetConsoleCP();
                   4196:        system_code_page = _getmbcp();
                   4197:        
                   4198:        if(active_code_page != system_code_page) {
                   4199:                if(_setmbcp(active_code_page) != 0) {
                   4200:                        active_code_page = system_code_page;
                   4201:                }
                   4202:        }
                   4203:        
1.1.1.17  root     4204:        msdos_upper_table_update();
1.1.1.23  root     4205:        msdos_lower_table_update();
1.1.1.17  root     4206:        msdos_filename_terminator_table_init();
                   4207:        msdos_filename_upper_table_init();
                   4208:        msdos_collating_table_update();
1.1       root     4209:        msdos_dbcs_table_update();
                   4210: }
                   4211: 
1.1.1.17  root     4212: void msdos_nls_tables_update()
1.1       root     4213: {
1.1.1.17  root     4214:        msdos_dbcs_table_update();
                   4215:        msdos_upper_table_update();
1.1.1.23  root     4216:        msdos_lower_table_update();
                   4217: //     msdos_collating_table_update();
1.1       root     4218: }
                   4219: 
                   4220: int msdos_lead_byte_check(UINT8 code)
                   4221: {
                   4222:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   4223:        
                   4224:        for(int i = 0;; i += 2) {
                   4225:                UINT8 lo = dbcs_table[i + 0];
                   4226:                UINT8 hi = dbcs_table[i + 1];
                   4227:                if(lo == 0 && hi == 0) {
                   4228:                        break;
                   4229:                }
                   4230:                if(lo <= code && code <= hi) {
                   4231:                        return(1);
                   4232:                }
                   4233:        }
                   4234:        return(0);
                   4235: }
                   4236: 
1.1.1.20  root     4237: int msdos_ctrl_code_check(UINT8 code)
                   4238: {
1.1.1.22  root     4239:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     4240: }
                   4241: 
1.1.1.36  root     4242: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   4243: {
                   4244:        int is_kanji_1st = 0;
                   4245:        int is_kanji_2nd = 0;
                   4246:        
                   4247:        for(int p = 0;; p++) {
                   4248:                if(is_kanji_1st) {
                   4249:                        is_kanji_1st = 0;
                   4250:                        is_kanji_2nd = 1;
                   4251:                } else if(msdos_lead_byte_check(buf[p])) {
                   4252:                        is_kanji_1st = 1;
                   4253:                }
                   4254:                if(p == n) {
                   4255:                        return(is_kanji_2nd);
                   4256:                }
                   4257:                is_kanji_2nd = 0;
                   4258:        }
                   4259: }
                   4260: 
1.1       root     4261: // file control
                   4262: 
1.1.1.45  root     4263: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     4264: {
                   4265:        static char tmp[MAX_PATH];
                   4266:        
                   4267:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     4268:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     4269:                memcpy(tmp, path + 1, strlen(path) - 2);
                   4270:        } else {
                   4271:                strcpy(tmp, path);
                   4272:        }
                   4273:        return(tmp);
                   4274: }
                   4275: 
1.1.1.45  root     4276: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     4277: {
                   4278:        static char tmp[MAX_PATH];
                   4279:        
                   4280:        strcpy(tmp, path);
1.1.1.45  root     4281:        
                   4282:        // for example "C:\" case, the end separator should not be removed
                   4283:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   4284:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     4285:        }
                   4286:        return(tmp);
                   4287: }
                   4288: 
1.1.1.45  root     4289: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     4290: {
                   4291:        static char tmp[MAX_PATH];
1.1.1.45  root     4292:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     4293:        
                   4294:        if(strlen(tmp_dir) == 0) {
                   4295:                strcpy(tmp, file);
                   4296:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   4297:                sprintf(tmp, "%s%s", tmp_dir, file);
                   4298:        } else {
                   4299:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   4300:        }
                   4301:        return(tmp);
                   4302: }
                   4303: 
1.1.1.45  root     4304: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     4305: {
                   4306:        static char tmp[MAX_PATH];
                   4307:        
                   4308:        if(lfn) {
                   4309:                strcpy(tmp, path);
                   4310:        } else {
                   4311:                // remove space in the path
1.1.1.45  root     4312:                const char *src = path;
                   4313:                char *dst = tmp;
1.1       root     4314:                
                   4315:                while(*src != '\0') {
                   4316:                        if(msdos_lead_byte_check(*src)) {
                   4317:                                *dst++ = *src++;
                   4318:                                *dst++ = *src++;
                   4319:                        } else if(*src != ' ') {
                   4320:                                *dst++ = *src++;
                   4321:                        } else {
                   4322:                                src++;  // skip space
                   4323:                        }
                   4324:                }
                   4325:                *dst = '\0';
                   4326:        }
1.1.1.14  root     4327:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   4328:                // redirect C:\COMMAND.COM to comspec_path
                   4329:                strcpy(tmp, comspec_path);
                   4330:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   4331:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   4332:                static int root_drive_protected = -1;
                   4333:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   4334:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   4335:                
1.1.1.60  root     4336:                if(GetFullPathNameA(tmp, MAX_PATH, temp, &name_temp) != 0 &&
1.1.1.14  root     4337:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   4338:                        strcpy(name, name_temp);
                   4339:                        name_temp[0] = '\0';
                   4340:                        
                   4341:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   4342:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   4343:                                if(root_drive_protected == -1) {
                   4344:                                        FILE *fp = NULL;
                   4345:                                        
                   4346:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   4347:                                        root_drive_protected = 1;
                   4348:                                        try {
                   4349:                                                if((fp = fopen(temp, "w")) != NULL) {
                   4350:                                                        if(fprintf(fp, "TEST") == 4) {
                   4351:                                                                root_drive_protected = 0;
                   4352:                                                        }
                   4353:                                                }
                   4354:                                        } catch(...) {
                   4355:                                        }
                   4356:                                        if(fp != NULL) {
                   4357:                                                fclose(fp);
                   4358:                                        }
                   4359:                                        if(_access(temp, 0) == 0) {
                   4360:                                                remove(temp);
                   4361:                                        }
                   4362:                                }
                   4363:                                if(root_drive_protected == 1) {
1.1.1.60  root     4364:                                        if(GetEnvironmentVariableA("TEMP", temp, MAX_PATH) != 0 ||
                   4365:                                           GetEnvironmentVariableA("TMP",  temp, MAX_PATH) != 0) {
1.1.1.14  root     4366:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4367:                                        }
                   4368:                                }
                   4369:                        }
                   4370:                }
                   4371:        }
1.1       root     4372:        return(tmp);
                   4373: }
                   4374: 
1.1.1.45  root     4375: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4376: {
1.1.1.32  root     4377:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4378:        static char env_path[ENV_SIZE];
                   4379:        char tmp[ENV_SIZE], *token;
                   4380:        
                   4381:        memset(env_path, 0, sizeof(env_path));
                   4382:        strcpy(tmp, src);
                   4383:        token = my_strtok(tmp, ";");
                   4384:        
                   4385:        while(token != NULL) {
                   4386:                if(token[0] != '\0') {
1.1.1.45  root     4387:                        const char *path = msdos_remove_double_quote(token);
                   4388:                        char short_path[MAX_PATH];
1.1.1.32  root     4389:                        if(path != NULL && strlen(path) != 0) {
                   4390:                                if(env_path[0] != '\0') {
                   4391:                                        strcat(env_path, ";");
                   4392:                                }
1.1.1.60  root     4393:                                if(GetShortPathNameA(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4394:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4395:                                } else {
                   4396:                                        my_strupr(short_path);
1.1.1.32  root     4397:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4398:                                }
                   4399:                        }
                   4400:                }
                   4401:                token = my_strtok(NULL, ";");
                   4402:        }
                   4403:        return(env_path);
                   4404: }
                   4405: 
1.1.1.45  root     4406: bool match(const char *text, const char *pattern)
1.1       root     4407: {
1.1.1.24  root     4408:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4409:        switch(*pattern) {
1.1       root     4410:        case '\0':
                   4411:                return !*text;
                   4412:        case '*':
1.1.1.14  root     4413:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4414:        case '?':
                   4415:                return *text && match(text + 1, pattern + 1);
                   4416:        default:
                   4417:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4418:        }
                   4419: }
                   4420: 
1.1.1.45  root     4421: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4422: {
1.1.1.45  root     4423:        const char *p = NULL;
1.1       root     4424:        
1.1.1.14  root     4425:        if(!*volume) {
                   4426:                return false;
                   4427:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4428:                return msdos_match_volume_label(p + 1, volume);
                   4429:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4430:                return msdos_match_volume_label(p + 1, volume);
                   4431:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4432:                char tmp[MAX_PATH];
                   4433:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4434:                return match(volume, tmp);
1.1       root     4435:        } else {
                   4436:                return match(volume, path);
                   4437:        }
                   4438: }
                   4439: 
1.1.1.45  root     4440: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4441: {
                   4442:        static char tmp[MAX_PATH];
                   4443:        char name[9], ext[4];
                   4444:        
                   4445:        memset(name, 0, sizeof(name));
                   4446:        memcpy(name, fcb->file_name, 8);
                   4447:        strcpy(name, msdos_trimmed_path(name, 0));
                   4448:        
                   4449:        memset(ext, 0, sizeof(ext));
                   4450:        memcpy(ext, fcb->file_name + 8, 3);
                   4451:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4452:        
                   4453:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4454:                strcpy(name, "*");
                   4455:        }
                   4456:        if(ext[0] == '\0') {
                   4457:                strcpy(tmp, name);
                   4458:        } else {
                   4459:                if(strcmp(ext, "???") == 0) {
                   4460:                        strcpy(ext, "*");
                   4461:                }
                   4462:                sprintf(tmp, "%s.%s", name, ext);
                   4463:        }
                   4464:        return(tmp);
                   4465: }
                   4466: 
1.1.1.45  root     4467: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4468: {
1.1.1.60  root     4469:        char tmp[MAX_PATH];
                   4470:        strcpy(tmp, path);
                   4471:        char *ext = my_strchr(tmp, '.');
1.1       root     4472:        
                   4473:        memset(fcb->file_name, 0x20, 8 + 3);
1.1.1.60  root     4474:        if(ext != NULL && tmp[0] != '.') {
1.1       root     4475:                *ext = '\0';
                   4476:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4477:        }
1.1.1.60  root     4478:        memcpy(fcb->file_name, tmp, strlen(tmp));
1.1       root     4479: }
                   4480: 
1.1.1.45  root     4481: const char *msdos_short_path(const char *path)
1.1       root     4482: {
                   4483:        static char tmp[MAX_PATH];
                   4484:        
1.1.1.60  root     4485:        if(GetShortPathNameA(path, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4486:                strcpy(tmp, path);
                   4487:        }
1.1       root     4488:        my_strupr(tmp);
                   4489:        return(tmp);
                   4490: }
                   4491: 
1.1.1.60  root     4492: const char *msdos_short_name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     4493: {
                   4494:        static char tmp[MAX_PATH];
1.1.1.45  root     4495:        
1.1.1.14  root     4496:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4497:                strcpy(tmp, fd->cAlternateFileName);
                   4498:        } else {
                   4499:                strcpy(tmp, fd->cFileName);
                   4500:        }
                   4501:        my_strupr(tmp);
                   4502:        return(tmp);
                   4503: }
                   4504: 
1.1.1.45  root     4505: const char *msdos_short_full_path(const char *path)
1.1       root     4506: {
                   4507:        static char tmp[MAX_PATH];
                   4508:        char full[MAX_PATH], *name;
                   4509:        
1.1.1.14  root     4510:        // Full works with non-existent files, but Short does not
1.1.1.60  root     4511:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1.1.14  root     4512:        *tmp = '\0';
1.1.1.60  root     4513:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0 && name > path) {
1.1.1.14  root     4514:                name[-1] = '\0';
1.1.1.60  root     4515:                DWORD len = GetShortPathNameA(full, tmp, MAX_PATH);
1.1.1.14  root     4516:                if(len == 0) {
                   4517:                        strcpy(tmp, full);
                   4518:                } else {
                   4519:                        tmp[len++] = '\\';
                   4520:                        strcpy(tmp + len, name);
                   4521:                }
                   4522:        }
1.1       root     4523:        my_strupr(tmp);
                   4524:        return(tmp);
                   4525: }
                   4526: 
1.1.1.45  root     4527: const char *msdos_short_full_dir(const char *path)
1.1       root     4528: {
                   4529:        static char tmp[MAX_PATH];
                   4530:        char full[MAX_PATH], *name;
                   4531:        
1.1.1.60  root     4532:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1       root     4533:        name[-1] = '\0';
1.1.1.60  root     4534:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4535:                strcpy(tmp, full);
                   4536:        }
1.1       root     4537:        my_strupr(tmp);
                   4538:        return(tmp);
                   4539: }
                   4540: 
1.1.1.45  root     4541: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4542: {
1.1.1.45  root     4543:        static char trimmed[MAX_PATH];
                   4544:        
                   4545:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4546: #if 0
                   4547:        // I have forgotten the reason of this routine... :-(
1.1       root     4548:        if(_access(trimmed, 0) != 0) {
                   4549:                process_t *process = msdos_process_info_get(current_psp);
                   4550:                static char tmp[MAX_PATH];
                   4551:                
                   4552:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4553:                if(_access(tmp, 0) == 0) {
                   4554:                        return(tmp);
                   4555:                }
                   4556:        }
1.1.1.14  root     4557: #endif
1.1       root     4558:        return(trimmed);
                   4559: }
                   4560: 
1.1.1.45  root     4561: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4562: {
                   4563:        char full[MAX_PATH], *name;
                   4564:        
1.1.1.60  root     4565:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4566:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4567:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4568:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4569:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4570:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4571:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4572:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4573:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4574:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4575:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4576:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4577:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4578:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4579:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4580:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4581:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4582:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4583:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4584:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4585:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4586:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4587:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4588:                        return(true);
                   4589:                } else if(name != NULL) {
                   4590:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4591:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4592:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4593: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4594:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4595:                                return(true);
                   4596:                        }
                   4597:                }
1.1.1.24  root     4598:        }
                   4599:        return(false);
1.1.1.11  root     4600: }
                   4601: 
1.1.1.45  root     4602: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4603: {
1.1.1.14  root     4604:        char full[MAX_PATH], *name;
1.1.1.8   root     4605:        
1.1.1.60  root     4606:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4607:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4608:        }
                   4609:        return(false);
                   4610: }
                   4611: 
1.1.1.45  root     4612: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4613: {
                   4614:        char full[MAX_PATH], *name;
                   4615:        
1.1.1.60  root     4616:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4617:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4618:                        return(1);
                   4619:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4620:                        return(2);
                   4621:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4622:                        return(3);
                   4623:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4624:                        return(4);
1.1.1.24  root     4625:                }
                   4626:        }
1.1.1.29  root     4627:        return(0);
                   4628: }
                   4629: 
1.1.1.45  root     4630: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4631: {
                   4632:        // 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     4633:        const char *p = NULL;
1.1.1.37  root     4634:        
                   4635:        if((p = strstr(path, ":")) != NULL) {
                   4636:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4637:                
                   4638:                // baud rate
                   4639:                int baud = max(110, min(9600, atoi(p + 1)));
                   4640:                UINT16 divisor = 115200 / baud;
                   4641:                
                   4642:                if((p = strstr(p + 1, ",")) != NULL) {
                   4643:                        // parity
                   4644:                        if(p[1] == 'N' || p[1] == 'n') {
                   4645:                                selector = (selector & ~0x38) | 0x00;
                   4646:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4647:                                selector = (selector & ~0x38) | 0x08;
                   4648:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4649:                                selector = (selector & ~0x38) | 0x18;
                   4650:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4651:                                selector = (selector & ~0x38) | 0x28;
                   4652:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4653:                                selector = (selector & ~0x38) | 0x38;
                   4654:                        }
                   4655:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4656:                                // word length
                   4657:                                if(p[1] == '8') {
                   4658:                                        selector = (selector & ~0x03) | 0x03;
                   4659:                                } else if(p[1] == '7') {
                   4660:                                        selector = (selector & ~0x03) | 0x02;
                   4661:                                } else if(p[1] == '6') {
                   4662:                                        selector = (selector & ~0x03) | 0x01;
                   4663:                                } else if(p[1] == '5') {
                   4664:                                        selector = (selector & ~0x03) | 0x00;
                   4665:                                }
                   4666:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4667:                                        // stop bits
                   4668:                                        float bits = atof(p + 1);
                   4669:                                        if(bits > 1.0F) {
                   4670:                                                selector |= 0x04;
                   4671:                                        } else {
                   4672:                                                selector &= ~0x04;
                   4673:                                        }
                   4674:                                }
                   4675:                        }
                   4676:                }
                   4677:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4678:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4679:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4680:                sio_write(sio_port - 1, 3, selector);
                   4681:        }
                   4682: }
                   4683: 
1.1.1.45  root     4684: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4685: {
                   4686:        char full[MAX_PATH], *name;
                   4687:        
1.1.1.60  root     4688:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.30  root     4689:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4690:                        return(1);
                   4691:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4692:                        return(1);
                   4693:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4694:                        return(2);
                   4695:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4696:                        return(3);
                   4697:                }
                   4698:        }
                   4699:        return(0);
                   4700: }
                   4701: 
1.1.1.44  root     4702: bool msdos_is_valid_drive(int drv)
                   4703: {
                   4704:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4705: }
                   4706: 
                   4707: bool msdos_is_removable_drive(int drv)
                   4708: {
                   4709:        char volume[] = "A:\\";
                   4710:        
                   4711:        volume[0] = 'A' + drv;
                   4712:        
1.1.1.60  root     4713:        return(GetDriveTypeA(volume) == DRIVE_REMOVABLE);
1.1.1.44  root     4714: }
                   4715: 
                   4716: bool msdos_is_cdrom_drive(int drv)
                   4717: {
                   4718:        char volume[] = "A:\\";
                   4719:        
                   4720:        volume[0] = 'A' + drv;
                   4721:        
1.1.1.60  root     4722:        return(GetDriveTypeA(volume) == DRIVE_CDROM);
1.1.1.44  root     4723: }
                   4724: 
                   4725: bool msdos_is_remote_drive(int drv)
                   4726: {
                   4727:        char volume[] = "A:\\";
                   4728:        
                   4729:        volume[0] = 'A' + drv;
                   4730:        
1.1.1.60  root     4731:        return(GetDriveTypeA(volume) == DRIVE_REMOTE);
1.1.1.44  root     4732: }
                   4733: 
                   4734: bool msdos_is_subst_drive(int drv)
                   4735: {
                   4736:        char device[] = "A:", path[MAX_PATH];
                   4737:        
                   4738:        device[0] = 'A' + drv;
                   4739:        
1.1.1.60  root     4740:        if(QueryDosDeviceA(device, path, MAX_PATH)) {
1.1.1.44  root     4741:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4742:                        return(true);
                   4743:                }
                   4744:        }
                   4745:        return(false);
                   4746: }
                   4747: 
1.1.1.45  root     4748: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4749: {
                   4750:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
1.1.1.60  root     4751:        WIN32_FIND_DATAA fd;
1.1.1.24  root     4752:        HANDLE hFind;
                   4753:        
1.1.1.60  root     4754:        if((hFind = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.24  root     4755:                FindClose(hFind);
1.1.1.63  root     4756:                return((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0);
                   4757:        }
                   4758:        return(false);
                   4759: }
                   4760: 
                   4761: bool msdos_is_existing_dir(const char *path)
                   4762: {
                   4763:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4764:        WIN32_FIND_DATAA fd;
                   4765:        HANDLE hFind;
                   4766:        
                   4767:        if((hFind = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
                   4768:                FindClose(hFind);
                   4769:                return((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
1.1.1.24  root     4770:        }
                   4771:        return(false);
1.1.1.8   root     4772: }
                   4773: 
1.1.1.45  root     4774: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4775: {
                   4776:        static char tmp[MAX_PATH];
1.1.1.28  root     4777:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4778:        
1.1.1.28  root     4779:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.60  root     4780:        if(GetFullPathNameA(command_path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4781:                sprintf(file_name, "COMMAND.COM");
                   4782:                if(_access(tmp, 0) == 0) {
                   4783:                        return(tmp);
                   4784:                }
                   4785:        }
1.1.1.28  root     4786:        
                   4787:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.60  root     4788:        if(GetModuleFileNameA(NULL, path, MAX_PATH) != 0 && GetFullPathNameA(path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4789:                sprintf(file_name, "COMMAND.COM");
                   4790:                if(_access(tmp, 0) == 0) {
                   4791:                        return(tmp);
                   4792:                }
                   4793:        }
1.1.1.28  root     4794:        
                   4795:        // check if COMMAND.COM is in the current directory
1.1.1.60  root     4796:        if(GetFullPathNameA("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4797:                if(_access(tmp, 0) == 0) {
                   4798:                        return(tmp);
                   4799:                }
                   4800:        }
1.1.1.28  root     4801:        
                   4802:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4803:        strcpy(path, env_path);
                   4804:        char *token = my_strtok(path, ";");
1.1.1.9   root     4805:        while(token != NULL) {
1.1.1.14  root     4806:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4807:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4808:                        if(_access(tmp, 0) == 0) {
                   4809:                                return(tmp);
                   4810:                        }
                   4811:                }
                   4812:                token = my_strtok(NULL, ";");
                   4813:        }
                   4814:        return(NULL);
                   4815: }
                   4816: 
1.1.1.14  root     4817: int msdos_drive_number(const char *path)
1.1       root     4818: {
                   4819:        char tmp[MAX_PATH], *name;
                   4820:        
1.1.1.60  root     4821:        if(GetFullPathNameA(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
1.1.1.45  root     4822:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4823:                        return(tmp[0] - 'a');
                   4824:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4825:                        return(tmp[0] - 'A');
                   4826:                }
1.1       root     4827:        }
1.1.1.45  root     4828: //     return(msdos_drive_number("."));
                   4829:        return(_getdrive() - 1);
1.1       root     4830: }
                   4831: 
1.1.1.45  root     4832: const char *msdos_volume_label(const char *path)
1.1       root     4833: {
                   4834:        static char tmp[MAX_PATH];
                   4835:        char volume[] = "A:\\";
                   4836:        
                   4837:        if(path[1] == ':') {
                   4838:                volume[0] = path[0];
                   4839:        } else {
                   4840:                volume[0] = 'A' + _getdrive() - 1;
                   4841:        }
1.1.1.60  root     4842:        if(!GetVolumeInformationA(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
1.1       root     4843:                memset(tmp, 0, sizeof(tmp));
                   4844:        }
                   4845:        return(tmp);
                   4846: }
                   4847: 
1.1.1.45  root     4848: const char *msdos_short_volume_label(const char *label)
1.1       root     4849: {
                   4850:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4851:        const char *src = label;
1.1       root     4852:        int remain = strlen(label);
                   4853:        char *dst_n = tmp;
                   4854:        char *dst_e = tmp + 9;
                   4855:        
                   4856:        strcpy(tmp, "        .   ");
                   4857:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4858:                if(msdos_lead_byte_check(*src)) {
                   4859:                        if(++i == 8) {
                   4860:                                break;
                   4861:                        }
                   4862:                        *dst_n++ = *src++;
                   4863:                        remain--;
                   4864:                }
                   4865:                *dst_n++ = *src++;
                   4866:                remain--;
                   4867:        }
                   4868:        if(remain > 0) {
                   4869:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4870:                        if(msdos_lead_byte_check(*src)) {
                   4871:                                if(++i == 3) {
                   4872:                                        break;
                   4873:                                }
                   4874:                                *dst_e++ = *src++;
                   4875:                                remain--;
                   4876:                        }
                   4877:                        *dst_e++ = *src++;
                   4878:                        remain--;
                   4879:                }
                   4880:                *dst_e = '\0';
                   4881:        } else {
                   4882:                *dst_n = '\0';
                   4883:        }
                   4884:        my_strupr(tmp);
                   4885:        return(tmp);
                   4886: }
                   4887: 
1.1.1.13  root     4888: errno_t msdos_maperr(unsigned long oserrno)
                   4889: {
                   4890:        _doserrno = oserrno;
1.1.1.14  root     4891:        switch(oserrno) {
1.1.1.13  root     4892:        case ERROR_FILE_NOT_FOUND:         // 2
                   4893:        case ERROR_PATH_NOT_FOUND:         // 3
                   4894:        case ERROR_INVALID_DRIVE:          // 15
                   4895:        case ERROR_NO_MORE_FILES:          // 18
                   4896:        case ERROR_BAD_NETPATH:            // 53
                   4897:        case ERROR_BAD_NET_NAME:           // 67
                   4898:        case ERROR_BAD_PATHNAME:           // 161
                   4899:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4900:                return ENOENT;
                   4901:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4902:                return EMFILE;
                   4903:        case ERROR_ACCESS_DENIED:          // 5
                   4904:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4905:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4906:        case ERROR_CANNOT_MAKE:            // 82
                   4907:        case ERROR_FAIL_I24:               // 83
                   4908:        case ERROR_DRIVE_LOCKED:           // 108
                   4909:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4910:        case ERROR_NOT_LOCKED:             // 158
                   4911:        case ERROR_LOCK_FAILED:            // 167
                   4912:                return EACCES;
                   4913:        case ERROR_INVALID_HANDLE:         // 6
                   4914:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4915:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4916:                return EBADF;
                   4917:        case ERROR_ARENA_TRASHED:          // 7
                   4918:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4919:        case ERROR_INVALID_BLOCK:          // 9
                   4920:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4921:                return ENOMEM;
                   4922:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4923:                return E2BIG;
                   4924:        case ERROR_BAD_FORMAT:             // 11
                   4925:                return ENOEXEC;
                   4926:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4927:                return EXDEV;
                   4928:        case ERROR_FILE_EXISTS:            // 80
                   4929:        case ERROR_ALREADY_EXISTS:         // 183
                   4930:                return EEXIST;
                   4931:        case ERROR_NO_PROC_SLOTS:          // 89
                   4932:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4933:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4934:                return EAGAIN;
                   4935:        case ERROR_BROKEN_PIPE:            // 109
                   4936:                return EPIPE;
                   4937:        case ERROR_DISK_FULL:              // 112
                   4938:                return ENOSPC;
                   4939:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4940:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4941:                return ECHILD;
                   4942:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4943:                return ENOTEMPTY;
                   4944:        }
1.1.1.14  root     4945:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4946:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4947:                return EACCES;
                   4948:        }
1.1.1.14  root     4949:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4950:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4951:                return ENOEXEC;
                   4952:        }
                   4953:        return EINVAL;
                   4954: }
                   4955: 
1.1.1.45  root     4956: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4957: {
1.1.1.14  root     4958:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4959:                return(_open(path, oflag));
1.1.1.13  root     4960:        }
1.1.1.14  root     4961:        
                   4962:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4963:        DWORD disposition;
1.1.1.14  root     4964:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4965:        default:
1.1.1.13  root     4966:        case _O_EXCL:
                   4967:                disposition = OPEN_EXISTING;
                   4968:                break;
                   4969:        case _O_CREAT:
                   4970:                disposition = OPEN_ALWAYS;
                   4971:                break;
                   4972:        case _O_CREAT | _O_EXCL:
                   4973:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4974:                disposition = CREATE_NEW;
                   4975:                break;
                   4976:        case _O_TRUNC:
                   4977:        case _O_TRUNC | _O_EXCL:
                   4978:                disposition = TRUNCATE_EXISTING;
                   4979:                break;
                   4980:        case _O_CREAT | _O_TRUNC:
                   4981:                disposition = CREATE_ALWAYS;
                   4982:                break;
                   4983:        }
1.1.1.14  root     4984:        
1.1.1.60  root     4985:        HANDLE h = CreateFileA(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4986:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4987:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4988:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4989:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4990:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.60  root     4991:                h = CreateFileA(path, GENERIC_READ,
1.1.1.13  root     4992:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4993:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4994:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4995:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4996:                        return(-1);
1.1.1.13  root     4997:                }
                   4998:        }
1.1.1.14  root     4999:        
1.1.1.13  root     5000:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     5001:        if(fd == -1) {
1.1.1.13  root     5002:                CloseHandle(h);
                   5003:        }
1.1.1.45  root     5004:        return(fd);
                   5005: }
                   5006: 
                   5007: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   5008: {
                   5009:        int fd = -1;
                   5010:        
                   5011:        *sio_port = *lpt_port = 0;
                   5012:        
                   5013:        if(msdos_is_con_path(path)) {
                   5014:                // MODE.COM opens CON device with read/write mode :-(
                   5015:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   5016:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   5017:                        oflag |= _O_RDONLY;
                   5018:                }
                   5019:                if((fd = msdos_open("CON", oflag)) == -1) {
                   5020: //                     fd = msdos_open("NUL", oflag);
                   5021:                }
                   5022:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   5023:                fd = msdos_open("NUL", oflag);
                   5024:                msdos_set_comm_params(*sio_port, path);
                   5025:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   5026:                fd = msdos_open("NUL", oflag);
                   5027:        } else if(msdos_is_device_path(path)) {
                   5028:                fd = msdos_open("NUL", oflag);
                   5029: //     } else if(oflag & _O_CREAT) {
                   5030: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   5031: //     } else {
                   5032: //             fd = _open(path, oflag);
                   5033:        }
                   5034:        return(fd);
                   5035: }
                   5036: 
                   5037: UINT16 msdos_device_info(const char *path)
                   5038: {
                   5039:        if(msdos_is_con_path(path)) {
                   5040:                return(0x80d3);
                   5041:        } else if(msdos_is_comm_path(path)) {
                   5042:                return(0x80a0);
                   5043:        } else if(msdos_is_prn_path(path)) {
                   5044: //             return(0xa8c0);
                   5045:                return(0x80a0);
                   5046:        } else if(msdos_is_device_path(path)) {
                   5047:                if(strstr(path, "EMMXXXX0") != NULL) {
                   5048:                        return(0xc0c0);
                   5049:                } else if(strstr(path, "MSCD001") != NULL) {
                   5050:                        return(0xc880);
                   5051:                } else {
                   5052:                        return(0x8084);
                   5053:                }
                   5054:        } else {
                   5055:                return(msdos_drive_number(path));
                   5056:        }
1.1.1.13  root     5057: }
                   5058: 
1.1.1.52  root     5059: 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     5060: {
                   5061:        static int id = 0;
                   5062:        char full[MAX_PATH], *name;
                   5063:        
1.1.1.60  root     5064:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1       root     5065:                strcpy(file_handler[fd].path, full);
                   5066:        } else {
                   5067:                strcpy(file_handler[fd].path, path);
                   5068:        }
1.1.1.14  root     5069:        // isatty makes no distinction between CON & NUL
                   5070:        // GetFileSize fails on CON, succeeds on NUL
                   5071:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     5072:                if(info == 0x80d3) {
                   5073:                        info = 0x8084;
                   5074:                }
1.1.1.14  root     5075:                atty = 0;
                   5076:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     5077: //             info = msdos_drive_number(".");
                   5078:                info = msdos_drive_number(path);
1.1.1.14  root     5079:        }
1.1       root     5080:        file_handler[fd].valid = 1;
                   5081:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     5082:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     5083:        file_handler[fd].mode = mode;
                   5084:        file_handler[fd].info = info;
                   5085:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     5086:        file_handler[fd].sio_port = sio_port;
                   5087:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     5088:        
                   5089:        // init system file table
                   5090:        if(fd < 20) {
                   5091:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   5092:                
                   5093:                memset(sft, 0, 0x3b);
                   5094:                
                   5095:                *(UINT16 *)(sft + 0x00) = 1;
                   5096:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
1.1.1.60  root     5097:                *(UINT8  *)(sft + 0x04) = GetFileAttributesA(file_handler[fd].path) & 0xff;
1.1.1.21  root     5098:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   5099:                
                   5100:                if(!(file_handler[fd].info & 0x80)) {
                   5101:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   5102:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   5103:                        
                   5104:                        FILETIME time, local;
                   5105:                        HANDLE hHandle;
                   5106:                        WORD dos_date = 0, dos_time = 0;
                   5107:                        DWORD file_size = 0;
                   5108:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   5109:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   5110:                                        FileTimeToLocalFileTime(&time, &local);
                   5111:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   5112:                                }
                   5113:                                file_size = GetFileSize(hHandle, NULL);
                   5114:                        }
                   5115:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   5116:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   5117:                        *(UINT32 *)(sft + 0x11) = file_size;
                   5118:                }
                   5119:                
                   5120:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   5121:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   5122:                my_strupr(fname);
                   5123:                my_strupr(ext);
                   5124:                memset(sft + 0x20, 0x20, 11);
                   5125:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   5126:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   5127:                
                   5128:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   5129:        }
1.1       root     5130: }
                   5131: 
                   5132: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   5133: {
                   5134:        strcpy(file_handler[dst].path, file_handler[src].path);
                   5135:        file_handler[dst].valid = 1;
                   5136:        file_handler[dst].id = file_handler[src].id;
                   5137:        file_handler[dst].atty = file_handler[src].atty;
                   5138:        file_handler[dst].mode = file_handler[src].mode;
                   5139:        file_handler[dst].info = file_handler[src].info;
                   5140:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     5141:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   5142:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     5143: }
                   5144: 
1.1.1.20  root     5145: void msdos_file_handler_close(int fd)
1.1       root     5146: {
                   5147:        file_handler[fd].valid = 0;
1.1.1.21  root     5148:        
                   5149:        if(fd < 20) {
                   5150:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   5151:        }
1.1       root     5152: }
                   5153: 
1.1.1.14  root     5154: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     5155: {
1.1.1.14  root     5156:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   5157:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   5158:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     5159: }
                   5160: 
                   5161: // find file
                   5162: 
                   5163: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   5164: {
                   5165:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5166:                return(0);      // search directory only !!!
                   5167:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   5168:                return(0);
                   5169:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   5170:                return(0);
                   5171:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5172:                return(0);
                   5173:        } else if((attribute & required_mask) != required_mask) {
                   5174:                return(0);
                   5175:        } else {
                   5176:                return(1);
                   5177:        }
                   5178: }
                   5179: 
1.1.1.60  root     5180: int msdos_find_file_has_8dot3name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     5181: {
1.1.1.14  root     5182:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     5183:                return(1);
1.1.1.13  root     5184:        }
                   5185:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     5186:        if(len > 12) {
1.1.1.42  root     5187:                return(0);
1.1.1.13  root     5188:        }
                   5189:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     5190:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     5191:                return(0);
1.1.1.13  root     5192:        }
1.1.1.42  root     5193:        return(1);
1.1.1.13  root     5194: }
                   5195: 
1.1.1.60  root     5196: void msdos_find_file_conv_local_time(WIN32_FIND_DATAA *fd)
1.1       root     5197: {
                   5198:        FILETIME local;
                   5199:        
                   5200:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   5201:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   5202:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   5203:        
                   5204:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   5205:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   5206:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   5207:        
                   5208:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   5209:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   5210:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   5211: }
                   5212: 
                   5213: // i/o
                   5214: 
                   5215: void msdos_stdio_reopen()
                   5216: {
                   5217:        if(!file_handler[0].valid) {
                   5218:                _dup2(DUP_STDIN, 0);
                   5219:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   5220:        }
                   5221:        if(!file_handler[1].valid) {
                   5222:                _dup2(DUP_STDOUT, 1);
                   5223:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   5224:        }
                   5225:        if(!file_handler[2].valid) {
                   5226:                _dup2(DUP_STDERR, 2);
                   5227:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   5228:        }
1.1.1.21  root     5229:        if(!file_handler[3].valid) {
                   5230:                _dup2(DUP_STDAUX, 3);
                   5231:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   5232:        }
                   5233:        if(!file_handler[4].valid) {
                   5234:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     5235: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   5236:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     5237:        }
                   5238:        for(int i = 0; i < 5; i++) {
                   5239:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   5240:                        msdos_psp_set_file_table(i, i, current_psp);
                   5241:                }
                   5242:        }
1.1       root     5243: }
                   5244: 
1.1.1.37  root     5245: int msdos_read(int fd, void *buffer, unsigned int count)
                   5246: {
                   5247:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5248:                // read from serial port
                   5249:                int read = 0;
                   5250:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5251:                        UINT8 *buf = (UINT8 *)buffer;
                   5252:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5253:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5254:                        DWORD timeout = timeGetTime() + 1000;
                   5255:                        while(read < count) {
                   5256:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   5257:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   5258:                                        timeout = timeGetTime() + 1000;
                   5259:                                } else {
                   5260:                                        if(timeGetTime() > timeout) {
                   5261:                                                break;
                   5262:                                        }
                   5263:                                        Sleep(10);
1.1.1.37  root     5264:                                }
                   5265:                        }
                   5266:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5267:                }
                   5268:                return(read);
                   5269:        }
                   5270:        return(_read(fd, buffer, count));
                   5271: }
                   5272: 
1.1       root     5273: int msdos_kbhit()
                   5274: {
                   5275:        msdos_stdio_reopen();
                   5276:        
1.1.1.20  root     5277:        process_t *process = msdos_process_info_get(current_psp);
                   5278:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5279:        
                   5280:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5281:                // stdin is redirected to file
1.1.1.20  root     5282:                return(eof(fd) == 0);
1.1       root     5283:        }
                   5284:        
                   5285:        // check keyboard status
1.1.1.35  root     5286:        if(key_recv != 0) {
1.1       root     5287:                return(1);
                   5288:        }
1.1.1.35  root     5289:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5290: #ifdef USE_SERVICE_THREAD
                   5291:                EnterCriticalSection(&key_buf_crit_sect);
                   5292: #endif
1.1.1.55  root     5293:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5294: #ifdef USE_SERVICE_THREAD
                   5295:                LeaveCriticalSection(&key_buf_crit_sect);
                   5296: #endif
                   5297:                if(!empty) return(1);
                   5298:        }
                   5299:        return(_kbhit());
1.1       root     5300: }
                   5301: 
                   5302: int msdos_getch_ex(int echo)
                   5303: {
                   5304:        static char prev = 0;
                   5305:        
                   5306:        msdos_stdio_reopen();
                   5307:        
1.1.1.20  root     5308:        process_t *process = msdos_process_info_get(current_psp);
                   5309:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5310:        
                   5311:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5312:                // stdin is redirected to file
                   5313: retry:
                   5314:                char data;
1.1.1.37  root     5315:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     5316:                        char tmp = data;
                   5317:                        if(data == 0x0a) {
                   5318:                                if(prev == 0x0d) {
                   5319:                                        goto retry; // CRLF -> skip LF
                   5320:                                } else {
                   5321:                                        data = 0x0d; // LF only -> CR
                   5322:                                }
                   5323:                        }
                   5324:                        prev = tmp;
                   5325:                        return(data);
                   5326:                }
                   5327:                return(EOF);
                   5328:        }
                   5329:        
                   5330:        // input from console
1.1.1.5   root     5331:        int key_char, key_scan;
1.1.1.33  root     5332:        if(key_recv != 0) {
1.1.1.5   root     5333:                key_char = (key_code >> 0) & 0xff;
                   5334:                key_scan = (key_code >> 8) & 0xff;
                   5335:                key_code >>= 16;
1.1.1.33  root     5336:                key_recv >>= 16;
1.1.1.5   root     5337:        } else {
1.1.1.54  root     5338:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35  root     5339:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5340: #ifdef USE_SERVICE_THREAD
                   5341:                                EnterCriticalSection(&key_buf_crit_sect);
                   5342: #endif
1.1.1.55  root     5343:                                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5344: #ifdef USE_SERVICE_THREAD
                   5345:                                LeaveCriticalSection(&key_buf_crit_sect);
                   5346: #endif
                   5347:                                if(!empty) break;
                   5348:                        }
1.1.1.23  root     5349:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   5350:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   5351:                                if(_kbhit()) {
1.1.1.32  root     5352:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5353: #ifdef USE_SERVICE_THREAD
                   5354:                                                EnterCriticalSection(&key_buf_crit_sect);
                   5355: #endif
1.1.1.51  root     5356:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     5357: #ifdef USE_SERVICE_THREAD
                   5358:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   5359: #endif
1.1.1.32  root     5360:                                        }
1.1.1.23  root     5361:                                } else {
                   5362:                                        Sleep(10);
                   5363:                                }
                   5364:                        } else {
                   5365:                                if(!update_key_buffer()) {
                   5366:                                        Sleep(10);
                   5367:                                }
1.1.1.14  root     5368:                        }
                   5369:                }
1.1.1.54  root     5370:                if(m_exit) {
1.1.1.33  root     5371:                        // insert CR to terminate input loops
1.1.1.14  root     5372:                        key_char = 0x0d;
                   5373:                        key_scan = 0;
1.1.1.32  root     5374:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5375: #ifdef USE_SERVICE_THREAD
                   5376:                        EnterCriticalSection(&key_buf_crit_sect);
                   5377: #endif
1.1.1.51  root     5378:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     5379: #ifdef USE_SERVICE_THREAD
                   5380:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5381: #endif
1.1.1.5   root     5382:                }
1.1       root     5383:        }
                   5384:        if(echo && key_char) {
                   5385:                msdos_putch(key_char);
                   5386:        }
                   5387:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5388: }
                   5389: 
                   5390: inline int msdos_getch()
                   5391: {
                   5392:        return(msdos_getch_ex(0));
                   5393: }
                   5394: 
                   5395: inline int msdos_getche()
                   5396: {
                   5397:        return(msdos_getch_ex(1));
                   5398: }
                   5399: 
                   5400: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5401: {
1.1.1.37  root     5402:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5403:                // write to serial port
1.1.1.38  root     5404:                int written = 0;
1.1.1.37  root     5405:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5406:                        UINT8 *buf = (UINT8 *)buffer;
                   5407:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5408:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5409:                        DWORD timeout = timeGetTime() + 1000;
                   5410:                        while(written < count) {
                   5411:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5412:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5413:                                        timeout = timeGetTime() + 1000;
                   5414:                                } else {
                   5415:                                        if(timeGetTime() > timeout) {
                   5416:                                                break;
                   5417:                                        }
                   5418:                                        Sleep(10);
                   5419:                                }
1.1.1.37  root     5420:                        }
                   5421:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5422:                }
1.1.1.38  root     5423:                return(written);
1.1.1.37  root     5424:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5425:                // write to printer port
                   5426:                UINT8 *buf = (UINT8 *)buffer;
                   5427:                for(unsigned int i = 0; i < count; i++) {
                   5428: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5429:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5430:                }
                   5431:                return(count);
                   5432:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5433:                // CR+LF -> LF
1.1.1.37  root     5434:                static int is_cr = 0;
1.1       root     5435:                UINT8 *buf = (UINT8 *)buffer;
                   5436:                for(unsigned int i = 0; i < count; i++) {
                   5437:                        UINT8 data = buf[i];
                   5438:                        if(is_cr) {
                   5439:                                if(data != 0x0a) {
                   5440:                                        UINT8 tmp = 0x0d;
                   5441:                                        _write(1, &tmp, 1);
                   5442:                                }
                   5443:                                _write(1, &data, 1);
                   5444:                                is_cr = 0;
                   5445:                        } else if(data == 0x0d) {
                   5446:                                is_cr = 1;
                   5447:                        } else {
                   5448:                                _write(1, &data, 1);
                   5449:                        }
                   5450:                }
                   5451:                return(count);
                   5452:        }
1.1.1.14  root     5453:        vram_flush();
1.1       root     5454:        return(_write(fd, buffer, count));
                   5455: }
                   5456: 
                   5457: void msdos_putch(UINT8 data)
1.1.1.50  root     5458: {
                   5459:        msdos_stdio_reopen();
                   5460:        
                   5461:        process_t *process = msdos_process_info_get(current_psp);
                   5462:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5463:        
                   5464:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5465:                // stdout is redirected to file
                   5466:                msdos_write(fd, &data, 1);
                   5467:                return;
                   5468:        }
                   5469:        
                   5470:        // call int 29h ?
1.1.1.58  root     5471:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     5472:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5473:                // int 29h is not hooked, no need to call int 29h
                   5474:                msdos_putch_fast(data);
                   5475: #ifdef USE_SERVICE_THREAD
                   5476:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5477:                // XXX: in usually we should not reach here
                   5478:                // this is called from service thread to echo the input
                   5479:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5480:                msdos_putch_fast(data);
                   5481: #endif
1.1.1.51  root     5482:        } else if(in_service_29h) {
1.1.1.50  root     5483:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5484:                msdos_putch_fast(data);
                   5485:        } else {
                   5486:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5487:                in_service_29h = true;
1.1.1.50  root     5488:                try {
                   5489:                        UINT32 tmp_pc = m_pc;
                   5490:                        UINT16 tmp_ax = REG16(AX);
                   5491:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5492:                        
                   5493:                        // call int 29h routine is at fffc:0027
                   5494:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5495:                        REG8(AL) = data;
                   5496:                        
                   5497:                        // run cpu until call int 29h routine is done
1.1.1.54  root     5498:                        while(!m_exit && tmp_pc != m_pc) {
1.1.1.50  root     5499:                                try {
                   5500:                                        hardware_run_cpu();
                   5501:                                } catch(...) {
                   5502:                                }
                   5503:                        }
                   5504:                        REG16(AX) = tmp_ax;
                   5505:                        REG16(BX) = tmp_bx;
                   5506:                } catch(...) {
                   5507:                }
1.1.1.51  root     5508:                in_service_29h = false;
1.1.1.50  root     5509:        }
                   5510: }
                   5511: 
                   5512: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5513: #ifdef USE_SERVICE_THREAD
                   5514: {
                   5515:        EnterCriticalSection(&putch_crit_sect);
                   5516:        msdos_putch_tmp(data);
                   5517:        LeaveCriticalSection(&putch_crit_sect);
                   5518: }
                   5519: void msdos_putch_tmp(UINT8 data)
                   5520: #endif
1.1       root     5521: {
1.1.1.34  root     5522:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5523:        SMALL_RECT rect;
                   5524:        COORD co;
1.1       root     5525:        static int p = 0;
                   5526:        static int is_kanji = 0;
                   5527:        static int is_esc = 0;
                   5528:        static int stored_x;
                   5529:        static int stored_y;
                   5530:        static WORD stored_a;
1.1.1.20  root     5531:        static char tmp[64], out[64];
1.1       root     5532:        
1.1.1.23  root     5533:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5534:        
                   5535:        // output to console
                   5536:        tmp[p++] = data;
                   5537:        
1.1.1.14  root     5538:        vram_flush();
                   5539:        
1.1       root     5540:        if(is_kanji) {
                   5541:                // kanji character
                   5542:                is_kanji = 0;
                   5543:        } else if(is_esc) {
                   5544:                // escape sequense
                   5545:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5546:                        p = is_esc = 0;
                   5547:                } else if(tmp[1] == '=' && p == 4) {
                   5548:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5549:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5550:                        SetConsoleCursorPosition(hStdout, co);
                   5551:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5552:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5553:                        cursor_moved = false;
1.1.1.59  root     5554:                        cursor_moved_by_crtc = false;
1.1       root     5555:                        p = is_esc = 0;
                   5556:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
1.1.1.59  root     5557:                        if(cursor_moved_by_crtc) {
                   5558:                                if(!restore_console_on_exit) {
                   5559:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5560:                                        scr_top = csbi.srWindow.Top;
                   5561:                                }
                   5562:                                co.X = mem[0x450 + REG8(BH) * 2];
                   5563:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5564:                                SetConsoleCursorPosition(hStdout, co);
                   5565:                                cursor_moved_by_crtc = false;
                   5566:                        }
1.1       root     5567:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5568:                        co.X = csbi.dwCursorPosition.X;
                   5569:                        co.Y = csbi.dwCursorPosition.Y;
                   5570:                        WORD wAttributes = csbi.wAttributes;
                   5571:                        
                   5572:                        if(tmp[1] == 'D') {
                   5573:                                co.Y++;
                   5574:                        } else if(tmp[1] == 'E') {
                   5575:                                co.X = 0;
                   5576:                                co.Y++;
                   5577:                        } else if(tmp[1] == 'M') {
                   5578:                                co.Y--;
                   5579:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5580:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5581:                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5582:                                co.X = 0;
                   5583:                                co.Y = csbi.srWindow.Top;
1.1       root     5584:                        } else if(tmp[1] == '[') {
                   5585:                                int param[256], params = 0;
                   5586:                                memset(param, 0, sizeof(param));
                   5587:                                for(int i = 2; i < p; i++) {
                   5588:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5589:                                                param[params] *= 10;
                   5590:                                                param[params] += tmp[i] - '0';
                   5591:                                        } else {
                   5592:                                                params++;
                   5593:                                        }
                   5594:                                }
                   5595:                                if(data == 'A') {
1.1.1.14  root     5596:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5597:                                } else if(data == 'B') {
1.1.1.14  root     5598:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5599:                                } else if(data == 'C') {
1.1.1.14  root     5600:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5601:                                } else if(data == 'D') {
1.1.1.14  root     5602:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5603:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5604:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5605:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5606:                                } else if(data == 'J') {
1.1.1.14  root     5607:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5608:                                        if(param[0] == 0) {
                   5609:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5610:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5611:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5612:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5613:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5614:                                                }
                   5615:                                        } else if(param[0] == 1) {
1.1.1.14  root     5616:                                                if(co.Y > csbi.srWindow.Top) {
                   5617:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
1.1.1.60  root     5618:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5619:                                                }
                   5620:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5621:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5622:                                        } else if(param[0] == 2) {
1.1.1.14  root     5623:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5624:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5625:                                                co.X = co.Y = 0;
                   5626:                                        }
                   5627:                                } else if(data == 'K') {
1.1.1.14  root     5628:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5629:                                        if(param[0] == 0) {
                   5630:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5631:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5632:                                        } else if(param[0] == 1) {
                   5633:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5634:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5635:                                        } else if(param[0] == 2) {
                   5636:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5637:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5638:                                        }
                   5639:                                } else if(data == 'L') {
1.1.1.14  root     5640:                                        if(params == 0) {
                   5641:                                                param[0] = 1;
1.1       root     5642:                                        }
1.1.1.14  root     5643:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5644:                                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5645:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5646:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5647:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5648:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.60  root     5649:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5650:                                        co.X = 0;
                   5651:                                } else if(data == 'M') {
1.1.1.14  root     5652:                                        if(params == 0) {
                   5653:                                                param[0] = 1;
                   5654:                                        }
                   5655:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5656:                                                clear_scr_buffer(csbi.wAttributes);
                   5657:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5658:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5659:                                        } else {
1.1.1.14  root     5660:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5661:                                                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5662:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5663:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5664:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5665:                                        }
                   5666:                                        co.X = 0;
                   5667:                                } else if(data == 'h') {
                   5668:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5669:                                                ci_new.bVisible = FALSE;
1.1       root     5670:                                        }
                   5671:                                } else if(data == 'l') {
                   5672:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5673:                                                ci_new.bVisible = TRUE;
1.1       root     5674:                                        }
                   5675:                                } else if(data == 'm') {
                   5676:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5677:                                        int reverse = 0, hidden = 0;
                   5678:                                        for(int i = 0; i < params; i++) {
                   5679:                                                if(param[i] == 1) {
                   5680:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5681:                                                } else if(param[i] == 4) {
                   5682:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5683:                                                } else if(param[i] == 7) {
                   5684:                                                        reverse = 1;
                   5685:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5686:                                                        hidden = 1;
                   5687:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5688:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5689:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5690:                                                                param[i] -= 16;
                   5691:                                                        } else {
                   5692:                                                                param[i] -= 30;
                   5693:                                                        }
                   5694:                                                        if(param[i] & 1) {
                   5695:                                                                wAttributes |= FOREGROUND_RED;
                   5696:                                                        }
                   5697:                                                        if(param[i] & 2) {
                   5698:                                                                wAttributes |= FOREGROUND_GREEN;
                   5699:                                                        }
                   5700:                                                        if(param[i] & 4) {
                   5701:                                                                wAttributes |= FOREGROUND_BLUE;
                   5702:                                                        }
                   5703:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5704:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5705:                                                        if((param[i] - 40) & 1) {
                   5706:                                                                wAttributes |= BACKGROUND_RED;
                   5707:                                                        }
                   5708:                                                        if((param[i] - 40) & 2) {
                   5709:                                                                wAttributes |= BACKGROUND_GREEN;
                   5710:                                                        }
                   5711:                                                        if((param[i] - 40) & 4) {
                   5712:                                                                wAttributes |= BACKGROUND_BLUE;
                   5713:                                                        }
                   5714:                                                }
                   5715:                                        }
                   5716:                                        if(reverse) {
                   5717:                                                wAttributes &= ~0xff;
                   5718:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5719:                                        }
                   5720:                                        if(hidden) {
                   5721:                                                wAttributes &= ~0x0f;
                   5722:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5723:                                        }
                   5724:                                } else if(data == 'n') {
                   5725:                                        if(param[0] == 6) {
                   5726:                                                char tmp[16];
                   5727:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5728:                                                int len = strlen(tmp);
1.1.1.32  root     5729:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5730: #ifdef USE_SERVICE_THREAD
                   5731:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5732: #endif
1.1.1.32  root     5733:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5734:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5735:                                                        }
1.1.1.35  root     5736: #ifdef USE_SERVICE_THREAD
                   5737:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5738: #endif
1.1       root     5739:                                                }
                   5740:                                        }
                   5741:                                } else if(data == 's') {
                   5742:                                        stored_x = co.X;
                   5743:                                        stored_y = co.Y;
                   5744:                                        stored_a = wAttributes;
                   5745:                                } else if(data == 'u') {
                   5746:                                        co.X = stored_x;
                   5747:                                        co.Y = stored_y;
                   5748:                                        wAttributes = stored_a;
                   5749:                                }
                   5750:                        }
                   5751:                        if(co.X < 0) {
                   5752:                                co.X = 0;
                   5753:                        } else if(co.X >= csbi.dwSize.X) {
                   5754:                                co.X = csbi.dwSize.X - 1;
                   5755:                        }
1.1.1.14  root     5756:                        if(co.Y < csbi.srWindow.Top) {
                   5757:                                co.Y = csbi.srWindow.Top;
                   5758:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5759:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5760:                        }
                   5761:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5762:                                SetConsoleCursorPosition(hStdout, co);
                   5763:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5764:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5765:                                cursor_moved = false;
                   5766:                        }
                   5767:                        if(wAttributes != csbi.wAttributes) {
                   5768:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5769:                        }
                   5770:                        p = is_esc = 0;
                   5771:                }
                   5772:                return;
                   5773:        } else {
                   5774:                if(msdos_lead_byte_check(data)) {
                   5775:                        is_kanji = 1;
                   5776:                        return;
                   5777:                } else if(data == 0x1b) {
                   5778:                        is_esc = 1;
                   5779:                        return;
                   5780:                }
                   5781:        }
1.1.1.20  root     5782:        
                   5783:        DWORD q = 0, num;
                   5784:        is_kanji = 0;
                   5785:        for(int i = 0; i < p; i++) {
                   5786:                UINT8 c = tmp[i];
                   5787:                if(is_kanji) {
                   5788:                        is_kanji = 0;
                   5789:                } else if(msdos_lead_byte_check(data)) {
                   5790:                        is_kanji = 1;
                   5791:                } else if(msdos_ctrl_code_check(data)) {
                   5792:                        out[q++] = '^';
                   5793:                        c += 'A' - 1;
                   5794:                }
                   5795:                out[q++] = c;
                   5796:        }
1.1.1.59  root     5797:        if(cursor_moved_by_crtc) {
                   5798:                if(!restore_console_on_exit) {
                   5799:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5800:                        scr_top = csbi.srWindow.Top;
                   5801:                }
                   5802:                co.X = mem[0x450 + REG8(BH) * 2];
                   5803:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5804:                SetConsoleCursorPosition(hStdout, co);
                   5805:                cursor_moved_by_crtc = false;
                   5806:        }
1.1.1.34  root     5807:        if(q == 1 && out[0] == 0x08) {
                   5808:                // back space
                   5809:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5810:                if(csbi.dwCursorPosition.X > 0) {
                   5811:                        co.X = csbi.dwCursorPosition.X - 1;
                   5812:                        co.Y = csbi.dwCursorPosition.Y;
                   5813:                        SetConsoleCursorPosition(hStdout, co);
                   5814:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5815:                        co.X = csbi.dwSize.X - 1;
                   5816:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5817:                        SetConsoleCursorPosition(hStdout, co);
                   5818:                } else {
1.1.1.60  root     5819:                        WriteConsoleA(hStdout, out, q, &num, NULL); // to make sure
1.1.1.34  root     5820:                }
                   5821:        } else {
1.1.1.60  root     5822:                WriteConsoleA(hStdout, out, q, &num, NULL);
1.1.1.34  root     5823:        }
1.1       root     5824:        p = 0;
1.1.1.14  root     5825:        
1.1.1.15  root     5826:        if(!restore_console_on_exit) {
                   5827:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5828:                scr_top = csbi.srWindow.Top;
                   5829:        }
1.1       root     5830:        cursor_moved = true;
                   5831: }
                   5832: 
                   5833: int msdos_aux_in()
                   5834: {
1.1.1.21  root     5835:        msdos_stdio_reopen();
                   5836:        
1.1.1.20  root     5837:        process_t *process = msdos_process_info_get(current_psp);
                   5838:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5839:        
                   5840:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5841:                char data = 0;
1.1.1.37  root     5842:                msdos_read(fd, &data, 1);
1.1       root     5843:                return(data);
                   5844:        } else {
                   5845:                return(EOF);
                   5846:        }
                   5847: }
                   5848: 
                   5849: void msdos_aux_out(char data)
                   5850: {
1.1.1.21  root     5851:        msdos_stdio_reopen();
                   5852:        
1.1.1.20  root     5853:        process_t *process = msdos_process_info_get(current_psp);
                   5854:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5855:        
                   5856:        if(fd < process->max_files && file_handler[fd].valid) {
                   5857:                msdos_write(fd, &data, 1);
1.1       root     5858:        }
                   5859: }
                   5860: 
                   5861: void msdos_prn_out(char data)
                   5862: {
1.1.1.21  root     5863:        msdos_stdio_reopen();
                   5864:        
1.1.1.20  root     5865:        process_t *process = msdos_process_info_get(current_psp);
                   5866:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5867:        
                   5868:        if(fd < process->max_files && file_handler[fd].valid) {
                   5869:                msdos_write(fd, &data, 1);
1.1       root     5870:        }
                   5871: }
                   5872: 
                   5873: // memory control
                   5874: 
1.1.1.52  root     5875: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5876: {
                   5877:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5878:        
                   5879:        mcb->mz = mz;
                   5880:        mcb->psp = psp;
1.1.1.30  root     5881:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5882:        
                   5883:        if(prog_name != NULL) {
                   5884:                memset(mcb->prog_name, 0, 8);
                   5885:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5886:        }
1.1       root     5887:        return(mcb);
                   5888: }
                   5889: 
                   5890: void msdos_mcb_check(mcb_t *mcb)
                   5891: {
                   5892:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5893:                #if 0
                   5894:                        // shutdown now !!!
                   5895:                        fatalerror("broken memory control block\n");
                   5896:                #else
                   5897:                        // return error code and continue
                   5898:                        throw(0x07); // broken memory control block
                   5899:                #endif
1.1       root     5900:        }
                   5901: }
                   5902: 
1.1.1.39  root     5903: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5904: {
                   5905:        int mcb_seg = seg - 1;
                   5906:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5907:        msdos_mcb_check(mcb);
                   5908:        
1.1.1.30  root     5909:        if(mcb->paragraphs > paragraphs) {
1.1       root     5910:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5911:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5912:                
                   5913:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5914:                mcb->mz = 'M';
1.1.1.30  root     5915:                mcb->paragraphs = paragraphs;
1.1       root     5916:        }
                   5917: }
                   5918: 
                   5919: void msdos_mem_merge(int seg)
                   5920: {
                   5921:        int mcb_seg = seg - 1;
                   5922:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5923:        msdos_mcb_check(mcb);
                   5924:        
                   5925:        while(1) {
                   5926:                if(mcb->mz == 'Z') {
                   5927:                        break;
                   5928:                }
1.1.1.30  root     5929:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5930:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5931:                msdos_mcb_check(next_mcb);
                   5932:                
                   5933:                if(next_mcb->psp != 0) {
                   5934:                        break;
                   5935:                }
                   5936:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5937:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5938:        }
                   5939: }
                   5940: 
1.1.1.8   root     5941: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5942: {
                   5943:        while(1) {
                   5944:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5945:                bool last_block;
1.1       root     5946:                
1.1.1.14  root     5947:                if(mcb->psp == 0) {
                   5948:                        msdos_mem_merge(mcb_seg + 1);
                   5949:                } else {
                   5950:                        msdos_mcb_check(mcb);
                   5951:                }
1.1.1.33  root     5952:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5953:                        // check if the next is dummy mcb to link to umb
                   5954:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5955:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5956:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5957:                }
                   5958:                if(!(new_process && !last_block)) {
1.1.1.30  root     5959:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5960:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5961:                                mcb->psp = current_psp;
                   5962:                                return(mcb_seg + 1);
                   5963:                        }
                   5964:                }
                   5965:                if(mcb->mz == 'Z') {
                   5966:                        break;
                   5967:                }
1.1.1.30  root     5968:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5969:        }
                   5970:        return(-1);
                   5971: }
                   5972: 
                   5973: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5974: {
                   5975:        int mcb_seg = seg - 1;
                   5976:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5977:        msdos_mcb_check(mcb);
1.1.1.30  root     5978:        int current_paragraphs = mcb->paragraphs;
1.1       root     5979:        
                   5980:        msdos_mem_merge(seg);
1.1.1.30  root     5981:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5982:                if(max_paragraphs) {
1.1.1.30  root     5983:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5984:                }
1.1       root     5985:                msdos_mem_split(seg, current_paragraphs);
                   5986:                return(-1);
                   5987:        }
                   5988:        msdos_mem_split(seg, paragraphs);
                   5989:        return(0);
                   5990: }
                   5991: 
                   5992: void msdos_mem_free(int seg)
                   5993: {
                   5994:        int mcb_seg = seg - 1;
                   5995:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5996:        msdos_mcb_check(mcb);
                   5997:        
                   5998:        mcb->psp = 0;
                   5999:        msdos_mem_merge(seg);
                   6000: }
                   6001: 
1.1.1.8   root     6002: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     6003: {
                   6004:        int max_paragraphs = 0;
                   6005:        
                   6006:        while(1) {
                   6007:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     6008:                bool last_block;
                   6009:                
1.1       root     6010:                msdos_mcb_check(mcb);
                   6011:                
1.1.1.33  root     6012:                if(!(last_block = (mcb->mz == 'Z'))) {
                   6013:                        // check if the next is dummy mcb to link to umb
                   6014:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   6015:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   6016:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   6017:                }
                   6018:                if(!(new_process && !last_block)) {
1.1.1.30  root     6019:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   6020:                                max_paragraphs = mcb->paragraphs;
1.1       root     6021:                        }
                   6022:                }
                   6023:                if(mcb->mz == 'Z') {
                   6024:                        break;
                   6025:                }
1.1.1.30  root     6026:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     6027:        }
1.1.1.14  root     6028:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     6029: }
                   6030: 
1.1.1.8   root     6031: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   6032: {
                   6033:        int last_seg = -1;
                   6034:        
                   6035:        while(1) {
                   6036:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   6037:                msdos_mcb_check(mcb);
                   6038:                
1.1.1.14  root     6039:                if(mcb->psp == psp) {
1.1.1.8   root     6040:                        last_seg = mcb_seg;
                   6041:                }
1.1.1.14  root     6042:                if(mcb->mz == 'Z') {
                   6043:                        break;
                   6044:                }
1.1.1.30  root     6045:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     6046:        }
                   6047:        return(last_seg);
                   6048: }
                   6049: 
1.1.1.19  root     6050: int msdos_mem_get_umb_linked()
                   6051: {
1.1.1.33  root     6052:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6053:        msdos_mcb_check(mcb);
1.1.1.19  root     6054:        
1.1.1.33  root     6055:        if(mcb->mz == 'M') {
                   6056:                return(-1);
1.1.1.19  root     6057:        }
                   6058:        return(0);
                   6059: }
                   6060: 
1.1.1.33  root     6061: void msdos_mem_link_umb()
1.1.1.19  root     6062: {
1.1.1.33  root     6063:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6064:        msdos_mcb_check(mcb);
1.1.1.19  root     6065:        
1.1.1.33  root     6066:        mcb->mz = 'M';
                   6067:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     6068:        
                   6069:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     6070: }
                   6071: 
1.1.1.33  root     6072: void msdos_mem_unlink_umb()
1.1.1.19  root     6073: {
1.1.1.33  root     6074:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6075:        msdos_mcb_check(mcb);
1.1.1.19  root     6076:        
1.1.1.33  root     6077:        mcb->mz = 'Z';
                   6078:        mcb->paragraphs = 0;
1.1.1.39  root     6079:        
                   6080:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     6081: }
                   6082: 
1.1.1.29  root     6083: #ifdef SUPPORT_HMA
                   6084: 
                   6085: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   6086: {
                   6087:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6088:        
                   6089:        mcb->ms[0] = 'M';
                   6090:        mcb->ms[1] = 'S';
                   6091:        mcb->owner = owner;
                   6092:        mcb->size = size;
                   6093:        mcb->next = next;
                   6094:        return(mcb);
                   6095: }
                   6096: 
                   6097: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   6098: {
                   6099:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   6100: }
                   6101: 
                   6102: int msdos_hma_mem_split(int offset, int size)
                   6103: {
                   6104:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6105:        
                   6106:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6107:                return(-1);
                   6108:        }
                   6109:        if(mcb->size >= size + 0x10) {
                   6110:                int new_offset = offset + 0x10 + size;
                   6111:                int new_size = mcb->size - 0x10 - size;
                   6112:                
                   6113:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   6114:                mcb->size = size;
                   6115:                mcb->next = new_offset;
                   6116:                return(0);
                   6117:        }
                   6118:        return(-1);
                   6119: }
                   6120: 
                   6121: void msdos_hma_mem_merge(int offset)
                   6122: {
                   6123:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6124:        
                   6125:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6126:                return;
                   6127:        }
                   6128:        while(1) {
                   6129:                if(mcb->next == 0) {
                   6130:                        break;
                   6131:                }
                   6132:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   6133:                
                   6134:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   6135:                        return;
                   6136:                }
                   6137:                if(next_mcb->owner != 0) {
                   6138:                        break;
                   6139:                }
                   6140:                mcb->size += 0x10 + next_mcb->size;
                   6141:                mcb->next = next_mcb->next;
                   6142:        }
                   6143: }
                   6144: 
                   6145: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   6146: {
                   6147:        int offset = 0x10; // first mcb in HMA
                   6148:        
                   6149:        while(1) {
                   6150:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6151:                
                   6152:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6153:                        return(-1);
                   6154:                }
                   6155:                if(mcb->owner == 0) {
                   6156:                        msdos_hma_mem_merge(offset);
                   6157:                }
                   6158:                if(mcb->owner == 0 && mcb->size >= size) {
                   6159:                        msdos_hma_mem_split(offset, size);
                   6160:                        mcb->owner = owner;
                   6161:                        return(offset);
                   6162:                }
                   6163:                if(mcb->next == 0) {
                   6164:                        break;
                   6165:                }
                   6166:                offset = mcb->next;
                   6167:        }
                   6168:        return(-1);
                   6169: }
                   6170: 
                   6171: int msdos_hma_mem_realloc(int offset, int size)
                   6172: {
                   6173:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6174:        
                   6175:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6176:                return(-1);
                   6177:        }
                   6178:        if(mcb->size < size) {
                   6179:                return(-1);
                   6180:        }
                   6181:        msdos_hma_mem_split(offset, size);
                   6182:        return(0);
                   6183: }
                   6184: 
                   6185: void msdos_hma_mem_free(int offset)
                   6186: {
                   6187:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6188:        
                   6189:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6190:                return;
                   6191:        }
                   6192:        mcb->owner = 0;
                   6193:        msdos_hma_mem_merge(offset);
                   6194: }
                   6195: 
                   6196: int msdos_hma_mem_get_free(int *available_offset)
                   6197: {
                   6198:        int offset = 0x10; // first mcb in HMA
                   6199:        int size = 0;
                   6200:        
                   6201:        while(1) {
                   6202:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6203:                
                   6204:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6205:                        return(0);
                   6206:                }
                   6207:                if(mcb->owner == 0 && size < mcb->size) {
                   6208:                        if(available_offset != NULL) {
                   6209:                                *available_offset = offset;
                   6210:                        }
                   6211:                        size = mcb->size;
                   6212:                }
                   6213:                if(mcb->next == 0) {
                   6214:                        break;
                   6215:                }
                   6216:                offset = mcb->next;
                   6217:        }
                   6218:        return(size);
                   6219: }
                   6220: 
                   6221: #endif
                   6222: 
1.1       root     6223: // environment
                   6224: 
1.1.1.45  root     6225: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     6226: {
                   6227:        char *dst = (char *)(mem + (env_seg << 4));
                   6228:        
                   6229:        while(1) {
                   6230:                if(dst[0] == 0) {
                   6231:                        break;
                   6232:                }
                   6233:                dst += strlen(dst) + 1;
                   6234:        }
                   6235:        *dst++ = 0; // end of environment
                   6236:        *dst++ = 1; // top of argv[0]
                   6237:        *dst++ = 0;
                   6238:        memcpy(dst, argv, strlen(argv));
                   6239:        dst += strlen(argv);
                   6240:        *dst++ = 0;
                   6241:        *dst++ = 0;
                   6242: }
                   6243: 
1.1.1.45  root     6244: const char *msdos_env_get_argv(int env_seg)
1.1       root     6245: {
                   6246:        static char env[ENV_SIZE];
                   6247:        char *src = env;
                   6248:        
                   6249:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6250:        while(1) {
                   6251:                if(src[0] == 0) {
                   6252:                        if(src[1] == 1) {
                   6253:                                return(src + 3);
                   6254:                        }
                   6255:                        break;
                   6256:                }
                   6257:                src += strlen(src) + 1;
                   6258:        }
                   6259:        return(NULL);
                   6260: }
                   6261: 
1.1.1.45  root     6262: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     6263: {
                   6264:        static char env[ENV_SIZE];
                   6265:        char *src = env;
                   6266:        
                   6267:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6268:        while(1) {
                   6269:                if(src[0] == 0) {
                   6270:                        break;
                   6271:                }
                   6272:                int len = strlen(src);
                   6273:                char *n = my_strtok(src, "=");
                   6274:                char *v = src + strlen(n) + 1;
                   6275:                
                   6276:                if(_stricmp(name, n) == 0) {
                   6277:                        return(v);
                   6278:                }
                   6279:                src += len + 1;
                   6280:        }
                   6281:        return(NULL);
                   6282: }
                   6283: 
1.1.1.45  root     6284: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     6285: {
                   6286:        char env[ENV_SIZE];
                   6287:        char *src = env;
                   6288:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     6289:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     6290:        int done = 0;
                   6291:        
                   6292:        memcpy(src, dst, ENV_SIZE);
                   6293:        memset(dst, 0, ENV_SIZE);
                   6294:        while(1) {
                   6295:                if(src[0] == 0) {
                   6296:                        break;
                   6297:                }
                   6298:                int len = strlen(src);
                   6299:                char *n = my_strtok(src, "=");
                   6300:                char *v = src + strlen(n) + 1;
                   6301:                char tmp[1024];
                   6302:                
                   6303:                if(_stricmp(name, n) == 0) {
                   6304:                        sprintf(tmp, "%s=%s", n, value);
                   6305:                        done = 1;
                   6306:                } else {
                   6307:                        sprintf(tmp, "%s=%s", n, v);
                   6308:                }
                   6309:                memcpy(dst, tmp, strlen(tmp));
                   6310:                dst += strlen(tmp) + 1;
                   6311:                src += len + 1;
                   6312:        }
                   6313:        if(!done) {
                   6314:                char tmp[1024];
                   6315:                
                   6316:                sprintf(tmp, "%s=%s", name, value);
                   6317:                memcpy(dst, tmp, strlen(tmp));
                   6318:                dst += strlen(tmp) + 1;
                   6319:        }
                   6320:        if(argv) {
                   6321:                *dst++ = 0; // end of environment
                   6322:                *dst++ = 1; // top of argv[0]
                   6323:                *dst++ = 0;
                   6324:                memcpy(dst, argv, strlen(argv));
                   6325:                dst += strlen(argv);
                   6326:                *dst++ = 0;
                   6327:                *dst++ = 0;
                   6328:        }
                   6329: }
                   6330: 
                   6331: // process
                   6332: 
1.1.1.8   root     6333: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     6334: {
                   6335:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6336:        
                   6337:        memset(psp, 0, PSP_SIZE);
                   6338:        psp->exit[0] = 0xcd;
                   6339:        psp->exit[1] = 0x20;
1.1.1.8   root     6340:        psp->first_mcb = mcb_seg;
1.1.1.46  root     6341: #if 1
1.1.1.49  root     6342:        psp->call5[0] = 0xcd;   // int 30h
                   6343:        psp->call5[1] = 0x30;
1.1.1.46  root     6344:        psp->call5[2] = 0xc3;   // ret
                   6345: #else
                   6346:        psp->call5[0] = 0x8a;   // mov ah, cl
                   6347:        psp->call5[1] = 0xe1;
                   6348:        psp->call5[2] = 0xcd;   // int 21h
                   6349:        psp->call5[3] = 0x21;
                   6350:        psp->call5[4] = 0xc3;   // ret
                   6351: #endif
1.1       root     6352:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6353:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6354:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6355:        psp->parent_psp = parent_psp;
1.1.1.20  root     6356:        if(parent_psp == (UINT16)-1) {
                   6357:                for(int i = 0; i < 20; i++) {
                   6358:                        if(file_handler[i].valid) {
                   6359:                                psp->file_table[i] = i;
                   6360:                        } else {
                   6361:                                psp->file_table[i] = 0xff;
                   6362:                        }
1.1       root     6363:                }
1.1.1.20  root     6364:        } else {
                   6365:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     6366:        }
                   6367:        psp->env_seg = env_seg;
                   6368:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6369:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6370:        psp->file_table_size = 20;
                   6371:        psp->file_table_ptr.w.l = 0x18;
                   6372:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6373:        psp->service[0] = 0xcd;
                   6374:        psp->service[1] = 0x21;
                   6375:        psp->service[2] = 0xcb;
                   6376:        return(psp);
                   6377: }
                   6378: 
1.1.1.20  root     6379: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6380: {
                   6381:        if(psp_seg && fd < 20) {
                   6382:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6383:                psp->file_table[fd] = value;
                   6384:        }
                   6385: }
                   6386: 
                   6387: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6388: {
                   6389:        if(psp_seg && fd < 20) {
                   6390:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6391:                fd = psp->file_table[fd];
                   6392:        }
                   6393:        return fd;
                   6394: }
                   6395: 
1.1.1.52  root     6396: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     6397: {
                   6398:        // load command file
                   6399:        int fd = -1;
1.1.1.45  root     6400:        int sio_port = 0;
                   6401:        int lpt_port = 0;
1.1       root     6402:        int dos_command = 0;
1.1.1.24  root     6403:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6404:        char pipe_stdin_path[MAX_PATH] = {0};
                   6405:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6406:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6407:        
                   6408:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6409:        int opt_len = mem[opt_ofs];
                   6410:        memset(opt, 0, sizeof(opt));
                   6411:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6412:        
1.1.1.14  root     6413:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6414:                // this is a batch file, run command.com
                   6415:                char tmp[MAX_PATH];
                   6416:                if(opt_len != 0) {
                   6417:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6418:                } else {
                   6419:                        sprintf(tmp, "/C %s", cmd);
                   6420:                }
                   6421:                strcpy(opt, tmp);
                   6422:                opt_len = strlen(opt);
                   6423:                mem[opt_ofs] = opt_len;
                   6424:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6425:                strcpy(command, comspec_path);
                   6426:                strcpy(name_tmp, "COMMAND.COM");
                   6427:        } else {
                   6428:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6429:                        // redirect C:\COMMAND.COM to comspec_path
                   6430:                        strcpy(command, comspec_path);
                   6431:                } else {
                   6432:                        strcpy(command, cmd);
                   6433:                }
1.1.1.60  root     6434:                if(GetFullPathNameA(command, MAX_PATH, path, &name) == 0) {
1.1.1.24  root     6435:                        return(-1);
                   6436:                }
1.1.1.14  root     6437:                memset(name_tmp, 0, sizeof(name_tmp));
                   6438:                strcpy(name_tmp, name);
                   6439:                
                   6440:                // check command.com
1.1.1.38  root     6441:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6442:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6443:                        if(opt_len == 0) {
                   6444: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6445:                                process_t *current_process = NULL;
                   6446:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6447:                                        if(process[i].psp == current_psp) {
                   6448:                                                current_process = &process[i];
                   6449:                                                break;
                   6450:                                        }
                   6451:                                }
                   6452:                                if(current_process != NULL) {
                   6453:                                        param->cmd_line.dw = current_process->dta.dw;
                   6454:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6455:                                        opt_len = mem[opt_ofs];
                   6456:                                        memset(opt, 0, sizeof(opt));
                   6457:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6458:                                }
                   6459:                        }
                   6460:                        for(int i = 0; i < opt_len; i++) {
                   6461:                                if(opt[i] == ' ') {
                   6462:                                        continue;
                   6463:                                }
                   6464:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6465:                                        for(int j = i + 3; j < opt_len; j++) {
                   6466:                                                if(opt[j] == ' ') {
                   6467:                                                        continue;
                   6468:                                                }
                   6469:                                                char *token = my_strtok(opt + j, " ");
                   6470:                                                
1.1.1.38  root     6471:                                                strcpy(command, token);
                   6472:                                                char tmp[MAX_PATH];
                   6473:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6474:                                                strcpy(opt, "");
                   6475:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6476:                                                        if(tmp[i] != ' ') {
                   6477:                                                                strcpy(opt, tmp + i);
                   6478:                                                                break;
                   6479:                                                        }
                   6480:                                                }
                   6481:                                                strcpy(tmp, opt);
1.1.1.38  root     6482:                                                
                   6483:                                                if(al == 0x00) {
1.1.1.39  root     6484:                                                        #define GET_FILE_PATH() { \
                   6485:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6486:                                                                        token++; \
                   6487:                                                                } \
                   6488:                                                                token++; \
                   6489:                                                                while(*token == ' ') { \
                   6490:                                                                        token++; \
                   6491:                                                                } \
                   6492:                                                                char *ptr = token; \
                   6493:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6494:                                                                        ptr++; \
                   6495:                                                                } \
                   6496:                                                                *ptr = '\0'; \
                   6497:                                                        }
                   6498:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6499:                                                                GET_FILE_PATH();
1.1.1.38  root     6500:                                                                strcpy(pipe_stdin_path, token);
                   6501:                                                                strcpy(opt, tmp);
                   6502:                                                        }
1.1.1.39  root     6503:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6504:                                                                GET_FILE_PATH();
1.1.1.38  root     6505:                                                                strcpy(pipe_stdout_path, token);
                   6506:                                                                strcpy(opt, tmp);
                   6507:                                                        }
1.1.1.39  root     6508:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6509:                                                                GET_FILE_PATH();
                   6510:                                                                strcpy(pipe_stderr_path, token);
                   6511:                                                                strcpy(opt, tmp);
                   6512:                                                        }
                   6513:                                                        #undef GET_FILE_PATH
                   6514:                                                        
                   6515:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6516:                                                                *token = '\0';
                   6517:                                                        }
                   6518:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6519:                                                                *token = '\0';
                   6520:                                                        }
                   6521:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6522:                                                                *token = '\0';
                   6523:                                                        }
1.1.1.38  root     6524:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6525:                                                                *token = '\0';
                   6526:                                                        }
                   6527:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6528:                                                                *token = '\0';
                   6529:                                                        }
1.1.1.14  root     6530:                                                }
1.1.1.39  root     6531:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6532:                                                        opt[i] = '\0';
                   6533:                                                }
1.1.1.38  root     6534:                                                opt_len = strlen(opt);
                   6535:                                                mem[opt_ofs] = opt_len;
                   6536:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6537:                                                dos_command = 1;
1.1.1.14  root     6538:                                                break;
1.1       root     6539:                                        }
                   6540:                                }
1.1.1.14  root     6541:                                break;
1.1       root     6542:                        }
                   6543:                }
                   6544:        }
                   6545:        
                   6546:        // load command file
                   6547:        strcpy(path, command);
                   6548:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6549:                sprintf(path, "%s.COM", command);
                   6550:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6551:                        sprintf(path, "%s.EXE", command);
                   6552:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6553:                                sprintf(path, "%s.BAT", command);
                   6554:                                if(_access(path, 0) == 0) {
                   6555:                                        // this is a batch file, run command.com
                   6556:                                        char tmp[MAX_PATH];
                   6557:                                        if(opt_len != 0) {
                   6558:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6559:                                        } else {
                   6560:                                                sprintf(tmp, "/C %s", path);
                   6561:                                        }
                   6562:                                        strcpy(opt, tmp);
                   6563:                                        opt_len = strlen(opt);
                   6564:                                        mem[opt_ofs] = opt_len;
                   6565:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6566:                                        strcpy(path, comspec_path);
                   6567:                                        strcpy(name_tmp, "COMMAND.COM");
                   6568:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6569:                                } else {
                   6570:                                        // search path in parent environments
                   6571:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6572:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6573:                                        if(env != NULL) {
                   6574:                                                char env_path[4096];
                   6575:                                                strcpy(env_path, env);
                   6576:                                                char *token = my_strtok(env_path, ";");
                   6577:                                                
                   6578:                                                while(token != NULL) {
                   6579:                                                        if(strlen(token) != 0) {
                   6580:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6581:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6582:                                                                        break;
                   6583:                                                                }
                   6584:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6585:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6586:                                                                        break;
                   6587:                                                                }
                   6588:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6589:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6590:                                                                        break;
                   6591:                                                                }
                   6592:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6593:                                                                if(_access(path, 0) == 0) {
                   6594:                                                                        // this is a batch file, run command.com
                   6595:                                                                        char tmp[MAX_PATH];
                   6596:                                                                        if(opt_len != 0) {
                   6597:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6598:                                                                        } else {
                   6599:                                                                                sprintf(tmp, "/C %s", path);
                   6600:                                                                        }
                   6601:                                                                        strcpy(opt, tmp);
                   6602:                                                                        opt_len = strlen(opt);
                   6603:                                                                        mem[opt_ofs] = opt_len;
                   6604:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6605:                                                                        strcpy(path, comspec_path);
                   6606:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6607:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6608:                                                                        break;
                   6609:                                                                }
1.1.1.8   root     6610:                                                        }
1.1.1.14  root     6611:                                                        token = my_strtok(NULL, ";");
1.1       root     6612:                                                }
                   6613:                                        }
                   6614:                                }
                   6615:                        }
                   6616:                }
                   6617:        }
                   6618:        if(fd == -1) {
1.1.1.38  root     6619:                // we can not find command.com in the path, so open comspec_path
                   6620:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6621:                        strcpy(command, comspec_path);
                   6622:                        strcpy(path, command);
                   6623:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6624:                }
                   6625:        }
                   6626:        if(fd == -1) {
1.1.1.52  root     6627:                if(!first_process && al == 0 && dos_command) {
1.1       root     6628:                        // may be dos command
                   6629:                        char tmp[MAX_PATH];
1.1.1.52  root     6630:                        if(opt_len != 0) {
                   6631:                                sprintf(tmp, "%s %s", command, opt);
                   6632:                        } else {
                   6633:                                sprintf(tmp, "%s", command);
                   6634:                        }
                   6635:                        retval = system(tmp);
1.1       root     6636:                        return(0);
                   6637:                } else {
                   6638:                        return(-1);
                   6639:                }
                   6640:        }
1.1.1.52  root     6641:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6642:        _read(fd, file_buffer, sizeof(file_buffer));
                   6643:        _close(fd);
                   6644:        
1.1.1.52  root     6645:        // check if this is win32 program
                   6646:        if(!first_process && al == 0) {
                   6647:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
                   6648:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
                   6649:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
                   6650:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
                   6651:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
                   6652:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
                   6653:                                char tmp[MAX_PATH];
                   6654:                                if(opt_len != 0) {
                   6655:                                        sprintf(tmp, "\"%s\" %s", path, opt);
                   6656:                                } else {
                   6657:                                        sprintf(tmp, "\"%s\"", path);
                   6658:                                }
                   6659:                                retval = system(tmp);
                   6660:                                return(0);
                   6661:                        }
                   6662:                }
                   6663:        }
                   6664:        
1.1       root     6665:        // copy environment
1.1.1.29  root     6666:        int umb_linked, env_seg, psp_seg;
1.1       root     6667:        
1.1.1.29  root     6668:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6669:                msdos_mem_unlink_umb();
                   6670:        }
1.1.1.8   root     6671:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6672:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6673:                        if(umb_linked != 0) {
                   6674:                                msdos_mem_link_umb();
                   6675:                        }
                   6676:                        return(-1);
                   6677:                }
1.1       root     6678:        }
                   6679:        if(param->env_seg == 0) {
                   6680:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6681:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6682:        } else {
                   6683:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6684:        }
                   6685:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6686:        
                   6687:        // check exe header
                   6688:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6689:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6690:        UINT16 cs, ss, ip, sp;
                   6691:        
                   6692:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6693:                // memory allocation
                   6694:                int header_size = header->header_size * 16;
                   6695:                int load_size = header->pages * 512 - header_size;
                   6696:                if(header_size + load_size < 512) {
                   6697:                        load_size = 512 - header_size;
                   6698:                }
                   6699:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6700:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6701:                        msdos_mem_free(env_seg);
                   6702:                        return(-1);
                   6703:                }
                   6704:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6705:                if(paragraphs > free_paragraphs) {
                   6706:                        paragraphs = free_paragraphs;
                   6707:                }
1.1.1.8   root     6708:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6709:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6710:                                if(umb_linked != 0) {
                   6711:                                        msdos_mem_link_umb();
                   6712:                                }
                   6713:                                msdos_mem_free(env_seg);
                   6714:                                return(-1);
                   6715:                        }
1.1       root     6716:                }
                   6717:                // relocation
                   6718:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6719:                for(int i = 0; i < header->relocations; i++) {
                   6720:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6721:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6722:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6723:                }
                   6724:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6725:                // segments
                   6726:                cs = header->init_cs + start_seg;
                   6727:                ss = header->init_ss + start_seg;
                   6728:                ip = header->init_ip;
                   6729:                sp = header->init_sp - 2; // for symdeb
                   6730:        } else {
                   6731:                // memory allocation
                   6732:                paragraphs = free_paragraphs;
1.1.1.8   root     6733:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6734:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6735:                                if(umb_linked != 0) {
                   6736:                                        msdos_mem_link_umb();
                   6737:                                }
                   6738:                                msdos_mem_free(env_seg);
                   6739:                                return(-1);
                   6740:                        }
1.1       root     6741:                }
                   6742:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6743:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6744:                // segments
                   6745:                cs = ss = psp_seg;
                   6746:                ip = 0x100;
                   6747:                sp = 0xfffe;
                   6748:        }
1.1.1.29  root     6749:        if(umb_linked != 0) {
                   6750:                msdos_mem_link_umb();
                   6751:        }
1.1       root     6752:        
                   6753:        // create psp
1.1.1.3   root     6754:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6755:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6756:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6757:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6758:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6759:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6760:        
                   6761:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6762:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6763:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6764:        
1.1.1.4   root     6765:        for(int i = 0; i < 8; i++) {
                   6766:                if(name_tmp[i] == '.') {
                   6767:                        mcb_psp->prog_name[i] = '\0';
                   6768:                        break;
                   6769:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6770:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6771:                        i++;
                   6772:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6773:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6774:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6775:                } else {
                   6776:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6777:                }
                   6778:        }
                   6779:        
1.1       root     6780:        // process info
                   6781:        process_t *process = msdos_process_info_create(psp_seg);
                   6782:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6783: #ifdef USE_DEBUGGER
                   6784:        strcpy(process->module_path, path);
                   6785: #endif
1.1       root     6786:        process->dta.w.l = 0x80;
                   6787:        process->dta.w.h = psp_seg;
                   6788:        process->switchar = '/';
                   6789:        process->max_files = 20;
                   6790:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6791:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6792:        process->parent_ds = SREG(DS);
1.1.1.31  root     6793:        process->parent_es = SREG(ES);
1.1       root     6794:        
                   6795:        current_psp = psp_seg;
1.1.1.23  root     6796:        msdos_sda_update(current_psp);
1.1       root     6797:        
                   6798:        if(al == 0x00) {
                   6799:                int_10h_feh_called = int_10h_ffh_called = false;
                   6800:                
1.1.1.38  root     6801:                // pipe
                   6802:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6803: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6804:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6805:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6806:                        } else {
                   6807:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6808:                        }
                   6809:                        if(fd != -1) {
                   6810:                                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     6811:                                psp->file_table[0] = fd;
                   6812:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6813:                        }
                   6814:                }
                   6815:                if(pipe_stdout_path[0] != '\0') {
                   6816:                        if(_access(pipe_stdout_path, 0) == 0) {
1.1.1.60  root     6817:                                SetFileAttributesA(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6818:                                DeleteFileA(pipe_stdout_path);
1.1.1.38  root     6819:                        }
1.1.1.45  root     6820: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6821:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6822:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6823:                        } else {
                   6824:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6825:                        }
                   6826:                        if(fd != -1) {
                   6827:                                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     6828:                                psp->file_table[1] = fd;
                   6829:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6830:                        }
                   6831:                }
1.1.1.39  root     6832:                if(pipe_stderr_path[0] != '\0') {
                   6833:                        if(_access(pipe_stderr_path, 0) == 0) {
1.1.1.60  root     6834:                                SetFileAttributesA(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6835:                                DeleteFileA(pipe_stderr_path);
1.1.1.39  root     6836:                        }
1.1.1.45  root     6837: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6838:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6839:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6840:                        } else {
                   6841:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6842:                        }
                   6843:                        if(fd != -1) {
                   6844:                                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     6845:                                psp->file_table[2] = fd;
                   6846:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6847:                        }
                   6848:                }
1.1.1.38  root     6849:                
1.1       root     6850:                // registers and segments
                   6851:                REG16(AX) = REG16(BX) = 0x00;
                   6852:                REG16(CX) = 0xff;
                   6853:                REG16(DX) = psp_seg;
                   6854:                REG16(SI) = ip;
                   6855:                REG16(DI) = sp;
                   6856:                REG16(SP) = sp;
1.1.1.3   root     6857:                SREG(DS) = SREG(ES) = psp_seg;
                   6858:                SREG(SS) = ss;
                   6859:                i386_load_segment_descriptor(DS);
                   6860:                i386_load_segment_descriptor(ES);
                   6861:                i386_load_segment_descriptor(SS);
1.1       root     6862:                
                   6863:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6864:                i386_jmp_far(cs, ip);
                   6865:        } else if(al == 0x01) {
                   6866:                // copy ss:sp and cs:ip to param block
                   6867:                param->sp = sp;
                   6868:                param->ss = ss;
                   6869:                param->ip = ip;
                   6870:                param->cs = cs;
1.1.1.31  root     6871:                
                   6872:                // the AX value to be passed to the child program is put on top of the child's stack
                   6873:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6874:        }
                   6875:        return(0);
                   6876: }
                   6877: 
                   6878: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6879: {
                   6880:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6881:        
                   6882:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6883:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6884:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6885:        
1.1.1.3   root     6886:        SREG(SS) = psp->stack.w.h;
                   6887:        i386_load_segment_descriptor(SS);
1.1       root     6888:        REG16(SP) = psp->stack.w.l;
                   6889:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6890:        
1.1.1.28  root     6891: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6892:        process_t *current_process = NULL;
                   6893:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6894:                if(process[i].psp == psp_seg) {
                   6895:                        current_process = &process[i];
                   6896:                        break;
                   6897:                }
                   6898:        }
                   6899:        if(current_process == NULL) {
                   6900:                throw(0x1f); // general failure
                   6901:        }
                   6902:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6903:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6904:        if(current_process->called_by_int2eh) {
                   6905:                REG16(AX) = ret;
                   6906:        }
                   6907:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6908:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6909:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6910:        i386_load_segment_descriptor(ES);
1.1       root     6911:        
                   6912:        if(mem_free) {
1.1.1.8   root     6913:                int mcb_seg;
                   6914:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6915:                        msdos_mem_free(mcb_seg + 1);
                   6916:                }
                   6917:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6918:                        msdos_mem_free(mcb_seg + 1);
                   6919:                }
1.1       root     6920:                
                   6921:                for(int i = 0; i < MAX_FILES; i++) {
                   6922:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6923:                                _close(i);
1.1.1.20  root     6924:                                msdos_file_handler_close(i);
                   6925:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6926:                        }
                   6927:                }
1.1.1.13  root     6928:                msdos_dta_info_free(psp_seg);
1.1       root     6929:        }
1.1.1.14  root     6930:        msdos_stdio_reopen();
1.1       root     6931:        
1.1.1.28  root     6932:        memset(current_process, 0, sizeof(process_t));
1.1       root     6933:        
                   6934:        current_psp = psp->parent_psp;
                   6935:        retval = ret;
1.1.1.23  root     6936:        msdos_sda_update(current_psp);
1.1       root     6937: }
                   6938: 
                   6939: // drive
                   6940: 
1.1.1.42  root     6941: int pcbios_update_drive_param(int drive_num, int force_update);
                   6942: 
1.1       root     6943: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6944: {
1.1.1.41  root     6945:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6946:                return(0);
                   6947:        }
1.1.1.42  root     6948:        pcbios_update_drive_param(drive_num, force_update);
                   6949:        
                   6950:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6951:        *seg = DPB_TOP >> 4;
                   6952:        *ofs = sizeof(dpb_t) * drive_num;
                   6953:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6954:        
                   6955:        memset(dpb, 0, sizeof(dpb_t));
                   6956:        
1.1.1.41  root     6957:        dpb->drive_num = drive_num;
                   6958:        dpb->unit_num = drive_num;
1.1.1.42  root     6959:        
                   6960:        if(drive_param->valid) {
                   6961:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6962:                
                   6963:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6964:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6965:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6966:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6967:                switch(geo->MediaType) {
                   6968:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6969:                        dpb->media_type = 0xff;
                   6970:                        break;
                   6971:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6972:                        dpb->media_type = 0xfe;
                   6973:                        break;
                   6974:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6975:                        dpb->media_type = 0xfd;
                   6976:                        break;
                   6977:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6978:                        dpb->media_type = 0xfc;
                   6979:                        break;
                   6980:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6981:                case F3_1Pt2_512:
                   6982:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6983:                case F5_720_512:
                   6984:                        dpb->media_type = 0xf9;
                   6985:                        break;
                   6986:                case FixedMedia:        // hard disk
                   6987:                case RemovableMedia:
                   6988:                case Unknown:
                   6989:                        dpb->media_type = 0xf8;
                   6990:                        break;
                   6991:                default:
                   6992:                        dpb->media_type = 0xf0;
                   6993:                        break;
                   6994:                }
                   6995:        }
1.1.1.41  root     6996:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6997:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6998:        dpb->info_sector = 0xffff;
                   6999:        dpb->backup_boot_sector = 0xffff;
                   7000:        dpb->free_clusters = 0xffff;
                   7001:        dpb->free_search_cluster = 0xffffffff;
                   7002:        
                   7003:        return(drive_param->valid);
1.1       root     7004: }
                   7005: 
                   7006: // pc bios
                   7007: 
1.1.1.35  root     7008: #ifdef USE_SERVICE_THREAD
                   7009: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   7010: {
                   7011: #if defined(HAS_I386)
                   7012:        if(m_SF != 0) {
                   7013:                m_SF = 0;
1.1.1.49  root     7014:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     7015:        } else {
                   7016:                m_SF = 1;
1.1.1.49  root     7017:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     7018:        }
                   7019: #else
                   7020:        if(m_SignVal < 0) {
                   7021:                m_SignVal = 0;
1.1.1.49  root     7022:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     7023:        } else {
                   7024:                m_SignVal = -1;
1.1.1.49  root     7025:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     7026:        }
                   7027: #endif
1.1.1.59  root     7028:        // dummy loop to wait BIOS/DOS service is done is at fffc:0013
1.1.1.49  root     7029:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     7030:        in_service = true;
                   7031:        service_exit = false;
                   7032:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   7033: }
                   7034: 
                   7035: void finish_service_loop()
                   7036: {
                   7037:        if(in_service && service_exit) {
                   7038: #if defined(HAS_I386)
                   7039:                if(m_SF != 0) {
                   7040:                        m_SF = 0;
                   7041:                } else {
                   7042:                        m_SF = 1;
                   7043:                }
                   7044: #else
                   7045:                if(m_SignVal < 0) {
                   7046:                        m_SignVal = 0;
                   7047:                } else {
                   7048:                        m_SignVal = -1;
                   7049:                }
                   7050: #endif
                   7051:                in_service = false;
                   7052:        }
                   7053: }
                   7054: #endif
                   7055: 
1.1.1.19  root     7056: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   7057: {
                   7058:        static unsigned __int64 start_msec_since_midnight = 0;
                   7059:        static unsigned __int64 start_msec_since_hostboot = 0;
                   7060:        
                   7061:        if(start_msec_since_midnight == 0) {
                   7062:                SYSTEMTIME time;
                   7063:                GetLocalTime(&time);
                   7064:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   7065:                start_msec_since_hostboot = cur_msec;
                   7066:        }
                   7067:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   7068:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   7069:        return (UINT32)tick;
                   7070: }
                   7071: 
                   7072: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   7073: {
                   7074:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   7075:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   7076:        
                   7077:        if(prev_tick > next_tick) {
                   7078:                mem[0x470] = 1;
                   7079:        }
                   7080:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   7081: }
                   7082: 
1.1.1.14  root     7083: inline void pcbios_irq0()
                   7084: {
                   7085:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     7086:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     7087: }
                   7088: 
1.1.1.16  root     7089: int pcbios_get_text_vram_address(int page)
1.1       root     7090: {
                   7091:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7092:                return TEXT_VRAM_TOP;
1.1       root     7093:        } else {
1.1.1.14  root     7094:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     7095:        }
                   7096: }
                   7097: 
1.1.1.16  root     7098: int pcbios_get_shadow_buffer_address(int page)
1.1       root     7099: {
1.1.1.14  root     7100:        if(!int_10h_feh_called) {
1.1.1.16  root     7101:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     7102:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7103:                return SHADOW_BUF_TOP;
                   7104:        } else {
1.1.1.14  root     7105:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     7106:        }
                   7107: }
                   7108: 
1.1.1.16  root     7109: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     7110: {
1.1.1.16  root     7111:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     7112: }
                   7113: 
1.1.1.56  root     7114: bool pcbios_set_font_size(int width, int height)
                   7115: {
1.1.1.61  root     7116:        if(set_console_font_size(width, height)) {
                   7117:                *(UINT16 *)(mem + 0x485) = height;
                   7118:                return(true);
                   7119:        }
                   7120:        return(false);
1.1.1.56  root     7121: }
                   7122: 
1.1.1.16  root     7123: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     7124: {
1.1.1.14  root     7125:        // clear the existing screen, not just the new one
                   7126:        int clr_height = max(height, scr_height);
                   7127:        
1.1.1.16  root     7128:        if(scr_width != width || scr_height != height) {
                   7129:                change_console_size(width, height);
1.1.1.14  root     7130:        }
                   7131:        mem[0x462] = 0;
                   7132:        *(UINT16 *)(mem + 0x44e) = 0;
                   7133:        
1.1.1.16  root     7134:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   7135:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   7136:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   7137:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     7138:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7139:        
1.1.1.23  root     7140:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     7141:        if(clr_screen) {
1.1.1.14  root     7142:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   7143:                        mem[ofs++] = 0x20;
                   7144:                        mem[ofs++] = 0x07;
                   7145:                }
                   7146:                
1.1.1.35  root     7147: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7148:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7149: #endif
1.1.1.14  root     7150:                for(int y = 0; y < clr_height; y++) {
                   7151:                        for(int x = 0; x < scr_width; x++) {
                   7152:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7153:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     7154:                        }
                   7155:                }
                   7156:                SMALL_RECT rect;
1.1.1.14  root     7157:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
1.1.1.60  root     7158:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7159:                vram_length_char = vram_last_length_char = 0;
                   7160:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     7161: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7162:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7163: #endif
1.1       root     7164:        }
1.1.1.14  root     7165:        COORD co;
                   7166:        co.X = 0;
                   7167:        co.Y = scr_top;
                   7168:        SetConsoleCursorPosition(hStdout, co);
                   7169:        cursor_moved = true;
1.1.1.59  root     7170:        cursor_moved_by_crtc = false;
1.1.1.14  root     7171:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     7172: }
                   7173: 
1.1.1.36  root     7174: void pcbios_update_cursor_position()
                   7175: {
                   7176:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7177:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   7178:        if(!restore_console_on_exit) {
                   7179:                scr_top = csbi.srWindow.Top;
                   7180:        }
                   7181:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   7182:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   7183: }
                   7184: 
1.1.1.16  root     7185: inline void pcbios_int_10h_00h()
                   7186: {
                   7187:        switch(REG8(AL) & 0x7f) {
                   7188:        case 0x70: // v-text mode
                   7189:        case 0x71: // extended cga v-text mode
                   7190:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   7191:                break;
1.1.1.61  root     7192:        case 0x73:
                   7193:        case 0x03:
                   7194:                change_console_size(80, 25); // for Windows10
                   7195:                pcbios_set_font_size(font_width, font_height);
1.1.1.16  root     7196:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   7197:                break;
                   7198:        }
                   7199:        if(REG8(AL) & 0x80) {
                   7200:                mem[0x487] |= 0x80;
                   7201:        } else {
                   7202:                mem[0x487] &= ~0x80;
                   7203:        }
                   7204:        mem[0x449] = REG8(AL) & 0x7f;
                   7205: }
                   7206: 
1.1       root     7207: inline void pcbios_int_10h_01h()
                   7208: {
1.1.1.13  root     7209:        mem[0x460] = REG8(CL);
                   7210:        mem[0x461] = REG8(CH);
1.1.1.14  root     7211:        
1.1.1.60  root     7212:        int size = (int)(REG8(CL) & 7) - (int)(REG8(CH) & 7) + 1;
                   7213:        
                   7214:        if(!((REG8(CH) & 0x20) != 0 || size < 0)) {
                   7215:                ci_new.bVisible = TRUE;
                   7216:                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
                   7217:        } else {
                   7218:                ci_new.bVisible = FALSE;
                   7219:        }
1.1       root     7220: }
                   7221: 
                   7222: inline void pcbios_int_10h_02h()
                   7223: {
1.1.1.14  root     7224:        // continuously setting the cursor effectively stops it blinking
1.1.1.59  root     7225:        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     7226:                COORD co;
                   7227:                co.X = REG8(DL);
1.1.1.14  root     7228:                co.Y = REG8(DH) + scr_top;
                   7229:                
                   7230:                // some programs hide the cursor by moving it off screen
                   7231:                static bool hidden = false;
1.1.1.23  root     7232:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7233:                
                   7234:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
1.1.1.59  root     7235:                        if(ci_new.bVisible) {
                   7236:                                ci_new.bVisible = FALSE;
1.1.1.14  root     7237:                                hidden = true;
                   7238:                        }
                   7239:                } else if(hidden) {
1.1.1.59  root     7240:                        if(!ci_new.bVisible) {
                   7241:                                ci_new.bVisible = TRUE;
1.1.1.14  root     7242:                        }
                   7243:                        hidden = false;
                   7244:                }
1.1.1.59  root     7245:                cursor_moved_by_crtc = false;
1.1       root     7246:        }
1.1.1.14  root     7247:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   7248:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     7249: }
                   7250: 
                   7251: inline void pcbios_int_10h_03h()
                   7252: {
1.1.1.14  root     7253:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7254:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7255:        REG8(CL) = mem[0x460];
                   7256:        REG8(CH) = mem[0x461];
                   7257: }
                   7258: 
                   7259: inline void pcbios_int_10h_05h()
                   7260: {
1.1.1.14  root     7261:        if(REG8(AL) >= vram_pages) {
                   7262:                return;
                   7263:        }
                   7264:        if(mem[0x462] != REG8(AL)) {
                   7265:                vram_flush();
                   7266:                
1.1.1.23  root     7267:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7268:                SMALL_RECT rect;
1.1.1.14  root     7269:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7270:                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7271:                
1.1.1.16  root     7272:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     7273:                        for(int x = 0; x < scr_width; x++) {
                   7274:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7275:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7276:                        }
                   7277:                }
1.1.1.16  root     7278:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     7279:                        for(int x = 0; x < scr_width; x++) {
                   7280:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   7281:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     7282:                        }
                   7283:                }
1.1.1.60  root     7284:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7285:                
                   7286:                COORD co;
1.1.1.14  root     7287:                co.X = mem[0x450 + REG8(AL) * 2];
                   7288:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   7289:                if(co.Y < scr_top + scr_height) {
                   7290:                        SetConsoleCursorPosition(hStdout, co);
                   7291:                }
1.1.1.59  root     7292:                cursor_moved_by_crtc = false;
1.1       root     7293:        }
1.1.1.14  root     7294:        mem[0x462] = REG8(AL);
                   7295:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   7296:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     7297:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     7298:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     7299:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     7300:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     7301:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7302: }
                   7303: 
                   7304: inline void pcbios_int_10h_06h()
                   7305: {
1.1.1.14  root     7306:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7307:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7308:                return;
                   7309:        }
                   7310:        vram_flush();
                   7311:        
1.1.1.23  root     7312:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7313:        SMALL_RECT rect;
1.1.1.14  root     7314:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7315:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7316:        
                   7317:        int right = min(REG8(DL), scr_width - 1);
                   7318:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7319:        
                   7320:        if(REG8(AL) == 0) {
1.1.1.14  root     7321:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7322:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7323:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7324:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7325:                        }
                   7326:                }
                   7327:        } else {
1.1.1.14  root     7328:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     7329:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7330:                                if(y2 <= bottom) {
                   7331:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7332:                                } else {
1.1.1.14  root     7333:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7334:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7335:                                }
1.1.1.14  root     7336:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7337:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7338:                        }
                   7339:                }
                   7340:        }
1.1.1.60  root     7341:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7342: }
                   7343: 
                   7344: inline void pcbios_int_10h_07h()
                   7345: {
1.1.1.14  root     7346:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7347:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7348:                return;
                   7349:        }
                   7350:        vram_flush();
                   7351:        
1.1.1.23  root     7352:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7353:        SMALL_RECT rect;
1.1.1.14  root     7354:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7355:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7356:        
                   7357:        int right = min(REG8(DL), scr_width - 1);
                   7358:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7359:        
                   7360:        if(REG8(AL) == 0) {
1.1.1.14  root     7361:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7362:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7363:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7364:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7365:                        }
                   7366:                }
                   7367:        } else {
1.1.1.14  root     7368:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     7369:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7370:                                if(y2 >= REG8(CH)) {
                   7371:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7372:                                } else {
1.1.1.14  root     7373:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7374:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7375:                                }
1.1.1.14  root     7376:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7377:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7378:                        }
                   7379:                }
                   7380:        }
1.1.1.60  root     7381:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7382: }
                   7383: 
                   7384: inline void pcbios_int_10h_08h()
                   7385: {
                   7386:        COORD co;
                   7387:        DWORD num;
                   7388:        
1.1.1.14  root     7389:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7390:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7391:        
                   7392:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7393:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7394:                co.Y += scr_top;
                   7395:                vram_flush();
1.1.1.60  root     7396:                ReadConsoleOutputCharacterA(hStdout, scr_char, 1, co, &num);
1.1       root     7397:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   7398:                REG8(AL) = scr_char[0];
                   7399:                REG8(AH) = scr_attr[0];
                   7400:        } else {
1.1.1.16  root     7401:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     7402:        }
                   7403: }
                   7404: 
                   7405: inline void pcbios_int_10h_09h()
                   7406: {
                   7407:        COORD co;
                   7408:        
1.1.1.14  root     7409:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7410:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7411:        
1.1.1.16  root     7412:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7413:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7414:        
                   7415:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7416: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7417:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7418: #endif
1.1.1.16  root     7419:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7420:                while(dest < end) {
                   7421:                        write_text_vram_char(dest - vram, REG8(AL));
                   7422:                        mem[dest++] = REG8(AL);
                   7423:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7424:                        mem[dest++] = REG8(BL);
1.1       root     7425:                }
1.1.1.35  root     7426: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7427:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7428: #endif
1.1       root     7429:        } else {
1.1.1.14  root     7430:                while(dest < end) {
1.1       root     7431:                        mem[dest++] = REG8(AL);
                   7432:                        mem[dest++] = REG8(BL);
                   7433:                }
                   7434:        }
                   7435: }
                   7436: 
                   7437: inline void pcbios_int_10h_0ah()
                   7438: {
                   7439:        COORD co;
                   7440:        
1.1.1.14  root     7441:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7442:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7443:        
1.1.1.16  root     7444:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7445:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7446:        
                   7447:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7448: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7449:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7450: #endif
1.1.1.16  root     7451:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7452:                while(dest < end) {
                   7453:                        write_text_vram_char(dest - vram, REG8(AL));
                   7454:                        mem[dest++] = REG8(AL);
                   7455:                        dest++;
1.1       root     7456:                }
1.1.1.35  root     7457: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7458:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7459: #endif
1.1       root     7460:        } else {
1.1.1.14  root     7461:                while(dest < end) {
1.1       root     7462:                        mem[dest++] = REG8(AL);
                   7463:                        dest++;
                   7464:                }
                   7465:        }
                   7466: }
                   7467: 
1.1.1.40  root     7468: inline void pcbios_int_10h_0ch()
                   7469: {
                   7470:        HDC hdc = get_console_window_device_context();
                   7471:        
                   7472:        if(hdc != NULL) {
                   7473:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7474:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7475:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7476:                
                   7477:                if(REG8(AL) & 0x80) {
                   7478:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7479:                        if(color != CLR_INVALID) {
                   7480:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7481:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7482:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7483:                        }
                   7484:                }
                   7485:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7486:        }
                   7487: }
                   7488: 
                   7489: inline void pcbios_int_10h_0dh()
                   7490: {
                   7491:        HDC hdc = get_console_window_device_context();
                   7492:        BYTE r = 0;
                   7493:        BYTE g = 0;
                   7494:        BYTE b = 0;
                   7495:        
                   7496:        if(hdc != NULL) {
                   7497:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7498:                if(color != CLR_INVALID) {
                   7499:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7500:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7501:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7502:                }
                   7503:        }
                   7504:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7505: }
                   7506: 
1.1       root     7507: inline void pcbios_int_10h_0eh()
                   7508: {
1.1.1.59  root     7509:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   7510:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     7511:        DWORD num;
                   7512:        COORD co;
                   7513:        
1.1.1.59  root     7514:        if(cursor_moved_by_crtc) {
                   7515:                if(!restore_console_on_exit) {
                   7516:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7517:                        scr_top = csbi.srWindow.Top;
                   7518:                }
                   7519:                co.X = mem[0x450 + REG8(BH) * 2];
                   7520:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   7521:                SetConsoleCursorPosition(hStdout, co);
                   7522:                cursor_moved_by_crtc = false;
                   7523:        }
1.1.1.54  root     7524:        co.X = mem[0x450 + mem[0x462] * 2];
                   7525:        co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14  root     7526:        
                   7527:        if(REG8(AL) == 7) {
                   7528:                //MessageBeep(-1);
                   7529:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7530:                if(REG8(AL) == 10) {
                   7531:                        vram_flush();
                   7532:                }
1.1.1.60  root     7533:                WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7534:                cursor_moved = true;
                   7535:        } else {
1.1.1.54  root     7536:                int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35  root     7537: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7538:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7539: #endif
1.1.1.54  root     7540:                int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
                   7541:                write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7542: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7543:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7544: #endif
1.1.1.54  root     7545:                
                   7546:                if(++co.X == scr_width) {
                   7547:                        co.X = 0;
                   7548:                        if(++co.Y == scr_height) {
                   7549:                                vram_flush();
1.1.1.60  root     7550:                                WriteConsoleA(hStdout, "\n", 1, &num, NULL);
1.1.1.14  root     7551:                                cursor_moved = true;
                   7552:                        }
                   7553:                }
1.1.1.54  root     7554:                if(!cursor_moved) {
                   7555:                        co.Y += scr_top;
                   7556:                        SetConsoleCursorPosition(hStdout, co);
                   7557:                        cursor_moved = true;
                   7558:                }
1.1.1.14  root     7559:                mem[dest] = REG8(AL);
                   7560:        }
1.1       root     7561: }
                   7562: 
                   7563: inline void pcbios_int_10h_0fh()
                   7564: {
                   7565:        REG8(AL) = mem[0x449];
                   7566:        REG8(AH) = mem[0x44a];
                   7567:        REG8(BH) = mem[0x462];
                   7568: }
                   7569: 
1.1.1.14  root     7570: inline void pcbios_int_10h_11h()
                   7571: {
                   7572:        switch(REG8(AL)) {
1.1.1.58  root     7573:        case 0x00:
                   7574:        case 0x10:
1.1.1.61  root     7575:                if(REG8(BH)) {
                   7576:                        change_console_size(80, 25); // for Windows10
                   7577:                        if(!pcbios_set_font_size(font_width, (int)REG8(BH))) {
                   7578:                                for(int h = min(font_height, (int)REG8(BH)); h <= max(font_height, (int)REG8(BH)); h++) {
                   7579:                                        if(h != (int)REG8(BH)) {
                   7580:                                                if(pcbios_set_font_size(font_width, h)) {
                   7581:                                                        break;
                   7582:                                                }
                   7583:                                        }
                   7584:                                }
                   7585:                        }
1.1.1.58  root     7586:                        pcbios_set_console_size(80, (25 * 16) / REG8(BH), true);
                   7587:                }
                   7588:                break;
1.1.1.16  root     7589:        case 0x01:
1.1.1.14  root     7590:        case 0x11:
1.1.1.61  root     7591:                change_console_size(80, 28); // for Windows10
                   7592:                if(!pcbios_set_font_size(font_width, 14)) {
                   7593:                        for(int h = min(font_height, 14); h <= max(font_height, 14); h++) {
                   7594:                                if(h != 14) {
                   7595:                                        if(pcbios_set_font_size(font_width, h)) {
                   7596:                                                break;
                   7597:                                        }
                   7598:                                }
                   7599:                        }
1.1.1.56  root     7600:                }
1.1.1.61  root     7601:                pcbios_set_console_size(80, 28, true); // 28 = 25 * 16 / 14
1.1.1.14  root     7602:                break;
1.1.1.16  root     7603:        case 0x02:
1.1.1.14  root     7604:        case 0x12:
1.1.1.61  root     7605:                change_console_size(80, 25); // for Windows10
                   7606:                if(!pcbios_set_font_size(8, 8)) {
                   7607:                        bool success = false;
                   7608:                        for(int y = 8; y <= 14; y++) {
                   7609:                                for(int x = min(font_width, 6); x <= max(font_width, 8); x++) {
                   7610:                                        if(pcbios_set_font_size(x, y)) {
                   7611:                                                success = true;
                   7612:                                                break;
                   7613:                                        }
                   7614:                                }
                   7615:                        }
                   7616:                        if(!success) {
                   7617:                                pcbios_set_font_size(font_width, font_height);
                   7618:                        }
1.1.1.56  root     7619:                }
1.1.1.61  root     7620:                pcbios_set_console_size(80, 50, true); // 50 = 25 * 16 / 8
1.1.1.14  root     7621:                break;
1.1.1.16  root     7622:        case 0x04:
1.1.1.14  root     7623:        case 0x14:
1.1.1.61  root     7624:                change_console_size(80, 25); // for Windows10
                   7625:                pcbios_set_font_size(font_width, font_height);
                   7626:                pcbios_set_console_size(80, 25, true);
1.1.1.58  root     7627:                break;
                   7628:        case 0x18:
1.1.1.61  root     7629:                change_console_size(80, 25); // for Windows10
                   7630:                pcbios_set_font_size(font_width, font_height);
                   7631:                pcbios_set_console_size(80, 25, true);
1.1.1.14  root     7632:                break;
                   7633:        case 0x30:
                   7634:                SREG(ES) = 0;
                   7635:                i386_load_segment_descriptor(ES);
                   7636:                REG16(BP) = 0;
                   7637:                REG16(CX) = mem[0x485];
                   7638:                REG8(DL) = mem[0x484];
                   7639:                break;
1.1.1.54  root     7640:        default:
                   7641:                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));
                   7642:                m_CF = 1;
                   7643:                break;
1.1.1.14  root     7644:        }
                   7645: }
                   7646: 
                   7647: inline void pcbios_int_10h_12h()
                   7648: {
1.1.1.16  root     7649:        switch(REG8(BL)) {
                   7650:        case 0x10:
1.1.1.14  root     7651:                REG16(BX) = 0x0003;
                   7652:                REG16(CX) = 0x0009;
1.1.1.16  root     7653:                break;
1.1.1.14  root     7654:        }
                   7655: }
                   7656: 
1.1       root     7657: inline void pcbios_int_10h_13h()
                   7658: {
1.1.1.3   root     7659:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7660:        COORD co;
                   7661:        DWORD num;
                   7662:        
                   7663:        co.X = REG8(DL);
1.1.1.14  root     7664:        co.Y = REG8(DH) + scr_top;
                   7665:        
                   7666:        vram_flush();
1.1       root     7667:        
                   7668:        switch(REG8(AL)) {
                   7669:        case 0x00:
                   7670:        case 0x01:
                   7671:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7672:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7673:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7674:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7675:                        SetConsoleCursorPosition(hStdout, co);
                   7676:                        
                   7677:                        if(csbi.wAttributes != REG8(BL)) {
                   7678:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7679:                        }
1.1.1.60  root     7680:                        WriteConsoleA(hStdout, &mem[ofs], REG16(CX), &num, NULL);
1.1.1.14  root     7681:                        
1.1       root     7682:                        if(csbi.wAttributes != REG8(BL)) {
                   7683:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7684:                        }
                   7685:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7686:                                if(!restore_console_on_exit) {
                   7687:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7688:                                        scr_top = csbi.srWindow.Top;
                   7689:                                }
1.1.1.14  root     7690:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7691:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7692:                                SetConsoleCursorPosition(hStdout, co);
                   7693:                        } else {
                   7694:                                cursor_moved = true;
                   7695:                        }
1.1.1.59  root     7696:                        cursor_moved_by_crtc = false;
1.1       root     7697:                } else {
1.1.1.3   root     7698:                        m_CF = 1;
1.1       root     7699:                }
                   7700:                break;
                   7701:        case 0x02:
                   7702:        case 0x03:
                   7703:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7704:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7705:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7706:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7707:                        SetConsoleCursorPosition(hStdout, co);
                   7708:                        
                   7709:                        WORD wAttributes = csbi.wAttributes;
                   7710:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7711:                                if(wAttributes != mem[ofs + 1]) {
                   7712:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7713:                                        wAttributes = mem[ofs + 1];
                   7714:                                }
1.1.1.60  root     7715:                                WriteConsoleA(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7716:                        }
                   7717:                        if(csbi.wAttributes != wAttributes) {
                   7718:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7719:                        }
                   7720:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7721:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7722:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7723:                                SetConsoleCursorPosition(hStdout, co);
                   7724:                        } else {
                   7725:                                cursor_moved = true;
                   7726:                        }
1.1.1.59  root     7727:                        cursor_moved_by_crtc = false;
1.1       root     7728:                } else {
1.1.1.3   root     7729:                        m_CF = 1;
1.1       root     7730:                }
                   7731:                break;
                   7732:        case 0x10:
                   7733:        case 0x11:
                   7734:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7735:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.60  root     7736:                        ReadConsoleOutputCharacterA(hStdout, scr_char, REG16(CX), co, &num);
1.1       root     7737:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7738:                        for(int i = 0; i < num; i++) {
                   7739:                                mem[ofs++] = scr_char[i];
                   7740:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7741:                                if(REG8(AL) & 0x01) {
1.1       root     7742:                                        mem[ofs++] = 0;
                   7743:                                        mem[ofs++] = 0;
                   7744:                                }
                   7745:                        }
                   7746:                } else {
1.1.1.16  root     7747:                        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     7748:                                mem[ofs++] = mem[src++];
                   7749:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7750:                                if(REG8(AL) & 0x01) {
1.1       root     7751:                                        mem[ofs++] = 0;
                   7752:                                        mem[ofs++] = 0;
                   7753:                                }
1.1.1.14  root     7754:                                if(++co.X == scr_width) {
                   7755:                                        if(++co.Y == scr_height) {
1.1       root     7756:                                                break;
                   7757:                                        }
                   7758:                                        co.X = 0;
                   7759:                                }
                   7760:                        }
                   7761:                }
                   7762:                break;
1.1.1.45  root     7763:        case 0x12: // ???
                   7764:        case 0x13: // ???
1.1       root     7765:        case 0x20:
                   7766:        case 0x21:
                   7767:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7768:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7769:                        int len = min(REG16(CX), scr_width * scr_height);
                   7770:                        for(int i = 0; i < len; i++) {
1.1       root     7771:                                scr_char[i] = mem[ofs++];
                   7772:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7773:                                if(REG8(AL) & 0x01) {
1.1       root     7774:                                        ofs += 2;
                   7775:                                }
                   7776:                        }
1.1.1.60  root     7777:                        WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7778:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7779:                } else {
1.1.1.16  root     7780:                        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     7781:                                mem[dest++] = mem[ofs++];
                   7782:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7783:                                if(REG8(AL) & 0x01) {
1.1       root     7784:                                        ofs += 2;
                   7785:                                }
1.1.1.14  root     7786:                                if(++co.X == scr_width) {
                   7787:                                        if(++co.Y == scr_height) {
1.1       root     7788:                                                break;
                   7789:                                        }
                   7790:                                        co.X = 0;
                   7791:                                }
                   7792:                        }
                   7793:                }
                   7794:                break;
                   7795:        default:
1.1.1.22  root     7796:                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     7797:                m_CF = 1;
1.1       root     7798:                break;
                   7799:        }
                   7800: }
                   7801: 
1.1.1.30  root     7802: inline void pcbios_int_10h_18h()
                   7803: {
                   7804:        switch(REG8(AL)) {
                   7805:        case 0x00:
                   7806:        case 0x01:
                   7807: //             REG8(AL) = 0x86;
                   7808:                REG8(AL) = 0x00;
                   7809:                break;
                   7810:        default:
                   7811:                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));
                   7812:                m_CF = 1;
                   7813:                break;
                   7814:        }
                   7815: }
                   7816: 
1.1.1.14  root     7817: inline void pcbios_int_10h_1ah()
                   7818: {
                   7819:        switch(REG8(AL)) {
                   7820:        case 0x00:
                   7821:                REG8(AL) = 0x1a;
                   7822:                REG8(BL) = 0x08;
                   7823:                REG8(BH) = 0x00;
                   7824:                break;
                   7825:        default:
1.1.1.22  root     7826:                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     7827:                m_CF = 1;
                   7828:                break;
                   7829:        }
                   7830: }
                   7831: 
1.1       root     7832: inline void pcbios_int_10h_1dh()
                   7833: {
                   7834:        switch(REG8(AL)) {
1.1.1.43  root     7835:        case 0x00:
                   7836:                // DOS/V Shift Status Line Control is not supported
                   7837:                m_CF = 1;
                   7838:                break;
1.1       root     7839:        case 0x01:
                   7840:                break;
                   7841:        case 0x02:
                   7842:                REG16(BX) = 0;
                   7843:                break;
                   7844:        default:
1.1.1.22  root     7845:                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));
                   7846:                m_CF = 1;
                   7847:                break;
                   7848:        }
                   7849: }
                   7850: 
                   7851: inline void pcbios_int_10h_4fh()
                   7852: {
                   7853:        switch(REG8(AL)) {
                   7854:        case 0x00:
                   7855:                REG8(AH) = 0x02; // not supported
                   7856:                break;
                   7857:        case 0x01:
                   7858:        case 0x02:
                   7859:        case 0x03:
                   7860:        case 0x04:
                   7861:        case 0x05:
                   7862:        case 0x06:
                   7863:        case 0x07:
                   7864:        case 0x08:
                   7865:        case 0x09:
                   7866:        case 0x0a:
                   7867:        case 0x0b:
                   7868:        case 0x0c:
                   7869:                REG8(AH) = 0x01; // failed
                   7870:                break;
                   7871:        default:
                   7872:                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     7873:                m_CF = 1;
1.1       root     7874:                break;
                   7875:        }
                   7876: }
                   7877: 
                   7878: inline void pcbios_int_10h_82h()
                   7879: {
                   7880:        static UINT8 mode = 0;
                   7881:        
                   7882:        switch(REG8(AL)) {
1.1.1.22  root     7883:        case 0x00:
1.1       root     7884:                if(REG8(BL) != 0xff) {
                   7885:                        mode = REG8(BL);
                   7886:                }
                   7887:                REG8(AL) = mode;
                   7888:                break;
                   7889:        default:
1.1.1.22  root     7890:                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     7891:                m_CF = 1;
1.1       root     7892:                break;
                   7893:        }
                   7894: }
                   7895: 
1.1.1.22  root     7896: inline void pcbios_int_10h_83h()
                   7897: {
                   7898:        static UINT8 mode = 0;
                   7899:        
                   7900:        switch(REG8(AL)) {
                   7901:        case 0x00:
                   7902:                REG16(AX) = 0; // offset???
                   7903:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7904:                i386_load_segment_descriptor(ES);
                   7905:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7906:                break;
                   7907:        default:
                   7908:                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));
                   7909:                m_CF = 1;
                   7910:                break;
                   7911:        }
                   7912: }
                   7913: 
                   7914: inline void pcbios_int_10h_90h()
                   7915: {
                   7916:        REG8(AL) = mem[0x449];
                   7917: }
                   7918: 
                   7919: inline void pcbios_int_10h_91h()
                   7920: {
                   7921:        REG8(AL) = 0x04; // VGA
                   7922: }
                   7923: 
                   7924: inline void pcbios_int_10h_efh()
                   7925: {
                   7926:        REG16(DX) = 0xffff;
                   7927: }
                   7928: 
1.1       root     7929: inline void pcbios_int_10h_feh()
                   7930: {
                   7931:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7932:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7933:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7934:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7935:        }
                   7936:        int_10h_feh_called = true;
                   7937: }
                   7938: 
                   7939: inline void pcbios_int_10h_ffh()
                   7940: {
                   7941:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7942:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7943:                COORD co;
                   7944:                DWORD num;
                   7945:                
1.1.1.14  root     7946:                vram_flush();
                   7947:                
                   7948:                co.X = (REG16(DI) >> 1) % scr_width;
                   7949:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7950:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7951:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7952:                int len;
                   7953:                for(len = 0; ofs < end; len++) {
                   7954:                        scr_char[len] = mem[ofs++];
                   7955:                        scr_attr[len] = mem[ofs++];
                   7956:                }
                   7957:                co.Y += scr_top;
1.1.1.60  root     7958:                WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7959:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7960:        }
                   7961:        int_10h_ffh_called = true;
                   7962: }
                   7963: 
1.1.1.42  root     7964: int pcbios_update_drive_param(int drive_num, int force_update)
                   7965: {
                   7966:        if(drive_num >= 0 && drive_num < 26) {
                   7967:                drive_param_t *drive_param = &drive_params[drive_num];
                   7968:                
                   7969:                if(force_update || !drive_param->initialized) {
                   7970:                        drive_param->valid = 0;
                   7971:                        char dev[64];
                   7972:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7973:                        
1.1.1.60  root     7974:                        HANDLE hFile = CreateFileA(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.42  root     7975:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7976:                                DWORD dwSize;
                   7977:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7978:                                        drive_param->valid = 1;
                   7979:                                }
                   7980:                                CloseHandle(hFile);
                   7981:                        }
                   7982:                        drive_param->initialized = 1;
                   7983:                }
                   7984:                return(drive_param->valid);
                   7985:        }
                   7986:        return(0);
                   7987: }
                   7988: 
                   7989: inline void pcbios_int_13h_00h()
                   7990: {
                   7991:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7992:        
                   7993:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7994:                REG8(AH) = 0x00; // successful completion
                   7995:        } else {
                   7996:                if(REG8(DL) & 0x80) {
                   7997:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7998:                } else {
                   7999:                        REG8(AH) = 0x80; // timeout (not ready)
                   8000:                }
                   8001:                m_CF = 1;
                   8002:        }
                   8003: }
                   8004: 
                   8005: inline void pcbios_int_13h_02h()
                   8006: {
                   8007:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8008:        
                   8009:        if(REG8(AL) == 0) {
                   8010:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8011:                m_CF = 1;
                   8012:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8013:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8014:                m_CF = 1;
                   8015:        } else {
                   8016:                drive_param_t *drive_param = &drive_params[drive_num];
                   8017:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8018:                char dev[64];
                   8019:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8020:                
1.1.1.60  root     8021:                HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1.1.42  root     8022:                if(hFile == INVALID_HANDLE_VALUE) {
                   8023:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8024:                        m_CF = 1;
                   8025:                } else {
                   8026:                        UINT32 sector_num = REG8(AL);
                   8027:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8028:                        UINT32 head = REG8(DH);
                   8029:                        UINT32 sector = REG8(CL) & 0x3f;
                   8030:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8031:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8032:                        DWORD dwSize;
                   8033:                        
                   8034: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8035: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8036: //                             m_CF = 1;
                   8037: //                     } else 
                   8038:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8039:                                REG8(AH) = 0x04; // sector not found/read error
                   8040:                                m_CF = 1;
                   8041:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8042:                                REG8(AH) = 0x04; // sector not found/read error
                   8043:                                m_CF = 1;
                   8044:                        } else {
                   8045:                                REG8(AH) = 0x00; // successful completion
                   8046:                        }
                   8047:                        CloseHandle(hFile);
                   8048:                }
                   8049:        }
                   8050: }
                   8051: 
                   8052: inline void pcbios_int_13h_03h()
                   8053: {
                   8054:        // this operation may cause serious damage for drives, so support only floppy disk...
                   8055:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8056:        
                   8057:        if(REG8(AL) == 0) {
                   8058:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8059:                m_CF = 1;
                   8060:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8061:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8062:                m_CF = 1;
                   8063:        } else if(!drive_params[drive_num].is_fdd()) {
                   8064:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8065:                m_CF = 1;
                   8066:        } else {
                   8067:                drive_param_t *drive_param = &drive_params[drive_num];
                   8068:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8069:                char dev[64];
                   8070:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8071:                
1.1.1.60  root     8072:                HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1.1.42  root     8073:                if(hFile == INVALID_HANDLE_VALUE) {
                   8074:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8075:                        m_CF = 1;
                   8076:                } else {
                   8077:                        UINT32 sector_num = REG8(AL);
                   8078:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8079:                        UINT32 head = REG8(DH);
                   8080:                        UINT32 sector = REG8(CL) & 0x3f;
                   8081:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8082:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8083:                        DWORD dwSize;
                   8084:                        
                   8085: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8086: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8087: //                             m_CF = 1;
                   8088: //                     } else 
                   8089:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8090:                                REG8(AH) = 0x04; // sector not found/read error
                   8091:                                m_CF = 1;
                   8092:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8093:                                REG8(AH) = 0x04; // sector not found/read error
                   8094:                                m_CF = 1;
                   8095:                        } else {
                   8096:                                REG8(AH) = 0x00; // successful completion
                   8097:                        }
                   8098:                        CloseHandle(hFile);
                   8099:                }
                   8100:        }
                   8101: }
                   8102: 
                   8103: inline void pcbios_int_13h_04h()
                   8104: {
                   8105:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8106:        
                   8107:        if(REG8(AL) == 0) {
                   8108:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8109:                m_CF = 1;
                   8110:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8111:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8112:                m_CF = 1;
                   8113:        } else {
                   8114:                drive_param_t *drive_param = &drive_params[drive_num];
                   8115:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8116:                char dev[64];
                   8117:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8118:                
1.1.1.60  root     8119:                HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1.1.42  root     8120:                if(hFile == INVALID_HANDLE_VALUE) {
                   8121:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8122:                        m_CF = 1;
                   8123:                } else {
                   8124:                        UINT32 sector_num = REG8(AL);
                   8125:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8126:                        UINT32 head = REG8(DH);
                   8127:                        UINT32 sector = REG8(CL) & 0x3f;
                   8128:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8129:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8130:                        DWORD dwSize;
                   8131:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   8132:                        
                   8133: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8134: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8135: //                             m_CF = 1;
                   8136: //                     } else 
                   8137:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8138:                                REG8(AH) = 0x04; // sector not found/read error
                   8139:                                m_CF = 1;
                   8140:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8141:                                REG8(AH) = 0x04; // sector not found/read error
                   8142:                                m_CF = 1;
                   8143:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   8144:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   8145:                                m_CF = 1;
                   8146:                        } else {
                   8147:                                REG8(AH) = 0x00; // successful completion
                   8148:                        }
                   8149:                        free(tmp_buffer);
                   8150:                        CloseHandle(hFile);
                   8151:                }
                   8152:        }
                   8153: }
                   8154: 
                   8155: inline void pcbios_int_13h_08h()
                   8156: {
                   8157:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8158:        
                   8159:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8160:                drive_param_t *drive_param = &drive_params[drive_num];
                   8161:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8162:                
                   8163:                REG16(AX) = 0x0000;
                   8164:                switch(geo->MediaType) {
                   8165:                case F5_360_512:
                   8166:                case F5_320_512:
                   8167:                case F5_320_1024:
                   8168:                case F5_180_512:
                   8169:                case F5_160_512:
                   8170:                        REG8(BL) = 0x01; // 320K/360K disk
                   8171:                        break;
                   8172:                case F5_1Pt2_512:
                   8173:                case F3_1Pt2_512:
                   8174:                case F3_1Pt23_1024:
                   8175:                case F5_1Pt23_1024:
                   8176:                        REG8(BL) = 0x02; // 1.2M disk
                   8177:                        break;
                   8178:                case F3_720_512:
                   8179:                case F3_640_512:
                   8180:                case F5_640_512:
                   8181:                case F5_720_512:
                   8182:                        REG8(BL) = 0x03; // 720K disk
                   8183:                        break;
                   8184:                case F3_1Pt44_512:
                   8185:                        REG8(BL) = 0x04; // 1.44M disk
                   8186:                        break;
                   8187:                case F3_2Pt88_512:
                   8188:                        REG8(BL) = 0x06; // 2.88M disk
                   8189:                        break;
                   8190:                case RemovableMedia:
                   8191:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   8192:                        break;
                   8193:                default:
                   8194:                        REG8(BL) = 0x00; // unknown
                   8195:                        break;
                   8196:                }
                   8197:                if(REG8(DL) & 0x80) {
                   8198:                        switch(GetLogicalDrives() & 0x0c) {
                   8199:                        case 0x00: REG8(DL) = 0x00; break;
                   8200:                        case 0x04:
                   8201:                        case 0x08: REG8(DL) = 0x01; break;
                   8202:                        case 0x0c: REG8(DL) = 0x02; break;
                   8203:                        }
                   8204:                } else {
                   8205:                        switch(GetLogicalDrives() & 0x03) {
                   8206:                        case 0x00: REG8(DL) = 0x00; break;
                   8207:                        case 0x01:
                   8208:                        case 0x02: REG8(DL) = 0x01; break;
                   8209:                        case 0x03: REG8(DL) = 0x02; break;
                   8210:                        }
                   8211:                }
                   8212:                REG8(DH) = drive_param->head_num();
                   8213:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   8214:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   8215:                REG8(CH) = cyl & 0xff;
                   8216:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   8217:        } else {
                   8218:                REG8(AH) = 0x07;
                   8219:                m_CF = 1;
                   8220:        }
                   8221: }
                   8222: 
                   8223: inline void pcbios_int_13h_10h()
                   8224: {
                   8225:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8226:        
                   8227:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8228:                REG8(AH) = 0x00; // successful completion
                   8229:        } else {
                   8230:                if(REG8(DL) & 0x80) {
                   8231:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   8232:                } else {
                   8233:                        REG8(AH) = 0x80; // timeout (not ready)
                   8234:                }
                   8235:                m_CF = 1;
                   8236:        }
                   8237: }
                   8238: 
                   8239: inline void pcbios_int_13h_15h()
                   8240: {
                   8241:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8242:        
                   8243:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8244:                if(REG8(DL) & 0x80) {
                   8245:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   8246:                } else {
                   8247:                        REG8(AH) = 0x03; // hard disk
                   8248:                }
                   8249:        } else {
                   8250:                REG8(AH) = 0x00; // no such drive
                   8251:        }
                   8252: }
                   8253: 
1.1.1.43  root     8254: inline void pcbios_int_13h_41h()
                   8255: {
                   8256:        if(REG16(BX) == 0x55aa) {
                   8257:                // IBM/MS INT 13 Extensions is not installed
                   8258:                REG8(AH) = 0x01;
                   8259:                m_CF = 1;
                   8260:        } else {
                   8261:                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));
                   8262:                REG8(AH) = 0x01;
                   8263:                m_CF = 1;
                   8264:        }
                   8265: }
                   8266: 
1.1.1.25  root     8267: inline void pcbios_int_14h_00h()
                   8268: {
1.1.1.29  root     8269:        if(REG16(DX) < 4) {
1.1.1.25  root     8270:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   8271:                UINT8 selector = sio_read(REG16(DX), 3);
                   8272:                selector &= ~0x3f;
                   8273:                selector |= REG8(AL) & 0x1f;
                   8274:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   8275:                sio_write(REG16(DX), 3, selector | 0x80);
                   8276:                sio_write(REG16(DX), 0, divisor & 0xff);
                   8277:                sio_write(REG16(DX), 1, divisor >> 8);
                   8278:                sio_write(REG16(DX), 3, selector);
                   8279:                REG8(AH) = sio_read(REG16(DX), 5);
                   8280:                REG8(AL) = sio_read(REG16(DX), 6);
                   8281:        } else {
                   8282:                REG8(AH) = 0x80;
                   8283:        }
                   8284: }
                   8285: 
                   8286: inline void pcbios_int_14h_01h()
                   8287: {
1.1.1.29  root     8288:        if(REG16(DX) < 4) {
1.1.1.25  root     8289:                UINT8 selector = sio_read(REG16(DX), 3);
                   8290:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8291:                sio_write(REG16(DX), 0, REG8(AL));
                   8292:                sio_write(REG16(DX), 3, selector);
                   8293:                REG8(AH) = sio_read(REG16(DX), 5);
                   8294:        } else {
                   8295:                REG8(AH) = 0x80;
                   8296:        }
                   8297: }
                   8298: 
                   8299: inline void pcbios_int_14h_02h()
                   8300: {
1.1.1.29  root     8301:        if(REG16(DX) < 4) {
1.1.1.25  root     8302:                UINT8 selector = sio_read(REG16(DX), 3);
                   8303:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8304:                REG8(AL) = sio_read(REG16(DX), 0);
                   8305:                sio_write(REG16(DX), 3, selector);
                   8306:                REG8(AH) = sio_read(REG16(DX), 5);
                   8307:        } else {
                   8308:                REG8(AH) = 0x80;
                   8309:        }
                   8310: }
                   8311: 
                   8312: inline void pcbios_int_14h_03h()
                   8313: {
1.1.1.29  root     8314:        if(REG16(DX) < 4) {
1.1.1.25  root     8315:                REG8(AH) = sio_read(REG16(DX), 5);
                   8316:                REG8(AL) = sio_read(REG16(DX), 6);
                   8317:        } else {
                   8318:                REG8(AH) = 0x80;
                   8319:        }
                   8320: }
                   8321: 
                   8322: inline void pcbios_int_14h_04h()
                   8323: {
1.1.1.29  root     8324:        if(REG16(DX) < 4) {
1.1.1.25  root     8325:                UINT8 selector = sio_read(REG16(DX), 3);
                   8326:                if(REG8(CH) <= 0x03) {
                   8327:                        selector = (selector & ~0x03) | REG8(CH);
                   8328:                }
                   8329:                if(REG8(BL) == 0x00) {
                   8330:                        selector &= ~0x04;
                   8331:                } else if(REG8(BL) == 0x01) {
                   8332:                        selector |= 0x04;
                   8333:                }
                   8334:                if(REG8(BH) == 0x00) {
                   8335:                        selector = (selector & ~0x38) | 0x00;
                   8336:                } else if(REG8(BH) == 0x01) {
                   8337:                        selector = (selector & ~0x38) | 0x08;
                   8338:                } else if(REG8(BH) == 0x02) {
                   8339:                        selector = (selector & ~0x38) | 0x18;
                   8340:                } else if(REG8(BH) == 0x03) {
                   8341:                        selector = (selector & ~0x38) | 0x28;
                   8342:                } else if(REG8(BH) == 0x04) {
                   8343:                        selector = (selector & ~0x38) | 0x38;
                   8344:                }
                   8345:                if(REG8(AL) == 0x00) {
                   8346:                        selector |= 0x40;
                   8347:                } else if(REG8(AL) == 0x01) {
                   8348:                        selector &= ~0x40;
                   8349:                }
                   8350:                if(REG8(CL) <= 0x0b) {
                   8351:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   8352:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   8353:                        sio_write(REG16(DX), 3, selector | 0x80);
                   8354:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   8355:                        sio_write(REG16(DX), 1, divisor >> 8);
                   8356:                }
                   8357:                sio_write(REG16(DX), 3, selector);
                   8358:                REG8(AH) = sio_read(REG16(DX), 5);
                   8359:                REG8(AL) = sio_read(REG16(DX), 6);
                   8360:        } else {
                   8361:                REG8(AH) = 0x80;
                   8362:        }
                   8363: }
                   8364: 
                   8365: inline void pcbios_int_14h_05h()
                   8366: {
1.1.1.29  root     8367:        if(REG16(DX) < 4) {
1.1.1.25  root     8368:                if(REG8(AL) == 0x00) {
                   8369:                        REG8(BL) = sio_read(REG16(DX), 4);
                   8370:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8371:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8372:                } else if(REG8(AL) == 0x01) {
                   8373:                        sio_write(REG16(DX), 4, REG8(BL));
                   8374:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8375:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8376:                } else {
                   8377:                        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));
                   8378:                }
                   8379:        } else {
                   8380:                REG8(AH) = 0x80;
                   8381:        }
                   8382: }
                   8383: 
1.1       root     8384: inline void pcbios_int_15h_23h()
                   8385: {
                   8386:        switch(REG8(AL)) {
1.1.1.22  root     8387:        case 0x00:
1.1.1.8   root     8388:                REG8(CL) = cmos_read(0x2d);
                   8389:                REG8(CH) = cmos_read(0x2e);
1.1       root     8390:                break;
1.1.1.22  root     8391:        case 0x01:
1.1.1.8   root     8392:                cmos_write(0x2d, REG8(CL));
                   8393:                cmos_write(0x2e, REG8(CH));
1.1       root     8394:                break;
                   8395:        default:
1.1.1.22  root     8396:                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     8397:                REG8(AH) = 0x86;
1.1.1.3   root     8398:                m_CF = 1;
1.1       root     8399:                break;
                   8400:        }
                   8401: }
                   8402: 
                   8403: inline void pcbios_int_15h_24h()
                   8404: {
                   8405:        switch(REG8(AL)) {
1.1.1.22  root     8406:        case 0x00:
1.1.1.3   root     8407:                i386_set_a20_line(0);
1.1       root     8408:                REG8(AH) = 0;
                   8409:                break;
1.1.1.22  root     8410:        case 0x01:
1.1.1.3   root     8411:                i386_set_a20_line(1);
1.1       root     8412:                REG8(AH) = 0;
                   8413:                break;
1.1.1.22  root     8414:        case 0x02:
1.1       root     8415:                REG8(AH) = 0;
1.1.1.3   root     8416:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     8417:                REG16(CX) = 0;
                   8418:                break;
1.1.1.22  root     8419:        case 0x03:
1.1       root     8420:                REG16(AX) = 0;
                   8421:                REG16(BX) = 0;
                   8422:                break;
1.1.1.22  root     8423:        default:
                   8424:                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));
                   8425:                REG8(AH) = 0x86;
                   8426:                m_CF = 1;
                   8427:                break;
1.1       root     8428:        }
                   8429: }
                   8430: 
                   8431: inline void pcbios_int_15h_49h()
                   8432: {
1.1.1.27  root     8433:        REG8(AH) = 0x00;
                   8434:        REG8(BL) = 0x00; // DOS/V
1.1.1.64! root     8435: //     REG8(BL) = 0x01; // standard DBCS DOS (hardware DBCS support)
1.1       root     8436: }
                   8437: 
1.1.1.22  root     8438: inline void pcbios_int_15h_50h()
                   8439: {
                   8440:        switch(REG8(AL)) {
                   8441:        case 0x00:
                   8442:        case 0x01:
                   8443:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8444:                        REG8(AH) = 0x01; // invalid font type in bh
                   8445:                        m_CF = 1;
1.1.1.27  root     8446:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8447:                        REG8(AH) = 0x02; // bl not zero
                   8448:                        m_CF = 1;
                   8449:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8450:                        REG8(AH) = 0x04; // invalid code page
                   8451:                        m_CF = 1;
1.1.1.27  root     8452:                } else if(REG8(AL) == 0x01) {
                   8453:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8454:                        m_CF = 1;
1.1.1.27  root     8455:                } else {
1.1.1.49  root     8456:                        // dummy font read routine is at fffc:000d
                   8457:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8458:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8459:                        REG16(BX) = 0x000d;
1.1.1.27  root     8460:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8461:                }
                   8462:                break;
                   8463:        default:
                   8464:                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));
                   8465:                REG8(AH) = 0x86;
                   8466:                m_CF = 1;
                   8467:                break;
                   8468:        }
                   8469: }
                   8470: 
1.1.1.30  root     8471: inline void pcbios_int_15h_53h()
                   8472: {
                   8473:        switch(REG8(AL)) {
                   8474:        case 0x00:
                   8475:                // APM is not installed
                   8476:                REG8(AH) = 0x86;
                   8477:                m_CF = 1;
                   8478:                break;
                   8479:        default:
                   8480:                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));
                   8481:                REG8(AH) = 0x86;
                   8482:                m_CF = 1;
                   8483:                break;
                   8484:        }
                   8485: }
                   8486: 
1.1.1.43  root     8487: inline void pcbios_int_15h_84h()
                   8488: {
                   8489:        // joystick support (from DOSBox)
                   8490:        switch(REG16(DX)) {
                   8491:        case 0x00:
                   8492:                REG16(AX) = 0x00f0;
                   8493:                REG16(DX) = 0x0201;
                   8494:                m_CF = 1;
                   8495:                break;
                   8496:        case 0x01:
                   8497:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8498:                m_CF = 1;
                   8499:                break;
                   8500:        default:
                   8501:                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));
                   8502:                REG8(AH) = 0x86;
                   8503:                m_CF = 1;
                   8504:                break;
                   8505:        }
                   8506: }
1.1.1.35  root     8507: 
                   8508: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8509: {
                   8510:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8511:        UINT32 msec = usec / 1000;
                   8512:        
1.1.1.54  root     8513:        while(msec && !m_exit) {
1.1.1.14  root     8514:                UINT32 tmp = min(msec, 100);
                   8515:                if(msec - tmp < 10) {
                   8516:                        tmp = msec;
                   8517:                }
                   8518:                Sleep(tmp);
                   8519:                msec -= tmp;
                   8520:        }
1.1.1.35  root     8521:        
                   8522: #ifdef USE_SERVICE_THREAD
                   8523:        service_exit = true;
                   8524: #endif
                   8525:        return(0);
                   8526: }
                   8527: 
                   8528: inline void pcbios_int_15h_86h()
                   8529: {
                   8530:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8531: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8532:                if(!in_service && !in_service_29h) {
                   8533:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8534:                } else {
                   8535: #endif
                   8536:                        pcbios_int_15h_86h_thread(NULL);
                   8537:                        REQUEST_HARDWRE_UPDATE();
                   8538: #ifdef USE_SERVICE_THREAD
                   8539:                }
1.1.1.35  root     8540: #endif
                   8541:        }
1.1       root     8542: }
                   8543: 
                   8544: inline void pcbios_int_15h_87h()
                   8545: {
                   8546:        // copy extended memory (from DOSBox)
                   8547:        int len = REG16(CX) * 2;
1.1.1.3   root     8548:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8549:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8550:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8551:        memcpy(mem + dst, mem + src, len);
                   8552:        REG16(AX) = 0x00;
                   8553: }
                   8554: 
                   8555: inline void pcbios_int_15h_88h()
                   8556: {
1.1.1.17  root     8557:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8558: }
                   8559: 
                   8560: inline void pcbios_int_15h_89h()
                   8561: {
1.1.1.21  root     8562: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8563:        // switch to protected mode (from DOSBox)
                   8564:        write_io_byte(0x20, 0x10);
                   8565:        write_io_byte(0x21, REG8(BH));
                   8566:        write_io_byte(0x21, 0x00);
1.1.1.64! root     8567:        write_io_byte(0x21, 0xff);
1.1       root     8568:        write_io_byte(0xa0, 0x10);
                   8569:        write_io_byte(0xa1, REG8(BL));
                   8570:        write_io_byte(0xa1, 0x00);
1.1.1.64! root     8571:        write_io_byte(0xa1, 0xff);
1.1.1.3   root     8572:        i386_set_a20_line(1);
1.1.1.64! root     8573:        m_gdtr.limit = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x08);
        !          8574:        m_gdtr.base  = *(UINT32 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x08 + 0x02) & 0xffffff;
        !          8575:        m_idtr.limit = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x10);
        !          8576:        m_idtr.base  = *(UINT32 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x10 + 0x02) & 0xffffff;
1.1.1.3   root     8577: #if defined(HAS_I386)
                   8578:        m_cr[0] |= 1;
                   8579: #else
                   8580:        m_msw |= 1;
                   8581: #endif
1.1.1.64! root     8582:        i386_sreg_load(0x18, DS, NULL);
        !          8583:        i386_sreg_load(0x20, ES, NULL);
        !          8584:        i386_sreg_load(0x28, SS, NULL);
1.1.1.21  root     8585:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1.1.64! root     8586:        REG16(SP) += 6; // clear stack of interrupt frame
        !          8587:        UINT32 flags = i386_get_flags();
        !          8588:        flags &= ~0x247fd5; // clear CF,PF,AF,ZF,SF,TF,IF,DF,OF,IOPL,NT,AC,ID
        !          8589:        i386_set_flags(flags);
1.1       root     8590:        REG16(AX) = 0x00;
1.1.1.21  root     8591:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8592: #else
1.1.1.21  root     8593:        // i86/i186/v30: protected mode is not supported
1.1       root     8594:        REG8(AH) = 0x86;
1.1.1.3   root     8595:        m_CF = 1;
1.1       root     8596: #endif
                   8597: }
                   8598: 
1.1.1.21  root     8599: inline void pcbios_int_15h_8ah()
                   8600: {
                   8601:        UINT32 size = MAX_MEM - 0x100000;
                   8602:        REG16(AX) = size & 0xffff;
                   8603:        REG16(DX) = size >> 16;
                   8604: }
                   8605: 
1.1.1.54  root     8606: #ifdef EXT_BIOS_TOP
                   8607: inline void pcbios_int_15h_c1h()
                   8608: {
                   8609:        SREG(ES) = EXT_BIOS_TOP >> 4;
                   8610:        i386_load_segment_descriptor(ES);
                   8611: }
                   8612: #endif
                   8613: 
                   8614: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
                   8615: {
                   8616:        // from DOSBox DoPS2Callback()
                   8617:        UINT16 mdat = 0x08;
                   8618:        INT16 xdiff = mouse.position.x - mouse.prev_position.x;
                   8619:        INT16 ydiff = mouse.prev_position.y - mouse.position.y;
                   8620:        
1.1.1.59  root     8621: #if 1
                   8622:        if(xdiff > +16) xdiff = +16;
                   8623:        if(xdiff < -16) xdiff = -16;
                   8624:        if(ydiff > +16) ydiff = +16;
                   8625:        if(ydiff < -16) ydiff = -16;
                   8626: #endif
                   8627:        
1.1.1.54  root     8628:        if(mouse.buttons[0].status) {
                   8629:                mdat |= 0x01;
                   8630:        }
                   8631:        if(mouse.buttons[1].status) {
                   8632:                mdat |= 0x02;
                   8633:        }
                   8634:        mouse.prev_position.x = mouse.position.x;
                   8635:        mouse.prev_position.y = mouse.position.y;
1.1.1.59  root     8636:        
1.1.1.54  root     8637:        if((xdiff > 0xff) || (xdiff < -0xff)) {
                   8638:                mdat |= 0x40;   // x overflow
                   8639:        }
                   8640:        if((ydiff > 0xff) || (ydiff < -0xff)) {
                   8641:                mdat |= 0x80;   // y overflow
                   8642:        }
                   8643:        xdiff %= 256;
                   8644:        ydiff %= 256;
                   8645:        if(xdiff < 0) {
                   8646:                xdiff = (0x100 + xdiff);
                   8647:                mdat |= 0x10;
                   8648:        }
                   8649:        if(ydiff < 0) {
                   8650:                ydiff = (0x100 + ydiff);
                   8651:                mdat |= 0x20;
                   8652:        }
                   8653:        *data_1st = (UINT16)mdat;
                   8654:        *data_2nd = (UINT16)(xdiff % 256);
                   8655:        *data_3rd = (UINT16)(ydiff % 256);
                   8656: }
                   8657: 
                   8658: inline void pcbios_int_15h_c2h()
                   8659: {
                   8660:        static UINT8 sampling_rate = 5;
                   8661:        static UINT8 resolution = 2;
                   8662:        static UINT8 scaling = 1;
                   8663:        
                   8664:        switch(REG8(AL)) {
                   8665:        case 0x00:
1.1.1.59  root     8666:                if(REG8(BH) == 0x00) {
1.1.1.64! root     8667:                        if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
        !          8668:                                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
        !          8669:                        } else {
        !          8670:                                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
        !          8671:                        }
1.1.1.59  root     8672:                        pic[1].imr |= 0x10; // disable irq12
                   8673:                        mouse.enabled_ps2 = false;
                   8674:                        REG8(AH) = 0x00; // successful
                   8675:                } else if(REG8(BH) == 0x01) {
1.1.1.64! root     8676:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~ENABLE_QUICK_EDIT_MODE);
1.1.1.59  root     8677:                        pic[1].imr &= ~0x10; // enable irq12
                   8678:                        mouse.enabled_ps2 = true;
1.1.1.54  root     8679:                        REG8(AH) = 0x00; // successful
                   8680:                } else {
                   8681:                        REG8(AH) = 0x01; // invalid function
                   8682:                        m_CF = 1;
                   8683:                }
                   8684:                break;
                   8685:        case 0x01:
                   8686:                REG8(BH) = 0x00; // device id
                   8687:                REG8(BL) = 0xaa; // mouse
                   8688:        case 0x05:
1.1.1.64! root     8689:                if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
        !          8690:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
        !          8691:                } else {
        !          8692:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
        !          8693:                }
1.1.1.59  root     8694:                pic[1].imr |= 0x10; // disable irq12
                   8695:                mouse.enabled_ps2 = false;
1.1.1.54  root     8696:                sampling_rate = 5;
                   8697:                resolution = 2;
                   8698:                scaling = 1;
                   8699:                REG8(AH) = 0x00; // successful
                   8700:                break;
                   8701:        case 0x02:
                   8702:                sampling_rate = REG8(BH);
                   8703:                REG8(AH) = 0x00; // successful
                   8704:                break;
                   8705:        case 0x03:
                   8706:                resolution = REG8(BH);
                   8707:                REG8(AH) = 0x00; // successful
                   8708:                break;
                   8709:        case 0x04:
                   8710:                REG8(BH) = 0x00; // device id
                   8711:                REG8(AH) = 0x00; // successful
                   8712:                break;
                   8713:        case 0x06:
                   8714:                switch(REG8(BH)) {
                   8715:                case 0x00:
                   8716:                        REG8(BL) = 0x00;
                   8717:                        if(mouse.buttons[1].status) {
                   8718:                                REG8(BL) |= 0x01;
                   8719:                        }
                   8720:                        if(mouse.buttons[0].status) {
                   8721:                                REG8(BL) |= 0x04;
                   8722:                        }
                   8723:                        if(scaling == 2) {
                   8724:                                REG8(BL) |= 0x10;
                   8725:                        }
                   8726:                        REG8(CL) = resolution;
                   8727:                        switch(sampling_rate) {
                   8728:                        case 0:  REG8(DL) =  10; break;
                   8729:                        case 1:  REG8(DL) =  20; break;
                   8730:                        case 2:  REG8(DL) =  40; break;
                   8731:                        case 3:  REG8(DL) =  60; break;
                   8732:                        case 4:  REG8(DL) =  80; break;
                   8733: //                     case 5:  REG8(DL) = 100; break;
                   8734:                        case 6:  REG8(DL) = 200; break;
                   8735:                        default: REG8(DL) = 100; break;
                   8736:                        }
                   8737:                        REG8(AH) = 0x00; // successful
                   8738:                        break;
                   8739:                case 0x01:
                   8740:                case 0x02:
                   8741:                        scaling = REG8(BH);
                   8742:                        REG8(AH) = 0x00; // successful
                   8743:                        break;
                   8744:                default:
                   8745:                        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));
                   8746:                        REG8(AH) = 0x01; // invalid function
                   8747:                        m_CF = 1;
                   8748:                        break;
                   8749:                }
                   8750:                break;
                   8751:        case 0x07: // set device handler addr
                   8752:                mouse.call_addr_ps2.w.l = REG16(BX);
                   8753:                mouse.call_addr_ps2.w.h = SREG(ES);
                   8754:                REG8(AH) = 0x00; // successful
                   8755:                break;
                   8756:        case 0x08:
                   8757:                REG8(AH) = 0x00; // successful
                   8758:                break;
                   8759:        case 0x09:
                   8760:                {
                   8761:                        UINT16 data_1st, data_2nd, data_3rd;
                   8762:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   8763:                        REG8(BL) = (UINT8)(data_1st & 0xff);
                   8764:                        REG8(CL) = (UINT8)(data_2nd & 0xff);
                   8765:                        REG8(DL) = (UINT8)(data_3rd & 0xff);
                   8766:                }
                   8767:                REG8(AH) = 0x00; // successful
                   8768:                break;
                   8769:        default:
                   8770:                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));
                   8771: //             REG8(AH) = 0x86;
                   8772:                REG8(AH) = 0x01; // invalid function
                   8773:                m_CF = 1;
                   8774:                break;
                   8775:        }
                   8776: }
                   8777: 
1.1.1.3   root     8778: #if defined(HAS_I386)
1.1       root     8779: inline void pcbios_int_15h_c9h()
                   8780: {
                   8781:        REG8(AH) = 0x00;
                   8782:        REG8(CH) = cpu_type;
                   8783:        REG8(CL) = cpu_step;
                   8784: }
1.1.1.3   root     8785: #endif
1.1       root     8786: 
                   8787: inline void pcbios_int_15h_cah()
                   8788: {
                   8789:        switch(REG8(AL)) {
1.1.1.22  root     8790:        case 0x00:
1.1       root     8791:                if(REG8(BL) > 0x3f) {
                   8792:                        REG8(AH) = 0x03;
1.1.1.3   root     8793:                        m_CF = 1;
1.1       root     8794:                } else if(REG8(BL) < 0x0e) {
                   8795:                        REG8(AH) = 0x04;
1.1.1.3   root     8796:                        m_CF = 1;
1.1       root     8797:                } else {
1.1.1.8   root     8798:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8799:                }
                   8800:                break;
1.1.1.22  root     8801:        case 0x01:
1.1       root     8802:                if(REG8(BL) > 0x3f) {
                   8803:                        REG8(AH) = 0x03;
1.1.1.3   root     8804:                        m_CF = 1;
1.1       root     8805:                } else if(REG8(BL) < 0x0e) {
                   8806:                        REG8(AH) = 0x04;
1.1.1.3   root     8807:                        m_CF = 1;
1.1       root     8808:                } else {
1.1.1.8   root     8809:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8810:                }
                   8811:                break;
                   8812:        default:
1.1.1.22  root     8813:                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     8814:                REG8(AH) = 0x86;
1.1.1.3   root     8815:                m_CF = 1;
1.1       root     8816:                break;
                   8817:        }
                   8818: }
                   8819: 
1.1.1.22  root     8820: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8821: {
1.1.1.22  root     8822:        switch(REG8(AL)) {
                   8823:        case 0x01:
1.1.1.64! root     8824:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x0100000) >> 10);
1.1.1.22  root     8825:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8826:                break;
1.1.1.64! root     8827: #if defined(HAS_I386)
        !          8828:        case 0x20:
        !          8829:                if (REG32(EDX) == 0x534d4150 && REG32(ECX) >= 20) {
        !          8830:                        if(REG32(EBX) < 3) {
        !          8831:                                UINT32 base = 0, len = 0, type = 0;
        !          8832:                                switch(REG32(EBX)) {
        !          8833:                                case 0:
        !          8834:                                        base = 0x000000;
        !          8835:                                        len  = MEMORY_END;
        !          8836:                                        type = 1;
        !          8837:                                        break;
        !          8838:                                case 1:
        !          8839:                                        base = DUMMY_TOP;
        !          8840:                                        len  = 0x100000 - DUMMY_TOP;
        !          8841:                                        type = 2;
        !          8842:                                        break;
        !          8843:                                case 2:
        !          8844:                                        base = 0x100000;
        !          8845:                                        len  = MAX_MEM - 0x100000;
        !          8846:                                        type = 1;
        !          8847:                                        break;
        !          8848:                                }
        !          8849:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x00) = base;
        !          8850:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x04) = 0;
        !          8851:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x08) = len;
        !          8852:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x0c) = 0;
        !          8853:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x10) = type;
        !          8854:                                
        !          8855:                                if(++REG32(EBX) >= 3) {
        !          8856:                                        REG32(EBX) = 0;
        !          8857:                                }
        !          8858:                                REG32(ECX) = 20;
        !          8859:                        } else {
        !          8860:                                m_CF = 1;
        !          8861:                        }
        !          8862:                        REG32(EAX) = 0x534d4150;
        !          8863:                        break;
        !          8864:                }
        !          8865:        case 0x81:
        !          8866:                REG32(EAX) = REG32(ECX) = ((min(MAX_MEM, 0x1000000) - 0x0100000) >> 10);
        !          8867:                REG32(EBX) = REG32(EDX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
        !          8868:                break;
1.1.1.17  root     8869: #endif
1.1.1.22  root     8870:        default:
                   8871:                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));
                   8872:                REG8(AH) = 0x86;
                   8873:                m_CF = 1;
                   8874:                break;
                   8875:        }
                   8876: }
1.1.1.17  root     8877: 
1.1.1.55  root     8878: bool pcbios_is_key_buffer_empty()
                   8879: {
                   8880:        return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
                   8881: }
                   8882: 
1.1.1.51  root     8883: void pcbios_clear_key_buffer()
                   8884: {
                   8885:        key_buf_char->clear();
                   8886:        key_buf_scan->clear();
                   8887:        
                   8888:        // update key buffer
                   8889:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8890: }
                   8891: 
                   8892: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8893: {
                   8894:        // update key buffer
                   8895:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8896:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8897:        UINT16 next = tail + 2;
                   8898:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8899:                next = *(UINT16 *)(mem + 0x480);
                   8900:        }
                   8901:        if(next != head) {
                   8902:                *(UINT16 *)(mem + 0x41c) = next;
                   8903:                mem[0x400 + (tail++)] = key_char;
                   8904:                mem[0x400 + (tail++)] = key_scan;
1.1.1.55  root     8905:        } else {
                   8906:                // store to extra key buffer
                   8907:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8908:                        key_buf_char->write(key_char);
                   8909:                        key_buf_scan->write(key_scan);
                   8910:                }
1.1.1.51  root     8911:        }
                   8912: }
                   8913: 
                   8914: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8915: {
                   8916:        // update key buffer
                   8917:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8918:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8919:        UINT16 next = head + 2;
                   8920:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8921:                next = *(UINT16 *)(mem + 0x480);
                   8922:        }
                   8923:        if(head != tail) {
                   8924:                *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55  root     8925:                *key_char = mem[0x400 + (head++)];
                   8926:                *key_scan = mem[0x400 + (head++)];
                   8927:                
                   8928:                // restore from extra key buffer
                   8929:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8930:                        if(!key_buf_char->empty()) {
                   8931:                                pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
                   8932:                        }
                   8933:                }
                   8934:                return(true);
                   8935:        } else {
                   8936:                *key_char = 0x00;
                   8937:                *key_scan = 0x00;
                   8938:                return(false);
1.1.1.51  root     8939:        }
                   8940: }
                   8941: 
1.1.1.60  root     8942: bool pcbios_check_key_buffer(int *key_char, int *key_scan)
                   8943: {
                   8944:        // do not remove from key buffer
                   8945:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8946:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8947:        if(head != tail) {
                   8948:                *key_char = mem[0x400 + (head++)];
                   8949:                *key_scan = mem[0x400 + (head++)];
                   8950:                return(true);
                   8951:        } else {
                   8952:                *key_char = 0x00;
                   8953:                *key_scan = 0x00;
                   8954:                return(false);
                   8955:        }
                   8956: }
                   8957: 
1.1.1.33  root     8958: void pcbios_update_key_code(bool wait)
1.1       root     8959: {
1.1.1.32  root     8960:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8961: #ifdef USE_SERVICE_THREAD
                   8962:                EnterCriticalSection(&key_buf_crit_sect);
                   8963: #endif
1.1.1.55  root     8964:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     8965: #ifdef USE_SERVICE_THREAD
                   8966:                LeaveCriticalSection(&key_buf_crit_sect);
                   8967: #endif
                   8968:                if(empty) {
1.1.1.32  root     8969:                        if(!update_key_buffer()) {
1.1.1.33  root     8970:                                if(wait) {
1.1.1.32  root     8971:                                        Sleep(10);
                   8972:                                } else {
                   8973:                                        maybe_idle();
                   8974:                                }
1.1.1.14  root     8975:                        }
                   8976:                }
1.1.1.34  root     8977:        }
                   8978:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8979: #ifdef USE_SERVICE_THREAD
                   8980:                EnterCriticalSection(&key_buf_crit_sect);
                   8981: #endif
1.1.1.51  root     8982:                int key_char, key_scan;
                   8983:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8984:                        key_code  = key_char << 0;
                   8985:                        key_code |= key_scan << 8;
1.1.1.35  root     8986:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8987:                }
                   8988:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8989:                        key_code |= key_char << 16;
                   8990:                        key_code |= key_scan << 24;
1.1.1.33  root     8991:                        key_recv |= 0xffff0000;
1.1.1.32  root     8992:                }
1.1.1.35  root     8993: #ifdef USE_SERVICE_THREAD
                   8994:                LeaveCriticalSection(&key_buf_crit_sect);
                   8995: #endif
1.1       root     8996:        }
                   8997: }
                   8998: 
1.1.1.35  root     8999: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     9000: {
1.1.1.54  root     9001:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     9002:                pcbios_update_key_code(true);
1.1       root     9003:        }
1.1.1.33  root     9004:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   9005:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   9006:                        if(REG8(AH) == 0x10) {
                   9007:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   9008:                        } else {
                   9009:                                key_code = ((key_code >> 16) & 0xff00);
                   9010:                        }
                   9011:                        key_recv >>= 16;
1.1       root     9012:                }
                   9013:        }
                   9014:        REG16(AX) = key_code & 0xffff;
                   9015:        key_code >>= 16;
1.1.1.33  root     9016:        key_recv >>= 16;
1.1.1.35  root     9017:        
                   9018: #ifdef USE_SERVICE_THREAD
                   9019:        service_exit = true;
                   9020: #endif
                   9021:        return(0);
                   9022: }
                   9023: 
                   9024: inline void pcbios_int_16h_00h()
                   9025: {
                   9026: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9027:        if(!in_service && !in_service_29h) {
                   9028:                start_service_loop(pcbios_int_16h_00h_thread);
                   9029:        } else {
                   9030: #endif
                   9031:                pcbios_int_16h_00h_thread(NULL);
                   9032:                REQUEST_HARDWRE_UPDATE();
                   9033: #ifdef USE_SERVICE_THREAD
                   9034:        }
1.1.1.35  root     9035: #endif
1.1       root     9036: }
                   9037: 
                   9038: inline void pcbios_int_16h_01h()
                   9039: {
1.1.1.33  root     9040:        if(key_recv == 0) {
                   9041:                pcbios_update_key_code(false);
1.1.1.5   root     9042:        }
1.1.1.33  root     9043:        if(key_recv != 0) {
                   9044:                UINT32 key_code_tmp = key_code;
                   9045:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   9046:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   9047:                                if(REG8(AH) == 0x11) {
                   9048:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   9049:                                } else {
                   9050:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   9051:                                }
                   9052:                        }
1.1       root     9053:                }
1.1.1.5   root     9054:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     9055: #if defined(HAS_I386)
1.1.1.33  root     9056:                m_ZF = 0;
                   9057: #else
                   9058:                m_ZeroVal = 1;
                   9059: #endif
                   9060:        } else {
                   9061: #if defined(HAS_I386)
                   9062:                m_ZF = 1;
1.1.1.3   root     9063: #else
1.1.1.33  root     9064:                m_ZeroVal = 0;
1.1.1.3   root     9065: #endif
1.1.1.33  root     9066:        }
1.1       root     9067: }
                   9068: 
                   9069: inline void pcbios_int_16h_02h()
                   9070: {
                   9071:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   9072:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   9073:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   9074:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   9075:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   9076:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   9077:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   9078:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   9079: }
                   9080: 
                   9081: inline void pcbios_int_16h_03h()
                   9082: {
                   9083:        static UINT16 status = 0;
                   9084:        
                   9085:        switch(REG8(AL)) {
                   9086:        case 0x05:
                   9087:                status = REG16(BX);
                   9088:                break;
                   9089:        case 0x06:
                   9090:                REG16(BX) = status;
                   9091:                break;
                   9092:        default:
1.1.1.3   root     9093:                m_CF = 1;
1.1       root     9094:                break;
                   9095:        }
                   9096: }
                   9097: 
                   9098: inline void pcbios_int_16h_05h()
                   9099: {
1.1.1.32  root     9100:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     9101: #ifdef USE_SERVICE_THREAD
                   9102:                EnterCriticalSection(&key_buf_crit_sect);
                   9103: #endif
1.1.1.51  root     9104:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     9105: #ifdef USE_SERVICE_THREAD
                   9106:                LeaveCriticalSection(&key_buf_crit_sect);
                   9107: #endif
1.1.1.32  root     9108:        }
1.1       root     9109:        REG8(AL) = 0x00;
                   9110: }
                   9111: 
1.1.1.60  root     9112: inline void pcbios_int_16h_09h()
                   9113: {
                   9114:        REG8(AL)  = 0x00;
                   9115: //     REG8(AL) |= 0x01;       // INT 16/AX=0300h supported    (set default delay and rate (PCjr and some PS/2))
                   9116: //     REG8(AL) |= 0x02;       // INT 16/AX=0304h supported    (turn off typematic repeat (PCjr and some PS/2))
                   9117:        REG8(AL) |= 0x04;       // INT 16/AX=0305h supported    (set repeat rate and delay (AT,PS))
                   9118:        REG8(AL) |= 0x08;       // INT 16/AX=0306h supported    (get current typematic rate and delay (newer PS/2s))
                   9119:        REG8(AL) |= 0x10;       // INT 16/AH=0Ah supported      (get keyboard id)
                   9120:        REG8(AL) |= 0x20;       // INT 16/AH=10h-12h supported  (enhanced keyboard support)
                   9121: //     REG8(AL) |= 0x40;       // INT 16/AH=20h-22h supported  (122-key keyboard support)
                   9122: //     REG8(AL) |= 0x80;       // reserved
                   9123: }
                   9124: 
                   9125: inline void pcbios_int_16h_0ah()
                   9126: {
                   9127: //     REG16(BX) = 0x41ab;     // MF2 Keyboard (usually in translate mode)
                   9128:        REG16(BX) = 0x83ab;     // MF2 Keyboard (pass-through mode)
                   9129: }
                   9130: 
                   9131: inline void pcbios_int_16h_11h()
                   9132: {
                   9133:        int key_char, key_scan;
                   9134:        
                   9135: #ifdef USE_SERVICE_THREAD
                   9136:        EnterCriticalSection(&key_buf_crit_sect);
                   9137: #endif
                   9138:        if(pcbios_check_key_buffer(&key_char, &key_scan)) {
                   9139:                REG8(AL) = key_char;
                   9140:                REG8(AH) = key_scan;
                   9141: #if defined(HAS_I386)
                   9142:                m_ZF = 0;
                   9143: #else
                   9144:                m_ZeroVal = 1;
                   9145: #endif
                   9146:        } else {
                   9147: #if defined(HAS_I386)
                   9148:                m_ZF = 1;
                   9149: #else
                   9150:                m_ZeroVal = 0;
                   9151: #endif
                   9152:        }
                   9153: #ifdef USE_SERVICE_THREAD
                   9154:        LeaveCriticalSection(&key_buf_crit_sect);
                   9155: #endif
                   9156: }
                   9157: 
1.1       root     9158: inline void pcbios_int_16h_12h()
                   9159: {
                   9160:        pcbios_int_16h_02h();
                   9161:        
                   9162:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   9163:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   9164:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   9165:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   9166:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   9167:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   9168:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   9169:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   9170: }
                   9171: 
                   9172: inline void pcbios_int_16h_13h()
                   9173: {
                   9174:        static UINT16 status = 0;
                   9175:        
                   9176:        switch(REG8(AL)) {
                   9177:        case 0x00:
                   9178:                status = REG16(DX);
                   9179:                break;
                   9180:        case 0x01:
                   9181:                REG16(DX) = status;
                   9182:                break;
                   9183:        default:
1.1.1.22  root     9184:                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     9185:                m_CF = 1;
1.1       root     9186:                break;
                   9187:        }
                   9188: }
                   9189: 
                   9190: inline void pcbios_int_16h_14h()
                   9191: {
                   9192:        static UINT8 status = 0;
                   9193:        
                   9194:        switch(REG8(AL)) {
                   9195:        case 0x00:
                   9196:        case 0x01:
                   9197:                status = REG8(AL);
                   9198:                break;
                   9199:        case 0x02:
                   9200:                REG8(AL) = status;
                   9201:                break;
                   9202:        default:
1.1.1.22  root     9203:                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     9204:                m_CF = 1;
1.1       root     9205:                break;
                   9206:        }
                   9207: }
                   9208: 
1.1.1.24  root     9209: inline void pcbios_int_16h_55h()
                   9210: {
                   9211:        switch(REG8(AL)) {
                   9212:        case 0x00:
                   9213:                // keyboard tsr is not present
                   9214:                break;
                   9215:        case 0xfe:
                   9216:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   9217:                break;
                   9218:        case 0xff:
                   9219:                break;
                   9220:        default:
                   9221:                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));
                   9222:                m_CF = 1;
                   9223:                break;
                   9224:        }
                   9225: }
                   9226: 
1.1.1.30  root     9227: inline void pcbios_int_16h_6fh()
                   9228: {
                   9229:        switch(REG8(AL)) {
                   9230:        case 0x00:
                   9231:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   9232:                break;
                   9233:        default:
                   9234:                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));
                   9235:                m_CF = 1;
                   9236:                break;
                   9237:        }
                   9238: }
                   9239: 
1.1.1.37  root     9240: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   9241: {
                   9242:        UINT8 hi = jis >> 8;
                   9243:        UINT8 lo = jis & 0xff;
                   9244:        
                   9245:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   9246:        hi = (hi - 0x21) / 2 + 0x81;
                   9247:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   9248:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   9249:        
                   9250:        return((hi << 8) + lo);
                   9251: }
                   9252: 
                   9253: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   9254: {
                   9255:        UINT8 hi = sjis >> 8;
                   9256:        UINT8 lo = sjis & 0xff;
                   9257:        
                   9258:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   9259:                return(0x2121);
                   9260:        }
                   9261:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   9262:                return(0x2121);
                   9263:        }
                   9264:        if(hi >= 0xf0 && hi <= 0xf3) {
                   9265:                // gaiji
                   9266:                if(lo >= 0x40 && lo <= 0x7e) {
                   9267:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   9268:                }
                   9269:                if(lo >= 0x80 && lo <= 0x9e) {
                   9270:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   9271:                }
                   9272:                if(lo >= 0x9f && lo <= 0xfc) {
                   9273:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   9274:                }
                   9275:        }
                   9276:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   9277:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   9278:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   9279:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   9280:        
                   9281:        return((hi << 8) + lo);
                   9282: }
                   9283: 
1.1.1.38  root     9284: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   9285: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   9286: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     9287: 
                   9288: void pcbios_printer_out(int c, UINT8 data)
                   9289: {
                   9290:        if(pio[c].conv_mode) {
                   9291:                if(pio[c].sjis_hi != 0) {
                   9292:                        if(!pio[c].jis_mode) {
                   9293:                                printer_out(c, 0x1c);
                   9294:                                printer_out(c, 0x26);
                   9295:                                pio[c].jis_mode = true;
                   9296:                        }
                   9297:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   9298:                        printer_out(c, jis >> 8);
                   9299:                        printer_out(c, jis & 0xff);
                   9300:                        pio[c].sjis_hi = 0;
                   9301:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   9302:                        printer_out(c, data);
                   9303:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9304:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9305:                        }
                   9306:                        pio[c].esc_len++;
                   9307:                        
                   9308:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9309:                        case 0x33: // 1Bh 33h XX
                   9310:                        case 0x4a: // 1Bh 4Ah XX
                   9311:                        case 0x4e: // 1Bh 4Eh XX
                   9312:                        case 0x51: // 1Bh 51h XX
                   9313:                        case 0x55: // 1Bh 55h XX
                   9314:                        case 0x6c: // 1Bh 6Ch XX
                   9315:                        case 0x71: // 1Bh 71h XX
                   9316:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     9317:                                if(pio[c].esc_len == 3) {
                   9318:                                        pio[c].esc_buf[0] = 0x00;
                   9319:                                }
                   9320:                                break;
1.1.1.38  root     9321:                        case 0x24: // 1Bh 24h XX XX
                   9322:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     9323:                                if(pio[c].esc_len == 4) {
                   9324:                                        pio[c].esc_buf[0] = 0x00;
                   9325:                                }
                   9326:                                break;
1.1.1.38  root     9327:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     9328:                                if(pio[c].esc_len >= 3) {
                   9329:                                        switch(pio[c].esc_buf[2]) {
                   9330:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   9331:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   9332:                                                        pio[c].esc_buf[0] = 0x00;
                   9333:                                                }
                   9334:                                                break;
                   9335:                                        case 32: case 33: case 38: case 39: case 40:
                   9336:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   9337:                                                        pio[c].esc_buf[0] = 0x00;
                   9338:                                                }
                   9339:                                                break;
1.1.1.38  root     9340:                                        case 71: case 72: case 73:
                   9341:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   9342:                                                        pio[c].esc_buf[0] = 0x00;
                   9343:                                                }
                   9344:                                                break;
1.1.1.37  root     9345:                                        default:
                   9346:                                                pio[c].esc_buf[0] = 0x00;
                   9347:                                                break;
                   9348:                                        }
                   9349:                                }
                   9350:                                break;
1.1.1.38  root     9351:                        case 0x40: // 1Bh 40h
1.1.1.37  root     9352:                                if(pio[c].jis_mode) {
                   9353:                                        printer_out(c, 0x1c);
                   9354:                                        printer_out(c, 0x2e);
                   9355:                                        pio[c].jis_mode = false;
                   9356:                                }
                   9357:                                pio[c].esc_buf[0] = 0x00;
                   9358:                                break;
1.1.1.38  root     9359:                        case 0x42: // 1Bh 42h data 00h
                   9360:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9361:                                if(pio[c].esc_len >= 3 && data == 0) {
                   9362:                                        pio[c].esc_buf[0] = 0x00;
                   9363:                                }
                   9364:                                break;
1.1.1.38  root     9365:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     9366:                                if(pio[c].esc_len >= 3 && data != 0) {
                   9367:                                        pio[c].esc_buf[0] = 0x00;
                   9368:                                }
                   9369:                                break;
1.1.1.38  root     9370:                        default: // 1Bh XX
1.1.1.37  root     9371:                                pio[c].esc_buf[0] = 0x00;
                   9372:                                break;
                   9373:                        }
                   9374:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   9375:                        printer_out(c, data);
                   9376:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9377:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9378:                        }
                   9379:                        pio[c].esc_len++;
                   9380:                        
                   9381:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9382:                        case 0x21: // 1Ch 21h XX
                   9383:                        case 0x2d: // 1Ch 2Dh XX
                   9384:                        case 0x57: // 1Ch 57h XX
                   9385:                        case 0x6b: // 1Ch 6Bh XX
                   9386:                        case 0x72: // 1Ch 72h XX
                   9387:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     9388:                                if(pio[c].esc_len == 3) {
                   9389:                                        pio[c].esc_buf[0] = 0x00;
                   9390:                                }
                   9391:                                break;
1.1.1.38  root     9392:                        case 0x26: // 1Ch 26h
1.1.1.37  root     9393:                                pio[c].jis_mode = true;
                   9394:                                pio[c].esc_buf[0] = 0x00;
                   9395:                                break;
1.1.1.38  root     9396:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     9397:                                pio[c].jis_mode = false;
                   9398:                                pio[c].esc_buf[0] = 0x00;
                   9399:                                break;
1.1.1.38  root     9400:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     9401:                                if(pio[c].esc_len == 76) {
                   9402:                                        pio[c].esc_buf[0] = 0x00;
                   9403:                                }
                   9404:                                break;
1.1.1.38  root     9405:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9406:                                if(pio[c].esc_len == 6) {
                   9407:                                        pio[c].esc_buf[0] = 0x00;
                   9408:                                }
                   9409:                                break;
1.1.1.38  root     9410:                        case 0x53: // 1Ch 53h XX XX
                   9411:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     9412:                                if(pio[c].esc_len == 4) {
                   9413:                                        pio[c].esc_buf[0] = 0x00;
                   9414:                                }
                   9415:                                break;
1.1.1.38  root     9416:                        default: // 1Ch XX
1.1.1.37  root     9417:                                pio[c].esc_buf[0] = 0x00;
                   9418:                                break;
                   9419:                        }
                   9420:                } else if(data == 0x1b || data == 0x1c) {
                   9421:                        printer_out(c, data);
                   9422:                        pio[c].esc_buf[0] = data;
                   9423:                        pio[c].esc_len = 1;
                   9424:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   9425:                        pio[c].sjis_hi = data;
                   9426:                } else {
                   9427:                        if(pio[c].jis_mode) {
                   9428:                                printer_out(c, 0x1c);
                   9429:                                printer_out(c, 0x2e);
                   9430:                                pio[c].jis_mode = false;
                   9431:                        }
                   9432:                        printer_out(c, data);
                   9433:                }
                   9434:        } else {
                   9435:                if(pio[c].jis_mode) {
                   9436:                        printer_out(c, 0x1c);
                   9437:                        printer_out(c, 0x2e);
                   9438:                        pio[c].jis_mode = false;
                   9439:                }
                   9440:                printer_out(c, data);
                   9441:        }
                   9442: }
                   9443: 
                   9444: inline void pcbios_int_17h_00h()
                   9445: {
                   9446:        if(REG16(DX) < 3) {
                   9447:                pcbios_printer_out(REG16(DX), REG8(AL));
                   9448:                REG8(AH) = 0xd0;
                   9449:        }
                   9450: }
                   9451: 
                   9452: inline void pcbios_int_17h_01h()
                   9453: {
                   9454:        if(REG16(DX) < 3) {
                   9455:                REG8(AH) = 0xd0;
                   9456:        }
                   9457: }
                   9458: 
                   9459: inline void pcbios_int_17h_02h()
                   9460: {
                   9461:        if(REG16(DX) < 3) {
                   9462:                REG8(AH) = 0xd0;
                   9463:        }
                   9464: }
                   9465: 
                   9466: inline void pcbios_int_17h_03h()
                   9467: {
                   9468:        switch(REG8(AL)) {
                   9469:        case 0x00:
                   9470:                if(REG16(DX) < 3) {
                   9471:                        if(pio[REG16(DX)].jis_mode) {
                   9472:                                printer_out(REG16(DX), 0x1c);
                   9473:                                printer_out(REG16(DX), 0x2e);
                   9474:                                pio[REG16(DX)].jis_mode = false;
                   9475:                        }
                   9476:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   9477:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   9478:                        }
                   9479:                        REG16(CX) = 0x0000;
                   9480:                        REG8(AH) = 0xd0;
                   9481:                }
                   9482:                break;
                   9483:        default:
                   9484:                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));
                   9485:                break;
                   9486:        }
                   9487: }
                   9488: 
                   9489: inline void pcbios_int_17h_50h()
                   9490: {
                   9491:        switch(REG8(AL)) {
                   9492:        case 0x00:
                   9493:                if(REG16(DX) < 3) {
                   9494:                        if(REG16(BX) = 0x0001) {
                   9495:                                pio[REG16(DX)].conv_mode = false;
                   9496:                                REG8(AL) = 0x00;
                   9497:                        } else if(REG16(BX) = 0x0051) {
                   9498:                                pio[REG16(DX)].conv_mode = true;
                   9499:                                REG8(AL) = 0x00;
                   9500:                        } else {
                   9501:                                REG8(AL) = 0x01;
                   9502:                        }
                   9503:                } else {
                   9504:                        REG8(AL) = 0x02;
                   9505:                }
                   9506:                break;
                   9507:        case 0x01:
                   9508:                if(REG16(DX) < 3) {
                   9509:                        if(pio[REG16(DX)].conv_mode) {
                   9510:                                REG16(BX) = 0x0051;
                   9511:                        } else {
                   9512:                                REG16(BX) = 0x0001;
                   9513:                        }
                   9514:                        REG8(AL) = 0x00;
                   9515:                } else {
                   9516:                        REG8(AL) = 0x02;
                   9517:                }
                   9518:                break;
                   9519:        default:
                   9520:                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));
                   9521:                break;
                   9522:        }
                   9523: }
                   9524: 
                   9525: inline void pcbios_int_17h_51h()
                   9526: {
                   9527:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9528:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9529:        } else {
                   9530:                REG16(DX) = 0x0000;
                   9531:        }
                   9532: }
                   9533: 
                   9534: inline void pcbios_int_17h_52h()
                   9535: {
                   9536:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9537:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9538:        } else {
                   9539:                REG16(DX) = 0x0000;
                   9540:        }
                   9541: }
                   9542: 
                   9543: inline void pcbios_int_17h_84h()
                   9544: {
                   9545:        if(REG16(DX) < 3) {
                   9546:                if(pio[REG16(DX)].jis_mode) {
                   9547:                        printer_out(REG16(DX), 0x1c);
                   9548:                        printer_out(REG16(DX), 0x2e);
                   9549:                        pio[REG16(DX)].jis_mode = false;
                   9550:                }
                   9551:                printer_out(REG16(DX), REG8(AL));
                   9552:                REG8(AH) = 0xd0;
                   9553:        }
                   9554: }
                   9555: 
                   9556: inline void pcbios_int_17h_85h()
                   9557: {
                   9558:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9559: }
                   9560: 
1.1       root     9561: inline void pcbios_int_1ah_00h()
                   9562: {
1.1.1.19  root     9563:        pcbios_update_daily_timer_counter(timeGetTime());
                   9564:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9565:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9566:        REG8(AL) = mem[0x470];
                   9567:        mem[0x470] = 0;
1.1       root     9568: }
                   9569: 
                   9570: inline int to_bcd(int t)
                   9571: {
                   9572:        int u = (t % 100) / 10;
                   9573:        return (u << 4) | (t % 10);
                   9574: }
                   9575: 
                   9576: inline void pcbios_int_1ah_02h()
                   9577: {
                   9578:        SYSTEMTIME time;
                   9579:        
                   9580:        GetLocalTime(&time);
                   9581:        REG8(CH) = to_bcd(time.wHour);
                   9582:        REG8(CL) = to_bcd(time.wMinute);
                   9583:        REG8(DH) = to_bcd(time.wSecond);
                   9584:        REG8(DL) = 0x00;
                   9585: }
                   9586: 
                   9587: inline void pcbios_int_1ah_04h()
                   9588: {
                   9589:        SYSTEMTIME time;
                   9590:        
                   9591:        GetLocalTime(&time);
                   9592:        REG8(CH) = to_bcd(time.wYear / 100);
                   9593:        REG8(CL) = to_bcd(time.wYear);
                   9594:        REG8(DH) = to_bcd(time.wMonth);
                   9595:        REG8(DL) = to_bcd(time.wDay);
                   9596: }
                   9597: 
                   9598: inline void pcbios_int_1ah_0ah()
                   9599: {
                   9600:        SYSTEMTIME time;
                   9601:        FILETIME file_time;
                   9602:        WORD dos_date, dos_time;
                   9603:        
                   9604:        GetLocalTime(&time);
                   9605:        SystemTimeToFileTime(&time, &file_time);
                   9606:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9607:        REG16(CX) = dos_date;
                   9608: }
                   9609: 
                   9610: // msdos system call
                   9611: 
1.1.1.43  root     9612: inline void msdos_int_21h_56h(int lfn);
                   9613: 
1.1       root     9614: inline void msdos_int_21h_00h()
                   9615: {
1.1.1.3   root     9616:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9617: }
                   9618: 
1.1.1.35  root     9619: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9620: {
                   9621:        REG8(AL) = msdos_getche();
1.1.1.33  root     9622:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9623:        
1.1.1.35  root     9624: #ifdef USE_SERVICE_THREAD
                   9625:        service_exit = true;
                   9626: #endif
                   9627:        return(0);
                   9628: }
                   9629: 
                   9630: inline void msdos_int_21h_01h()
                   9631: {
                   9632: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9633:        if(!in_service && !in_service_29h &&
1.1.1.58  root     9634:           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9635:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9636:                // msdos_putch() will be used in this service
                   9637:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9638:                start_service_loop(msdos_int_21h_01h_thread);
                   9639:        } else {
                   9640: #endif
                   9641:                msdos_int_21h_01h_thread(NULL);
                   9642:                REQUEST_HARDWRE_UPDATE();
                   9643: #ifdef USE_SERVICE_THREAD
                   9644:        }
1.1.1.35  root     9645: #endif
1.1       root     9646: }
                   9647: 
                   9648: inline void msdos_int_21h_02h()
                   9649: {
1.1.1.33  root     9650:        UINT8 data = REG8(DL);
                   9651:        msdos_putch(data);
                   9652:        REG8(AL) = data;
                   9653:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9654: }
                   9655: 
                   9656: inline void msdos_int_21h_03h()
                   9657: {
                   9658:        REG8(AL) = msdos_aux_in();
                   9659: }
                   9660: 
                   9661: inline void msdos_int_21h_04h()
                   9662: {
                   9663:        msdos_aux_out(REG8(DL));
                   9664: }
                   9665: 
                   9666: inline void msdos_int_21h_05h()
                   9667: {
                   9668:        msdos_prn_out(REG8(DL));
                   9669: }
                   9670: 
                   9671: inline void msdos_int_21h_06h()
                   9672: {
                   9673:        if(REG8(DL) == 0xff) {
                   9674:                if(msdos_kbhit()) {
                   9675:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9676: #if defined(HAS_I386)
                   9677:                        m_ZF = 0;
                   9678: #else
                   9679:                        m_ZeroVal = 1;
                   9680: #endif
1.1       root     9681:                } else {
                   9682:                        REG8(AL) = 0;
1.1.1.3   root     9683: #if defined(HAS_I386)
                   9684:                        m_ZF = 1;
                   9685: #else
                   9686:                        m_ZeroVal = 0;
                   9687: #endif
1.1.1.14  root     9688:                        maybe_idle();
1.1       root     9689:                }
                   9690:        } else {
1.1.1.33  root     9691:                UINT8 data = REG8(DL);
                   9692:                msdos_putch(data);
                   9693:                REG8(AL) = data;
1.1       root     9694:        }
                   9695: }
                   9696: 
1.1.1.35  root     9697: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9698: {
                   9699:        REG8(AL) = msdos_getch();
1.1.1.26  root     9700:        
1.1.1.35  root     9701: #ifdef USE_SERVICE_THREAD
                   9702:        service_exit = true;
                   9703: #endif
                   9704:        return(0);
1.1       root     9705: }
                   9706: 
1.1.1.35  root     9707: inline void msdos_int_21h_07h()
                   9708: {
                   9709: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9710:        if(!in_service && !in_service_29h) {
                   9711:                start_service_loop(msdos_int_21h_07h_thread);
                   9712:        } else {
                   9713: #endif
                   9714:                msdos_int_21h_07h_thread(NULL);
                   9715:                REQUEST_HARDWRE_UPDATE();
                   9716: #ifdef USE_SERVICE_THREAD
                   9717:        }
1.1.1.35  root     9718: #endif
                   9719: }
                   9720: 
                   9721: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9722: {
                   9723:        REG8(AL) = msdos_getch();
1.1.1.33  root     9724:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9725:        
1.1.1.35  root     9726: #ifdef USE_SERVICE_THREAD
                   9727:        service_exit = true;
                   9728: #endif
                   9729:        return(0);
                   9730: }
                   9731: 
                   9732: inline void msdos_int_21h_08h()
                   9733: {
                   9734: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9735:        if(!in_service && !in_service_29h) {
                   9736:                start_service_loop(msdos_int_21h_08h_thread);
                   9737:        } else {
                   9738: #endif
                   9739:                msdos_int_21h_08h_thread(NULL);
                   9740:                REQUEST_HARDWRE_UPDATE();
                   9741: #ifdef USE_SERVICE_THREAD
                   9742:        }
1.1.1.35  root     9743: #endif
1.1       root     9744: }
                   9745: 
                   9746: inline void msdos_int_21h_09h()
                   9747: {
1.1.1.21  root     9748:        msdos_stdio_reopen();
                   9749:        
1.1.1.20  root     9750:        process_t *process = msdos_process_info_get(current_psp);
                   9751:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9752:        
1.1.1.14  root     9753:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9754:        int len = 0;
1.1       root     9755:        
1.1.1.14  root     9756:        while(str[len] != '$' && len < 0x10000) {
                   9757:                len++;
                   9758:        }
1.1.1.20  root     9759:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9760:                // stdout is redirected to file
1.1.1.20  root     9761:                msdos_write(fd, str, len);
1.1       root     9762:        } else {
                   9763:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9764:                        msdos_putch(str[i]);
1.1       root     9765:                }
                   9766:        }
1.1.1.33  root     9767:        REG8(AL) = '$';
                   9768:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9769: }
                   9770: 
1.1.1.35  root     9771: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9772: {
1.1.1.3   root     9773:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9774:        int max = mem[ofs] - 1;
                   9775:        UINT8 *buf = mem + ofs + 2;
                   9776:        int chr, p = 0;
                   9777:        
                   9778:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9779:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9780:                        p = 0;
1.1.1.33  root     9781:                        msdos_putch(0x03);
                   9782:                        msdos_putch(0x0d);
                   9783:                        msdos_putch(0x0a);
1.1.1.26  root     9784:                        break;
1.1.1.33  root     9785:                } else if(ctrl_break_pressed) {
                   9786:                        // skip this byte
1.1.1.26  root     9787:                } else if(chr == 0x00) {
1.1       root     9788:                        // skip 2nd byte
                   9789:                        msdos_getch();
                   9790:                } else if(chr == 0x08) {
                   9791:                        // back space
                   9792:                        if(p > 0) {
                   9793:                                p--;
1.1.1.20  root     9794:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9795:                                        msdos_putch(0x08);
                   9796:                                        msdos_putch(0x08);
                   9797:                                        msdos_putch(0x20);
                   9798:                                        msdos_putch(0x20);
                   9799:                                        msdos_putch(0x08);
                   9800:                                        msdos_putch(0x08);
1.1.1.36  root     9801:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9802:                                        p--;
                   9803:                                        msdos_putch(0x08);
                   9804:                                        msdos_putch(0x08);
                   9805:                                        msdos_putch(0x20);
                   9806:                                        msdos_putch(0x20);
                   9807:                                        msdos_putch(0x08);
                   9808:                                        msdos_putch(0x08);
1.1.1.34  root     9809:                                } else {
                   9810:                                        msdos_putch(0x08);
                   9811:                                        msdos_putch(0x20);
                   9812:                                        msdos_putch(0x08);
                   9813:                                }
                   9814:                        }
                   9815:                } else if(chr == 0x1b) {
                   9816:                        // escape
                   9817:                        while(p > 0) {
                   9818:                                p--;
                   9819:                                if(msdos_ctrl_code_check(buf[p])) {
                   9820:                                        msdos_putch(0x08);
                   9821:                                        msdos_putch(0x08);
                   9822:                                        msdos_putch(0x20);
                   9823:                                        msdos_putch(0x20);
                   9824:                                        msdos_putch(0x08);
                   9825:                                        msdos_putch(0x08);
1.1.1.20  root     9826:                                } else {
1.1.1.34  root     9827:                                        msdos_putch(0x08);
                   9828:                                        msdos_putch(0x20);
                   9829:                                        msdos_putch(0x08);
1.1.1.20  root     9830:                                }
1.1       root     9831:                        }
                   9832:                } else if(p < max) {
                   9833:                        buf[p++] = chr;
                   9834:                        msdos_putch(chr);
                   9835:                }
                   9836:        }
                   9837:        buf[p] = 0x0d;
                   9838:        mem[ofs + 1] = p;
1.1.1.33  root     9839:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9840:        
1.1.1.35  root     9841: #ifdef USE_SERVICE_THREAD
                   9842:        service_exit = true;
                   9843: #endif
                   9844:        return(0);
                   9845: }
                   9846: 
                   9847: inline void msdos_int_21h_0ah()
                   9848: {
                   9849:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9850: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9851:                if(!in_service && !in_service_29h &&
1.1.1.58  root     9852:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9853:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9854:                        // msdos_putch() will be used in this service
                   9855:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9856:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9857:                } else {
                   9858: #endif
                   9859:                        msdos_int_21h_0ah_thread(NULL);
                   9860:                        REQUEST_HARDWRE_UPDATE();
                   9861: #ifdef USE_SERVICE_THREAD
                   9862:                }
1.1.1.35  root     9863: #endif
                   9864:        }
1.1       root     9865: }
                   9866: 
                   9867: inline void msdos_int_21h_0bh()
                   9868: {
                   9869:        if(msdos_kbhit()) {
                   9870:                REG8(AL) = 0xff;
                   9871:        } else {
                   9872:                REG8(AL) = 0x00;
1.1.1.14  root     9873:                maybe_idle();
1.1       root     9874:        }
1.1.1.33  root     9875:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9876: }
                   9877: 
                   9878: inline void msdos_int_21h_0ch()
                   9879: {
                   9880:        // clear key buffer
1.1.1.21  root     9881:        msdos_stdio_reopen();
                   9882:        
1.1.1.20  root     9883:        process_t *process = msdos_process_info_get(current_psp);
                   9884:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9885:        
                   9886:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9887:                // stdin is redirected to file
                   9888:        } else {
                   9889:                while(msdos_kbhit()) {
                   9890:                        msdos_getch();
                   9891:                }
                   9892:        }
                   9893:        
                   9894:        switch(REG8(AL)) {
                   9895:        case 0x01:
                   9896:                msdos_int_21h_01h();
                   9897:                break;
                   9898:        case 0x06:
                   9899:                msdos_int_21h_06h();
                   9900:                break;
                   9901:        case 0x07:
                   9902:                msdos_int_21h_07h();
                   9903:                break;
                   9904:        case 0x08:
                   9905:                msdos_int_21h_08h();
                   9906:                break;
                   9907:        case 0x0a:
                   9908:                msdos_int_21h_0ah();
                   9909:                break;
                   9910:        default:
1.1.1.48  root     9911:                // the buffer is flushed but no input is attempted
1.1       root     9912:                break;
                   9913:        }
                   9914: }
                   9915: 
                   9916: inline void msdos_int_21h_0dh()
                   9917: {
                   9918: }
                   9919: 
                   9920: inline void msdos_int_21h_0eh()
                   9921: {
                   9922:        if(REG8(DL) < 26) {
                   9923:                _chdrive(REG8(DL) + 1);
                   9924:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9925:                msdos_sda_update(current_psp);
1.1       root     9926:        }
                   9927:        REG8(AL) = 26; // zdrive
                   9928: }
                   9929: 
1.1.1.14  root     9930: inline void msdos_int_21h_0fh()
                   9931: {
                   9932:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9933:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9934:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     9935:        HANDLE hFile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     9936:        
1.1.1.14  root     9937:        if(hFile == INVALID_HANDLE_VALUE) {
                   9938:                REG8(AL) = 0xff;
                   9939:        } else {
                   9940:                REG8(AL) = 0;
                   9941:                fcb->current_block = 0;
                   9942:                fcb->record_size = 128;
                   9943:                fcb->file_size = GetFileSize(hFile, NULL);
                   9944:                fcb->handle = hFile;
                   9945:                fcb->cur_record = 0;
                   9946:        }
                   9947: }
                   9948: 
                   9949: inline void msdos_int_21h_10h()
                   9950: {
                   9951:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9952:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9953:        
                   9954:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9955: }
                   9956: 
1.1       root     9957: inline void msdos_int_21h_11h()
                   9958: {
1.1.1.3   root     9959:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9960:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9961:        
                   9962:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9963:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9964:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9965:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9966:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     9967:        WIN32_FIND_DATAA fd;
1.1       root     9968:        
1.1.1.13  root     9969:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9970:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9971:                FindClose(dtainfo->find_handle);
                   9972:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9973:        }
                   9974:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9975:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9976:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9977:        
1.1.1.14  root     9978:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9979:                dtainfo->allowable_mask &= ~8;
1.1       root     9980:        }
1.1.1.60  root     9981:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     9982:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9983:                      !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     9984:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     9985:                                FindClose(dtainfo->find_handle);
                   9986:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9987:                                break;
                   9988:                        }
                   9989:                }
                   9990:        }
1.1.1.13  root     9991:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9992:                if(ext_fcb->flag == 0xff) {
                   9993:                        ext_find->flag = 0xff;
                   9994:                        memset(ext_find->reserved, 0, 5);
                   9995:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9996:                }
                   9997:                find->drive = _getdrive();
1.1.1.13  root     9998:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9999:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10000:                find->nt_res = 0;
                   10001:                msdos_find_file_conv_local_time(&fd);
                   10002:                find->create_time_ms = 0;
                   10003:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10004:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10005:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10006:                find->cluster_hi = find->cluster_lo = 0;
                   10007:                find->file_size = fd.nFileSizeLow;
                   10008:                REG8(AL) = 0x00;
1.1.1.14  root     10009:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10010:                if(ext_fcb->flag == 0xff) {
                   10011:                        ext_find->flag = 0xff;
                   10012:                        memset(ext_find->reserved, 0, 5);
                   10013:                        ext_find->attribute = 8;
                   10014:                }
                   10015:                find->drive = _getdrive();
                   10016:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   10017:                find->attribute = 8;
                   10018:                find->nt_res = 0;
                   10019:                msdos_find_file_conv_local_time(&fd);
                   10020:                find->create_time_ms = 0;
                   10021:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10022:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10023:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10024:                find->cluster_hi = find->cluster_lo = 0;
                   10025:                find->file_size = 0;
1.1.1.14  root     10026:                dtainfo->allowable_mask &= ~8;
1.1       root     10027:                REG8(AL) = 0x00;
                   10028:        } else {
                   10029:                REG8(AL) = 0xff;
                   10030:        }
                   10031: }
                   10032: 
                   10033: inline void msdos_int_21h_12h()
                   10034: {
1.1.1.3   root     10035:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     10036: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     10037:        
                   10038:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     10039:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10040:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   10041:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.60  root     10042:        WIN32_FIND_DATAA fd;
1.1       root     10043:        
1.1.1.13  root     10044:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   10045:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     10046:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     10047:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     10048:                              !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     10049:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     10050:                                        FindClose(dtainfo->find_handle);
                   10051:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10052:                                        break;
                   10053:                                }
                   10054:                        }
                   10055:                } else {
1.1.1.13  root     10056:                        FindClose(dtainfo->find_handle);
                   10057:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10058:                }
                   10059:        }
1.1.1.13  root     10060:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10061:                if(ext_fcb->flag == 0xff) {
                   10062:                        ext_find->flag = 0xff;
                   10063:                        memset(ext_find->reserved, 0, 5);
                   10064:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10065:                }
                   10066:                find->drive = _getdrive();
1.1.1.13  root     10067:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     10068:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10069:                find->nt_res = 0;
                   10070:                msdos_find_file_conv_local_time(&fd);
                   10071:                find->create_time_ms = 0;
                   10072:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10073:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10074:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10075:                find->cluster_hi = find->cluster_lo = 0;
                   10076:                find->file_size = fd.nFileSizeLow;
                   10077:                REG8(AL) = 0x00;
1.1.1.14  root     10078:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10079:                if(ext_fcb->flag == 0xff) {
                   10080:                        ext_find->flag = 0xff;
                   10081:                        memset(ext_find->reserved, 0, 5);
                   10082:                        ext_find->attribute = 8;
                   10083:                }
                   10084:                find->drive = _getdrive();
                   10085:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   10086:                find->attribute = 8;
                   10087:                find->nt_res = 0;
                   10088:                msdos_find_file_conv_local_time(&fd);
                   10089:                find->create_time_ms = 0;
                   10090:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   10091:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   10092:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   10093:                find->cluster_hi = find->cluster_lo = 0;
                   10094:                find->file_size = 0;
1.1.1.14  root     10095:                dtainfo->allowable_mask &= ~8;
1.1       root     10096:                REG8(AL) = 0x00;
                   10097:        } else {
                   10098:                REG8(AL) = 0xff;
                   10099:        }
                   10100: }
                   10101: 
                   10102: inline void msdos_int_21h_13h()
                   10103: {
1.1.1.3   root     10104:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     10105:                REG8(AL) = 0xff;
                   10106:        } else {
                   10107:                REG8(AL) = 0x00;
                   10108:        }
                   10109: }
                   10110: 
1.1.1.16  root     10111: inline void msdos_int_21h_14h()
                   10112: {
                   10113:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10114:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10115:        process_t *process = msdos_process_info_get(current_psp);
                   10116:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10117:        DWORD num = 0;
                   10118:        
                   10119:        memset(mem + dta_laddr, 0, fcb->record_size);
                   10120:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10121:                REG8(AL) = 1;
                   10122:        } else {
                   10123:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   10124:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   10125:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   10126:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10127:        }
                   10128: }
                   10129: 
                   10130: inline void msdos_int_21h_15h()
1.1.1.14  root     10131: {
                   10132:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10133:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     10134:        process_t *process = msdos_process_info_get(current_psp);
                   10135:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10136:        DWORD num = 0;
1.1.1.14  root     10137:        
1.1.1.16  root     10138:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10139:                REG8(AL) = 1;
                   10140:        } else {
                   10141:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10142:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   10143:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   10144:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   10145:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   10146:        }
                   10147: }
                   10148: 
                   10149: inline void msdos_int_21h_16h()
                   10150: {
                   10151:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10152:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10153:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10154:        HANDLE hFile = CreateFileA(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     10155:        
1.1.1.14  root     10156:        if(hFile == INVALID_HANDLE_VALUE) {
                   10157:                REG8(AL) = 0xff;
                   10158:        } else {
                   10159:                REG8(AL) = 0;
                   10160:                fcb->current_block = 0;
                   10161:                fcb->record_size = 128;
                   10162:                fcb->file_size = 0;
                   10163:                fcb->handle = hFile;
                   10164:                fcb->cur_record = 0;
                   10165:        }
                   10166: }
                   10167: 
1.1.1.16  root     10168: inline void msdos_int_21h_17h()
                   10169: {
                   10170:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10171:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     10172: //     const char *path_src = msdos_fcb_path(fcb_src);
                   10173:        char path_src[MAX_PATH];
                   10174:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   10175:        
1.1.1.16  root     10176:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   10177:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     10178: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   10179:        char path_dst[MAX_PATH];
                   10180:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     10181:        
                   10182:        if(rename(path_src, path_dst)) {
                   10183:                REG8(AL) = 0xff;
                   10184:        } else {
                   10185:                REG8(AL) = 0;
                   10186:        }
                   10187: }
                   10188: 
1.1       root     10189: inline void msdos_int_21h_18h()
                   10190: {
                   10191:        REG8(AL) = 0x00;
                   10192: }
                   10193: 
                   10194: inline void msdos_int_21h_19h()
                   10195: {
                   10196:        REG8(AL) = _getdrive() - 1;
                   10197: }
                   10198: 
                   10199: inline void msdos_int_21h_1ah()
                   10200: {
                   10201:        process_t *process = msdos_process_info_get(current_psp);
                   10202:        
                   10203:        process->dta.w.l = REG16(DX);
1.1.1.3   root     10204:        process->dta.w.h = SREG(DS);
1.1.1.23  root     10205:        msdos_sda_update(current_psp);
1.1       root     10206: }
                   10207: 
                   10208: inline void msdos_int_21h_1bh()
                   10209: {
                   10210:        int drive_num = _getdrive() - 1;
                   10211:        UINT16 seg, ofs;
                   10212:        
                   10213:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10214:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10215:                REG8(AL) = dpb->highest_sector_num + 1;
                   10216:                REG16(CX) = dpb->bytes_per_sector;
                   10217:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10218:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10219:        } else {
                   10220:                REG8(AL) = 0xff;
1.1.1.3   root     10221:                m_CF = 1;
1.1       root     10222:        }
                   10223: 
                   10224: }
                   10225: 
                   10226: inline void msdos_int_21h_1ch()
                   10227: {
1.1.1.41  root     10228:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     10229:        UINT16 seg, ofs;
                   10230:        
                   10231:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10232:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10233:                REG8(AL) = dpb->highest_sector_num + 1;
                   10234:                REG16(CX) = dpb->bytes_per_sector;
                   10235:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10236:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10237:        } else {
                   10238:                REG8(AL) = 0xff;
1.1.1.3   root     10239:                m_CF = 1;
1.1       root     10240:        }
                   10241: 
                   10242: }
                   10243: 
                   10244: inline void msdos_int_21h_1dh()
                   10245: {
                   10246:        REG8(AL) = 0;
                   10247: }
                   10248: 
                   10249: inline void msdos_int_21h_1eh()
                   10250: {
                   10251:        REG8(AL) = 0;
                   10252: }
                   10253: 
                   10254: inline void msdos_int_21h_1fh()
                   10255: {
                   10256:        int drive_num = _getdrive() - 1;
                   10257:        UINT16 seg, ofs;
                   10258:        
                   10259:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10260:                REG8(AL) = 0;
1.1.1.3   root     10261:                SREG(DS) = seg;
                   10262:                i386_load_segment_descriptor(DS);
1.1       root     10263:                REG16(BX) = ofs;
                   10264:        } else {
                   10265:                REG8(AL) = 0xff;
1.1.1.3   root     10266:                m_CF = 1;
1.1       root     10267:        }
                   10268: }
                   10269: 
                   10270: inline void msdos_int_21h_20h()
                   10271: {
                   10272:        REG8(AL) = 0;
                   10273: }
                   10274: 
1.1.1.14  root     10275: inline void msdos_int_21h_21h()
                   10276: {
                   10277:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10278:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10279:        
                   10280:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10281:                REG8(AL) = 1;
                   10282:        } else {
                   10283:                process_t *process = msdos_process_info_get(current_psp);
                   10284:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10285:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     10286:                DWORD num = 0;
1.1.1.14  root     10287:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10288:                        REG8(AL) = 1;
                   10289:                } else {
                   10290:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10291:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     10292:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     10293:                }
                   10294:        }
                   10295: }
                   10296: 
                   10297: inline void msdos_int_21h_22h()
                   10298: {
                   10299:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10300:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10301:        
                   10302:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10303:                REG8(AL) = 0xff;
                   10304:        } else {
                   10305:                process_t *process = msdos_process_info_get(current_psp);
                   10306:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     10307:                DWORD num = 0;
1.1.1.14  root     10308:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   10309:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10310:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10311:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     10312:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     10313:        }
                   10314: }
                   10315: 
1.1.1.16  root     10316: inline void msdos_int_21h_23h()
                   10317: {
                   10318:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10319:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10320:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10321:        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     10322:        
                   10323:        if(hFile == INVALID_HANDLE_VALUE) {
                   10324:                REG8(AL) = 0xff;
                   10325:        } else {
                   10326:                UINT32 size = GetFileSize(hFile, NULL);
                   10327:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   10328:                REG8(AL) = 0;
                   10329:        }
                   10330: }
                   10331: 
                   10332: inline void msdos_int_21h_24h()
                   10333: {
                   10334:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10335:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10336:        
                   10337:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   10338: }
                   10339: 
1.1       root     10340: inline void msdos_int_21h_25h()
                   10341: {
                   10342:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     10343:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     10344: }
                   10345: 
                   10346: inline void msdos_int_21h_26h()
                   10347: {
                   10348:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   10349:        
                   10350:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     10351:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     10352:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10353:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10354:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10355:        psp->parent_psp = 0;
                   10356: }
                   10357: 
1.1.1.16  root     10358: inline void msdos_int_21h_27h()
                   10359: {
                   10360:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10361:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10362:        
                   10363:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10364:                REG8(AL) = 1;
                   10365:        } else {
                   10366:                process_t *process = msdos_process_info_get(current_psp);
                   10367:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10368:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   10369:                DWORD num = 0;
                   10370:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   10371:                        REG8(AL) = 1;
                   10372:                } else {
                   10373:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10374:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10375:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10376:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10377:                }
                   10378:        }
                   10379: }
                   10380: 
                   10381: inline void msdos_int_21h_28h()
                   10382: {
                   10383:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10384:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   10385:        
                   10386:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10387:                REG8(AL) = 0xff;
                   10388:        } else {
                   10389:                process_t *process = msdos_process_info_get(current_psp);
                   10390:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10391:                DWORD num = 0;
                   10392:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   10393:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10394:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   10395:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   10396:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   10397:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10398:        }
                   10399: }
                   10400: 
1.1       root     10401: inline void msdos_int_21h_29h()
                   10402: {
1.1.1.20  root     10403:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   10404:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     10405:        UINT8 drv = 0;
                   10406:        char sep_chars[] = ":.;,=+";
                   10407:        char end_chars[] = "\\<>|/\"[]";
                   10408:        char spc_chars[] = " \t";
                   10409:        
1.1.1.20  root     10410:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   10411:        buffer[1023] = 0;
                   10412:        memset(name, 0x20, sizeof(name));
                   10413:        memset(ext, 0x20, sizeof(ext));
                   10414:        
1.1       root     10415:        if(REG8(AL) & 1) {
1.1.1.20  root     10416:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   10417:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     10418:                        ofs++;
                   10419:                }
                   10420:        }
1.1.1.20  root     10421:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     10422:        
1.1.1.24  root     10423:        if(buffer[ofs + 1] == ':') {
                   10424:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   10425:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     10426:                        ofs += 2;
1.1.1.24  root     10427:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10428:                                ofs++;
                   10429:                        }
                   10430:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   10431:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     10432:                        ofs += 2;
1.1.1.24  root     10433:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10434:                                ofs++;
                   10435:                        }
1.1       root     10436:                }
                   10437:        }
1.1.1.20  root     10438:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10439:                UINT8 c = buffer[ofs];
                   10440:                if(is_kanji) {
                   10441:                        is_kanji = 0;
                   10442:                } else if(msdos_lead_byte_check(c)) {
                   10443:                        is_kanji = 1;
                   10444:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10445:                        break;
                   10446:                } else if(c >= 'a' && c <= 'z') {
                   10447:                        c -= 0x20;
                   10448:                }
                   10449:                ofs++;
                   10450:                name[i] = c;
                   10451:        }
1.1.1.20  root     10452:        if(buffer[ofs] == '.') {
1.1       root     10453:                ofs++;
1.1.1.20  root     10454:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10455:                        UINT8 c = buffer[ofs];
                   10456:                        if(is_kanji) {
                   10457:                                is_kanji = 0;
                   10458:                        } else if(msdos_lead_byte_check(c)) {
                   10459:                                is_kanji = 1;
                   10460:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10461:                                break;
                   10462:                        } else if(c >= 'a' && c <= 'z') {
                   10463:                                c -= 0x20;
                   10464:                        }
                   10465:                        ofs++;
                   10466:                        ext[i] = c;
                   10467:                }
                   10468:        }
1.1.1.20  root     10469:        int si = REG16(SI) + ofs;
1.1.1.3   root     10470:        int ds = SREG(DS);
1.1       root     10471:        while(si > 0xffff) {
                   10472:                si -= 0x10;
                   10473:                ds++;
                   10474:        }
                   10475:        REG16(SI) = si;
1.1.1.3   root     10476:        SREG(DS) = ds;
                   10477:        i386_load_segment_descriptor(DS);
1.1       root     10478:        
1.1.1.3   root     10479:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     10480:        if(!(REG8(AL) & 2) || drv != 0) {
                   10481:                fcb[0] = drv;
                   10482:        }
                   10483:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   10484:                memcpy(fcb + 1, name, 8);
                   10485:        }
                   10486:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   10487:                memcpy(fcb + 9, ext, 3);
                   10488:        }
                   10489:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     10490:                if(fcb[i] == '*') {
                   10491:                        found_star = 1;
                   10492:                }
                   10493:                if(found_star) {
                   10494:                        fcb[i] = '?';
                   10495:                }
                   10496:        }
1.1.1.20  root     10497:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     10498:                if(fcb[i] == '*') {
                   10499:                        found_star = 1;
                   10500:                }
                   10501:                if(found_star) {
                   10502:                        fcb[i] = '?';
                   10503:                }
                   10504:        }
                   10505:        
1.1.1.44  root     10506:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     10507:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   10508:                        REG8(AL) = 0x01;
1.1.1.20  root     10509:                } else {
                   10510:                        REG8(AL) = 0x00;
1.1       root     10511:                }
                   10512:        } else {
                   10513:                REG8(AL) = 0xff;
                   10514:        }
                   10515: }
                   10516: 
                   10517: inline void msdos_int_21h_2ah()
                   10518: {
                   10519:        SYSTEMTIME sTime;
                   10520:        
                   10521:        GetLocalTime(&sTime);
                   10522:        REG16(CX) = sTime.wYear;
                   10523:        REG8(DH) = (UINT8)sTime.wMonth;
                   10524:        REG8(DL) = (UINT8)sTime.wDay;
                   10525:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10526: }
                   10527: 
                   10528: inline void msdos_int_21h_2bh()
                   10529: {
1.1.1.14  root     10530:        REG8(AL) = 0xff;
1.1       root     10531: }
                   10532: 
                   10533: inline void msdos_int_21h_2ch()
                   10534: {
                   10535:        SYSTEMTIME sTime;
                   10536:        
                   10537:        GetLocalTime(&sTime);
                   10538:        REG8(CH) = (UINT8)sTime.wHour;
                   10539:        REG8(CL) = (UINT8)sTime.wMinute;
                   10540:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10541:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10542: }
                   10543: 
                   10544: inline void msdos_int_21h_2dh()
                   10545: {
                   10546:        REG8(AL) = 0x00;
                   10547: }
                   10548: 
                   10549: inline void msdos_int_21h_2eh()
                   10550: {
                   10551:        process_t *process = msdos_process_info_get(current_psp);
                   10552:        
                   10553:        process->verify = REG8(AL);
                   10554: }
                   10555: 
                   10556: inline void msdos_int_21h_2fh()
                   10557: {
                   10558:        process_t *process = msdos_process_info_get(current_psp);
                   10559:        
                   10560:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10561:        SREG(ES) = process->dta.w.h;
                   10562:        i386_load_segment_descriptor(ES);
1.1       root     10563: }
                   10564: 
                   10565: inline void msdos_int_21h_30h()
                   10566: {
                   10567:        // Version Flag / OEM
1.1.1.27  root     10568:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10569: #ifdef SUPPORT_HMA
                   10570:                REG16(BX) = 0x0000;
                   10571: #else
                   10572:                REG16(BX) = 0x1000; // DOS is in HMA
                   10573: #endif
1.1       root     10574:        } else {
1.1.1.27  root     10575:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10576:                // but this is not correct on Windows 98 SE
                   10577: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10578:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10579:        }
1.1.1.27  root     10580:        REG16(CX) = 0x0000;
1.1.1.30  root     10581:        REG8(AL) = dos_major_version;   // 7
                   10582:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10583: }
                   10584: 
                   10585: inline void msdos_int_21h_31h()
                   10586: {
1.1.1.29  root     10587:        try {
                   10588:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10589:        } catch(...) {
                   10590:                // recover the broken mcb
                   10591:                int mcb_seg = current_psp - 1;
                   10592:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10593:                
1.1.1.29  root     10594:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10595:                        mcb->mz = 'M';
                   10596:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10597:                        
1.1.1.29  root     10598:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10599:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10600:                        } else {
1.1.1.39  root     10601:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10602:                        }
                   10603:                } else {
                   10604:                        mcb->mz = 'Z';
1.1.1.30  root     10605:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10606:                }
                   10607:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10608:        }
1.1       root     10609:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10610: }
                   10611: 
                   10612: inline void msdos_int_21h_32h()
                   10613: {
                   10614:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10615:        UINT16 seg, ofs;
                   10616:        
                   10617:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10618:                REG8(AL) = 0;
1.1.1.3   root     10619:                SREG(DS) = seg;
                   10620:                i386_load_segment_descriptor(DS);
1.1       root     10621:                REG16(BX) = ofs;
                   10622:        } else {
                   10623:                REG8(AL) = 0xff;
1.1.1.3   root     10624:                m_CF = 1;
1.1       root     10625:        }
                   10626: }
                   10627: 
                   10628: inline void msdos_int_21h_33h()
                   10629: {
                   10630:        char path[MAX_PATH];
1.1.1.48  root     10631:        char drive = 3; // C:
1.1       root     10632:        
                   10633:        switch(REG8(AL)) {
                   10634:        case 0x00:
1.1.1.33  root     10635:                REG8(DL) = ctrl_break_checking;
1.1       root     10636:                break;
                   10637:        case 0x01:
1.1.1.33  root     10638:                ctrl_break_checking = REG8(DL);
                   10639:                break;
                   10640:        case 0x02:
                   10641:                {
                   10642:                        UINT8 old = ctrl_break_checking;
                   10643:                        ctrl_break_checking = REG8(DL);
                   10644:                        REG8(DL) = old;
                   10645:                }
                   10646:                break;
                   10647:        case 0x03:
                   10648:        case 0x04:
                   10649:                // DOS 4.0+ - Unused
1.1       root     10650:                break;
                   10651:        case 0x05:
1.1.1.60  root     10652:                if(GetSystemDirectoryA(path, MAX_PATH) != 0) {
1.1.1.48  root     10653:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10654:                                drive = path[0] - 'a' + 1;
                   10655:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10656:                                drive = path[0] - 'A' + 1;
                   10657:                        }
1.1       root     10658:                }
1.1.1.48  root     10659:                REG8(DL) = (UINT8)drive;
1.1       root     10660:                break;
                   10661:        case 0x06:
1.1.1.2   root     10662:                // MS-DOS version (7.10)
1.1       root     10663:                REG8(BL) = 7;
1.1.1.2   root     10664:                REG8(BH) = 10;
1.1       root     10665:                REG8(DL) = 0;
1.1.1.29  root     10666: #ifdef SUPPORT_HMA
                   10667:                REG8(DH) = 0x00;
                   10668: #else
                   10669:                REG8(DH) = 0x10; // DOS is in HMA
                   10670: #endif
1.1       root     10671:                break;
1.1.1.6   root     10672:        case 0x07:
                   10673:                if(REG8(DL) == 0) {
                   10674:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10675:                } else if(REG8(DL) == 1) {
                   10676:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10677:                }
                   10678:                break;
1.1       root     10679:        default:
1.1.1.22  root     10680:                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     10681: //             REG16(AX) = 0x01;
                   10682: //             m_CF = 1;
                   10683:                REG8(AL) = 0xff;
1.1       root     10684:                break;
                   10685:        }
                   10686: }
                   10687: 
1.1.1.23  root     10688: inline void msdos_int_21h_34h()
                   10689: {
                   10690:        SREG(ES) = SDA_TOP >> 4;
                   10691:        i386_load_segment_descriptor(ES);
                   10692:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   10693: }
                   10694: 
1.1       root     10695: inline void msdos_int_21h_35h()
                   10696: {
                   10697:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10698:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10699:        i386_load_segment_descriptor(ES);
1.1       root     10700: }
                   10701: 
                   10702: inline void msdos_int_21h_36h()
                   10703: {
                   10704:        struct _diskfree_t df = {0};
                   10705:        
                   10706:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10707:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10708:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10709:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10710:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10711:        } else {
                   10712:                REG16(AX) = 0xffff;
                   10713:        }
                   10714: }
                   10715: 
                   10716: inline void msdos_int_21h_37h()
                   10717: {
1.1.1.22  root     10718:        static UINT8 dev_flag = 0xff;
1.1       root     10719:        
                   10720:        switch(REG8(AL)) {
                   10721:        case 0x00:
1.1.1.22  root     10722:                {
                   10723:                        process_t *process = msdos_process_info_get(current_psp);
                   10724:                        REG8(AL) = 0x00;
                   10725:                        REG8(DL) = process->switchar;
                   10726:                }
1.1       root     10727:                break;
                   10728:        case 0x01:
1.1.1.22  root     10729:                {
                   10730:                        process_t *process = msdos_process_info_get(current_psp);
                   10731:                        REG8(AL) = 0x00;
                   10732:                        process->switchar = REG8(DL);
1.1.1.23  root     10733:                        msdos_sda_update(current_psp);
1.1.1.22  root     10734:                }
                   10735:                break;
                   10736:        case 0x02:
                   10737:                REG8(DL) = dev_flag;
                   10738:                break;
                   10739:        case 0x03:
                   10740:                dev_flag = REG8(DL);
                   10741:                break;
                   10742:        case 0xd0:
                   10743:        case 0xd1:
                   10744:        case 0xd2:
                   10745:        case 0xd3:
                   10746:        case 0xd4:
                   10747:        case 0xd5:
                   10748:        case 0xd6:
                   10749:        case 0xd7:
                   10750:        case 0xdc:
                   10751:        case 0xdd:
                   10752:        case 0xde:
                   10753:        case 0xdf:
1.1.1.48  root     10754:                // DIET v1.43e
                   10755: //             REG16(AX) = 1;
                   10756:                REG8(AL) = 0xff;
1.1       root     10757:                break;
                   10758:        default:
1.1.1.22  root     10759:                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     10760: //             REG16(AX) = 1;
                   10761:                REG8(AL) = 0xff;
1.1       root     10762:                break;
                   10763:        }
                   10764: }
                   10765: 
1.1.1.52  root     10766: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10767: {
                   10768:        char LCdata[80];
                   10769:        
1.1.1.19  root     10770:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.60  root     10771:        GetLocaleInfoA(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10772:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.60  root     10773:        GetLocaleInfoA(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10774:        ci->currency_format = *LCdata - '0';
1.1.1.60  root     10775:        GetLocaleInfoA(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10776:        ci->date_format = *LCdata - '0';
1.1.1.60  root     10777:        GetLocaleInfoA(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10778:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.60  root     10779:        GetLocaleInfoA(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10780:        *ci->date_sep = *LCdata;
1.1.1.60  root     10781:        GetLocaleInfoA(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10782:        *ci->dec_sep = *LCdata;
1.1.1.60  root     10783:        GetLocaleInfoA(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10784:        *ci->list_sep = *LCdata;
1.1.1.60  root     10785:        GetLocaleInfoA(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10786:        *ci->thou_sep = *LCdata;
1.1.1.60  root     10787:        GetLocaleInfoA(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10788:        *ci->time_sep = *LCdata;
1.1.1.60  root     10789:        GetLocaleInfoA(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10790:        if(strchr(LCdata, 'H') != NULL) {
                   10791:                ci->time_format = 1;
                   10792:        }
1.1.1.49  root     10793:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10794:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.60  root     10795:        GetLocaleInfoA(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10796:        return atoi(LCdata);
                   10797: }
                   10798: 
1.1.1.42  root     10799: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10800: {
                   10801:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10802: }
                   10803: 
1.1.1.43  root     10804: void set_country_info(country_info_t *ci, int size)
                   10805: {
                   10806:        char LCdata[80];
                   10807:        
                   10808:        if(size >= 0x00 + 2) {
                   10809:                memset(LCdata, 0, sizeof(LCdata));
                   10810:                *LCdata = '0' + ci->date_format;
1.1.1.60  root     10811:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
1.1.1.43  root     10812:        }
                   10813:        if(size >= 0x02 + 5) {
                   10814:                memset(LCdata, 0, sizeof(LCdata));
                   10815:                memcpy(LCdata, &ci->currency_symbol, 4);
1.1.1.60  root     10816:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
1.1.1.43  root     10817:        }
                   10818:        if(size >= 0x07 + 2) {
                   10819:                memset(LCdata, 0, sizeof(LCdata));
                   10820:                *LCdata = *ci->thou_sep;
1.1.1.60  root     10821:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
1.1.1.43  root     10822:        }
                   10823:        if(size >= 0x09 + 2) {
                   10824:                memset(LCdata, 0, sizeof(LCdata));
                   10825:                *LCdata = *ci->dec_sep;
1.1.1.60  root     10826:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
1.1.1.43  root     10827:        }
                   10828:        if(size >= 0x0b + 2) {
                   10829:                memset(LCdata, 0, sizeof(LCdata));
                   10830:                *LCdata = *ci->date_sep;
1.1.1.60  root     10831:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
1.1.1.43  root     10832:        }
                   10833:        if(size >= 0x0d + 2) {
                   10834:                memset(LCdata, 0, sizeof(LCdata));
                   10835:                *LCdata = *ci->time_sep;
1.1.1.60  root     10836:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
1.1.1.43  root     10837:        }
                   10838:        if(size >= 0x0f + 1) {
                   10839:                memset(LCdata, 0, sizeof(LCdata));
                   10840:                *LCdata = '0' + ci->currency_format;
1.1.1.60  root     10841:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
1.1.1.43  root     10842:        }
                   10843:        if(size >= 0x10 + 1) {
                   10844:                sprintf(LCdata, "%d", ci->currency_dec_digits);
1.1.1.60  root     10845:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
1.1.1.43  root     10846:        }
                   10847:        if(size >= 0x11 + 1) {
                   10848:                // FIXME: is time format always H/h:mm:ss ???
                   10849:                if(ci->time_format & 1) {
                   10850:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10851:                } else {
                   10852:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10853:                }
1.1.1.60  root     10854:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
1.1.1.43  root     10855:        }
                   10856:        if(size >= 0x12 + 4) {
                   10857:                // 12h  DWORD   address of case map routine
                   10858:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10859:        }
                   10860:        if(size >= 0x16 + 2) {
                   10861:                memset(LCdata, 0, sizeof(LCdata));
                   10862:                *LCdata = *ci->list_sep;
1.1.1.60  root     10863:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
1.1.1.43  root     10864:        }
                   10865: }
                   10866: 
1.1.1.42  root     10867: #ifndef SUBLANG_SWAHILI
                   10868:        #define SUBLANG_SWAHILI 0x01
                   10869: #endif
                   10870: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10871:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10872: #endif
                   10873: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10874:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10875: #endif
                   10876: #ifndef LANG_BANGLA
                   10877:        #define LANG_BANGLA 0x45
                   10878: #endif
                   10879: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10880:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10881: #endif
                   10882: 
                   10883: static const struct {
                   10884:        int code;
                   10885:        USHORT usPrimaryLanguage;
                   10886:        USHORT usSubLanguage;
                   10887: } country_table[] = {
                   10888:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10889:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10890:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10891:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10892:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10893:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10894: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10895: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10896: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10897: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10898: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10899:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10900:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10901: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10902:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10903: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10904:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10905:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10906:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10907:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10908:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10909:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10910:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10911: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10912: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10913:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10914:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10915:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10916:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10917:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10918: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10919: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10920: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10921:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10922: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10923: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10924: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10925: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10926:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10927:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10928:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10929: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10930:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10931:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10932:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10933:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10934:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10935:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10936:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10937:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10938:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10939:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10940:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10941:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10942:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10943: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10944:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10945:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10946:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10947:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10948:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10949:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10950:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10951:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10952:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10953:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10954: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10955:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10956: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10957:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10958:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10959:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10960:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10961:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10962:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10963:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10964:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10965: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10966:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10967: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10968: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10969:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10970: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10971:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10972:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10973:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10974:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10975:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10976:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10977:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10978: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10979: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10980:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10981: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10982:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10983:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10984:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10985:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10986: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10987: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10988: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10989:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10990:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10991:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10992:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10993:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10994:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10995:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10996:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10997:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10998:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10999:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   11000:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   11001:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   11002:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   11003:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   11004:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   11005:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   11006:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   11007:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   11008:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   11009:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     11010: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     11011:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   11012: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   11013:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   11014:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   11015:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   11016:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   11017:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   11018:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   11019:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   11020:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   11021:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   11022:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   11023:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   11024:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   11025:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   11026:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   11027:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   11028:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   11029:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   11030:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   11031:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   11032:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   11033:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   11034:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   11035:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   11036:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   11037:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   11038: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   11039:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   11040:        {-1, 0, 0},
                   11041: };
                   11042: 
1.1.1.14  root     11043: inline void msdos_int_21h_38h()
                   11044: {
                   11045:        switch(REG8(AL)) {
                   11046:        case 0x00:
1.1.1.19  root     11047:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     11048:                break;
                   11049:        default:
1.1.1.42  root     11050:                for(int i = 0;; i++) {
                   11051:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   11052:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   11053:                                break;
                   11054:                        } else if(country_table[i].code == -1) {
                   11055: //                             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));
                   11056: //                             REG16(AX) = 2;
                   11057: //                             m_CF = 1;
1.1.1.48  root     11058:                                // get current coutry info
1.1.1.42  root     11059:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   11060:                                break;
                   11061:                        }
                   11062:                }
1.1.1.14  root     11063:                break;
                   11064:        }
                   11065: }
                   11066: 
1.1       root     11067: inline void msdos_int_21h_39h(int lfn)
                   11068: {
1.1.1.3   root     11069:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11070:                REG16(AX) = errno;
1.1.1.3   root     11071:                m_CF = 1;
1.1       root     11072:        }
                   11073: }
                   11074: 
                   11075: inline void msdos_int_21h_3ah(int lfn)
                   11076: {
1.1.1.3   root     11077:        if(_rmdir(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_3bh(int lfn)
                   11084: {
1.1.1.45  root     11085:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     11086:        
                   11087:        if(_chdir(path)) {
1.1.1.17  root     11088:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     11089:                m_CF = 1;
1.1.1.44  root     11090:        } else {
                   11091:                int drv = _getdrive() - 1;
                   11092:                if(path[1] == ':') {
                   11093:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   11094:                                drv = path[0] - 'A';
                   11095:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   11096:                                drv = path[0] - 'a';
                   11097:                        }
                   11098:                }
                   11099:                msdos_cds_update(drv, path);
1.1       root     11100:        }
                   11101: }
                   11102: 
                   11103: inline void msdos_int_21h_3ch()
                   11104: {
1.1.1.45  root     11105:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     11106:        int attr = GetFileAttributesA(path);
1.1.1.37  root     11107:        int fd = -1;
                   11108:        int sio_port = 0;
                   11109:        int lpt_port = 0;
1.1       root     11110:        
1.1.1.45  root     11111:        if(msdos_is_device_path(path)) {
                   11112:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     11113:        } else {
                   11114:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11115:        }
                   11116:        if(fd != -1) {
                   11117:                if(attr == -1) {
                   11118:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   11119:                }
1.1.1.60  root     11120:                SetFileAttributesA(path, attr);
1.1       root     11121:                REG16(AX) = fd;
1.1.1.45  root     11122:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11123:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11124:        } else {
                   11125:                REG16(AX) = errno;
1.1.1.3   root     11126:                m_CF = 1;
1.1       root     11127:        }
                   11128: }
                   11129: 
                   11130: inline void msdos_int_21h_3dh()
                   11131: {
1.1.1.45  root     11132:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11133:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     11134:        int fd = -1;
                   11135:        int sio_port = 0;
                   11136:        int lpt_port = 0;
1.1       root     11137:        
                   11138:        if(mode < 0x03) {
1.1.1.45  root     11139:                if(msdos_is_device_path(path)) {
                   11140:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     11141:                } else {
1.1.1.13  root     11142:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     11143:                }
1.1       root     11144:                if(fd != -1) {
                   11145:                        REG16(AX) = fd;
1.1.1.45  root     11146:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11147:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11148:                } else {
                   11149:                        REG16(AX) = errno;
1.1.1.3   root     11150:                        m_CF = 1;
1.1       root     11151:                }
                   11152:        } else {
                   11153:                REG16(AX) = 0x0c;
1.1.1.3   root     11154:                m_CF = 1;
1.1       root     11155:        }
                   11156: }
                   11157: 
                   11158: inline void msdos_int_21h_3eh()
                   11159: {
                   11160:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11161:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11162:        
1.1.1.20  root     11163:        if(fd < process->max_files && file_handler[fd].valid) {
                   11164:                _close(fd);
                   11165:                msdos_file_handler_close(fd);
                   11166:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     11167:        } else {
                   11168:                REG16(AX) = 0x06;
1.1.1.3   root     11169:                m_CF = 1;
1.1       root     11170:        }
                   11171: }
                   11172: 
1.1.1.35  root     11173: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   11174: {
                   11175:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   11176:        int max = REG16(CX);
                   11177:        int p = 0;
                   11178:        
                   11179:        while(max > p) {
                   11180:                int chr = msdos_getch();
                   11181:                
                   11182:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   11183:                        p = 0;
                   11184:                        buf[p++] = 0x0d;
                   11185:                        if(max > p) {
                   11186:                                buf[p++] = 0x0a;
                   11187:                        }
                   11188:                        msdos_putch(0x03);
                   11189:                        msdos_putch(0x0d);
                   11190:                        msdos_putch(0x0a);
                   11191:                        break;
                   11192:                } else if(ctrl_break_pressed) {
                   11193:                        // skip this byte
                   11194:                } else if(chr == 0x00) {
                   11195:                        // skip 2nd byte
                   11196:                        msdos_getch();
                   11197:                } else if(chr == 0x0d) {
                   11198:                        // carriage return
                   11199:                        buf[p++] = 0x0d;
                   11200:                        if(max > p) {
                   11201:                                buf[p++] = 0x0a;
                   11202:                        }
                   11203:                        msdos_putch('\n');
                   11204:                        break;
                   11205:                } else if(chr == 0x08) {
                   11206:                        // back space
                   11207:                        if(p > 0) {
                   11208:                                p--;
                   11209:                                if(msdos_ctrl_code_check(buf[p])) {
                   11210:                                        msdos_putch(0x08);
                   11211:                                        msdos_putch(0x08);
                   11212:                                        msdos_putch(0x20);
                   11213:                                        msdos_putch(0x20);
                   11214:                                        msdos_putch(0x08);
                   11215:                                        msdos_putch(0x08);
1.1.1.36  root     11216:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   11217:                                        p--;
                   11218:                                        msdos_putch(0x08);
                   11219:                                        msdos_putch(0x08);
                   11220:                                        msdos_putch(0x20);
                   11221:                                        msdos_putch(0x20);
                   11222:                                        msdos_putch(0x08);
                   11223:                                        msdos_putch(0x08);
1.1.1.35  root     11224:                                } else {
                   11225:                                        msdos_putch(0x08);
                   11226:                                        msdos_putch(0x20);
                   11227:                                        msdos_putch(0x08);
                   11228:                                }
                   11229:                        }
                   11230:                } else if(chr == 0x1b) {
                   11231:                        // escape
                   11232:                        while(p > 0) {
                   11233:                                p--;
                   11234:                                if(msdos_ctrl_code_check(buf[p])) {
                   11235:                                        msdos_putch(0x08);
                   11236:                                        msdos_putch(0x08);
                   11237:                                        msdos_putch(0x20);
                   11238:                                        msdos_putch(0x20);
                   11239:                                        msdos_putch(0x08);
                   11240:                                        msdos_putch(0x08);
                   11241:                                } else {
                   11242:                                        msdos_putch(0x08);
                   11243:                                        msdos_putch(0x20);
                   11244:                                        msdos_putch(0x08);
                   11245:                                }
                   11246:                        }
                   11247:                } else {
                   11248:                        buf[p++] = chr;
                   11249:                        msdos_putch(chr);
                   11250:                }
                   11251:        }
                   11252:        REG16(AX) = p;
                   11253:        
                   11254: #ifdef USE_SERVICE_THREAD
                   11255:        service_exit = true;
                   11256: #endif
                   11257:        return(0);
                   11258: }
                   11259: 
1.1       root     11260: inline void msdos_int_21h_3fh()
                   11261: {
                   11262:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11263:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11264:        
1.1.1.20  root     11265:        if(fd < process->max_files && file_handler[fd].valid) {
                   11266:                if(file_mode[file_handler[fd].mode].in) {
                   11267:                        if(file_handler[fd].atty) {
1.1       root     11268:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     11269:                                if(REG16(CX) != 0) {
                   11270: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     11271:                                        if(!in_service && !in_service_29h &&
1.1.1.58  root     11272:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     11273:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   11274:                                                // msdos_putch() will be used in this service
                   11275:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   11276:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   11277:                                        } else {
                   11278: #endif
                   11279:                                                msdos_int_21h_3fh_thread(NULL);
                   11280:                                                REQUEST_HARDWRE_UPDATE();
                   11281: #ifdef USE_SERVICE_THREAD
                   11282:                                        }
1.1.1.35  root     11283: #endif
                   11284:                                } else {
                   11285:                                        REG16(AX) = 0;
1.1       root     11286:                                }
                   11287:                        } else {
1.1.1.37  root     11288:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11289:                        }
                   11290:                } else {
                   11291:                        REG16(AX) = 0x05;
1.1.1.3   root     11292:                        m_CF = 1;
1.1       root     11293:                }
                   11294:        } else {
                   11295:                REG16(AX) = 0x06;
1.1.1.3   root     11296:                m_CF = 1;
1.1       root     11297:        }
                   11298: }
                   11299: 
                   11300: inline void msdos_int_21h_40h()
                   11301: {
                   11302:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11303:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11304:        
1.1.1.20  root     11305:        if(fd < process->max_files && file_handler[fd].valid) {
                   11306:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     11307:                        if(REG16(CX)) {
1.1.1.20  root     11308:                                if(file_handler[fd].atty) {
1.1       root     11309:                                        // BX is stdout/stderr or is redirected to stdout
                   11310:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     11311:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     11312:                                        }
                   11313:                                        REG16(AX) = REG16(CX);
                   11314:                                } else {
1.1.1.20  root     11315:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11316:                                }
                   11317:                        } else {
1.1.1.20  root     11318:                                UINT32 pos = _tell(fd);
                   11319:                                _lseek(fd, 0, SEEK_END);
                   11320:                                UINT32 size = _tell(fd);
1.1.1.12  root     11321:                                if(pos < size) {
1.1.1.20  root     11322:                                        _lseek(fd, pos, SEEK_SET);
                   11323:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     11324:                                } else {
                   11325:                                        for(UINT32 i = size; i < pos; i++) {
                   11326:                                                UINT8 tmp = 0;
1.1.1.23  root     11327:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     11328:                                        }
1.1.1.20  root     11329:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     11330:                                }
1.1.1.23  root     11331:                                REG16(AX) = 0;
1.1       root     11332:                        }
                   11333:                } else {
                   11334:                        REG16(AX) = 0x05;
1.1.1.3   root     11335:                        m_CF = 1;
1.1       root     11336:                }
                   11337:        } else {
                   11338:                REG16(AX) = 0x06;
1.1.1.3   root     11339:                m_CF = 1;
1.1       root     11340:        }
                   11341: }
                   11342: 
                   11343: inline void msdos_int_21h_41h(int lfn)
                   11344: {
1.1.1.3   root     11345:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11346:                REG16(AX) = errno;
1.1.1.3   root     11347:                m_CF = 1;
1.1       root     11348:        }
                   11349: }
                   11350: 
                   11351: inline void msdos_int_21h_42h()
                   11352: {
                   11353:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11354:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11355:        
1.1.1.20  root     11356:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11357:                if(REG8(AL) < 0x03) {
1.1.1.35  root     11358:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     11359:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   11360:                        UINT32 pos = _tell(fd);
1.1       root     11361:                        REG16(AX) = pos & 0xffff;
                   11362:                        REG16(DX) = (pos >> 16);
                   11363:                } else {
                   11364:                        REG16(AX) = 0x01;
1.1.1.3   root     11365:                        m_CF = 1;
1.1       root     11366:                }
                   11367:        } else {
                   11368:                REG16(AX) = 0x06;
1.1.1.3   root     11369:                m_CF = 1;
1.1       root     11370:        }
                   11371: }
                   11372: 
                   11373: inline void msdos_int_21h_43h(int lfn)
                   11374: {
1.1.1.45  root     11375:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     11376:        int attr;
                   11377:        
1.1.1.14  root     11378:        if(!lfn && REG8(AL) > 2) {
                   11379:                REG16(AX) = 0x01;
                   11380:                m_CF = 1;
                   11381:                return;
                   11382:        }
                   11383:        switch(REG8(lfn ? BL : AL)) {
1.1       root     11384:        case 0x00:
1.1.1.60  root     11385:                if((attr = GetFileAttributesA(path)) != -1) {
1.1.1.14  root     11386:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   11387:                } else {
                   11388:                        REG16(AX) = (UINT16)GetLastError();
                   11389:                        m_CF = 1;
                   11390:                }
                   11391:                break;
                   11392:        case 0x01:
1.1.1.60  root     11393:                if(!SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)))) {
1.1.1.14  root     11394:                        REG16(AX) = (UINT16)GetLastError();
                   11395:                        m_CF = 1;
                   11396:                }
                   11397:                break;
                   11398:        case 0x02:
                   11399:                {
1.1.1.60  root     11400:                        DWORD compressed_size = GetCompressedFileSizeA(path, NULL), file_size = 0;
1.1.1.45  root     11401:                        if(compressed_size != INVALID_FILE_SIZE) {
                   11402:                                if(compressed_size != 0) {
1.1.1.60  root     11403:                                        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.45  root     11404:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   11405:                                                file_size = GetFileSize(hFile, NULL);
                   11406:                                                CloseHandle(hFile);
                   11407:                                        }
                   11408:                                        if(compressed_size == file_size) {
                   11409:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11410:                                                // this isn't correct if the file is in the NTFS MFT
1.1.1.60  root     11411:                                                if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1.1.45  root     11412:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   11413:                                                }
1.1.1.14  root     11414:                                        }
                   11415:                                }
1.1.1.45  root     11416:                                REG16(AX) = LOWORD(compressed_size);
                   11417:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     11418:                        } else {
                   11419:                                REG16(AX) = (UINT16)GetLastError();
                   11420:                                m_CF = 1;
1.1       root     11421:                        }
1.1.1.14  root     11422:                }
                   11423:                break;
                   11424:        case 0x03:
                   11425:        case 0x05:
                   11426:        case 0x07:
1.1.1.48  root     11427:                if(lfn) {
1.1.1.60  root     11428:                        HANDLE hFile = CreateFileA(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     11429:                        if(hFile != INVALID_HANDLE_VALUE) {
                   11430:                                FILETIME local, time;
                   11431:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   11432:                                if(REG8(BL) == 7) {
                   11433:                                        ULARGE_INTEGER hund;
                   11434:                                        hund.LowPart = local.dwLowDateTime;
                   11435:                                        hund.HighPart = local.dwHighDateTime;
                   11436:                                        hund.QuadPart += REG16(SI) * 100000;
                   11437:                                        local.dwLowDateTime = hund.LowPart;
                   11438:                                        local.dwHighDateTime = hund.HighPart;
                   11439:                                }
                   11440:                                LocalFileTimeToFileTime(&local, &time);
                   11441:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   11442:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   11443:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   11444:                                        REG16(AX) = (UINT16)GetLastError();
                   11445:                                        m_CF = 1;
                   11446:                                }
                   11447:                                CloseHandle(hFile);
                   11448:                        } else {
                   11449:                                REG16(AX) = (UINT16)GetLastError();
                   11450:                                m_CF = 1;
1.1       root     11451:                        }
1.1.1.48  root     11452:                } else {
                   11453:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   11454:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   11455:                        // 214307 DR DOS 6.0 - Set File Owner
                   11456: //                     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));
                   11457:                        REG16(AX) = 0x01;
                   11458:                        m_CF = 1;
1.1.1.14  root     11459:                }
                   11460:                break;
                   11461:        case 0x04:
                   11462:        case 0x06:
                   11463:        case 0x08:
1.1.1.48  root     11464:                if(lfn) {
1.1.1.14  root     11465:                        WIN32_FILE_ATTRIBUTE_DATA fad;
1.1.1.60  root     11466:                        if(GetFileAttributesExA(path, GetFileExInfoStandard, (LPVOID)&fad)) {
1.1.1.14  root     11467:                                FILETIME *time, local;
                   11468:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   11469:                                                   0x06 ? &fad.ftLastAccessTime :
                   11470:                                                          &fad.ftCreationTime;
                   11471:                                FileTimeToLocalFileTime(time, &local);
                   11472:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   11473:                                if(REG8(BL) == 0x08) {
                   11474:                                        ULARGE_INTEGER hund;
                   11475:                                        hund.LowPart = local.dwLowDateTime;
                   11476:                                        hund.HighPart = local.dwHighDateTime;
                   11477:                                        hund.QuadPart /= 100000;
                   11478:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   11479:                                }
                   11480:                        } else {
                   11481:                                REG16(AX) = (UINT16)GetLastError();
                   11482:                                m_CF = 1;
1.1       root     11483:                        }
1.1.1.48  root     11484:                } else {
                   11485:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   11486:                        // 214306 DR DOS 6.0 - Get File Owner
                   11487: //                     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));
                   11488:                        REG16(AX) = 0x01;
                   11489:                        m_CF = 1;
1.1.1.14  root     11490:                }
                   11491:                break;
1.1.1.43  root     11492:        case 0xff:
1.1.1.48  root     11493:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     11494:                        if(REG8(CL) == 0x39) {
                   11495:                                msdos_int_21h_39h(1);
                   11496:                                break;
                   11497:                        } else if(REG8(CL) == 0x56) {
                   11498:                                msdos_int_21h_56h(1);
                   11499:                                break;
                   11500:                        }
                   11501:                }
1.1.1.14  root     11502:        default:
1.1.1.22  root     11503:                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     11504:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     11505:                m_CF = 1;
                   11506:                break;
                   11507:        }
                   11508: }
                   11509: 
                   11510: inline void msdos_int_21h_44h()
                   11511: {
1.1.1.22  root     11512:        static UINT16 iteration_count = 0;
                   11513:        
1.1.1.44  root     11514:        process_t *process;
                   11515:        int fd, drv;
1.1.1.14  root     11516:        
                   11517:        switch(REG8(AL)) {
                   11518:        case 0x00:
                   11519:        case 0x01:
                   11520:        case 0x02:
                   11521:        case 0x03:
                   11522:        case 0x04:
                   11523:        case 0x05:
                   11524:        case 0x06:
                   11525:        case 0x07:
1.1.1.44  root     11526:                process = msdos_process_info_get(current_psp);
                   11527:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11528:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11529:                        REG16(AX) = 0x06;
                   11530:                        m_CF = 1;
                   11531:                        return;
1.1.1.14  root     11532:                }
                   11533:                break;
                   11534:        case 0x08:
                   11535:        case 0x09:
1.1.1.44  root     11536:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11537:                if(!msdos_is_valid_drive(drv)) {
                   11538:                        // invalid drive
1.1.1.14  root     11539:                        REG16(AX) = 0x0f;
                   11540:                        m_CF = 1;
                   11541:                        return;
1.1       root     11542:                }
                   11543:                break;
                   11544:        }
                   11545:        switch(REG8(AL)) {
1.1.1.48  root     11546:        case 0x00: // Get Device Information
1.1.1.20  root     11547:                REG16(DX) = file_handler[fd].info;
1.1       root     11548:                break;
1.1.1.48  root     11549:        case 0x01: // Set Device Information
1.1.1.45  root     11550:                if(REG8(DH) != 0) {
                   11551: //                     REG16(AX) = 0x0d; // data invalid
                   11552: //                     m_CF = 1;
                   11553:                        file_handler[fd].info = REG16(DX);
                   11554:                } else {
                   11555:                        file_handler[fd].info &= 0xff00;
                   11556:                        file_handler[fd].info |= REG8(DL);
                   11557:                }
1.1       root     11558:                break;
1.1.1.48  root     11559:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11560:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11561:                        // from DOSBox
                   11562:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11563:                        case 0x00:
                   11564:                                if(REG16(CX) >= 6) {
                   11565:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11566:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11567:                                        REG16(AX) = 6; // number of bytes actually read
                   11568:                                } else {
                   11569:                                        REG16(AX) = 0x0d; // data invalid
                   11570:                                        m_CF = 1;
                   11571:                                }
                   11572:                                break;
                   11573:                        case 0x01:
                   11574:                                if(REG16(CX) >= 6) {
                   11575:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11576:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11577:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11578:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11579:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11580:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11581:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11582:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11583:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11584:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11585:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11586:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11587:                                                } else {
                   11588:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11589:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11590:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11591:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11592:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11593:                                                }
                   11594:                                        }
                   11595:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11596:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11597:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11598:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11599:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11600:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11601:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11602:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11603:                                        
                   11604:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   11605:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   11606:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11607:                                        REG16(AX) = 6; // number of bytes actually read
                   11608:                                } else {
                   11609:                                        REG16(AX) = 0x0d; // data invalid
                   11610:                                        m_CF = 1;
                   11611:                                }
                   11612:                                break;
                   11613:                        case 0x02:
                   11614:                                if(REG16(CX) >= 2) {
                   11615:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11616:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11617:                                        REG16(AX) = 2; // number of bytes actually read
                   11618:                                } else {
                   11619:                                        REG16(AX) = 0x0d; // data invalid
                   11620:                                        m_CF = 1;
                   11621:                                }
                   11622:                                break;
                   11623:                        case 0x03:
                   11624:                                if(REG16(CX) >= 4) {
                   11625:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11626:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11627:                                        REG16(AX) = 4; // number of bytes actually read
                   11628:                                } else {
                   11629:                                        REG16(AX) = 0x0d; // data invalid
                   11630:                                        m_CF = 1;
                   11631:                                }
                   11632:                                break;
                   11633:                        default:
                   11634:                                REG16(AX) = 0x01; // function number invalid
                   11635:                                m_CF = 1;
                   11636:                        }
                   11637:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11638:                        if(REG16(CX) >= 5) {
                   11639:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11640:                                REG16(AX) = 5; // number of bytes actually read
                   11641:                        } else {
                   11642:                                REG16(AX) = 0x0d; // data invalid
                   11643:                                m_CF = 1;
                   11644:                        }
                   11645:                } else {
                   11646: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11647: //                     REG16(AX) = REG16(CX);
                   11648:                        REG16(AX) = 0x05; // access denied
                   11649:                        m_CF = 1;
                   11650:                }
                   11651:                break;
1.1.1.48  root     11652:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11653: //             REG16(AX) = 0x05;
                   11654: //             m_CF = 1;
                   11655:                REG16(AX) = 0x00; // success
                   11656:                break;
1.1.1.48  root     11657:        case 0x04: // Read From Block Device Control Channel
                   11658:        case 0x05: // Write To Block Device Control Channel
1.1       root     11659:                REG16(AX) = 0x05;
1.1.1.3   root     11660:                m_CF = 1;
1.1       root     11661:                break;
1.1.1.48  root     11662:        case 0x06: // Get Input Status
1.1.1.20  root     11663:                if(file_mode[file_handler[fd].mode].in) {
                   11664:                        if(file_handler[fd].atty) {
1.1.1.14  root     11665:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11666:                        } else {
1.1.1.20  root     11667:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11668:                        }
1.1.1.14  root     11669:                } else {
                   11670:                        REG8(AL) = 0x00;
1.1       root     11671:                }
                   11672:                break;
1.1.1.48  root     11673:        case 0x07: // Get Output Status
1.1.1.20  root     11674:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11675:                        REG8(AL) = 0xff;
                   11676:                } else {
                   11677:                        REG8(AL) = 0x00;
1.1       root     11678:                }
                   11679:                break;
1.1.1.48  root     11680:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11681:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11682:                        // removable drive
                   11683:                        REG16(AX) = 0x00;
1.1       root     11684:                } else {
1.1.1.14  root     11685:                        // fixed drive
                   11686:                        REG16(AX) = 0x01;
1.1       root     11687:                }
                   11688:                break;
1.1.1.48  root     11689:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11690:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11691:                        // remote drive
                   11692:                        REG16(DX) = 0x1000;
1.1.1.44  root     11693:                } else if(msdos_is_subst_drive(drv)) {
                   11694:                        // subst drive
                   11695:                        REG16(DX) = 0x8000;
1.1       root     11696:                } else {
1.1.1.14  root     11697:                        // local drive
1.1.1.44  root     11698:                        REG16(DX) = 0x0000;
1.1       root     11699:                }
                   11700:                break;
1.1.1.48  root     11701:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11702:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11703:                        REG16(DX) = 0x8000;
                   11704:                } else {
                   11705:                        REG16(DX) = 0x0000;
                   11706:                }
1.1.1.21  root     11707:                break;
1.1.1.48  root     11708:        case 0x0b: // Set Sharing Retry Count
1.1       root     11709:                break;
1.1.1.48  root     11710:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11711:                if(REG8(CL) == 0x45) {
                   11712:                        // set iteration (retry) count
                   11713:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11714:                } else if(REG8(CL) == 0x4a) {
                   11715:                        // select code page
                   11716:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11717:                        msdos_nls_tables_update();
1.1.1.44  root     11718:                } else if(REG8(CL) == 0x4c) {
                   11719:                        // start code-page preparation
                   11720:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11721:                        int count = 1, offset = 0;
                   11722:                        if(active_code_page != 437) {
                   11723:                                ids[count++] = active_code_page;
                   11724:                        }
                   11725:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11726:                                ids[count++] = system_code_page;
                   11727:                        }
                   11728:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11729:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11730:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11731:                        for(int i = 0; i < count; i++) {
                   11732:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11733:                        }
                   11734:                } else if(REG8(CL) == 0x4d) {
                   11735:                        // end code-page preparation
                   11736:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11737:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11738:                } else if(REG8(CL) == 0x5f) {
                   11739:                        // set display information
                   11740:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11741:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11742:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11743:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11744:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11745:                                
                   11746:                                if(cur_width != new_width || cur_height != new_height) {
                   11747:                                        pcbios_set_console_size(new_width, new_height, true);
                   11748:                                }
                   11749:                        }
1.1.1.22  root     11750:                } else if(REG8(CL) == 0x65) {
                   11751:                        // get iteration (retry) count
                   11752:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11753:                } else if(REG8(CL) == 0x6a) {
                   11754:                        // query selected code page
                   11755:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11756:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11757:                        
                   11758:                        CPINFO info;
                   11759:                        GetCPInfo(active_code_page, &info);
                   11760:                        
                   11761:                        if(info.MaxCharSize != 1) {
                   11762:                                for(int i = 0;; i++) {
                   11763:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11764:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11765:                                        
                   11766:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11767:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11768:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11769:                                        
                   11770:                                        if(lo == 0 && hi == 0) {
                   11771:                                                break;
                   11772:                                        }
                   11773:                                }
                   11774:                        }
1.1.1.44  root     11775:                } else if(REG8(CL) == 0x6b) {
                   11776:                        // query prepare list
                   11777:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11778:                        int count = 1, offset = 0;
                   11779:                        if(active_code_page != 437) {
                   11780:                                ids[count++] = active_code_page;
                   11781:                        }
                   11782:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11783:                                ids[count++] = system_code_page;
                   11784:                        }
                   11785:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11786:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11787:                        for(int i = 0; i < count; i++) {
                   11788:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11789:                        }
                   11790:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11791:                        for(int i = 0; i < count; i++) {
                   11792:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11793:                        }
1.1.1.22  root     11794:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11795:                        // get display information
1.1.1.50  root     11796:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11797:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11798:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11799:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11800:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11801:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11802:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11803:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11804:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11805:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11806:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11807:                } else {
                   11808:                        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));
                   11809:                        REG16(AX) = 0x01; // invalid function
                   11810:                        m_CF = 1;
                   11811:                }
                   11812:                break;
1.1.1.48  root     11813:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11814:                if(REG8(CL) == 0x40) {
                   11815:                        // set device parameters
1.1.1.48  root     11816: //             } else if(REG8(CL) == 0x41) {
                   11817: //                     // write logical device track
                   11818: //             } else if(REG8(CL) == 0x42) {
                   11819: //                     // format and verify logical device track
1.1.1.22  root     11820:                } else if(REG8(CL) == 0x46) {
                   11821:                        // set volume serial number
1.1.1.48  root     11822:                } else if(REG8(CL) == 0x47) {
                   11823:                        // set access flag
                   11824: //             } else if(REG8(CL) == 0x48) {
                   11825: //                     // set media lock state
                   11826: //             } else if(REG8(CL) == 0x49) {
                   11827: //                     // eject media in drive
1.1.1.22  root     11828:                } else if(REG8(CL) == 0x4a) {
                   11829:                        // lock logical volume
                   11830:                } else if(REG8(CL) == 0x4b) {
                   11831:                        // lock physical volume
                   11832:                } else if(REG8(CL) == 0x60) {
                   11833:                        // get device parameters
1.1.1.42  root     11834:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11835:                        
1.1.1.42  root     11836:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11837:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11838:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11839:                                
                   11840:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11841:                                switch(geo->MediaType) {
                   11842:                                case F5_360_512:
                   11843:                                case F5_320_512:
                   11844:                                case F5_320_1024:
                   11845:                                case F5_180_512:
                   11846:                                case F5_160_512:
                   11847:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11848:                                        break;
                   11849:                                case F5_1Pt2_512:
                   11850:                                case F3_1Pt2_512:
                   11851:                                case F3_1Pt23_1024:
                   11852:                                case F5_1Pt23_1024:
                   11853:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11854:                                        break;
                   11855:                                case F3_720_512:
                   11856:                                case F3_640_512:
                   11857:                                case F5_640_512:
                   11858:                                case F5_720_512:
                   11859:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11860:                                        break;
                   11861:                                case F8_256_128:
                   11862:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11863:                                        break;
                   11864:                                case FixedMedia:
                   11865:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11866:                                        break;
                   11867:                                case F3_1Pt44_512:
                   11868:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11869:                                        break;
                   11870:                                case F3_2Pt88_512:
                   11871:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11872:                                        break;
                   11873:                                default:
                   11874:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11875: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11876:                                        break;
1.1.1.22  root     11877:                                }
1.1.1.42  root     11878:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11879:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11880:                                switch(geo->MediaType) {
                   11881:                                case F5_360_512:
                   11882:                                case F5_320_512:
                   11883:                                case F5_320_1024:
                   11884:                                case F5_180_512:
                   11885:                                case F5_160_512:
                   11886:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11887:                                        break;
                   11888:                                default:
                   11889:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11890:                                        break;
                   11891:                                }
                   11892:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11893:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11894:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11895:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11896:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11897:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11898:                                switch(geo->MediaType) {
                   11899:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11900:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11901:                                        break;
                   11902:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11903:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11904:                                        break;
                   11905:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11906:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11907:                                        break;
                   11908:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11909:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11910:                                        break;
                   11911:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11912:                                case F3_1Pt2_512:
                   11913:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11914:                                case F5_720_512:
                   11915:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11916:                                        break;
                   11917:                                case FixedMedia:        // hard disk
                   11918:                                case RemovableMedia:
                   11919:                                case Unknown:
                   11920:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11921:                                        break;
                   11922:                                default:
                   11923:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11924:                                        break;
                   11925:                                }
                   11926:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11927:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11928:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11929:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11930:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11931:                                // 21h  BYTE    device type
                   11932:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11933:                        } else {
                   11934:                                REG16(AX) = 0x0f; // invalid drive
                   11935:                                m_CF = 1;
                   11936:                        }
1.1.1.48  root     11937: //             } else if(REG8(CL) == 0x61) {
                   11938: //                     // read logical device track
                   11939: //             } else if(REG8(CL) == 0x62) {
                   11940: //                     // verify logical device track
1.1.1.22  root     11941:                } else if(REG8(CL) == 0x66) {
                   11942:                        // get volume serial number
                   11943:                        char path[] = "A:\\";
                   11944:                        char volume_label[MAX_PATH];
                   11945:                        DWORD serial_number = 0;
                   11946:                        char file_system[MAX_PATH];
                   11947:                        
                   11948:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11949:                        
1.1.1.60  root     11950:                        if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1.1.22  root     11951:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11952:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11953:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11954:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11955:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11956:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11957:                        } else {
                   11958:                                REG16(AX) = 0x0f; // invalid drive
                   11959:                                m_CF = 1;
                   11960:                        }
                   11961:                } else if(REG8(CL) == 0x67) {
                   11962:                        // get access flag
                   11963:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11964:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11965:                } else if(REG8(CL) == 0x68) {
                   11966:                        // sense media type
1.1.1.42  root     11967:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11968:                        
1.1.1.42  root     11969:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11970:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11971:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11972:                                
                   11973:                                switch(geo->MediaType) {
                   11974:                                case F3_720_512:
                   11975:                                case F5_720_512:
                   11976:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11977:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11978:                                        break;
                   11979:                                case F3_1Pt44_512:
                   11980:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11981:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11982:                                        break;
                   11983:                                case F3_2Pt88_512:
                   11984:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11985:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11986:                                        break;
                   11987:                                default:
                   11988:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11989:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11990:                                        break;
1.1.1.22  root     11991:                                }
                   11992:                        } else {
                   11993:                                REG16(AX) = 0x0f; // invalid drive
                   11994:                                m_CF = 1;
                   11995:                        }
                   11996:                } else if(REG8(CL) == 0x6a) {
                   11997:                        // unlock logical volume
                   11998:                } else if(REG8(CL) == 0x6b) {
                   11999:                        // unlock physical volume
1.1.1.48  root     12000: //             } else if(REG8(CL) == 0x6c) {
                   12001: //                     // get lock flag
                   12002: //             } else if(REG8(CL) == 0x6d) {
                   12003: //                     // enumerate open files
                   12004: //             } else if(REG8(CL) == 0x6e) {
                   12005: //                     // find swap file
                   12006: //             } else if(REG8(CL) == 0x6f) {
                   12007: //                     // get drive map information
                   12008: //             } else if(REG8(CL) == 0x70) {
                   12009: //                     // get current lock state
                   12010: //             } else if(REG8(CL) == 0x71) {
                   12011: //                     // get first cluster
1.1.1.22  root     12012:                } else {
                   12013:                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   12014:                        REG16(AX) = 0x01; // invalid function
                   12015:                        m_CF = 1;
                   12016:                }
                   12017:                break;
1.1.1.48  root     12018:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     12019:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12020:                        REG16(AX) = 0x0f; // invalid drive
                   12021:                        m_CF = 1;
                   12022:                } else {
                   12023:                        REG8(AL) = 0;
1.1.1.22  root     12024:                }
                   12025:                break;
1.1.1.48  root     12026:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     12027:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12028:                        REG16(AX) = 0x0f; // invalid drive
                   12029:                        m_CF = 1;
1.1.1.22  root     12030:                }
                   12031:                break;
1.1.1.48  root     12032:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     12033:                switch(REG8(CL)) {
                   12034:                case 0x45:
                   12035:                case 0x4a:
1.1.1.48  root     12036:                case 0x4c:
                   12037:                case 0x4d:
1.1.1.22  root     12038:                case 0x65:
                   12039:                case 0x6a:
1.1.1.48  root     12040:                case 0x6b:
1.1.1.22  root     12041:                case 0x7f:
                   12042:                        REG16(AX) = 0x0000; // supported
                   12043:                        break;
                   12044:                default:
                   12045:                        REG8(AL) = 0x01; // ioctl capability not available
                   12046:                        m_CF = 1;
                   12047:                        break;
                   12048:                }
                   12049:                break;
1.1.1.48  root     12050:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     12051:                switch(REG8(CL)) {
                   12052:                case 0x40:
                   12053:                case 0x46:
                   12054:                case 0x4a:
                   12055:                case 0x4b:
                   12056:                case 0x60:
                   12057:                case 0x66:
                   12058:                case 0x67:
                   12059:                case 0x68:
                   12060:                case 0x6a:
                   12061:                case 0x6b:
1.1.1.48  root     12062:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   12063:                                // CH = 00h     Unknown
                   12064:                                // CH = 01h     COMn:
                   12065:                                // CH = 03h     CON
                   12066:                                // CH = 05h     LPTn:
                   12067:                                REG16(AX) = 0x0000; // supported
                   12068:                                break;
                   12069:                        }
1.1.1.22  root     12070:                default:
                   12071:                        REG8(AL) = 0x01; // ioctl capability not available
                   12072:                        m_CF = 1;
                   12073:                        break;
                   12074:                }
                   12075:                break;
1.1.1.48  root     12076:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   12077:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   12078:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   12079:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   12080:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   12081:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   12082:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   12083:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   12084:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   12085:        case 0x59: // DR Multiuser DOS 5.0 - API
                   12086:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     12087:                m_CF = 1;
                   12088:                break;
1.1       root     12089:        default:
1.1.1.22  root     12090:                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     12091:                REG16(AX) = 0x01;
1.1.1.3   root     12092:                m_CF = 1;
1.1       root     12093:                break;
                   12094:        }
                   12095: }
                   12096: 
                   12097: inline void msdos_int_21h_45h()
                   12098: {
                   12099:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12100:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12101:        
1.1.1.20  root     12102:        if(fd < process->max_files && file_handler[fd].valid) {
                   12103:                int dup_fd = _dup(fd);
                   12104:                if(dup_fd != -1) {
                   12105:                        REG16(AX) = dup_fd;
                   12106:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12107: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12108:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12109:                } else {
                   12110:                        REG16(AX) = errno;
1.1.1.3   root     12111:                        m_CF = 1;
1.1       root     12112:                }
                   12113:        } else {
                   12114:                REG16(AX) = 0x06;
1.1.1.3   root     12115:                m_CF = 1;
1.1       root     12116:        }
                   12117: }
                   12118: 
                   12119: inline void msdos_int_21h_46h()
                   12120: {
                   12121:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12122:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12123:        int dup_fd = REG16(CX);
                   12124:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     12125:        
1.1.1.20  root     12126:        if(REG16(BX) == REG16(CX)) {
                   12127:                REG16(AX) = 0x06;
                   12128:                m_CF = 1;
                   12129:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   12130:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   12131:                        _close(tmp_fd);
                   12132:                        msdos_file_handler_close(tmp_fd);
                   12133:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   12134:                }
                   12135:                if(_dup2(fd, dup_fd) != -1) {
                   12136:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12137: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12138:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12139:                } else {
                   12140:                        REG16(AX) = errno;
1.1.1.3   root     12141:                        m_CF = 1;
1.1       root     12142:                }
                   12143:        } else {
                   12144:                REG16(AX) = 0x06;
1.1.1.3   root     12145:                m_CF = 1;
1.1       root     12146:        }
                   12147: }
                   12148: 
                   12149: inline void msdos_int_21h_47h(int lfn)
                   12150: {
                   12151:        char path[MAX_PATH];
                   12152:        
                   12153:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     12154:                if(!lfn) {
                   12155:                        strcpy(path, msdos_short_path(path));
                   12156:                }
1.1       root     12157:                if(path[1] == ':') {
                   12158:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     12159:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     12160:                } else {
1.1.1.45  root     12161:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     12162:                }
                   12163:        } else {
                   12164:                REG16(AX) = errno;
1.1.1.3   root     12165:                m_CF = 1;
1.1       root     12166:        }
                   12167: }
                   12168: 
                   12169: inline void msdos_int_21h_48h()
                   12170: {
1.1.1.19  root     12171:        int seg, umb_linked;
1.1       root     12172:        
1.1.1.8   root     12173:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     12174:                // unlink umb not to allocate memory in umb
                   12175:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   12176:                        msdos_mem_unlink_umb();
                   12177:                }
1.1.1.8   root     12178:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12179:                        REG16(AX) = seg;
                   12180:                } else {
                   12181:                        REG16(AX) = 0x08;
                   12182:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   12183:                        m_CF = 1;
                   12184:                }
1.1.1.19  root     12185:                if(umb_linked != 0) {
                   12186:                        msdos_mem_link_umb();
                   12187:                }
1.1.1.8   root     12188:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   12189:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12190:                        REG16(AX) = seg;
                   12191:                } else {
                   12192:                        REG16(AX) = 0x08;
                   12193:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   12194:                        m_CF = 1;
                   12195:                }
                   12196:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   12197:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12198:                        REG16(AX) = seg;
                   12199:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12200:                        REG16(AX) = seg;
                   12201:                } else {
                   12202:                        REG16(AX) = 0x08;
                   12203:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   12204:                        m_CF = 1;
                   12205:                }
1.1       root     12206:        }
                   12207: }
                   12208: 
                   12209: inline void msdos_int_21h_49h()
                   12210: {
1.1.1.14  root     12211:        int mcb_seg = SREG(ES) - 1;
                   12212:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   12213:        
                   12214:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12215:                msdos_mem_free(SREG(ES));
                   12216:        } else {
1.1.1.33  root     12217:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     12218:                m_CF = 1;
                   12219:        }
1.1       root     12220: }
                   12221: 
                   12222: inline void msdos_int_21h_4ah()
                   12223: {
1.1.1.14  root     12224:        int mcb_seg = SREG(ES) - 1;
                   12225:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     12226:        int max_paragraphs;
                   12227:        
1.1.1.14  root     12228:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12229:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   12230:                        REG16(AX) = 0x08;
                   12231:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   12232:                        m_CF = 1;
                   12233:                }
                   12234:        } else {
1.1.1.33  root     12235:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     12236:                m_CF = 1;
1.1       root     12237:        }
                   12238: }
                   12239: 
                   12240: inline void msdos_int_21h_4bh()
                   12241: {
1.1.1.3   root     12242:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12243:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     12244:        
                   12245:        switch(REG8(AL)) {
                   12246:        case 0x00:
                   12247:        case 0x01:
                   12248:                if(msdos_process_exec(command, param, REG8(AL))) {
                   12249:                        REG16(AX) = 0x02;
1.1.1.3   root     12250:                        m_CF = 1;
1.1       root     12251:                }
                   12252:                break;
1.1.1.14  root     12253:        case 0x03:
                   12254:                {
                   12255:                        int fd;
                   12256:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   12257:                                REG16(AX) = 0x02;
                   12258:                                m_CF = 1;
                   12259:                                break;
                   12260:                        }
                   12261:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   12262:                        _close(fd);
                   12263:                        
                   12264:                        UINT16 *overlay = (UINT16 *)param;
                   12265:                        
                   12266:                        // check exe header
                   12267:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   12268:                        int header_size = 0;
                   12269:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   12270:                                header_size = header->header_size * 16;
                   12271:                                // relocation
                   12272:                                int start_seg = overlay[1];
                   12273:                                for(int i = 0; i < header->relocations; i++) {
                   12274:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   12275:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   12276:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   12277:                                }
                   12278:                        }
                   12279:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   12280:                }
                   12281:                break;
1.1.1.48  root     12282:        case 0x04:
                   12283:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   12284: //     case 0x05:
                   12285: //             // DOS 5+ - Set Execution State
                   12286:        case 0x80:
                   12287:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   12288:        case 0xf0:
                   12289:        case 0xf1:
                   12290:                // DIET v1.10+
1.1.1.43  root     12291:        case 0xfd:
                   12292:        case 0xfe:
                   12293:                // unknown function called in FreeCOM
                   12294:                REG16(AX) = 0x01;
                   12295:                m_CF = 1;
                   12296:                break;
1.1       root     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       root     12299:                REG16(AX) = 0x01;
1.1.1.3   root     12300:                m_CF = 1;
1.1       root     12301:                break;
                   12302:        }
                   12303: }
                   12304: 
                   12305: inline void msdos_int_21h_4ch()
                   12306: {
                   12307:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   12308: }
                   12309: 
                   12310: inline void msdos_int_21h_4dh()
                   12311: {
                   12312:        REG16(AX) = retval;
                   12313: }
                   12314: 
                   12315: inline void msdos_int_21h_4eh()
                   12316: {
                   12317:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12318:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12319:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     12320:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     12321:        WIN32_FIND_DATAA fd;
1.1       root     12322:        
1.1.1.14  root     12323:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12324:        find->find_magic = FIND_MAGIC;
                   12325:        find->dta_index = dtainfo - dtalist;
1.1       root     12326:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12327:        dtainfo->allowable_mask = REG8(CL);
1.1.1.58  root     12328:        // note: SO1 dir command sets 0x3f, but only directories and volue label are found if bit3 is set :-(
                   12329:        if((dtainfo->allowable_mask & 0x3f) == 0x3f) {
                   12330:                dtainfo->allowable_mask &= ~0x08;
                   12331:        }
1.1.1.14  root     12332:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12333:        
1.1.1.14  root     12334:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12335:                dtainfo->allowable_mask &= ~8;
1.1       root     12336:        }
1.1.1.60  root     12337:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     12338:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12339:                      !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12340:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12341:                                FindClose(dtainfo->find_handle);
                   12342:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12343:                                break;
                   12344:                        }
                   12345:                }
                   12346:        }
1.1.1.13  root     12347:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12348:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12349:                msdos_find_file_conv_local_time(&fd);
                   12350:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12351:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12352:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12353:                REG16(AX) = 0;
1.1.1.14  root     12354:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12355:                find->attrib = 8;
                   12356:                find->size = 0;
                   12357:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12358:                dtainfo->allowable_mask &= ~8;
1.1       root     12359:                REG16(AX) = 0;
                   12360:        } else {
                   12361:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12362:                m_CF = 1;
1.1       root     12363:        }
                   12364: }
                   12365: 
                   12366: inline void msdos_int_21h_4fh()
                   12367: {
                   12368:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12369:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12370:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.60  root     12371:        WIN32_FIND_DATAA fd;
1.1       root     12372:        
1.1.1.14  root     12373:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   12374:                REG16(AX) = 0x12;
                   12375:                m_CF = 1;
                   12376:                return;
                   12377:        }
                   12378:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     12379:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     12380:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12381:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12382:                              !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12383:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12384:                                        FindClose(dtainfo->find_handle);
                   12385:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12386:                                        break;
                   12387:                                }
                   12388:                        }
                   12389:                } else {
1.1.1.13  root     12390:                        FindClose(dtainfo->find_handle);
                   12391:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12392:                }
                   12393:        }
1.1.1.13  root     12394:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12395:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12396:                msdos_find_file_conv_local_time(&fd);
                   12397:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12398:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12399:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12400:                REG16(AX) = 0;
1.1.1.14  root     12401:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12402:                find->attrib = 8;
                   12403:                find->size = 0;
                   12404:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12405:                dtainfo->allowable_mask &= ~8;
1.1       root     12406:                REG16(AX) = 0;
                   12407:        } else {
                   12408:                REG16(AX) = 0x12;
1.1.1.3   root     12409:                m_CF = 1;
1.1       root     12410:        }
                   12411: }
                   12412: 
                   12413: inline void msdos_int_21h_50h()
                   12414: {
1.1.1.8   root     12415:        if(current_psp != REG16(BX)) {
                   12416:                process_t *process = msdos_process_info_get(current_psp);
                   12417:                if(process != NULL) {
                   12418:                        process->psp = REG16(BX);
                   12419:                }
                   12420:                current_psp = REG16(BX);
1.1.1.23  root     12421:                msdos_sda_update(current_psp);
1.1.1.8   root     12422:        }
1.1       root     12423: }
                   12424: 
                   12425: inline void msdos_int_21h_51h()
                   12426: {
                   12427:        REG16(BX) = current_psp;
                   12428: }
                   12429: 
                   12430: inline void msdos_int_21h_52h()
                   12431: {
1.1.1.25  root     12432:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     12433:        i386_load_segment_descriptor(ES);
1.1.1.25  root     12434:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     12435: }
                   12436: 
1.1.1.43  root     12437: inline void msdos_int_21h_53h()
                   12438: {
                   12439:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   12440:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   12441:        
                   12442:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   12443:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   12444:        dpb->shift_count = 0;
                   12445:        dpb->reserved_sectors = 0;
                   12446:        dpb->fat_num = bpb->fat_num;
                   12447:        dpb->root_entries = bpb->root_entries;
                   12448:        dpb->first_data_sector = 0;
                   12449:        if(bpb->sectors_per_cluster != 0) {
                   12450:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   12451:        } else {
                   12452:                dpb->highest_cluster_num = 0;
                   12453:        }
                   12454:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   12455:        dpb->first_dir_sector = 0;
                   12456:        dpb->device_driver_header = 0;
                   12457:        dpb->media_type = bpb->media_type;
                   12458:        dpb->drive_accessed = 0;
                   12459:        dpb->next_dpb_ofs = 0xffff;
                   12460:        dpb->next_dpb_seg = 0xffff;
                   12461:        dpb->first_free_cluster = 0;
                   12462:        dpb->free_clusters = 0xffff;
                   12463: }
                   12464: 
1.1       root     12465: inline void msdos_int_21h_54h()
                   12466: {
                   12467:        process_t *process = msdos_process_info_get(current_psp);
                   12468:        
                   12469:        REG8(AL) = process->verify;
                   12470: }
                   12471: 
                   12472: inline void msdos_int_21h_55h()
                   12473: {
                   12474:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   12475:        
                   12476:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   12477:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   12478:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   12479:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   12480:        psp->parent_psp = current_psp;
                   12481: }
                   12482: 
                   12483: inline void msdos_int_21h_56h(int lfn)
                   12484: {
                   12485:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     12486:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   12487:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     12488:        
1.1.1.63  root     12489:        if(msdos_is_existing_file(dst) || msdos_is_existing_dir(dst)) {
                   12490:                REG16(AX) = 0x05; // access denied
                   12491:                m_CF = 1;
                   12492:        } else if(rename(src, dst)) {
1.1       root     12493:                REG16(AX) = errno;
1.1.1.3   root     12494:                m_CF = 1;
1.1       root     12495:        }
                   12496: }
                   12497: 
                   12498: inline void msdos_int_21h_57h()
                   12499: {
                   12500:        FILETIME time, local;
1.1.1.14  root     12501:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     12502:        HANDLE hHandle;
1.1       root     12503:        
1.1.1.21  root     12504:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     12505:                REG16(AX) = (UINT16)GetLastError();
                   12506:                m_CF = 1;
                   12507:                return;
                   12508:        }
                   12509:        ctime = atime = mtime = NULL;
                   12510:        
1.1       root     12511:        switch(REG8(AL)) {
                   12512:        case 0x00:
1.1.1.6   root     12513:        case 0x01:
1.1.1.14  root     12514:                mtime = &time;
1.1.1.6   root     12515:                break;
                   12516:        case 0x04:
                   12517:        case 0x05:
1.1.1.14  root     12518:                atime = &time;
1.1       root     12519:                break;
1.1.1.6   root     12520:        case 0x06:
                   12521:        case 0x07:
1.1.1.14  root     12522:                ctime = &time;
                   12523:                break;
                   12524:        default:
1.1.1.22  root     12525:                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     12526:                REG16(AX) = 0x01;
                   12527:                m_CF = 1;
                   12528:                return;
                   12529:        }
                   12530:        if(REG8(AL) & 1) {
1.1       root     12531:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12532:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12533:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12534:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12535:                        m_CF = 1;
1.1       root     12536:                }
1.1.1.14  root     12537:        } else {
1.1.1.21  root     12538:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12539:                        // assume a device and use the current time
                   12540:                        GetSystemTimeAsFileTime(&time);
                   12541:                }
                   12542:                FileTimeToLocalFileTime(&time, &local);
                   12543:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12544:        }
                   12545: }
                   12546: 
                   12547: inline void msdos_int_21h_58h()
                   12548: {
                   12549:        switch(REG8(AL)) {
                   12550:        case 0x00:
1.1.1.7   root     12551:                REG16(AX) = malloc_strategy;
                   12552:                break;
                   12553:        case 0x01:
1.1.1.24  root     12554: //             switch(REG16(BX)) {
                   12555:                switch(REG8(BL)) {
1.1.1.7   root     12556:                case 0x0000:
                   12557:                case 0x0001:
                   12558:                case 0x0002:
                   12559:                case 0x0040:
                   12560:                case 0x0041:
                   12561:                case 0x0042:
                   12562:                case 0x0080:
                   12563:                case 0x0081:
                   12564:                case 0x0082:
                   12565:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12566:                        msdos_sda_update(current_psp);
1.1.1.7   root     12567:                        break;
                   12568:                default:
1.1.1.22  root     12569:                        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     12570:                        REG16(AX) = 0x01;
                   12571:                        m_CF = 1;
                   12572:                        break;
                   12573:                }
                   12574:                break;
                   12575:        case 0x02:
1.1.1.19  root     12576:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12577:                break;
                   12578:        case 0x03:
1.1.1.24  root     12579: //             switch(REG16(BX)) {
                   12580:                switch(REG8(BL)) {
1.1.1.7   root     12581:                case 0x0000:
1.1.1.19  root     12582:                        msdos_mem_unlink_umb();
                   12583:                        break;
1.1.1.7   root     12584:                case 0x0001:
1.1.1.19  root     12585:                        msdos_mem_link_umb();
1.1.1.7   root     12586:                        break;
                   12587:                default:
1.1.1.22  root     12588:                        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     12589:                        REG16(AX) = 0x01;
                   12590:                        m_CF = 1;
                   12591:                        break;
                   12592:                }
1.1       root     12593:                break;
                   12594:        default:
1.1.1.22  root     12595:                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     12596:                REG16(AX) = 0x01;
1.1.1.3   root     12597:                m_CF = 1;
1.1       root     12598:                break;
                   12599:        }
                   12600: }
                   12601: 
                   12602: inline void msdos_int_21h_59h()
                   12603: {
1.1.1.47  root     12604:        if(REG16(BX) == 0x0000) {
                   12605:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12606:                
                   12607:                REG16(AX) = sda->extended_error_code;
                   12608:                REG8(BH)  = sda->error_class;
                   12609:                REG8(BL)  = sda->suggested_action;
                   12610:                REG8(CH)  = sda->locus_of_last_error;
                   12611:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12612:                if(sda->int21h_5d0ah_called != 0) {
                   12613:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12614:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12615: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12616:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12617: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12618: //                     i386_load_segment_descriptor(DS);
                   12619:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12620:                        i386_load_segment_descriptor(ES);
                   12621:                }
                   12622:                sda->int21h_5d0ah_called = 0;
                   12623: //     } else if(REG16(BX) == 0x0001) {
                   12624: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12625:        } else {
                   12626:                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));
                   12627:                REG16(AX) = 0x01;
                   12628:                m_CF = 1;
                   12629:        }
1.1       root     12630: }
                   12631: 
                   12632: inline void msdos_int_21h_5ah()
                   12633: {
1.1.1.3   root     12634:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12635:        int len = strlen(path);
                   12636:        char tmp[MAX_PATH];
                   12637:        
1.1.1.60  root     12638:        if(GetTempFileNameA(path, "TMP", 0, tmp)) {
1.1       root     12639:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12640:                
1.1.1.60  root     12641:                SetFileAttributesA(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12642:                REG16(AX) = fd;
                   12643:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12644:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12645:                
                   12646:                strcpy(path, tmp);
                   12647:                int dx = REG16(DX) + len;
1.1.1.3   root     12648:                int ds = SREG(DS);
1.1       root     12649:                while(dx > 0xffff) {
                   12650:                        dx -= 0x10;
                   12651:                        ds++;
                   12652:                }
                   12653:                REG16(DX) = dx;
1.1.1.3   root     12654:                SREG(DS) = ds;
                   12655:                i386_load_segment_descriptor(DS);
1.1       root     12656:        } else {
                   12657:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12658:                m_CF = 1;
1.1       root     12659:        }
                   12660: }
                   12661: 
                   12662: inline void msdos_int_21h_5bh()
                   12663: {
1.1.1.45  root     12664:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12665:        
1.1.1.45  root     12666:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12667:                // already exists
                   12668:                REG16(AX) = 0x50;
1.1.1.3   root     12669:                m_CF = 1;
1.1       root     12670:        } else {
                   12671:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12672:                
                   12673:                if(fd != -1) {
1.1.1.60  root     12674:                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12675:                        REG16(AX) = fd;
                   12676:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12677:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12678:                } else {
                   12679:                        REG16(AX) = errno;
1.1.1.3   root     12680:                        m_CF = 1;
1.1       root     12681:                }
                   12682:        }
                   12683: }
                   12684: 
                   12685: inline void msdos_int_21h_5ch()
                   12686: {
                   12687:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12688:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12689:        
1.1.1.20  root     12690:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12691:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12692:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12693:                        UINT32 pos = _tell(fd);
                   12694:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12695:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12696:                                REG16(AX) = errno;
1.1.1.3   root     12697:                                m_CF = 1;
1.1       root     12698:                        }
1.1.1.20  root     12699:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12700:                        
1.1       root     12701:                        // some seconds may be passed in _locking()
1.1.1.35  root     12702:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12703:                } else {
                   12704:                        REG16(AX) = 0x01;
1.1.1.3   root     12705:                        m_CF = 1;
1.1       root     12706:                }
                   12707:        } else {
                   12708:                REG16(AX) = 0x06;
1.1.1.3   root     12709:                m_CF = 1;
1.1       root     12710:        }
                   12711: }
                   12712: 
1.1.1.22  root     12713: inline void msdos_int_21h_5dh()
                   12714: {
                   12715:        switch(REG8(AL)) {
1.1.1.45  root     12716:        case 0x00:
                   12717:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12718:                        // current system
                   12719:                        static bool reenter = false;
                   12720:                        if(!reenter) {
                   12721:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12722:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12723:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12724:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12725:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12726:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12727:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12728:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12729:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12730:                                i386_load_segment_descriptor(DS);
                   12731:                                i386_load_segment_descriptor(ES);
                   12732:                                reenter = true;
                   12733:                                try {
                   12734:                                        msdos_syscall(0x21);
                   12735:                                } catch(...) {
                   12736:                                }
                   12737:                                reenter = false;
                   12738:                        }
                   12739:                } else {
                   12740:                        REG16(AX) = 0x49; //  network software not installed
                   12741:                        m_CF = 1;
                   12742:                }
                   12743:                break;
1.1.1.22  root     12744:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     12745:                SREG(DS) = (SDA_TOP >> 4);
                   12746:                i386_load_segment_descriptor(DS);
                   12747:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12748:                REG16(CX) = 0x80;
                   12749:                REG16(DX) = 0x1a;
                   12750:                break;
1.1.1.45  root     12751:        case 0x07: // get redirected printer mode
                   12752:        case 0x08: // set redirected printer mode
                   12753:        case 0x09: // flush redirected printer output
                   12754:                REG16(AX) = 0x49; //  network software not installed
                   12755:                m_CF = 1;
                   12756:                break;
1.1.1.43  root     12757:        case 0x0a: // set extended error information
                   12758:                {
                   12759:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12760:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12761:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12762:                        // XXX: which one is correct ???
                   12763: #if 1
                   12764:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12765:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12766:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12767:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12768:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12769: #else
                   12770:                        // PC DOS 7 Technical Update
                   12771:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12772:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12773:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12774:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12775: #endif
                   12776:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12777: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12778:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12779: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12780:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12781:                }
                   12782:                break;
1.1.1.23  root     12783:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12784:                REG16(AX) = 0x01;
                   12785:                m_CF = 1;
                   12786:                break;
                   12787:        default:
                   12788:                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));
                   12789:                REG16(AX) = 0x01;
                   12790:                m_CF = 1;
                   12791:                break;
                   12792:        }
                   12793: }
                   12794: 
1.1.1.42  root     12795: inline void msdos_int_21h_5eh()
                   12796: {
                   12797:        switch(REG8(AL)) {
                   12798:        case 0x00:
                   12799:                {
                   12800:                        char name[256] = {0};
                   12801:                        DWORD dwSize = 256;
                   12802:                        
1.1.1.60  root     12803:                        if(GetComputerNameA(name, &dwSize)) {
1.1.1.42  root     12804:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12805:                                for(int i = 0; i < 15; i++) {
                   12806:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12807:                                }
                   12808:                                dest[15] = '\0';
                   12809:                                REG8(CH) = 0x01; // nonzero valid
                   12810:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12811:                        } else {
                   12812:                                REG16(AX) = 0x01;
                   12813:                                m_CF = 1;
                   12814:                        }
                   12815:                }
                   12816:                break;
                   12817:        default:
1.1.1.45  root     12818: //             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));
                   12819: //             REG16(AX) = 0x01;
                   12820:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12821:                m_CF = 1;
                   12822:                break;
                   12823:        }
                   12824: }
                   12825: 
1.1.1.30  root     12826: inline void msdos_int_21h_5fh()
                   12827: {
                   12828:        switch(REG8(AL)) {
1.1.1.42  root     12829:        case 0x05:
1.1.1.44  root     12830:                REG16(BP) = 0;
                   12831:                for(int i = 0; i < 26; i++) {
                   12832:                        if(msdos_is_remote_drive(i)) {
                   12833:                                REG16(BP)++;
1.1.1.42  root     12834:                        }
                   12835:                }
1.1.1.30  root     12836:        case 0x02:
1.1.1.44  root     12837:                for(int i = 0, index = 0; i < 26; i++) {
                   12838:                        if(msdos_is_remote_drive(i)) {
                   12839:                                if(index == REG16(BX)) {
                   12840:                                        char volume[] = "A:";
1.1.1.30  root     12841:                                        volume[0] = 'A' + i;
1.1.1.44  root     12842:                                        DWORD dwSize = 128;
                   12843:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
1.1.1.60  root     12844:                                        WNetGetConnectionA(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
1.1.1.44  root     12845:                                        REG8(BH) = 0x00; // valid
                   12846:                                        REG8(BL) = 0x04; // disk drive
                   12847:                                        REG16(CX) = 0x00; // LANtastic
                   12848:                                        return;
1.1.1.30  root     12849:                                }
1.1.1.44  root     12850:                                index++;
1.1.1.30  root     12851:                        }
                   12852:                }
                   12853:                REG16(AX) = 0x12; // no more files
                   12854:                m_CF = 1;
                   12855:                break;
1.1.1.44  root     12856:        case 0x07:
                   12857:                if(msdos_is_valid_drive(REG8(DL))) {
                   12858:                        msdos_cds_update(REG8(DL));
                   12859:                } else {
                   12860:                        REG16(AX) = 0x0f; // invalid drive
                   12861:                        m_CF = 1;
                   12862:                }
                   12863:                break;
                   12864:        case 0x08:
                   12865:                if(msdos_is_valid_drive(REG8(DL))) {
                   12866:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12867:                        cds->drive_attrib = 0x0000;
                   12868:                } else {
                   12869:                        REG16(AX) = 0x0f; // invalid drive
                   12870:                        m_CF = 1;
                   12871:                }
                   12872:                break;
1.1.1.30  root     12873:        default:
1.1.1.45  root     12874: //             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));
                   12875: //             REG16(AX) = 0x01;
                   12876:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12877:                m_CF = 1;
                   12878:                break;
                   12879:        }
                   12880: }
                   12881: 
1.1       root     12882: inline void msdos_int_21h_60h(int lfn)
                   12883: {
1.1.1.45  root     12884:        char full[MAX_PATH];
                   12885:        const char *path = NULL;
1.1.1.14  root     12886:        
1.1       root     12887:        if(lfn) {
1.1.1.14  root     12888:                char *name;
                   12889:                *full = '\0';
1.1.1.60  root     12890:                GetFullPathNameA((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12891:                switch(REG8(CL)) {
                   12892:                case 1:
1.1.1.60  root     12893:                        GetShortPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12894:                        my_strupr(full);
                   12895:                        break;
                   12896:                case 2:
1.1.1.60  root     12897:                        GetLongPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12898:                        break;
                   12899:                }
                   12900:                path = full;
                   12901:        } else {
                   12902:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12903:        }
                   12904:        if(*path != '\0') {
                   12905:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12906:        } else {
1.1.1.14  root     12907:                REG16(AX) = (UINT16)GetLastError();
                   12908:                m_CF = 1;
1.1       root     12909:        }
                   12910: }
                   12911: 
                   12912: inline void msdos_int_21h_61h()
                   12913: {
                   12914:        REG8(AL) = 0;
                   12915: }
                   12916: 
                   12917: inline void msdos_int_21h_62h()
                   12918: {
                   12919:        REG16(BX) = current_psp;
                   12920: }
                   12921: 
                   12922: inline void msdos_int_21h_63h()
                   12923: {
                   12924:        switch(REG8(AL)) {
                   12925:        case 0x00:
1.1.1.3   root     12926:                SREG(DS) = (DBCS_TABLE >> 4);
                   12927:                i386_load_segment_descriptor(DS);
1.1       root     12928:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12929:                REG8(AL) = 0x00;
                   12930:                break;
1.1.1.22  root     12931:        case 0x01: // set korean input mode
                   12932:        case 0x02: // get korean input mode
                   12933:                REG8(AL) = 0xff; // not supported
                   12934:                break;
1.1       root     12935:        default:
1.1.1.22  root     12936:                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     12937:                REG16(AX) = 0x01;
1.1.1.3   root     12938:                m_CF = 1;
1.1       root     12939:                break;
                   12940:        }
                   12941: }
                   12942: 
1.1.1.25  root     12943: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12944: {
1.1.1.25  root     12945:        switch(func) {
1.1.1.17  root     12946:        case 0x01:
                   12947:                if(REG16(CX) >= 5) {
1.1.1.19  root     12948:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12949:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12950:                                REG16(CX) = sizeof(data);
                   12951:                        ZeroMemory(data, sizeof(data));
                   12952:                        data[0] = 0x01;
                   12953:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12954:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12955:                        *(UINT16 *)(data + 5) = active_code_page;
                   12956:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12957: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12958:                } else {
1.1.1.25  root     12959:                        return(0x08); // insufficient memory
1.1.1.17  root     12960:                }
                   12961:                break;
                   12962:        case 0x02:
                   12963:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12964:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12965:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12966: //             REG16(AX) = active_code_page;
1.1.1.17  root     12967:                REG16(CX) = 0x05;
                   12968:                break;
1.1.1.23  root     12969:        case 0x03:
                   12970:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12971:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12972:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12973: //             REG16(AX) = active_code_page;
1.1.1.23  root     12974:                REG16(CX) = 0x05;
                   12975:                break;
1.1.1.17  root     12976:        case 0x04:
                   12977:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12978:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12979:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12980: //             REG16(AX) = active_code_page;
1.1.1.17  root     12981:                REG16(CX) = 0x05;
                   12982:                break;
                   12983:        case 0x05:
                   12984:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12985:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12986:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12987: //             REG16(AX) = active_code_page;
1.1.1.17  root     12988:                REG16(CX) = 0x05;
                   12989:                break;
                   12990:        case 0x06:
                   12991:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12992:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12993:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12994: //             REG16(AX) = active_code_page;
1.1.1.17  root     12995:                REG16(CX) = 0x05;
                   12996:                break;
1.1       root     12997:        case 0x07:
1.1.1.3   root     12998:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12999:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   13000:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     13001: //             REG16(AX) = active_code_page;
1.1       root     13002:                REG16(CX) = 0x05;
                   13003:                break;
1.1.1.25  root     13004:        default:
                   13005:                return(0x01); // function number invalid
                   13006:        }
                   13007:        return(0x00);
                   13008: }
                   13009: 
                   13010: inline void msdos_int_21h_65h()
                   13011: {
                   13012:        char tmp[0x10000];
                   13013:        
                   13014:        switch(REG8(AL)) {
1.1.1.43  root     13015:        case 0x00:
                   13016:                if(REG16(CX) >= 7) {
                   13017:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   13018:                        REG16(AX) = system_code_page;
                   13019:                } else {
                   13020:                        REG16(AX) = 0x0c;
                   13021:                        m_CF = 1;
                   13022:                }
                   13023:                break;
1.1.1.25  root     13024:        case 0x01:
                   13025:        case 0x02:
                   13026:        case 0x03:
                   13027:        case 0x04:
                   13028:        case 0x05:
                   13029:        case 0x06:
                   13030:        case 0x07:
                   13031:                {
                   13032:                        UINT16 result = get_extended_country_info(REG8(AL));
                   13033:                        if(result) {
                   13034:                                REG16(AX) = result;
                   13035:                                m_CF = 1;
                   13036:                        } else {
                   13037:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   13038:                        }
                   13039:                }
                   13040:                break;
1.1       root     13041:        case 0x20:
1.1.1.25  root     13042:        case 0xa0:
1.1.1.19  root     13043:                memset(tmp, 0, sizeof(tmp));
                   13044:                tmp[0] = REG8(DL);
1.1       root     13045:                my_strupr(tmp);
                   13046:                REG8(DL) = tmp[0];
                   13047:                break;
                   13048:        case 0x21:
1.1.1.25  root     13049:        case 0xa1:
1.1       root     13050:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13051:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     13052:                my_strupr(tmp);
1.1.1.3   root     13053:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     13054:                break;
                   13055:        case 0x22:
1.1.1.25  root     13056:        case 0xa2:
1.1.1.3   root     13057:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     13058:                break;
1.1.1.25  root     13059:        case 0x23:
                   13060:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     13061:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     13062:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     13063:                        REG16(AX) = 0x00;
                   13064:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   13065:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     13066:                        REG16(AX) = 0x01;
                   13067:                } else {
                   13068:                        REG16(AX) = 0x02;
                   13069:                }
                   13070:                break;
1.1       root     13071:        default:
1.1.1.22  root     13072:                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     13073:                REG16(AX) = 0x01;
1.1.1.3   root     13074:                m_CF = 1;
1.1       root     13075:                break;
                   13076:        }
                   13077: }
                   13078: 
                   13079: inline void msdos_int_21h_66h()
                   13080: {
                   13081:        switch(REG8(AL)) {
                   13082:        case 0x01:
                   13083:                REG16(BX) = active_code_page;
                   13084:                REG16(DX) = system_code_page;
                   13085:                break;
                   13086:        case 0x02:
                   13087:                if(active_code_page == REG16(BX)) {
                   13088:                        REG16(AX) = 0xeb41;
                   13089:                } else if(_setmbcp(REG16(BX)) == 0) {
                   13090:                        active_code_page = REG16(BX);
1.1.1.17  root     13091:                        msdos_nls_tables_update();
1.1       root     13092:                        REG16(AX) = 0xeb41;
1.1.1.32  root     13093:                        SetConsoleCP(active_code_page);
                   13094:                        SetConsoleOutputCP(active_code_page);
1.1       root     13095:                } else {
                   13096:                        REG16(AX) = 0x25;
1.1.1.3   root     13097:                        m_CF = 1;
1.1       root     13098:                }
                   13099:                break;
                   13100:        default:
1.1.1.22  root     13101:                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     13102:                REG16(AX) = 0x01;
1.1.1.3   root     13103:                m_CF = 1;
1.1       root     13104:                break;
                   13105:        }
                   13106: }
                   13107: 
                   13108: inline void msdos_int_21h_67h()
                   13109: {
                   13110:        process_t *process = msdos_process_info_get(current_psp);
                   13111:        
                   13112:        if(REG16(BX) <= MAX_FILES) {
                   13113:                process->max_files = max(REG16(BX), 20);
                   13114:        } else {
                   13115:                REG16(AX) = 0x08;
1.1.1.3   root     13116:                m_CF = 1;
1.1       root     13117:        }
                   13118: }
                   13119: 
                   13120: inline void msdos_int_21h_68h()
                   13121: {
                   13122:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13123:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     13124:        
1.1.1.20  root     13125:        if(fd < process->max_files && file_handler[fd].valid) {
                   13126:                // fflush(_fdopen(fd, ""));
1.1       root     13127:        } else {
                   13128:                REG16(AX) = 0x06;
1.1.1.3   root     13129:                m_CF = 1;
1.1       root     13130:        }
                   13131: }
                   13132: 
                   13133: inline void msdos_int_21h_69h()
                   13134: {
1.1.1.3   root     13135:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13136:        char path[] = "A:\\";
                   13137:        char volume_label[MAX_PATH];
                   13138:        DWORD serial_number = 0;
                   13139:        char file_system[MAX_PATH];
                   13140:        
                   13141:        if(REG8(BL) == 0) {
                   13142:                path[0] = 'A' + _getdrive() - 1;
                   13143:        } else {
                   13144:                path[0] = 'A' + REG8(BL) - 1;
                   13145:        }
                   13146:        
                   13147:        switch(REG8(AL)) {
                   13148:        case 0x00:
1.1.1.60  root     13149:                if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1       root     13150:                        info->info_level = 0;
                   13151:                        info->serial_number = serial_number;
                   13152:                        memset(info->volume_label, 0x20, 11);
                   13153:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   13154:                        memset(info->file_system, 0x20, 8);
                   13155:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   13156:                } else {
                   13157:                        REG16(AX) = errno;
1.1.1.3   root     13158:                        m_CF = 1;
1.1       root     13159:                }
                   13160:                break;
                   13161:        case 0x01:
                   13162:                REG16(AX) = 0x03;
1.1.1.3   root     13163:                m_CF = 1;
1.1.1.45  root     13164:                break;
                   13165:        default:
                   13166:                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));
                   13167:                REG16(AX) = 0x01;
                   13168:                m_CF = 1;
                   13169:                break;
1.1       root     13170:        }
                   13171: }
                   13172: 
                   13173: inline void msdos_int_21h_6ah()
                   13174: {
                   13175:        REG8(AH) = 0x68;
                   13176:        msdos_int_21h_68h();
                   13177: }
                   13178: 
                   13179: inline void msdos_int_21h_6bh()
                   13180: {
1.1.1.45  root     13181:        REG8(AL) = 0x00;
1.1       root     13182: }
                   13183: 
                   13184: inline void msdos_int_21h_6ch(int lfn)
                   13185: {
1.1.1.45  root     13186:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     13187:        int mode = REG8(BL) & 0x03;
                   13188:        
                   13189:        if(mode < 0x03) {
1.1.1.29  root     13190:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     13191:                        // file exists
                   13192:                        if(REG8(DL) & 1) {
1.1.1.37  root     13193:                                int fd = -1;
                   13194:                                int sio_port = 0;
                   13195:                                int lpt_port = 0;
1.1       root     13196:                                
1.1.1.45  root     13197:                                if(msdos_is_device_path(path)) {
                   13198:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     13199:                                } else {
1.1.1.13  root     13200:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     13201:                                }
1.1       root     13202:                                if(fd != -1) {
                   13203:                                        REG16(AX) = fd;
                   13204:                                        REG16(CX) = 1;
1.1.1.45  root     13205:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13206:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13207:                                } else {
                   13208:                                        REG16(AX) = errno;
1.1.1.3   root     13209:                                        m_CF = 1;
1.1       root     13210:                                }
                   13211:                        } else if(REG8(DL) & 2) {
1.1.1.60  root     13212:                                int attr = GetFileAttributesA(path);
1.1.1.37  root     13213:                                int fd = -1;
                   13214:                                int sio_port = 0;
                   13215:                                int lpt_port = 0;
1.1       root     13216:                                
1.1.1.45  root     13217:                                if(msdos_is_device_path(path)) {
                   13218:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     13219:                                } else {
                   13220:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13221:                                }
                   13222:                                if(fd != -1) {
                   13223:                                        if(attr == -1) {
                   13224:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   13225:                                        }
1.1.1.60  root     13226:                                        SetFileAttributesA(path, attr);
1.1       root     13227:                                        REG16(AX) = fd;
                   13228:                                        REG16(CX) = 3;
1.1.1.45  root     13229:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13230:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13231:                                } else {
                   13232:                                        REG16(AX) = errno;
1.1.1.3   root     13233:                                        m_CF = 1;
1.1       root     13234:                                }
                   13235:                        } else {
                   13236:                                REG16(AX) = 0x50;
1.1.1.3   root     13237:                                m_CF = 1;
1.1       root     13238:                        }
                   13239:                } else {
                   13240:                        // file not exists
                   13241:                        if(REG8(DL) & 0x10) {
                   13242:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13243:                                
                   13244:                                if(fd != -1) {
1.1.1.60  root     13245:                                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     13246:                                        REG16(AX) = fd;
                   13247:                                        REG16(CX) = 2;
                   13248:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     13249:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13250:                                } else {
                   13251:                                        REG16(AX) = errno;
1.1.1.3   root     13252:                                        m_CF = 1;
1.1       root     13253:                                }
                   13254:                        } else {
                   13255:                                REG16(AX) = 0x02;
1.1.1.3   root     13256:                                m_CF = 1;
1.1       root     13257:                        }
                   13258:                }
                   13259:        } else {
                   13260:                REG16(AX) = 0x0c;
1.1.1.3   root     13261:                m_CF = 1;
1.1       root     13262:        }
                   13263: }
                   13264: 
1.1.1.43  root     13265: inline void msdos_int_21h_70h()
                   13266: {
                   13267:        switch(REG8(AL)) {
1.1.1.48  root     13268:        case 0x00: // get ??? info
                   13269:        case 0x01: // set above info
                   13270: //             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));
                   13271:                REG16(AX) = 0x7000;
                   13272:                m_CF = 1;
                   13273:                break;
                   13274:        case 0x02: // set general internationalization info
1.1.1.43  root     13275:                if(REG16(CX) >= 7) {
                   13276:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   13277:                        msdos_nls_tables_update();
                   13278:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   13279:                        REG16(AX) = system_code_page;
                   13280:                } else {
                   13281:                        REG16(AX) = 0x0c;
                   13282:                        m_CF = 1;
                   13283:                }
                   13284:                break;
                   13285:        default:
                   13286:                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     13287:                REG16(AX) = 0x7000;
1.1.1.43  root     13288:                m_CF = 1;
                   13289:                break;
                   13290:        }
                   13291: }
                   13292: 
1.1       root     13293: inline void msdos_int_21h_710dh()
                   13294: {
                   13295:        // reset drive
                   13296: }
                   13297: 
1.1.1.48  root     13298: inline void msdos_int_21h_7141h()
1.1.1.17  root     13299: {
                   13300:        if(REG16(SI) == 0) {
1.1.1.48  root     13301:                msdos_int_21h_41h(1);
1.1.1.17  root     13302:                return;
                   13303:        }
                   13304:        if(REG16(SI) != 1) {
                   13305:                REG16(AX) = 5;
                   13306:                m_CF = 1;
                   13307:        }
                   13308:        /* wild card and matching attributes... */
                   13309:        char tmp[MAX_PATH * 2];
                   13310:        // copy search pathname (and quick check overrun)
                   13311:        ZeroMemory(tmp, sizeof(tmp));
                   13312:        tmp[MAX_PATH - 1] = '\0';
                   13313:        tmp[MAX_PATH] = 1;
                   13314:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   13315:        
                   13316:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   13317:                REG16(AX) = 1;
                   13318:                m_CF = 1;
                   13319:                return;
                   13320:        }
                   13321:        for(char *s = tmp; *s; ++s) {
                   13322:                if(*s == '/') {
                   13323:                        *s = '\\';
                   13324:                }
                   13325:        }
1.1.1.60  root     13326:        char *tmp_name = my_strrchr(tmp, '\\');
1.1.1.17  root     13327:        if(tmp_name) {
                   13328:                ++tmp_name;
                   13329:        } else {
                   13330:                tmp_name = strchr(tmp, ':');
                   13331:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   13332:        }
                   13333:        
                   13334:        WIN32_FIND_DATAA fd;
                   13335:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   13336:        if(fh == INVALID_HANDLE_VALUE) {
                   13337:                REG16(AX) = 2;
                   13338:                m_CF = 1;
                   13339:                return;
                   13340:        }
                   13341:        do {
                   13342:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   13343:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     13344:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     13345:                                REG16(AX) = 5;
                   13346:                                m_CF = 1;
                   13347:                                break;
                   13348:                        }
                   13349:                }
                   13350:        } while(FindNextFileA(fh, &fd));
                   13351:        if(!m_CF) {
                   13352:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   13353:                        m_CF = 1;
                   13354:                        REG16(AX) = 2;
                   13355:                }
                   13356:        }
                   13357:        FindClose(fh);
                   13358: }
                   13359: 
1.1       root     13360: inline void msdos_int_21h_714eh()
                   13361: {
                   13362:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13363:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   13364:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.60  root     13365:        WIN32_FIND_DATAA fd;
1.1       root     13366:        
1.1.1.13  root     13367:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   13368:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13369:                FindClose(dtainfo->find_handle);
                   13370:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13371:        }
                   13372:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     13373:        dtainfo->allowable_mask = REG8(CL);
                   13374:        dtainfo->required_mask = REG8(CH);
                   13375:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     13376:        
1.1.1.14  root     13377:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   13378:                dtainfo->allowable_mask &= ~8;
1.1       root     13379:        }
1.1.1.60  root     13380:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     13381:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13382:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13383:                                FindClose(dtainfo->find_handle);
                   13384:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13385:                                break;
                   13386:                        }
                   13387:                }
                   13388:        }
1.1.1.13  root     13389:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13390:                find->attrib = fd.dwFileAttributes;
                   13391:                msdos_find_file_conv_local_time(&fd);
                   13392:                if(REG16(SI) == 0) {
                   13393:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13394:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13395:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13396:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13397:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13398:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13399:                } else {
                   13400:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13401:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13402:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13403:                }
                   13404:                find->size_hi = fd.nFileSizeHigh;
                   13405:                find->size_lo = fd.nFileSizeLow;
                   13406:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13407:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13408:                REG16(AX) = dtainfo - dtalist + 1;
                   13409:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13410:                // volume label
                   13411:                find->attrib = 8;
                   13412:                find->size_hi = find->size_lo = 0;
                   13413:                strcpy(find->full_name, process->volume_label);
                   13414:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13415:                dtainfo->allowable_mask &= ~8;
                   13416:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     13417:        } else {
                   13418:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     13419:                m_CF = 1;
1.1       root     13420:        }
                   13421: }
                   13422: 
                   13423: inline void msdos_int_21h_714fh()
                   13424: {
                   13425:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13426:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1.1.60  root     13427:        WIN32_FIND_DATAA fd;
1.1       root     13428:        
1.1.1.14  root     13429:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13430:                REG16(AX) = 6;
1.1.1.13  root     13431:                m_CF = 1;
                   13432:                return;
                   13433:        }
1.1.1.14  root     13434:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13435:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     13436:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     13437:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13438:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13439:                                        FindClose(dtainfo->find_handle);
                   13440:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13441:                                        break;
                   13442:                                }
                   13443:                        }
                   13444:                } else {
1.1.1.13  root     13445:                        FindClose(dtainfo->find_handle);
                   13446:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13447:                }
                   13448:        }
1.1.1.13  root     13449:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13450:                find->attrib = fd.dwFileAttributes;
                   13451:                msdos_find_file_conv_local_time(&fd);
                   13452:                if(REG16(SI) == 0) {
                   13453:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13454:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13455:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13456:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13457:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13458:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13459:                } else {
                   13460:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13461:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13462:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13463:                }
                   13464:                find->size_hi = fd.nFileSizeHigh;
                   13465:                find->size_lo = fd.nFileSizeLow;
                   13466:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13467:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13468:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13469:                // volume label
                   13470:                find->attrib = 8;
                   13471:                find->size_hi = find->size_lo = 0;
                   13472:                strcpy(find->full_name, process->volume_label);
                   13473:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13474:                dtainfo->allowable_mask &= ~8;
1.1       root     13475:        } else {
                   13476:                REG16(AX) = 0x12;
1.1.1.3   root     13477:                m_CF = 1;
1.1       root     13478:        }
                   13479: }
                   13480: 
                   13481: inline void msdos_int_21h_71a0h()
                   13482: {
                   13483:        DWORD max_component_len, file_sys_flag;
                   13484:        
1.1.1.60  root     13485:        if(GetVolumeInformationA((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))) {
1.1.1.14  root     13486:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   13487:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     13488:                REG16(CX) = (UINT16)max_component_len;          // 255
                   13489:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   13490:        } else {
                   13491:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13492:                m_CF = 1;
1.1       root     13493:        }
                   13494: }
                   13495: 
                   13496: inline void msdos_int_21h_71a1h()
                   13497: {
1.1.1.14  root     13498:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13499:                REG16(AX) = 6;
1.1.1.13  root     13500:                m_CF = 1;
                   13501:                return;
                   13502:        }
1.1.1.14  root     13503:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13504:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13505:                FindClose(dtainfo->find_handle);
                   13506:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13507:        }
                   13508: }
                   13509: 
                   13510: inline void msdos_int_21h_71a6h()
                   13511: {
                   13512:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13513:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13514:        
1.1.1.3   root     13515:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13516:        struct _stat64 status;
                   13517:        DWORD serial_number = 0;
                   13518:        
1.1.1.20  root     13519:        if(fd < process->max_files && file_handler[fd].valid) {
                   13520:                if(_fstat64(fd, &status) == 0) {
                   13521:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13522:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13523:                                char volume[] = "A:\\";
1.1.1.20  root     13524:                                volume[0] = file_handler[fd].path[1];
1.1.1.60  root     13525:                                GetVolumeInformationA(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
1.1       root     13526:                        }
1.1.1.60  root     13527:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributesA(file_handler[fd].path);
1.1       root     13528:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13529:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13530:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13531:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13532:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13533:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13534:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13535:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13536:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13537:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13538:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13539:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13540:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13541:                } else {
                   13542:                        REG16(AX) = errno;
1.1.1.3   root     13543:                        m_CF = 1;
1.1       root     13544:                }
                   13545:        } else {
                   13546:                REG16(AX) = 0x06;
1.1.1.3   root     13547:                m_CF = 1;
1.1       root     13548:        }
                   13549: }
                   13550: 
                   13551: inline void msdos_int_21h_71a7h()
                   13552: {
                   13553:        switch(REG8(BL)) {
                   13554:        case 0x00:
1.1.1.3   root     13555:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13556:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13557:                        m_CF = 1;
1.1       root     13558:                }
                   13559:                break;
                   13560:        case 0x01:
                   13561:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13562:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13563:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13564:                        m_CF = 1;
1.1       root     13565:                }
                   13566:                break;
                   13567:        default:
1.1.1.22  root     13568:                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     13569:                REG16(AX) = 0x7100;
1.1.1.3   root     13570:                m_CF = 1;
1.1       root     13571:                break;
                   13572:        }
                   13573: }
                   13574: 
                   13575: inline void msdos_int_21h_71a8h()
                   13576: {
                   13577:        if(REG8(DH) == 0) {
                   13578:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13579:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13580:                memset(fcb, 0x20, sizeof(fcb));
                   13581:                int len = strlen(tmp);
1.1.1.21  root     13582:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13583:                        if(tmp[i] == '.') {
                   13584:                                pos = 8;
                   13585:                        } else {
                   13586:                                if(msdos_lead_byte_check(tmp[i])) {
                   13587:                                        fcb[pos++] = tmp[i++];
                   13588:                                }
                   13589:                                fcb[pos++] = tmp[i];
                   13590:                        }
                   13591:                }
1.1.1.3   root     13592:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13593:        } else {
1.1.1.3   root     13594:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13595:        }
                   13596: }
                   13597: 
1.1.1.22  root     13598: inline void msdos_int_21h_71aah()
                   13599: {
                   13600:        char drv[] = "A:", path[MAX_PATH];
                   13601:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13602:        
                   13603:        if(REG8(BL) == 0) {
                   13604:                drv[0] = 'A' + _getdrive() - 1;
                   13605:        } else {
                   13606:                drv[0] = 'A' + REG8(BL) - 1;
                   13607:        }
                   13608:        switch(REG8(BH)) {
                   13609:        case 0x00:
1.1.1.44  root     13610:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13611:                        REG16(AX) = 0x0f; // invalid drive
                   13612:                        m_CF = 1;
1.1.1.60  root     13613:                } else if(DefineDosDeviceA(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
1.1.1.44  root     13614:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13615:                        m_CF = 1;
                   13616:                }
                   13617:                break;
                   13618:        case 0x01:
1.1.1.44  root     13619:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13620:                        REG16(AX) = 0x0f; // invalid drive
                   13621:                        m_CF = 1;
1.1.1.60  root     13622:                } else if(DefineDosDeviceA(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13623:                        REG16(AX) = 0x0f; // invalid drive
                   13624:                        m_CF = 1;
                   13625:                }
                   13626:                break;
                   13627:        case 0x02:
1.1.1.44  root     13628:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13629:                        REG16(AX) = 0x0f; // invalid drive
                   13630:                        m_CF = 1;
1.1.1.60  root     13631:                } else if(QueryDosDeviceA(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13632:                        REG16(AX) = 0x0f; // invalid drive
                   13633:                        m_CF = 1;
                   13634:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13635:                        REG16(AX) = 0x0f; // invalid drive
                   13636:                        m_CF = 1;
                   13637:                } else {
                   13638:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13639:                }
                   13640:                break;
                   13641:        default:
                   13642:                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     13643:                REG16(AX) = 0x7100;
1.1.1.22  root     13644:                m_CF = 1;
                   13645:                break;
                   13646:        }
                   13647: }
                   13648: 
1.1.1.14  root     13649: inline void msdos_int_21h_7300h()
                   13650: {
1.1.1.44  root     13651:        REG8(AL) = REG8(CL);
                   13652:        REG8(AH) = 0;
1.1.1.14  root     13653: }
                   13654: 
                   13655: inline void msdos_int_21h_7302h()
                   13656: {
                   13657:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13658:        UINT16 seg, ofs;
                   13659:        
                   13660:        if(REG16(CX) < 0x3f) {
                   13661:                REG8(AL) = 0x18;
                   13662:                m_CF = 1;
                   13663:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13664:                REG8(AL) = 0xff;
                   13665:                m_CF = 1;
                   13666:        } else {
                   13667:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13668:        }
                   13669: }
                   13670: 
1.1       root     13671: inline void msdos_int_21h_7303h()
                   13672: {
1.1.1.3   root     13673:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13674:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13675:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13676:        
1.1.1.60  root     13677:        if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1       root     13678:                info->size_of_structure = sizeof(ext_space_info_t);
                   13679:                info->structure_version = 0;
                   13680:                info->sectors_per_cluster = sectors_per_cluster;
                   13681:                info->bytes_per_sector = bytes_per_sector;
                   13682:                info->available_clusters_on_drive = free_clusters;
                   13683:                info->total_clusters_on_drive = total_clusters;
                   13684:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13685:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13686:                info->available_allocation_units = free_clusters;       // ???
                   13687:                info->total_allocation_units = total_clusters;          // ???
                   13688:        } else {
                   13689:                REG16(AX) = errno;
1.1.1.3   root     13690:                m_CF = 1;
1.1       root     13691:        }
                   13692: }
                   13693: 
1.1.1.30  root     13694: inline void msdos_int_21h_dbh()
                   13695: {
                   13696:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13697:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13698:        REG8(AL) = dos_info->last_drive;
                   13699: }
                   13700: 
                   13701: inline void msdos_int_21h_dch()
                   13702: {
                   13703:        // Novell NetWare - Connection Services - Get Connection Number
                   13704:        REG8(AL) = 0x00;
                   13705: }
                   13706: 
1.1.1.32  root     13707: inline void msdos_int_24h()
                   13708: {
                   13709:        const char *message = NULL;
                   13710:        int key = 0;
                   13711:        
                   13712:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13713:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13714:                        if(active_code_page == 932) {
                   13715:                                message = critical_error_table[i].message_japanese;
                   13716:                        }
                   13717:                        if(message == NULL) {
                   13718:                                message = critical_error_table[i].message_english;
                   13719:                        }
                   13720:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13721:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13722:                        
                   13723:                        SREG(ES) = WORK_TOP >> 4;
                   13724:                        i386_load_segment_descriptor(ES);
                   13725:                        REG16(DI) = 0x0000;
                   13726:                        break;
                   13727:                }
                   13728:        }
                   13729:        fprintf(stderr, "\n%s", message);
                   13730:        if(!(REG8(AH) & 0x80)) {
                   13731:                if(REG8(AH) & 0x01) {
                   13732:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13733:                } else {
                   13734:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13735:                }
                   13736:        }
                   13737:        fprintf(stderr, "\n");
                   13738:        
1.1.1.33  root     13739:        {
1.1.1.32  root     13740:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13741:        }
1.1.1.32  root     13742:        if(REG8(AH) & 0x10) {
                   13743:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13744:        }
                   13745:        if(REG8(AH) & 0x20) {
                   13746:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13747:        }
                   13748:        if(REG8(AH) & 0x08) {
                   13749:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13750:        }
                   13751:        fprintf(stderr, "? ");
                   13752:        
                   13753:        while(1) {
                   13754:                while(!_kbhit()) {
                   13755:                        Sleep(10);
                   13756:                }
                   13757:                key = _getch();
                   13758:                
                   13759:                if(key == 'I' || key == 'i') {
                   13760:                        if(REG8(AH) & 0x20) {
                   13761:                                REG8(AL) = 0;
                   13762:                                break;
                   13763:                        }
                   13764:                } else if(key == 'R' || key == 'r') {
                   13765:                        if(REG8(AH) & 0x10) {
                   13766:                                REG8(AL) = 1;
                   13767:                                break;
                   13768:                        }
                   13769:                } else if(key == 'A' || key == 'a') {
                   13770:                        REG8(AL) = 2;
                   13771:                        break;
                   13772:                } else if(key == 'F' || key == 'f') {
                   13773:                        if(REG8(AH) & 0x08) {
                   13774:                                REG8(AL) = 3;
                   13775:                                break;
                   13776:                        }
                   13777:                }
                   13778:        }
                   13779:        fprintf(stderr, "%c\n", key);
                   13780: }
                   13781: 
1.1       root     13782: inline void msdos_int_25h()
                   13783: {
                   13784:        UINT16 seg, ofs;
                   13785:        DWORD dwSize;
                   13786:        
1.1.1.3   root     13787: #if defined(HAS_I386)
                   13788:        I386OP(pushf)();
                   13789: #else
                   13790:        PREFIX86(_pushf());
                   13791: #endif
1.1       root     13792:        
                   13793:        if(!(REG8(AL) < 26)) {
                   13794:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13795:                m_CF = 1;
1.1       root     13796:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13797:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13798:                m_CF = 1;
1.1       root     13799:        } else {
                   13800:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13801:                char dev[64];
                   13802:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13803:                
1.1.1.60  root     13804:                HANDLE hFile = CreateFileA(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1       root     13805:                if(hFile == INVALID_HANDLE_VALUE) {
                   13806:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13807:                        m_CF = 1;
1.1       root     13808:                } else {
1.1.1.19  root     13809:                        UINT32 top_sector  = REG16(DX);
                   13810:                        UINT16 sector_num  = REG16(CX);
                   13811:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13812:                        
                   13813:                        if(sector_num == 0xffff) {
                   13814:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13815:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13816:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13817:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13818:                                buffer_addr = (seg << 4) + ofs;
                   13819:                        }
                   13820: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13821: //                             REG8(AL) = 0x02; // drive not ready
                   13822: //                             m_CF = 1;
                   13823: //                     } else 
                   13824:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13825:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13826:                                m_CF = 1;
1.1.1.19  root     13827:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13828:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13829:                                m_CF = 1;
1.1       root     13830:                        }
                   13831:                        CloseHandle(hFile);
                   13832:                }
                   13833:        }
                   13834: }
                   13835: 
                   13836: inline void msdos_int_26h()
                   13837: {
1.1.1.42  root     13838:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13839:        UINT16 seg, ofs;
                   13840:        DWORD dwSize;
                   13841:        
1.1.1.3   root     13842: #if defined(HAS_I386)
                   13843:        I386OP(pushf)();
                   13844: #else
                   13845:        PREFIX86(_pushf());
                   13846: #endif
1.1       root     13847:        
                   13848:        if(!(REG8(AL) < 26)) {
                   13849:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13850:                m_CF = 1;
1.1       root     13851:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13852:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13853:                m_CF = 1;
1.1       root     13854:        } else {
                   13855:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13856:                char dev[64];
                   13857:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13858:                
                   13859:                if(dpb->media_type == 0xf8) {
                   13860:                        // this drive is not a floppy
1.1.1.6   root     13861: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13862: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13863: //                     }
1.1       root     13864:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13865:                        m_CF = 1;
1.1       root     13866:                } else {
1.1.1.60  root     13867:                        HANDLE hFile = CreateFileA(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
1.1       root     13868:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13869:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13870:                                m_CF = 1;
1.1       root     13871:                        } else {
1.1.1.19  root     13872:                                UINT32 top_sector  = REG16(DX);
                   13873:                                UINT16 sector_num  = REG16(CX);
                   13874:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13875:                                
                   13876:                                if(sector_num == 0xffff) {
                   13877:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13878:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13879:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13880:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13881:                                        buffer_addr = (seg << 4) + ofs;
                   13882:                                }
1.1       root     13883:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13884:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13885:                                        m_CF = 1;
1.1.1.19  root     13886:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13887:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13888:                                        m_CF = 1;
1.1.1.19  root     13889:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13890:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13891:                                        m_CF = 1;
1.1       root     13892:                                }
                   13893:                                CloseHandle(hFile);
                   13894:                        }
                   13895:                }
                   13896:        }
                   13897: }
                   13898: 
                   13899: inline void msdos_int_27h()
                   13900: {
1.1.1.29  root     13901:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13902:        try {
                   13903:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13904:        } catch(...) {
                   13905:                // recover the broken mcb
                   13906:                int mcb_seg = SREG(CS) - 1;
                   13907:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13908:                
1.1.1.29  root     13909:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13910:                        mcb->mz = 'M';
                   13911:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13912:                        
1.1.1.29  root     13913:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13914:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13915:                        } else {
1.1.1.39  root     13916:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13917:                        }
                   13918:                } else {
                   13919:                        mcb->mz = 'Z';
1.1.1.30  root     13920:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13921:                }
                   13922:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13923:        }
1.1.1.3   root     13924:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13925: }
                   13926: 
                   13927: inline void msdos_int_29h()
                   13928: {
1.1.1.50  root     13929:        msdos_putch_fast(REG8(AL));
1.1       root     13930: }
                   13931: 
                   13932: inline void msdos_int_2eh()
                   13933: {
                   13934:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13935:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13936:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13937:        char *token = my_strtok(tmp, " ");
                   13938:        strcpy(command, token);
                   13939:        strcpy(opt, token + strlen(token) + 1);
                   13940:        
                   13941:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13942:        param->env_seg = 0;
                   13943:        param->cmd_line.w.l = 44;
                   13944:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13945:        param->fcb1.w.l = 24;
                   13946:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13947:        param->fcb2.w.l = 24;
                   13948:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13949:        
                   13950:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13951:        
                   13952:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13953:        cmd_line->len = strlen(opt);
                   13954:        strcpy(cmd_line->cmd, opt);
                   13955:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13956:        
1.1.1.28  root     13957:        try {
                   13958:                if(msdos_process_exec(command, param, 0)) {
                   13959:                        REG16(AX) = 0xffff; // error before processing command
                   13960:                } else {
                   13961:                        // set flag to set retval to ax when the started process is terminated
                   13962:                        process_t *process = msdos_process_info_get(current_psp);
                   13963:                        process->called_by_int2eh = true;
                   13964:                }
                   13965:        } catch(...) {
                   13966:                REG16(AX) = 0xffff; // error before processing command
                   13967:        }
1.1       root     13968: }
                   13969: 
1.1.1.29  root     13970: inline void msdos_int_2fh_05h()
                   13971: {
                   13972:        switch(REG8(AL)) {
                   13973:        case 0x00:
1.1.1.49  root     13974:                // critical error handler is installed
1.1.1.32  root     13975:                REG8(AL) = 0xff;
                   13976:                break;
                   13977:        case 0x01:
                   13978:        case 0x02:
                   13979:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13980:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13981:                                const char *message = NULL;
                   13982:                                if(active_code_page == 932) {
                   13983:                                        message = standard_error_table[i].message_japanese;
                   13984:                                }
                   13985:                                if(message == NULL) {
                   13986:                                        message = standard_error_table[i].message_english;
                   13987:                                }
                   13988:                                strcpy((char *)(mem + WORK_TOP), message);
                   13989:                                
                   13990:                                SREG(ES) = WORK_TOP >> 4;
                   13991:                                i386_load_segment_descriptor(ES);
                   13992:                                REG16(DI) = 0x0000;
                   13993:                                REG8(AL) = 0x01;
                   13994:                                break;
                   13995:                        }
                   13996:                }
1.1.1.29  root     13997:                break;
                   13998:        default:
                   13999:                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     14000:                REG16(AX) = 0x01;
1.1.1.29  root     14001:                m_CF = 1;
                   14002:        }
                   14003: }
                   14004: 
1.1.1.44  root     14005: inline void msdos_int_2fh_06h()
                   14006: {
                   14007:        switch(REG8(AL)) {
                   14008:        case 0x00:
                   14009:                // ASSIGN is not installed
1.1.1.49  root     14010: //             REG8(AL) = 0x00;
1.1.1.44  root     14011:                break;
                   14012:        case 0x01:
                   14013:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   14014:                REG16(AX) = 0x01;
                   14015:                m_CF = 1;
                   14016:                break;
                   14017:        default:
                   14018:                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));
                   14019:                REG16(AX) = 0x01;
                   14020:                m_CF = 1;
                   14021:                break;
                   14022:        }
                   14023: }
                   14024: 
1.1.1.22  root     14025: inline void msdos_int_2fh_11h()
                   14026: {
                   14027:        switch(REG8(AL)) {
                   14028:        case 0x00:
1.1.1.29  root     14029:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     14030: #ifdef SUPPORT_MSCDEX
                   14031:                        // MSCDEX is installed
                   14032:                        REG8(AL) = 0xff;
                   14033:                        i386_write_stack(0xadad);
                   14034: #else
1.1.1.29  root     14035:                        // MSCDEX is not installed
                   14036: //                     REG8(AL) = 0x00;
1.1.1.53  root     14037: #endif
1.1.1.29  root     14038:                } else {
                   14039:                        // Network Redirector is not installed
                   14040: //                     REG8(AL) = 0x00;
                   14041:                }
1.1.1.22  root     14042:                break;
                   14043:        default:
1.1.1.43  root     14044: //             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     14045:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     14046:                m_CF = 1;
                   14047:                break;
                   14048:        }
                   14049: }
                   14050: 
1.1.1.21  root     14051: inline void msdos_int_2fh_12h()
                   14052: {
                   14053:        switch(REG8(AL)) {
1.1.1.22  root     14054:        case 0x00:
1.1.1.29  root     14055:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     14056:                REG8(AL) = 0xff;
                   14057:                break;
1.1.1.29  root     14058: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   14059:        case 0x02:
                   14060:                {
                   14061:                        UINT16 stack = i386_read_stack();
                   14062:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   14063:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   14064:                        i386_load_segment_descriptor(ES);
                   14065:                }
                   14066:                break;
1.1.1.30  root     14067:        case 0x03:
                   14068:                SREG(DS) = (DEVICE_TOP >> 4);
                   14069:                i386_load_segment_descriptor(DS);
                   14070:                break;
1.1.1.29  root     14071:        case 0x04:
                   14072:                {
                   14073:                        UINT16 stack = i386_read_stack();
                   14074:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   14075: #if defined(HAS_I386)
                   14076:                        m_ZF = (REG8(AL) == '\\');
                   14077: #else
                   14078:                        m_ZeroVal = (REG8(AL) != '\\');
                   14079: #endif
                   14080:                }
                   14081:                break;
                   14082:        case 0x05:
1.1.1.49  root     14083:                {
                   14084:                        UINT16 c = i386_read_stack();
                   14085:                        if((c >> 0) & 0xff) {
                   14086:                                msdos_putch((c >> 0) & 0xff);
                   14087:                        }
                   14088:                        if((c >> 8) & 0xff) {
                   14089:                                msdos_putch((c >> 8) & 0xff);
                   14090:                        }
                   14091:                }
1.1.1.29  root     14092:                break;
1.1.1.49  root     14093: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     14094: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   14095: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     14096: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     14097: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   14098: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   14099: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   14100:        case 0x0d:
                   14101:                {
                   14102:                        SYSTEMTIME time;
                   14103:                        FILETIME file_time;
                   14104:                        WORD dos_date, dos_time;
                   14105:                        GetLocalTime(&time);
                   14106:                        SystemTimeToFileTime(&time, &file_time);
                   14107:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   14108:                        REG16(AX) = dos_date;
                   14109:                        REG16(DX) = dos_time;
                   14110:                }
                   14111:                break;
                   14112: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   14113: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   14114: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   14115:        case 0x11:
                   14116:                {
                   14117:                        char path[MAX_PATH], *p;
                   14118:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14119:                        my_strupr(path);
                   14120:                        while((p = my_strchr(path, '/')) != NULL) {
                   14121:                                *p = '\\';
                   14122:                        }
                   14123:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   14124:                }
                   14125:                break;
                   14126:        case 0x12:
                   14127:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14128:                break;
                   14129:        case 0x13:
                   14130:                {
                   14131:                        char tmp[2] = {0};
                   14132:                        tmp[0] = i386_read_stack();
                   14133:                        my_strupr(tmp);
                   14134:                        REG8(AL) = tmp[0];
                   14135:                }
                   14136:                break;
                   14137:        case 0x14:
                   14138: #if defined(HAS_I386)
                   14139:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   14140: #else
                   14141:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14142: #endif
                   14143:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14144:                break;
                   14145: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     14146:        case 0x16:
                   14147:                if(REG16(BX) < 20) {
                   14148:                        SREG(ES) = SFT_TOP >> 4;
                   14149:                        i386_load_segment_descriptor(ES);
                   14150:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   14151:                        
                   14152:                        // update system file table
                   14153:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   14154:                        if(file_handler[REG16(BX)].valid) {
                   14155:                                int count = 0;
                   14156:                                for(int i = 0; i < 20; i++) {
                   14157:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   14158:                                                count++;
                   14159:                                        }
                   14160:                                }
                   14161:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   14162:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   14163:                                _lseek(REG16(BX), 0, SEEK_END);
                   14164:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   14165:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   14166:                        } else {
                   14167:                                memset(sft, 0, 0x3b);
                   14168:                        }
                   14169:                } else {
                   14170:                        REG16(AX) = 0x06;
                   14171:                        m_CF = 1;
                   14172:                }
                   14173:                break;
1.1.1.49  root     14174:        case 0x17:
                   14175:                {
                   14176:                        UINT16 drive = i386_read_stack();
                   14177:                        if(msdos_is_valid_drive(drive)) {
                   14178:                                msdos_cds_update(drive);
                   14179:                        }
                   14180:                        REG16(SI) = 88 * drive;
                   14181:                        SREG(DS) = (CDS_TOP >> 4);
                   14182:                        i386_load_segment_descriptor(DS);
                   14183:                }
                   14184:                break;
1.1.1.29  root     14185: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   14186: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   14187:        case 0x1a:
                   14188:                {
                   14189:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   14190:                        if(path[1] == ':') {
                   14191:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   14192:                                        REG8(AL) = path[0] - 'a' + 1;
                   14193:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   14194:                                        REG8(AL) = path[0] - 'A' + 1;
                   14195:                                } else {
                   14196:                                        REG8(AL) = 0xff; // invalid
                   14197:                                }
                   14198:                                strcpy(full, path);
                   14199:                                strcpy(path, full + 2);
1.1.1.60  root     14200:                        } else if(GetFullPathNameA(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
1.1.1.29  root     14201:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   14202:                                        REG8(AL) = full[0] - 'a' + 1;
                   14203:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   14204:                                        REG8(AL) = full[0] - 'A' + 1;
                   14205:                                } else {
                   14206:                                        REG8(AL) = 0xff; // invalid
                   14207:                                }
                   14208:                        } else {
                   14209:                                REG8(AL) = 0x00; // default
                   14210:                        }
                   14211:                }
                   14212:                break;
                   14213:        case 0x1b:
                   14214:                {
                   14215:                        int year = REG16(CX) + 1980;
                   14216:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   14217:                }
                   14218:                break;
                   14219: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   14220: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   14221:        case 0x1e:
                   14222:                {
                   14223:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   14224:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
1.1.1.60  root     14225:                        if(GetFullPathNameA(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathNameA(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
1.1.1.29  root     14226: #if defined(HAS_I386)
                   14227:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   14228: #else
                   14229:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   14230: #endif
                   14231:                        } else {
                   14232: #if defined(HAS_I386)
                   14233:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   14234: #else
                   14235:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   14236: #endif
                   14237:                        }
                   14238:                }
                   14239:                break;
1.1.1.49  root     14240:        case 0x1f:
                   14241:                {
                   14242:                        UINT16 drive = i386_read_stack();
                   14243:                        if(msdos_is_valid_drive(drive)) {
                   14244:                                msdos_cds_update(drive);
                   14245:                        }
                   14246:                        REG16(SI) = 88 * drive;
                   14247:                        SREG(ES) = (CDS_TOP >> 4);
                   14248:                        i386_load_segment_descriptor(ES);
                   14249:                }
                   14250:                break;
1.1.1.21  root     14251:        case 0x20:
                   14252:                {
                   14253:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   14254:                        
                   14255:                        if(fd < 20) {
                   14256:                                SREG(ES) = current_psp;
                   14257:                                i386_load_segment_descriptor(ES);
                   14258:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   14259:                        } else {
                   14260:                                REG16(AX) = 0x06;
                   14261:                                m_CF = 1;
                   14262:                        }
                   14263:                }
                   14264:                break;
1.1.1.29  root     14265:        case 0x21:
                   14266:                msdos_int_21h_60h(0);
                   14267:                break;
1.1.1.49  root     14268:        case 0x22:
                   14269:                {
                   14270:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14271:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   14272:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   14273:                        }
                   14274:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   14275:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   14276:                        }
                   14277:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   14278:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   14279:                        }
                   14280:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   14281:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   14282:                        }
                   14283:                }
                   14284:                break;
1.1.1.29  root     14285: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   14286: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   14287:        case 0x25:
                   14288:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14289:                break;
                   14290:        case 0x26:
                   14291:                REG8(AL) = REG8(CL);
                   14292:                msdos_int_21h_3dh();
                   14293:                break;
                   14294:        case 0x27:
                   14295:                msdos_int_21h_3eh();
                   14296:                break;
                   14297:        case 0x28:
                   14298:                REG16(AX) = REG16(BP);
                   14299:                msdos_int_21h_42h();
                   14300:                break;
                   14301:        case 0x29:
                   14302:                msdos_int_21h_3fh();
                   14303:                break;
                   14304: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   14305:        case 0x2b:
                   14306:                REG16(AX) = REG16(BP);
                   14307:                msdos_int_21h_44h();
                   14308:                break;
                   14309:        case 0x2c:
                   14310:                REG16(BX) = DEVICE_TOP >> 4;
                   14311:                REG16(AX) = 22;
                   14312:                break;
                   14313:        case 0x2d:
                   14314:                {
                   14315:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14316:                        REG16(AX) = sda->extended_error_code;
                   14317:                }
                   14318:                break;
                   14319:        case 0x2e:
                   14320:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     14321:                        SREG(ES) = 0x0001;
                   14322:                        i386_load_segment_descriptor(ES);
                   14323:                        REG16(DI) = 0x00;
                   14324:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     14325:                        // dummy parameter error message read routine is at fffc:0010
                   14326:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     14327:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     14328:                        REG16(DI) = 0x0010;
1.1.1.22  root     14329:                }
                   14330:                break;
1.1.1.29  root     14331:        case 0x2f:
                   14332:                if(REG16(DX) != 0) {
1.1.1.30  root     14333:                        dos_major_version = REG8(DL);
                   14334:                        dos_minor_version = REG8(DH);
1.1.1.29  root     14335:                } else {
                   14336:                        REG8(DL) = 7;
                   14337:                        REG8(DH) = 10;
                   14338:                }
                   14339:                break;
                   14340: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   14341: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     14342:        default:
                   14343:                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));
                   14344:                REG16(AX) = 0x01;
                   14345:                m_CF = 1;
                   14346:                break;
                   14347:        }
                   14348: }
                   14349: 
1.1.1.30  root     14350: inline void msdos_int_2fh_13h()
                   14351: {
                   14352:        static UINT16 prevDS = 0, prevDX = 0;
                   14353:        static UINT16 prevES = 0, prevBX = 0;
                   14354:        UINT16 tmp;
                   14355:        
                   14356:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   14357:        i386_load_segment_descriptor(DS);
                   14358:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   14359:        
                   14360:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   14361:        i386_load_segment_descriptor(ES);
                   14362:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   14363: }
                   14364: 
1.1.1.22  root     14365: inline void msdos_int_2fh_14h()
                   14366: {
                   14367:        switch(REG8(AL)) {
                   14368:        case 0x00:
1.1.1.29  root     14369:                // NLSFUNC.COM is installed
                   14370:                REG8(AL) = 0xff;
1.1.1.25  root     14371:                break;
                   14372:        case 0x01:
                   14373:        case 0x03:
                   14374:                REG8(AL) = 0x00;
                   14375:                active_code_page = REG16(BX);
                   14376:                msdos_nls_tables_update();
                   14377:                break;
                   14378:        case 0x02:
                   14379:                REG8(AL) = get_extended_country_info(REG16(BP));
                   14380:                break;
                   14381:        case 0x04:
1.1.1.42  root     14382:                for(int i = 0;; i++) {
                   14383:                        if(country_table[i].code == REG16(DX)) {
                   14384:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   14385:                                break;
                   14386:                        } else if(country_table[i].code == -1) {
                   14387:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14388:                                break;
                   14389:                        }
                   14390:                }
1.1.1.25  root     14391:                REG8(AL) = 0x00;
1.1.1.22  root     14392:                break;
                   14393:        default:
                   14394:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14395:                REG16(AX) = 0x01;
                   14396:                m_CF = 1;
                   14397:                break;
                   14398:        }
                   14399: }
                   14400: 
                   14401: inline void msdos_int_2fh_15h()
                   14402: {
                   14403:        switch(REG8(AL)) {
1.1.1.29  root     14404:        case 0x00: // CD-ROM - Installation Check
                   14405:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     14406: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     14407:                        // MSCDEX is installed
                   14408:                        REG16(BX) = 0;
                   14409:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     14410:                                if(msdos_is_cdrom_drive(i)) {
                   14411:                                        if(REG16(BX) == 0) {
                   14412:                                                REG16(CX) = i;
1.1.1.43  root     14413:                                        }
1.1.1.44  root     14414:                                        REG16(BX)++;
1.1.1.43  root     14415:                                }
                   14416:                        }
                   14417: #else
1.1.1.29  root     14418:                        // MSCDEX is not installed
                   14419: //                     REG8(AL) = 0x00;
1.1.1.43  root     14420: #endif
1.1.1.29  root     14421:                } else {
                   14422:                        // GRAPHICS.COM is not installed
                   14423: //                     REG8(AL) = 0x00;
                   14424:                }
1.1.1.22  root     14425:                break;
1.1.1.43  root     14426:        case 0x0b:
1.1.1.44  root     14427:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   14428:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   14429:                REG16(BX) = 0xadad;
1.1.1.43  root     14430:                break;
                   14431:        case 0x0d:
1.1.1.44  root     14432:                for(int i = 0, n = 0; i < 26; i++) {
                   14433:                        if(msdos_is_cdrom_drive(i)) {
                   14434:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     14435:                        }
                   14436:                }
                   14437:                break;
1.1.1.22  root     14438:        case 0xff:
1.1.1.29  root     14439:                if(REG16(BX) == 0x0000) {
                   14440:                        // CORELCDX is not installed
                   14441:                } else {
                   14442:                        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));
                   14443:                        REG16(AX) = 0x01;
                   14444:                        m_CF = 1;
                   14445:                }
1.1.1.22  root     14446:                break;
1.1.1.21  root     14447:        default:
1.1.1.22  root     14448:                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     14449:                REG16(AX) = 0x01;
                   14450:                m_CF = 1;
                   14451:                break;
                   14452:        }
                   14453: }
                   14454: 
1.1       root     14455: inline void msdos_int_2fh_16h()
                   14456: {
                   14457:        switch(REG8(AL)) {
                   14458:        case 0x00:
1.1.1.14  root     14459:                if(no_windows) {
1.1.1.29  root     14460:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   14461: //                     REG8(AL) = 0x00;
1.1.1.14  root     14462:                } else {
1.1.1.30  root     14463:                        REG8(AL) = win_major_version;
                   14464:                        REG8(AH) = win_minor_version;
1.1       root     14465:                }
                   14466:                break;
1.1.1.43  root     14467:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     14468:                // from DOSBox
                   14469:                i386_set_a20_line(1);
                   14470:                break;
1.1.1.49  root     14471:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     14472:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   14473:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   14474:                break;
                   14475:        case 0x07:
                   14476:                // Virtual Device Call API
                   14477:                break;
1.1.1.22  root     14478:        case 0x0a:
                   14479:                if(!no_windows) {
                   14480:                        REG16(AX) = 0x0000;
1.1.1.30  root     14481:                        REG8(BH) = win_major_version;
                   14482:                        REG8(BL) = win_minor_version;
1.1.1.49  root     14483: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     14484:                        REG16(CX) = 0x0003; // enhanced
                   14485:                }
                   14486:                break;
1.1.1.30  root     14487:        case 0x0b:
                   14488:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     14489:        case 0x0e:
                   14490:        case 0x0f:
1.1.1.30  root     14491:        case 0x10:
1.1.1.22  root     14492:        case 0x11:
                   14493:        case 0x12:
                   14494:        case 0x13:
                   14495:        case 0x14:
1.1.1.30  root     14496:        case 0x15:
1.1.1.43  root     14497:        case 0x81:
                   14498:        case 0x82:
1.1.1.44  root     14499:        case 0x84:
1.1.1.49  root     14500:        case 0x85:
1.1.1.33  root     14501:        case 0x86:
1.1.1.22  root     14502:        case 0x87:
1.1.1.30  root     14503:        case 0x89:
1.1.1.33  root     14504:        case 0x8a:
1.1.1.22  root     14505:                // function not supported, do not clear AX
                   14506:                break;
1.1.1.14  root     14507:        case 0x80:
                   14508:                Sleep(10);
1.1.1.35  root     14509:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     14510:                REG8(AL) = 0x00;
1.1.1.14  root     14511:                break;
1.1.1.33  root     14512:        case 0x83:
                   14513:                REG16(BX) = 0x01; // system vm id
                   14514:                break;
1.1.1.22  root     14515:        case 0x8e:
                   14516:                REG16(AX) = 0x00; // failed
                   14517:                break;
1.1.1.20  root     14518:        case 0x8f:
                   14519:                switch(REG8(DH)) {
                   14520:                case 0x01:
1.1.1.49  root     14521: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14522: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14523:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14524:                        break;
                   14525:                default:
                   14526:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14527:                        break;
                   14528:                }
                   14529:                break;
1.1       root     14530:        default:
1.1.1.22  root     14531:                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));
                   14532:                REG16(AX) = 0x01;
                   14533:                m_CF = 1;
                   14534:                break;
                   14535:        }
                   14536: }
                   14537: 
                   14538: inline void msdos_int_2fh_19h()
                   14539: {
                   14540:        switch(REG8(AL)) {
                   14541:        case 0x00:
1.1.1.29  root     14542:                // SHELLB.COM is not installed
                   14543: //             REG8(AL) = 0x00;
1.1.1.22  root     14544:                break;
                   14545:        case 0x01:
                   14546:        case 0x02:
                   14547:        case 0x03:
                   14548:        case 0x04:
                   14549:                REG16(AX) = 0x01;
                   14550:                m_CF = 1;
                   14551:                break;
1.1.1.29  root     14552:        case 0x80:
                   14553:                // IBM ROM-DOS v4.0 is not installed
                   14554: //             REG8(AL) = 0x00;
                   14555:                break;
1.1.1.22  root     14556:        default:
                   14557:                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     14558:                REG16(AX) = 0x01;
1.1.1.3   root     14559:                m_CF = 1;
1.1       root     14560:                break;
                   14561:        }
                   14562: }
                   14563: 
                   14564: inline void msdos_int_2fh_1ah()
                   14565: {
                   14566:        switch(REG8(AL)) {
                   14567:        case 0x00:
1.1.1.29  root     14568:                // ANSI.SYS is installed
1.1       root     14569:                REG8(AL) = 0xff;
                   14570:                break;
1.1.1.49  root     14571:        case 0x01:
1.1.1.50  root     14572:                if(REG8(CL) == 0x5f) {
                   14573:                        // set display information
                   14574:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14575:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14576:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14577:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14578:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14579:                                
                   14580:                                if(cur_width != new_width || cur_height != new_height) {
                   14581:                                        pcbios_set_console_size(new_width, new_height, true);
                   14582:                                }
                   14583:                        }
                   14584:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14585:                        // get display information
1.1.1.50  root     14586:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14587:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14588:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14589:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14590:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14591:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14592:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14593:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14594:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14595:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14596:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14597:                } else {
                   14598:                        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));
                   14599:                        REG16(AX) = 0x01;
                   14600:                        m_CF = 1;
                   14601:                }
                   14602:                break;
1.1       root     14603:        default:
1.1.1.22  root     14604:                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));
                   14605:                REG16(AX) = 0x01;
                   14606:                m_CF = 1;
                   14607:                break;
                   14608:        }
                   14609: }
                   14610: 
1.1.1.30  root     14611: inline void msdos_int_2fh_40h()
1.1.1.22  root     14612: {
                   14613:        switch(REG8(AL)) {
                   14614:        case 0x00:
1.1.1.30  root     14615:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14616:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14617:                break;
1.1.1.43  root     14618:        case 0x10:
                   14619:                // OS/2 v2.0+ - Installation Check
                   14620:                REG16(AX) = 0x01;
                   14621:                m_CF = 1;
                   14622:                break;
1.1.1.22  root     14623:        default:
                   14624:                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     14625:                REG16(AX) = 0x01;
1.1.1.3   root     14626:                m_CF = 1;
1.1       root     14627:                break;
                   14628:        }
                   14629: }
                   14630: 
                   14631: inline void msdos_int_2fh_43h()
                   14632: {
                   14633:        switch(REG8(AL)) {
                   14634:        case 0x00:
1.1.1.29  root     14635:                // XMS is installed ?
1.1.1.19  root     14636: #ifdef SUPPORT_XMS
                   14637:                if(support_xms) {
                   14638:                        REG8(AL) = 0x80;
1.1.1.44  root     14639:                }
                   14640: #endif
                   14641:                break;
                   14642:        case 0x08:
                   14643: #ifdef SUPPORT_XMS
                   14644:                if(support_xms) {
                   14645:                        REG8(AL) = 0x43;
                   14646:                        REG8(BL) = 0x01; // IBM PC/AT
                   14647:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14648:                }
1.1.1.19  root     14649: #endif
                   14650:                break;
                   14651:        case 0x10:
                   14652:                SREG(ES) = XMS_TOP >> 4;
                   14653:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14654:                REG16(BX) = 0x15;
1.1       root     14655:                break;
1.1.1.44  root     14656:        case 0xe0:
                   14657:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14658:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14659:                        break;
                   14660:                }
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));
                   14663:                REG16(AX) = 0x01;
                   14664:                m_CF = 1;
                   14665:                break;
                   14666:        }
                   14667: }
                   14668: 
                   14669: inline void msdos_int_2fh_46h()
                   14670: {
                   14671:        switch(REG8(AL)) {
                   14672:        case 0x80:
1.1.1.29  root     14673:                // Windows v3.0 is not installed
                   14674: //             REG8(AL) = 0x00;
1.1.1.22  root     14675:                break;
                   14676:        default:
                   14677:                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));
                   14678:                REG16(AX) = 0x01;
                   14679:                m_CF = 1;
                   14680:                break;
                   14681:        }
                   14682: }
                   14683: 
                   14684: inline void msdos_int_2fh_48h()
                   14685: {
                   14686:        switch(REG8(AL)) {
                   14687:        case 0x00:
1.1.1.29  root     14688:                // DOSKEY is not installed
                   14689: //             REG8(AL) = 0x00;
1.1.1.22  root     14690:                break;
                   14691:        case 0x10:
                   14692:                msdos_int_21h_0ah();
                   14693:                REG16(AX) = 0x00;
                   14694:                break;
                   14695:        default:
                   14696:                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     14697:                REG16(AX) = 0x01;
1.1.1.3   root     14698:                m_CF = 1;
1.1       root     14699:                break;
                   14700:        }
                   14701: }
                   14702: 
                   14703: inline void msdos_int_2fh_4ah()
                   14704: {
                   14705:        switch(REG8(AL)) {
1.1.1.29  root     14706: #ifdef SUPPORT_HMA
                   14707:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14708:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14709:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14710:                                // restore first free mcb in high memory area
                   14711:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14712:                        }
                   14713:                        int offset = 0xffff;
                   14714:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14715:                                REG16(DI) = offset + 0x10;
                   14716:                        } else {
                   14717:                                REG16(DI) = 0xffff;
                   14718:                        }
                   14719:                } else {
                   14720:                        // HMA is already used
                   14721:                        REG16(BX) = 0;
                   14722:                        REG16(DI) = 0xffff;
                   14723:                }
                   14724:                SREG(ES) = 0xffff;
                   14725:                i386_load_segment_descriptor(ES);
                   14726:                break;
                   14727:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14728:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14729:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14730:                                // restore first free mcb in high memory area
                   14731:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14732:                        }
                   14733:                        int size = REG16(BX), offset;
                   14734:                        if((size % 16) != 0) {
                   14735:                                size &= ~15;
                   14736:                                size += 16;
                   14737:                        }
                   14738:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14739:                                REG16(BX) = size;
                   14740:                                REG16(DI) = offset + 0x10;
                   14741:                                is_hma_used_by_int_2fh = true;
                   14742:                        } else {
                   14743:                                REG16(BX) = 0;
                   14744:                                REG16(DI) = 0xffff;
                   14745:                        }
                   14746:                } else {
                   14747:                        // HMA is already used
                   14748:                        REG16(BX) = 0;
                   14749:                        REG16(DI) = 0xffff;
                   14750:                }
                   14751:                SREG(ES) = 0xffff;
                   14752:                i386_load_segment_descriptor(ES);
                   14753:                break;
                   14754:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14755:                if(REG8(DL) == 0x00) {
                   14756:                        if(!is_hma_used_by_xms) {
                   14757:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14758:                                        // restore first free mcb in high memory area
                   14759:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14760:                                        is_hma_used_by_int_2fh = false;
                   14761:                                }
                   14762:                                int size = REG16(BX), offset;
                   14763:                                if((size % 16) != 0) {
                   14764:                                        size &= ~15;
                   14765:                                        size += 16;
                   14766:                                }
                   14767:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14768: //                                     REG16(BX) = size;
                   14769:                                        SREG(ES) = 0xffff;
                   14770:                                        i386_load_segment_descriptor(ES);
                   14771:                                        REG16(DI) = offset + 0x10;
                   14772:                                        is_hma_used_by_int_2fh = true;
                   14773:                                } else {
                   14774:                                        REG16(DI) = 0xffff;
                   14775:                                }
                   14776:                        } else {
                   14777:                                REG16(DI) = 0xffff;
                   14778:                        }
                   14779:                } else if(REG8(DL) == 0x01) {
                   14780:                        if(!is_hma_used_by_xms) {
                   14781:                                int size = REG16(BX);
                   14782:                                if((size % 16) != 0) {
                   14783:                                        size &= ~15;
                   14784:                                        size += 16;
                   14785:                                }
                   14786:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14787:                                        // memory block address is not changed
                   14788:                                } else {
                   14789:                                        REG16(DI) = 0xffff;
                   14790:                                }
                   14791:                        } else {
                   14792:                                REG16(DI) = 0xffff;
                   14793:                        }
                   14794:                } else if(REG8(DL) == 0x02) {
                   14795:                        if(!is_hma_used_by_xms) {
                   14796:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14797:                                        // restore first free mcb in high memory area
                   14798:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14799:                                        is_hma_used_by_int_2fh = false;
                   14800:                                } else {
                   14801:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14802:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14803:                                                is_hma_used_by_int_2fh = false;
                   14804:                                        }
                   14805:                                }
                   14806:                        }
                   14807:                } else {
                   14808:                        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));
                   14809:                        REG16(AX) = 0x01;
                   14810:                        m_CF = 1;
                   14811:                }
                   14812:                break;
                   14813:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14814:                if(!is_hma_used_by_xms) {
                   14815:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14816:                                // restore first free mcb in high memory area
                   14817:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14818:                                is_hma_used_by_int_2fh = false;
                   14819:                        }
                   14820:                        REG16(AX) = 0x0000;
                   14821:                        SREG(ES) = 0xffff;
                   14822:                        i386_load_segment_descriptor(ES);
                   14823:                        REG16(DI) = 0x10;
                   14824:                }
                   14825:                break;
                   14826: #else
1.1       root     14827:        case 0x01:
                   14828:        case 0x02:
1.1.1.29  root     14829:                // HMA is already used
1.1.1.27  root     14830:                REG16(BX) = 0x0000;
1.1.1.3   root     14831:                SREG(ES) = 0xffff;
                   14832:                i386_load_segment_descriptor(ES);
1.1       root     14833:                REG16(DI) = 0xffff;
                   14834:                break;
1.1.1.19  root     14835:        case 0x03:
                   14836:                // unable to allocate
                   14837:                REG16(DI) = 0xffff;
                   14838:                break;
                   14839:        case 0x04:
                   14840:                // function not supported, do not clear AX
                   14841:                break;
1.1.1.29  root     14842: #endif
                   14843:        case 0x10:
1.1.1.42  root     14844:                switch(REG16(BX)) {
                   14845:                case 0x0000:
                   14846:                case 0x0001:
                   14847:                case 0x0002:
                   14848:                case 0x0003:
                   14849:                case 0x0004:
                   14850:                case 0x0005:
                   14851:                case 0x0006:
                   14852:                case 0x0007:
                   14853:                case 0x0008:
                   14854:                case 0x000a:
                   14855:                case 0x1234:
                   14856:                        // SMARTDRV v4.00+ is not installed
                   14857:                        break;
                   14858:                default:
1.1.1.29  root     14859:                        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));
                   14860:                        REG16(AX) = 0x01;
                   14861:                        m_CF = 1;
1.1.1.42  root     14862:                        break;
1.1.1.29  root     14863:                }
                   14864:                break;
                   14865:        case 0x11:
1.1.1.42  root     14866:                switch(REG16(BX)) {
                   14867:                case 0x0000:
                   14868:                case 0x0001:
                   14869:                case 0x0002:
                   14870:                case 0x0003:
                   14871:                case 0x0004:
                   14872:                case 0x0005:
                   14873:                case 0x0006:
                   14874:                case 0x0007:
                   14875:                case 0x0008:
                   14876:                case 0x0009:
                   14877:                case 0x000a:
                   14878:                case 0x000b:
                   14879:                case 0xfffe:
                   14880:                case 0xffff:
1.1.1.29  root     14881:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14882:                        break;
                   14883:                default:
                   14884:                        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));
                   14885:                        REG16(AX) = 0x01;
                   14886:                        m_CF = 1;
                   14887:                        break;
                   14888:                }
                   14889:                break;
                   14890:        case 0x12:
                   14891:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14892:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14893:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14894:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14895:                } else {
                   14896:                        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));
                   14897:                        REG16(AX) = 0x01;
                   14898:                        m_CF = 1;
                   14899:                }
1.1.1.22  root     14900:                break;
1.1.1.42  root     14901:        case 0x13:
                   14902:                // DBLSPACE.BIN is not installed
                   14903:                break;
1.1.1.22  root     14904:        default:
                   14905:                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));
                   14906:                REG16(AX) = 0x01;
                   14907:                m_CF = 1;
                   14908:                break;
                   14909:        }
                   14910: }
                   14911: 
                   14912: inline void msdos_int_2fh_4bh()
                   14913: {
                   14914:        switch(REG8(AL)) {
1.1.1.24  root     14915:        case 0x01:
1.1.1.22  root     14916:        case 0x02:
1.1.1.29  root     14917:                // Task Switcher is not installed
1.1.1.24  root     14918:                break;
                   14919:        case 0x03:
                   14920:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14921:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14922:                break;
1.1.1.30  root     14923:        case 0x04:
                   14924:                REG16(BX) = 0x0000; // free switcher id successfully
                   14925:                break;
1.1.1.43  root     14926:        case 0x05:
                   14927:                REG16(BX) = 0x0000; // no instance data chain
                   14928:                SREG(ES) = 0x0000;
                   14929:                i386_load_segment_descriptor(ES);
                   14930:                break;
1.1       root     14931:        default:
1.1.1.22  root     14932:                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     14933:                REG16(AX) = 0x01;
1.1.1.3   root     14934:                m_CF = 1;
1.1       root     14935:                break;
                   14936:        }
                   14937: }
                   14938: 
1.1.1.44  root     14939: inline void msdos_int_2fh_4dh()
                   14940: {
                   14941:        switch(REG8(AL)) {
                   14942:        case 0x00:
                   14943:                // KKCFUNC is not installed ???
                   14944:                break;
                   14945:        default:
                   14946: //             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));
                   14947:                REG16(AX) = 0x01; // invalid function
                   14948:                m_CF = 1;
                   14949:                break;
                   14950:        }
                   14951: }
                   14952: 
1.1       root     14953: inline void msdos_int_2fh_4fh()
                   14954: {
                   14955:        switch(REG8(AL)) {
                   14956:        case 0x00:
1.1.1.29  root     14957:                // BILING is installed
1.1.1.27  root     14958:                REG16(AX) = 0x0000;
                   14959:                REG8(DL) = 0x01;        // major version
                   14960:                REG8(DH) = 0x00;        // minor version
1.1       root     14961:                break;
                   14962:        case 0x01:
1.1.1.27  root     14963:                REG16(AX) = 0x0000;
1.1       root     14964:                REG16(BX) = active_code_page;
                   14965:                break;
                   14966:        default:
1.1.1.22  root     14967:                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));
                   14968:                REG16(AX) = 0x01;
                   14969:                m_CF = 1;
                   14970:                break;
                   14971:        }
                   14972: }
                   14973: 
                   14974: inline void msdos_int_2fh_55h()
                   14975: {
                   14976:        switch(REG8(AL)) {
                   14977:        case 0x00:
                   14978:        case 0x01:
                   14979: //             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));
                   14980:                break;
                   14981:        default:
                   14982:                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     14983:                REG16(AX) = 0x01;
1.1.1.3   root     14984:                m_CF = 1;
1.1       root     14985:                break;
                   14986:        }
                   14987: }
                   14988: 
1.1.1.44  root     14989: inline void msdos_int_2fh_56h()
                   14990: {
                   14991:        switch(REG8(AL)) {
                   14992:        case 0x00:
                   14993:                // INTERLNK is not installed
                   14994:                break;
                   14995:        case 0x01:
                   14996:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14997: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14998: //                     REG8(AL) = 0x00;
                   14999: //             }
                   15000:                break;
                   15001:        default:
                   15002:                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));
                   15003:                REG16(AX) = 0x01;
                   15004:                m_CF = 1;
                   15005:                break;
                   15006:        }
                   15007: }
                   15008: 
1.1.1.24  root     15009: inline void msdos_int_2fh_adh()
                   15010: {
                   15011:        switch(REG8(AL)) {
                   15012:        case 0x00:
1.1.1.29  root     15013:                // DISPLAY.SYS is installed
1.1.1.24  root     15014:                REG8(AL) = 0xff;
                   15015:                REG16(BX) = 0x100; // ???
                   15016:                break;
                   15017:        case 0x01:
                   15018:                active_code_page = REG16(BX);
                   15019:                msdos_nls_tables_update();
                   15020:                REG16(AX) = 0x01;
                   15021:                break;
                   15022:        case 0x02:
                   15023:                REG16(BX) = active_code_page;
                   15024:                break;
                   15025:        case 0x03:
                   15026:                // FIXME
                   15027:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   15028:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   15029:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   15030:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   15031:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   15032:                break;
                   15033:        case 0x80:
1.1.1.49  root     15034:                // KEYB.COM is not installed
                   15035:                break;
1.1.1.24  root     15036:        default:
                   15037:                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));
                   15038:                REG16(AX) = 0x01;
                   15039:                m_CF = 1;
                   15040:                break;
                   15041:        }
                   15042: }
                   15043: 
1.1       root     15044: inline void msdos_int_2fh_aeh()
                   15045: {
                   15046:        switch(REG8(AL)) {
                   15047:        case 0x00:
1.1.1.28  root     15048:                // FIXME: we need to check the given command line
                   15049:                REG8(AL) = 0x00; // the command should be executed as usual
                   15050: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     15051:                break;
                   15052:        case 0x01:
                   15053:                {
                   15054:                        char command[MAX_PATH];
                   15055:                        memset(command, 0, sizeof(command));
1.1.1.3   root     15056:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     15057:                        
                   15058:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   15059:                        param->env_seg = 0;
                   15060:                        param->cmd_line.w.l = 44;
                   15061:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   15062:                        param->fcb1.w.l = 24;
                   15063:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   15064:                        param->fcb2.w.l = 24;
                   15065:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   15066:                        
                   15067:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   15068:                        
                   15069:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     15070:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   15071:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     15072:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   15073:                        
1.1.1.28  root     15074:                        try {
                   15075:                                msdos_process_exec(command, param, 0);
                   15076:                        } catch(...) {
                   15077:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     15078:                        }
                   15079:                }
                   15080:                break;
                   15081:        default:
1.1.1.22  root     15082:                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     15083:                REG16(AX) = 0x01;
1.1.1.3   root     15084:                m_CF = 1;
1.1       root     15085:                break;
                   15086:        }
                   15087: }
                   15088: 
1.1.1.34  root     15089: inline void msdos_int_2fh_b7h()
                   15090: {
                   15091:        switch(REG8(AL)) {
                   15092:        case 0x00:
                   15093:                // APPEND is not installed
                   15094: //             REG8(AL) = 0x00;
                   15095:                break;
1.1.1.44  root     15096:        case 0x06:
                   15097:                REG16(BX) = 0x0000;
                   15098:                break;
1.1.1.34  root     15099:        case 0x07:
1.1.1.43  root     15100:        case 0x11:
1.1.1.34  root     15101:                // COMMAND.COM calls this service without checking APPEND is installed
                   15102:                break;
                   15103:        default:
                   15104:                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));
                   15105:                REG16(AX) = 0x01;
                   15106:                m_CF = 1;
                   15107:                break;
                   15108:        }
                   15109: }
                   15110: 
1.1.1.24  root     15111: inline void msdos_int_33h_0000h()
                   15112: {
                   15113:        REG16(AX) = 0xffff; // hardware/driver installed
                   15114:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15115: }
                   15116: 
                   15117: inline void msdos_int_33h_0001h()
                   15118: {
1.1.1.34  root     15119:        if(mouse.hidden > 0) {
                   15120:                mouse.hidden--;
                   15121:        }
                   15122:        if(mouse.hidden == 0) {
1.1.1.64! root     15123:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~ENABLE_QUICK_EDIT_MODE);
1.1.1.59  root     15124:                pic[1].imr &= ~0x10; // enable irq12
1.1.1.24  root     15125:        }
                   15126: }
                   15127: 
                   15128: inline void msdos_int_33h_0002h()
                   15129: {
1.1.1.34  root     15130:        mouse.hidden++;
1.1.1.64! root     15131:        if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
        !          15132:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
        !          15133:        } else {
        !          15134:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
        !          15135:        }
1.1.1.59  root     15136:        pic[1].imr |= 0x10; // disable irq12
1.1.1.24  root     15137: }
                   15138: 
                   15139: inline void msdos_int_33h_0003h()
                   15140: {
1.1.1.34  root     15141: //     if(mouse.hidden > 0) {
                   15142:                update_console_input();
                   15143: //     }
1.1.1.24  root     15144:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     15145:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   15146:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   15147: }
                   15148: 
                   15149: inline void msdos_int_33h_0004h()
                   15150: {
                   15151:        mouse.position.x = REG16(CX);
                   15152:        mouse.position.x = REG16(DX);
1.1.1.24  root     15153: }
                   15154: 
                   15155: inline void msdos_int_33h_0005h()
                   15156: {
1.1.1.34  root     15157: //     if(mouse.hidden > 0) {
                   15158:                update_console_input();
                   15159: //     }
1.1.1.24  root     15160:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   15161:                int idx = REG16(BX);
1.1.1.34  root     15162:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   15163:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   15164:                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     15165:                mouse.buttons[idx].pressed_times = 0;
                   15166:        } else {
                   15167:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   15168:        }
                   15169:        REG16(AX) = mouse.get_buttons();
                   15170: }
                   15171: 
                   15172: inline void msdos_int_33h_0006h()
                   15173: {
1.1.1.34  root     15174: //     if(mouse.hidden > 0) {
                   15175:                update_console_input();
                   15176: //     }
1.1.1.24  root     15177:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   15178:                int idx = REG16(BX);
1.1.1.34  root     15179:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   15180:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   15181:                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     15182:                mouse.buttons[idx].released_times = 0;
                   15183:        } else {
                   15184:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   15185:        }
                   15186:        REG16(AX) = mouse.get_buttons();
                   15187: }
                   15188: 
                   15189: inline void msdos_int_33h_0007h()
                   15190: {
                   15191:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   15192:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   15193: }
                   15194: 
                   15195: inline void msdos_int_33h_0008h()
                   15196: {
                   15197:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   15198:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   15199: }
                   15200: 
                   15201: inline void msdos_int_33h_0009h()
                   15202: {
                   15203:        mouse.hot_spot[0] = REG16(BX);
                   15204:        mouse.hot_spot[1] = REG16(CX);
                   15205: }
                   15206: 
1.1.1.49  root     15207: inline void msdos_int_33h_000ah()
                   15208: {
                   15209:        mouse.screen_mask = REG16(CX);
                   15210:        mouse.cursor_mask = REG16(DX);
                   15211: }
                   15212: 
1.1.1.24  root     15213: inline void msdos_int_33h_000bh()
                   15214: {
1.1.1.34  root     15215: //     if(mouse.hidden > 0) {
                   15216:                update_console_input();
                   15217: //     }
1.1.1.24  root     15218:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15219:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15220:        mouse.prev_position.x = mouse.position.x;
                   15221:        mouse.prev_position.y = mouse.position.y;
                   15222:        REG16(CX) = dx;
                   15223:        REG16(DX) = dy;
                   15224: }
                   15225: 
                   15226: inline void msdos_int_33h_000ch()
                   15227: {
                   15228:        mouse.call_mask = REG16(CX);
                   15229:        mouse.call_addr.w.l = REG16(DX);
                   15230:        mouse.call_addr.w.h = SREG(ES);
                   15231: }
                   15232: 
                   15233: inline void msdos_int_33h_000fh()
                   15234: {
                   15235:        mouse.mickey.x = REG16(CX);
                   15236:        mouse.mickey.y = REG16(DX);
                   15237: }
                   15238: 
                   15239: inline void msdos_int_33h_0011h()
                   15240: {
                   15241:        REG16(AX) = 0xffff;
                   15242:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15243: }
                   15244: 
                   15245: inline void msdos_int_33h_0014h()
                   15246: {
                   15247:        UINT16 old_mask = mouse.call_mask;
                   15248:        UINT16 old_ofs = mouse.call_addr.w.l;
                   15249:        UINT16 old_seg = mouse.call_addr.w.h;
                   15250:        
                   15251:        mouse.call_mask = REG16(CX);
                   15252:        mouse.call_addr.w.l = REG16(DX);
                   15253:        mouse.call_addr.w.h = SREG(ES);
                   15254:        
                   15255:        REG16(CX) = old_mask;
                   15256:        REG16(DX) = old_ofs;
                   15257:        SREG(ES) = old_seg;
                   15258:        i386_load_segment_descriptor(ES);
                   15259: }
                   15260: 
                   15261: inline void msdos_int_33h_0015h()
                   15262: {
                   15263:        REG16(BX) = sizeof(mouse);
                   15264: }
                   15265: 
                   15266: inline void msdos_int_33h_0016h()
                   15267: {
                   15268:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   15269: }
                   15270: 
                   15271: inline void msdos_int_33h_0017h()
                   15272: {
                   15273:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   15274: }
                   15275: 
1.1.1.43  root     15276: inline void msdos_int_33h_0018h()
                   15277: {
                   15278:        for(int i = 0; i < 8; i++) {
                   15279:                if(REG16(CX) & (1 << i)) {
                   15280:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   15281:                                // event handler already exists
                   15282:                                REG16(AX) = 0xffff;
                   15283:                                break;
                   15284:                        }
                   15285:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   15286:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   15287:                }
                   15288:        }
                   15289: }
                   15290: 
                   15291: inline void msdos_int_33h_0019h()
                   15292: {
                   15293:        UINT16 call_mask = REG16(CX);
                   15294:        
                   15295:        REG16(CX) = 0;
                   15296:        
                   15297:        for(int i = 0; i < 8; i++) {
                   15298:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   15299:                        for(int j = 0; j < 8; j++) {
                   15300:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   15301:                                        REG16(CX) |= (1 << j);
                   15302:                                }
                   15303:                        }
                   15304:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   15305:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   15306:                        break;
                   15307:                }
                   15308:        }
                   15309: }
                   15310: 
1.1.1.24  root     15311: inline void msdos_int_33h_001ah()
                   15312: {
                   15313:        mouse.sensitivity[0] = REG16(BX);
                   15314:        mouse.sensitivity[1] = REG16(CX);
                   15315:        mouse.sensitivity[2] = REG16(DX);
                   15316: }
                   15317: 
                   15318: inline void msdos_int_33h_001bh()
                   15319: {
                   15320:        REG16(BX) = mouse.sensitivity[0];
                   15321:        REG16(CX) = mouse.sensitivity[1];
                   15322:        REG16(DX) = mouse.sensitivity[2];
                   15323: }
                   15324: 
                   15325: inline void msdos_int_33h_001dh()
                   15326: {
                   15327:        mouse.display_page = REG16(BX);
                   15328: }
                   15329: 
                   15330: inline void msdos_int_33h_001eh()
                   15331: {
                   15332:        REG16(BX) = mouse.display_page;
                   15333: }
                   15334: 
1.1.1.34  root     15335: inline void msdos_int_33h_001fh()
                   15336: {
                   15337:        // from DOSBox
                   15338:        REG16(BX) = 0x0000;
                   15339:        SREG(ES) = 0x0000;
                   15340:        i386_load_segment_descriptor(ES);
                   15341:        mouse.enabled = false;
                   15342:        mouse.old_hidden = mouse.hidden;
                   15343:        mouse.hidden = 1;
                   15344: }
                   15345: 
                   15346: inline void msdos_int_33h_0020h()
                   15347: {
                   15348:        // from DOSBox
                   15349:        mouse.enabled = true;
                   15350:        mouse.hidden = mouse.old_hidden;
                   15351: }
                   15352: 
1.1.1.24  root     15353: inline void msdos_int_33h_0021h()
                   15354: {
                   15355:        REG16(AX) = 0xffff;
                   15356:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15357: }
                   15358: 
                   15359: inline void msdos_int_33h_0022h()
                   15360: {
                   15361:        mouse.language = REG16(BX);
                   15362: }
                   15363: 
                   15364: inline void msdos_int_33h_0023h()
                   15365: {
                   15366:        REG16(BX) = mouse.language;
                   15367: }
                   15368: 
                   15369: inline void msdos_int_33h_0024h()
                   15370: {
                   15371:        REG16(BX) = 0x0805; // V8.05
                   15372:        REG16(CX) = 0x0400; // PS/2
                   15373: }
                   15374: 
1.1.1.49  root     15375: inline void msdos_int_33h_0025h()
                   15376: {
                   15377:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   15378: }
                   15379: 
1.1.1.24  root     15380: inline void msdos_int_33h_0026h()
                   15381: {
                   15382:        REG16(BX) = 0x0000;
                   15383:        REG16(CX) = mouse.max_position.x;
                   15384:        REG16(DX) = mouse.max_position.y;
                   15385: }
                   15386: 
1.1.1.49  root     15387: inline void msdos_int_33h_0027h()
                   15388: {
                   15389: //     if(mouse.hidden > 0) {
                   15390:                update_console_input();
                   15391: //     }
                   15392:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15393:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15394:        mouse.prev_position.x = mouse.position.x;
                   15395:        mouse.prev_position.y = mouse.position.y;
                   15396:        REG16(AX) = mouse.screen_mask;
                   15397:        REG16(BX) = mouse.cursor_mask;
                   15398:        REG16(CX) = dx;
                   15399:        REG16(DX) = dy;
                   15400: }
                   15401: 
                   15402: inline void msdos_int_33h_0028h()
                   15403: {
                   15404:        if(REG16(CX) != 0) {
                   15405:                UINT8 tmp = REG8(AL);
                   15406:                REG8(AL) = REG8(CL);
                   15407:                pcbios_int_10h_00h();
                   15408:                REG8(AL) = tmp;
                   15409:        }
                   15410:        REG8(CL) = 0x00; // successful
                   15411: }
                   15412: 
                   15413: inline void msdos_int_33h_0029h()
                   15414: {
                   15415:        switch(REG16(CX)) {
                   15416:        case 0x0000:
                   15417:                REG16(CX) = 0x0003;
                   15418:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   15419:                break;
                   15420:        case 0x0003:
                   15421:                REG16(CX) = 0x0070;
                   15422:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15423:                break;
                   15424:        case 0x0070:
                   15425:                REG16(CX) = 0x0071;
                   15426:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15427:                break;
                   15428:        case 0x0071:
                   15429:                REG16(CX) = 0x0073;
                   15430:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   15431:                break;
                   15432:        default:
                   15433:                REG16(CX) = 0x0000;
                   15434:                break;
                   15435:        }
                   15436:        if(REG16(CX) != 0) {
                   15437:                SREG(DS) = (WORK_TOP >> 4);
                   15438:        } else {
                   15439:                SREG(DS) = 0x0000;
                   15440:        }
                   15441:        i386_load_segment_descriptor(DS);
                   15442:        REG16(DX) = 0x0000;
                   15443: }
                   15444: 
1.1.1.24  root     15445: inline void msdos_int_33h_002ah()
                   15446: {
1.1.1.34  root     15447:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     15448:        REG16(BX) = mouse.hot_spot[0];
                   15449:        REG16(CX) = mouse.hot_spot[1];
                   15450:        REG16(DX) = 4; // PS/2
                   15451: }
                   15452: 
                   15453: inline void msdos_int_33h_0031h()
                   15454: {
                   15455:        REG16(AX) = mouse.min_position.x;
                   15456:        REG16(BX) = mouse.min_position.y;
                   15457:        REG16(CX) = mouse.max_position.x;
                   15458:        REG16(DX) = mouse.max_position.y;
                   15459: }
                   15460: 
                   15461: inline void msdos_int_33h_0032h()
                   15462: {
                   15463:        REG16(AX) = 0;
1.1.1.49  root     15464:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     15465:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     15466:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     15467: //     REG16(AX) |= 0x1000; // 0028h
                   15468: //     REG16(AX) |= 0x0800; // 0029h
                   15469:        REG16(AX) |= 0x0400; // 002ah
                   15470: //     REG16(AX) |= 0x0200; // 002bh
                   15471: //     REG16(AX) |= 0x0100; // 002ch
                   15472: //     REG16(AX) |= 0x0080; // 002dh
                   15473: //     REG16(AX) |= 0x0040; // 002eh
                   15474:        REG16(AX) |= 0x0020; // 002fh
                   15475: //     REG16(AX) |= 0x0010; // 0030h
                   15476:        REG16(AX) |= 0x0008; // 0031h
                   15477:        REG16(AX) |= 0x0004; // 0032h
                   15478: //     REG16(AX) |= 0x0002; // 0033h
                   15479: //     REG16(AX) |= 0x0001; // 0034h
                   15480: }
                   15481: 
1.1.1.49  root     15482: inline void msdos_int_33h_004dh()
                   15483: {
                   15484:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   15485: }
                   15486: 
                   15487: inline void msdos_int_33h_006dh()
                   15488: {
                   15489:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   15490:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   15491: }
                   15492: 
1.1.1.19  root     15493: inline void msdos_int_67h_40h()
                   15494: {
                   15495:        if(!support_ems) {
                   15496:                REG8(AH) = 0x84;
                   15497:        } else {
                   15498:                REG8(AH) = 0x00;
                   15499:        }
                   15500: }
                   15501: 
                   15502: inline void msdos_int_67h_41h()
                   15503: {
                   15504:        if(!support_ems) {
                   15505:                REG8(AH) = 0x84;
                   15506:        } else {
                   15507:                REG8(AH) = 0x00;
                   15508:                REG16(BX) = EMS_TOP >> 4;
                   15509:        }
                   15510: }
                   15511: 
                   15512: inline void msdos_int_67h_42h()
                   15513: {
                   15514:        if(!support_ems) {
                   15515:                REG8(AH) = 0x84;
                   15516:        } else {
                   15517:                REG8(AH) = 0x00;
                   15518:                REG16(BX) = free_ems_pages;
                   15519:                REG16(DX) = MAX_EMS_PAGES;
                   15520:        }
                   15521: }
                   15522: 
                   15523: inline void msdos_int_67h_43h()
                   15524: {
                   15525:        if(!support_ems) {
                   15526:                REG8(AH) = 0x84;
                   15527:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15528:                REG8(AH) = 0x87;
                   15529:        } else if(REG16(BX) > free_ems_pages) {
                   15530:                REG8(AH) = 0x88;
                   15531:        } else if(REG16(BX) == 0) {
                   15532:                REG8(AH) = 0x89;
                   15533:        } else {
1.1.1.31  root     15534:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15535:                        if(!ems_handles[i].allocated) {
                   15536:                                ems_allocate_pages(i, REG16(BX));
                   15537:                                REG8(AH) = 0x00;
                   15538:                                REG16(DX) = i;
                   15539:                                return;
                   15540:                        }
                   15541:                }
                   15542:                REG8(AH) = 0x85;
                   15543:        }
                   15544: }
                   15545: 
                   15546: inline void msdos_int_67h_44h()
                   15547: {
                   15548:        if(!support_ems) {
                   15549:                REG8(AH) = 0x84;
1.1.1.31  root     15550:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15551:                REG8(AH) = 0x83;
                   15552:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15553:                REG8(AH) = 0x8a;
                   15554: //     } else if(!(REG8(AL) < 4)) {
                   15555: //             REG8(AH) = 0x8b;
                   15556:        } else if(REG16(BX) == 0xffff) {
                   15557:                ems_unmap_page(REG8(AL) & 3);
                   15558:                REG8(AH) = 0x00;
                   15559:        } else {
                   15560:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15561:                REG8(AH) = 0x00;
                   15562:        }
                   15563: }
                   15564: 
                   15565: inline void msdos_int_67h_45h()
                   15566: {
                   15567:        if(!support_ems) {
                   15568:                REG8(AH) = 0x84;
1.1.1.31  root     15569:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15570:                REG8(AH) = 0x83;
                   15571:        } else {
                   15572:                ems_release_pages(REG16(DX));
                   15573:                REG8(AH) = 0x00;
                   15574:        }
                   15575: }
                   15576: 
                   15577: inline void msdos_int_67h_46h()
                   15578: {
                   15579:        if(!support_ems) {
                   15580:                REG8(AH) = 0x84;
                   15581:        } else {
1.1.1.29  root     15582: //             REG16(AX) = 0x0032; // EMS 3.2
                   15583:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15584:        }
                   15585: }
                   15586: 
                   15587: inline void msdos_int_67h_47h()
                   15588: {
                   15589:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15590:        process_t *process = msdos_process_info_get(current_psp);
                   15591:        
                   15592:        if(!support_ems) {
                   15593:                REG8(AH) = 0x84;
1.1.1.31  root     15594: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15595: //             REG8(AH) = 0x83;
                   15596:        } else if(process->ems_pages_stored) {
                   15597:                REG8(AH) = 0x8d;
                   15598:        } else {
                   15599:                for(int i = 0; i < 4; i++) {
                   15600:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15601:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15602:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15603:                }
                   15604:                process->ems_pages_stored = true;
                   15605:                REG8(AH) = 0x00;
                   15606:        }
                   15607: }
                   15608: 
                   15609: inline void msdos_int_67h_48h()
                   15610: {
                   15611:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15612:        process_t *process = msdos_process_info_get(current_psp);
                   15613:        
                   15614:        if(!support_ems) {
                   15615:                REG8(AH) = 0x84;
1.1.1.31  root     15616: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15617: //             REG8(AH) = 0x83;
                   15618:        } else if(!process->ems_pages_stored) {
                   15619:                REG8(AH) = 0x8e;
                   15620:        } else {
                   15621:                for(int i = 0; i < 4; i++) {
                   15622:                        if(process->ems_pages[i].mapped) {
                   15623:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15624:                        } else {
                   15625:                                ems_unmap_page(i);
                   15626:                        }
                   15627:                }
                   15628:                process->ems_pages_stored = false;
                   15629:                REG8(AH) = 0x00;
                   15630:        }
                   15631: }
                   15632: 
                   15633: inline void msdos_int_67h_4bh()
                   15634: {
                   15635:        if(!support_ems) {
                   15636:                REG8(AH) = 0x84;
                   15637:        } else {
                   15638:                REG8(AH) = 0x00;
                   15639:                REG16(BX) = 0;
1.1.1.31  root     15640:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15641:                        if(ems_handles[i].allocated) {
                   15642:                                REG16(BX)++;
                   15643:                        }
                   15644:                }
                   15645:        }
                   15646: }
                   15647: 
                   15648: inline void msdos_int_67h_4ch()
                   15649: {
                   15650:        if(!support_ems) {
                   15651:                REG8(AH) = 0x84;
1.1.1.31  root     15652:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15653:                REG8(AH) = 0x83;
                   15654:        } else {
                   15655:                REG8(AH) = 0x00;
                   15656:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15657:        }
                   15658: }
                   15659: 
                   15660: inline void msdos_int_67h_4dh()
                   15661: {
                   15662:        if(!support_ems) {
                   15663:                REG8(AH) = 0x84;
                   15664:        } else {
                   15665:                REG8(AH) = 0x00;
                   15666:                REG16(BX) = 0;
1.1.1.31  root     15667:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15668:                        if(ems_handles[i].allocated) {
                   15669:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15670:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15671:                                REG16(BX)++;
                   15672:                        }
                   15673:                }
                   15674:        }
                   15675: }
                   15676: 
1.1.1.20  root     15677: inline void msdos_int_67h_4eh()
                   15678: {
                   15679:        if(!support_ems) {
                   15680:                REG8(AH) = 0x84;
                   15681:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15682:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15683:                        // save page map
                   15684:                        for(int i = 0; i < 4; i++) {
                   15685:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15686:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15687:                        }
                   15688:                }
                   15689:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15690:                        // restore page map
                   15691:                        for(int i = 0; i < 4; i++) {
                   15692:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15693:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15694:                                
1.1.1.31  root     15695:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15696:                                        ems_map_page(i, handle, page);
                   15697:                                } else {
                   15698:                                        ems_unmap_page(i);
                   15699:                                }
                   15700:                        }
                   15701:                }
                   15702:                REG8(AH) = 0x00;
                   15703:        } else if(REG8(AL) == 0x03) {
                   15704:                REG8(AH) = 0x00;
1.1.1.21  root     15705:                REG8(AL) = 4 * 4;
                   15706:        } else {
1.1.1.22  root     15707:                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     15708:                REG8(AH) = 0x8f;
                   15709:        }
                   15710: }
                   15711: 
                   15712: inline void msdos_int_67h_4fh()
                   15713: {
                   15714:        if(!support_ems) {
                   15715:                REG8(AH) = 0x84;
                   15716:        } else if(REG8(AL) == 0x00) {
                   15717:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15718:                
                   15719:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15720:                for(int i = 0; i < count; i++) {
                   15721:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15722:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15723:                        
                   15724: //                     if(!(physical < 4)) {
                   15725: //                             REG8(AH) = 0x8b;
                   15726: //                             return;
                   15727: //                     }
                   15728:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15729:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15730:                        *(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     15731:                }
                   15732:                REG8(AH) = 0x00;
                   15733:        } else if(REG8(AL) == 0x01) {
                   15734:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15735:                
                   15736:                for(int i = 0; i < count; i++) {
                   15737:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15738:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15739:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15740:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15741:                        
                   15742: //                     if(!(physical < 4)) {
                   15743: //                             REG8(AH) = 0x8b;
                   15744: //                             return;
                   15745: //                     } else
1.1.1.41  root     15746:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15747:                                ems_map_page(physical & 3, handle, logical);
                   15748:                        } else {
1.1.1.41  root     15749:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15750:                        }
                   15751:                }
                   15752:                REG8(AH) = 0x00;
                   15753:        } else if(REG8(AL) == 0x02) {
                   15754:                REG8(AH) = 0x00;
                   15755:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15756:        } else {
1.1.1.22  root     15757:                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     15758:                REG8(AH) = 0x8f;
                   15759:        }
                   15760: }
                   15761: 
                   15762: inline void msdos_int_67h_50h()
                   15763: {
                   15764:        if(!support_ems) {
                   15765:                REG8(AH) = 0x84;
1.1.1.31  root     15766:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15767:                REG8(AH) = 0x83;
                   15768:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15769:                for(int i = 0; i < REG16(CX); i++) {
                   15770:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15771:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15772:                        
                   15773:                        if(REG8(AL) == 0x01) {
                   15774:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15775:                        }
                   15776: //                     if(!(physical < 4)) {
                   15777: //                             REG8(AH) = 0x8b;
                   15778: //                             return;
                   15779: //                     } else
                   15780:                        if(logical == 0xffff) {
                   15781:                                ems_unmap_page(physical & 3);
                   15782:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15783:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15784:                        } else {
                   15785:                                REG8(AH) = 0x8a;
                   15786:                                return;
                   15787:                        }
                   15788:                }
                   15789:                REG8(AH) = 0x00;
                   15790:        } else {
1.1.1.22  root     15791:                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     15792:                REG8(AH) = 0x8f;
                   15793:        }
                   15794: }
                   15795: 
1.1.1.19  root     15796: inline void msdos_int_67h_51h()
                   15797: {
                   15798:        if(!support_ems) {
                   15799:                REG8(AH) = 0x84;
1.1.1.31  root     15800:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15801:                REG8(AH) = 0x83;
                   15802:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15803:                REG8(AH) = 0x87;
                   15804:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15805:                REG8(AH) = 0x88;
                   15806:        } else {
                   15807:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15808:                REG8(AH) = 0x00;
                   15809:        }
                   15810: }
                   15811: 
1.1.1.20  root     15812: inline void msdos_int_67h_52h()
                   15813: {
                   15814:        if(!support_ems) {
                   15815:                REG8(AH) = 0x84;
1.1.1.31  root     15816: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15817: //             REG8(AH) = 0x83;
1.1.1.20  root     15818:        } else if(REG8(AL) == 0x00) {
                   15819:                REG8(AL) = 0x00; // handle is volatile
                   15820:                REG8(AH) = 0x00;
                   15821:        } else if(REG8(AL) == 0x01) {
                   15822:                if(REG8(BL) == 0x00) {
                   15823:                        REG8(AH) = 0x00;
                   15824:                } else {
                   15825:                        REG8(AH) = 0x90; // undefined attribute type
                   15826:                }
                   15827:        } else if(REG8(AL) == 0x02) {
                   15828:                REG8(AL) = 0x00; // only volatile handles supported
                   15829:                REG8(AH) = 0x00;
                   15830:        } else {
1.1.1.22  root     15831:                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     15832:                REG8(AH) = 0x8f;
                   15833:        }
                   15834: }
                   15835: 
1.1.1.19  root     15836: inline void msdos_int_67h_53h()
                   15837: {
                   15838:        if(!support_ems) {
                   15839:                REG8(AH) = 0x84;
1.1.1.31  root     15840:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15841:                REG8(AH) = 0x83;
                   15842:        } else if(REG8(AL) == 0x00) {
                   15843:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15844:                REG8(AH) = 0x00;
                   15845:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15846:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15847:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15848:                                REG8(AH) = 0xa1;
                   15849:                                return;
                   15850:                        }
                   15851:                }
                   15852:                REG8(AH) = 0x00;
                   15853:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15854:        } else {
1.1.1.22  root     15855:                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     15856:                REG8(AH) = 0x8f;
1.1.1.19  root     15857:        }
                   15858: }
                   15859: 
                   15860: inline void msdos_int_67h_54h()
                   15861: {
                   15862:        if(!support_ems) {
                   15863:                REG8(AH) = 0x84;
                   15864:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15865:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15866:                        if(ems_handles[i].allocated) {
                   15867:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15868:                        } else {
                   15869:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15870:                        }
                   15871:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15872:                }
                   15873:                REG8(AH) = 0x00;
                   15874:                REG8(AL) = MAX_EMS_HANDLES;
                   15875:        } else if(REG8(AL) == 0x01) {
                   15876:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15877:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15878:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15879:                                REG8(AH) = 0x00;
                   15880:                                REG16(DX) = i;
                   15881:                                break;
                   15882:                        }
                   15883:                }
                   15884:        } else if(REG8(AL) == 0x02) {
                   15885:                REG8(AH) = 0x00;
                   15886:                REG16(BX) = MAX_EMS_HANDLES;
                   15887:        } else {
1.1.1.22  root     15888:                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     15889:                REG8(AH) = 0x8f;
                   15890:        }
                   15891: }
                   15892: 
1.1.1.49  root     15893: inline void msdos_int_67h_55h()
                   15894: {
                   15895:        if(!support_ems) {
                   15896:                REG8(AH) = 0x84;
                   15897:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15898:                REG8(AH) = 0x83;
                   15899:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15900:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15901:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15902:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15903:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15904:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15905:                
                   15906:                for(int i = 0; i < (int)entries; i++) {
                   15907:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15908:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15909:                        
                   15910:                        if(REG8(AL) == 0x01) {
                   15911:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15912:                        }
                   15913: //                     if(!(physical < 4)) {
                   15914: //                             REG8(AH) = 0x8b;
                   15915: //                             return;
                   15916: //                     } else
                   15917:                        if(logical == 0xffff) {
                   15918:                                ems_unmap_page(physical & 3);
                   15919:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15920:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15921:                        } else {
                   15922:                                REG8(AH) = 0x8a;
                   15923:                                return;
                   15924:                        }
                   15925:                }
                   15926:                i386_jmp_far(jump_seg, jump_ofs);
                   15927:                REG8(AH) = 0x00;
                   15928:        } else {
                   15929:                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));
                   15930:                REG8(AH) = 0x8f;
                   15931:        }
                   15932: }
                   15933: 
                   15934: inline void msdos_int_67h_56h()
                   15935: {
                   15936:        if(!support_ems) {
                   15937:                REG8(AH) = 0x84;
                   15938:        } else if(REG8(AL) == 0x02) {
                   15939:                REG16(BX) = (2 + 2) * 4;
                   15940:                REG8(AH) = 0x00;
                   15941:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15942:                REG8(AH) = 0x83;
                   15943:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15944:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15945:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15946:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15947:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15948:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15949: #if 0
                   15950:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15951:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15952:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15953: #endif
                   15954:                UINT16 handles[4], pages[4];
                   15955:                
                   15956:                // alter page map and call routine is at fffc:001f
                   15957:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15958:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15959:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15960:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15961:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15962:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15963:                } else {
                   15964:                        // invalid call addr :-(
                   15965:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15966:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15967:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15968:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15969:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15970:                }
                   15971:                // do call far (push cs/ip) in old mapping
                   15972:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15973:                
                   15974:                // get old mapping data
                   15975: #if 0
                   15976:                for(int i = 0; i < (int)old_entries; i++) {
                   15977:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15978:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15979:                        
                   15980:                        if(REG8(AL) == 0x01) {
                   15981:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15982:                        }
                   15983: //                     if(!(physical < 4)) {
                   15984: //                             REG8(AH) = 0x8b;
                   15985: //                             return;
                   15986: //                     } else
                   15987:                        if(logical == 0xffff) {
                   15988:                                ems_unmap_page(physical & 3);
                   15989:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15990:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15991:                        } else {
                   15992:                                REG8(AH) = 0x8a;
                   15993:                                return;
                   15994:                        }
                   15995:                }
                   15996: #endif
                   15997:                for(int i = 0; i < 4; i++) {
                   15998:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15999:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   16000:                }
                   16001:                
                   16002:                // set new mapping
                   16003:                for(int i = 0; i < (int)new_entries; i++) {
                   16004:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   16005:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   16006:                        
                   16007:                        if(REG8(AL) == 0x01) {
                   16008:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   16009:                        }
                   16010: //                     if(!(physical < 4)) {
                   16011: //                             REG8(AH) = 0x8b;
                   16012: //                             return;
                   16013: //                     } else
                   16014:                        if(logical == 0xffff) {
                   16015:                                ems_unmap_page(physical & 3);
                   16016:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   16017:                                ems_map_page(physical & 3, REG16(DX), logical);
                   16018:                        } else {
                   16019:                                REG8(AH) = 0x8a;
                   16020:                                return;
                   16021:                        }
                   16022:                }
                   16023:                
                   16024:                // push old mapping data in new mapping
                   16025:                for(int i = 0; i < 4; i++) {
                   16026:                        i386_push16(handles[i]);
                   16027:                        i386_push16(pages  [i]);
                   16028:                }
                   16029:                REG8(AH) = 0x00;
                   16030:        } else {
                   16031:                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));
                   16032:                REG8(AH) = 0x8f;
                   16033:        }
                   16034: }
                   16035: 
1.1.1.20  root     16036: inline void msdos_int_67h_57h_tmp()
                   16037: {
                   16038:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16039:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16040:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   16041:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   16042:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   16043:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   16044:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16045:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   16046:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   16047:        
1.1.1.32  root     16048:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     16049:        UINT32 src_addr, dest_addr;
                   16050:        UINT32 src_addr_max, dest_addr_max;
                   16051:        
                   16052:        if(src_type == 0) {
                   16053:                src_buffer = mem;
                   16054:                src_addr = (src_seg << 4) + src_ofs;
                   16055:                src_addr_max = MAX_MEM;
                   16056:        } else {
1.1.1.31  root     16057:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     16058:                        REG8(AH) = 0x83;
                   16059:                        return;
                   16060:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   16061:                        REG8(AH) = 0x8a;
                   16062:                        return;
                   16063:                }
1.1.1.32  root     16064:                if(ems_handles[src_handle].buffer != NULL) {
                   16065:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   16066:                }
1.1.1.20  root     16067:                src_addr = src_ofs;
1.1.1.32  root     16068:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     16069:        }
                   16070:        if(dest_type == 0) {
                   16071:                dest_buffer = mem;
                   16072:                dest_addr = (dest_seg << 4) + dest_ofs;
                   16073:                dest_addr_max = MAX_MEM;
                   16074:        } else {
1.1.1.31  root     16075:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     16076:                        REG8(AH) = 0x83;
                   16077:                        return;
                   16078:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   16079:                        REG8(AH) = 0x8a;
                   16080:                        return;
                   16081:                }
1.1.1.32  root     16082:                if(ems_handles[dest_handle].buffer != NULL) {
                   16083:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   16084:                }
1.1.1.20  root     16085:                dest_addr = dest_ofs;
1.1.1.32  root     16086:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     16087:        }
1.1.1.32  root     16088:        if(src_buffer != NULL && dest_buffer != NULL) {
                   16089:                for(int i = 0; i < copy_length; i++) {
                   16090:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16091:                                if(REG8(AL) == 0x00) {
                   16092:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16093:                                } else if(REG8(AL) == 0x01) {
                   16094:                                        UINT8 tmp = dest_buffer[dest_addr];
                   16095:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   16096:                                        src_buffer[src_addr++] = tmp;
                   16097:                                }
                   16098:                        } else {
                   16099:                                REG8(AH) = 0x93;
                   16100:                                return;
1.1.1.20  root     16101:                        }
                   16102:                }
1.1.1.32  root     16103:                REG8(AH) = 0x00;
                   16104:        } else {
                   16105:                REG8(AH) = 0x80;
1.1.1.20  root     16106:        }
                   16107: }
                   16108: 
                   16109: inline void msdos_int_67h_57h()
                   16110: {
                   16111:        if(!support_ems) {
                   16112:                REG8(AH) = 0x84;
                   16113:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   16114:                struct {
                   16115:                        UINT16 handle;
                   16116:                        UINT16 page;
                   16117:                        bool mapped;
                   16118:                } tmp_pages[4];
                   16119:                
                   16120:                // unmap pages to copy memory data to ems buffer
                   16121:                for(int i = 0; i < 4; i++) {
                   16122:                        tmp_pages[i].handle = ems_pages[i].handle;
                   16123:                        tmp_pages[i].page   = ems_pages[i].page;
                   16124:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   16125:                        ems_unmap_page(i);
                   16126:                }
                   16127:                
                   16128:                // run move/exchange operation
                   16129:                msdos_int_67h_57h_tmp();
                   16130:                
                   16131:                // restore unmapped pages
                   16132:                for(int i = 0; i < 4; i++) {
                   16133:                        if(tmp_pages[i].mapped) {
                   16134:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   16135:                        }
                   16136:                }
                   16137:        } else {
1.1.1.22  root     16138:                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     16139:                REG8(AH) = 0x8f;
                   16140:        }
                   16141: }
                   16142: 
                   16143: inline void msdos_int_67h_58h()
                   16144: {
                   16145:        if(!support_ems) {
                   16146:                REG8(AH) = 0x84;
                   16147:        } else if(REG8(AL) == 0x00) {
                   16148:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     16149:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   16150:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     16151:                }
                   16152:                REG8(AH) = 0x00;
                   16153:                REG16(CX) = 4;
                   16154:        } else if(REG8(AL) == 0x01) {
                   16155:                REG8(AH) = 0x00;
                   16156:                REG16(CX) = 4;
                   16157:        } else {
1.1.1.22  root     16158:                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     16159:                REG8(AH) = 0x8f;
                   16160:        }
                   16161: }
                   16162: 
1.1.1.42  root     16163: inline void msdos_int_67h_59h()
                   16164: {
                   16165:        if(!support_ems) {
                   16166:                REG8(AH) = 0x84;
                   16167:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     16168:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   16169:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   16170:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   16171:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   16172:                REG8(AH) = 0x00;
                   16173: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     16174:        } else if(REG8(AL) == 0x01) {
                   16175:                REG8(AH) = 0x00;
                   16176:                REG16(BX) = free_ems_pages;
                   16177:                REG16(DX) = MAX_EMS_PAGES;
                   16178:        } else {
                   16179:                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));
                   16180:                REG8(AH) = 0x8f;
                   16181:        }
                   16182: }
                   16183: 
1.1.1.20  root     16184: inline void msdos_int_67h_5ah()
                   16185: {
                   16186:        if(!support_ems) {
1.1.1.19  root     16187:                REG8(AH) = 0x84;
1.1.1.20  root     16188:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   16189:                REG8(AH) = 0x87;
                   16190:        } else if(REG16(BX) > free_ems_pages) {
                   16191:                REG8(AH) = 0x88;
                   16192: //     } else if(REG16(BX) == 0) {
                   16193: //             REG8(AH) = 0x89;
                   16194:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     16195:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     16196:                        if(!ems_handles[i].allocated) {
                   16197:                                ems_allocate_pages(i, REG16(BX));
                   16198:                                REG8(AH) = 0x00;
                   16199:                                REG16(DX) = i;
                   16200:                                return;
                   16201:                        }
                   16202:                }
                   16203:                REG8(AH) = 0x85;
                   16204:        } else {
1.1.1.22  root     16205:                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     16206:                REG8(AH) = 0x8f;
1.1.1.19  root     16207:        }
                   16208: }
                   16209: 
1.1.1.49  root     16210: inline void msdos_int_67h_5bh()
                   16211: {
                   16212:        static UINT8  stored_bl = 0x00;
                   16213:        static UINT16 stored_es = 0x0000;
                   16214:        static UINT16 stored_di = 0x0000;
                   16215:        
                   16216:        if(!support_ems) {
                   16217:                REG8(AH) = 0x84;
                   16218:        } else if(REG8(AL) == 0x00) {
                   16219:                if(stored_bl == 0x00) {
                   16220:                        if(!(stored_es == 0 && stored_di == 0)) {
                   16221:                                for(int i = 0; i < 4; i++) {
                   16222:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   16223:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   16224:                                }
                   16225:                        }
                   16226:                        SREG(ES) = stored_es;
                   16227:                        i386_load_segment_descriptor(ES);
                   16228:                        REG16(DI) = stored_di;
                   16229:                } else {
                   16230:                        REG8(BL) = stored_bl;
                   16231:                }
                   16232:                REG8(AH) = 0x00;
                   16233:        } else if(REG8(AL) == 0x01) {
                   16234:                if(REG8(BL) == 0x00) {
                   16235:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   16236:                                for(int i = 0; i < 4; i++) {
                   16237:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   16238:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   16239:                                        
                   16240:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   16241:                                                ems_map_page(i, handle, page);
                   16242:                                        } else {
                   16243:                                                ems_unmap_page(i);
                   16244:                                        }
                   16245:                                }
                   16246:                        }
                   16247:                }
                   16248:                stored_bl = REG8(BL);
                   16249:                stored_es = SREG(ES);
                   16250:                stored_di = REG16(DI);
                   16251:                REG8(AH) = 0x00;
                   16252:        } else if(REG8(AL) == 0x02) {
                   16253:                REG16(DX) = 4 * 4;
                   16254:                REG8(AH) = 0x00;
                   16255:        } else if(REG8(AL) == 0x03) {
                   16256:                REG8(BL) = 0x00; // not supported
                   16257:                REG8(AH) = 0x00;
                   16258:        } else if(REG8(AL) == 0x04) {
                   16259:                REG8(AH) = 0x00;
                   16260:        } else if(REG8(AL) == 0x05) {
                   16261:                REG8(BL) = 0x00; // not supported
                   16262:                REG8(AH) = 0x00;
                   16263:        } else {
                   16264:                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));
                   16265:                REG8(AH) = 0x8f;
                   16266:        }
                   16267: }
                   16268: 
1.1.1.43  root     16269: inline void msdos_int_67h_5dh()
                   16270: {
                   16271:        if(!support_ems) {
                   16272:                REG8(AH) = 0x84;
                   16273:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   16274:                REG8(AH) = 0xa4; // operating system denied access
                   16275:        } else {
                   16276:                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));
                   16277:                REG8(AH) = 0x8f;
                   16278:        }
                   16279: }
                   16280: 
1.1.1.49  root     16281: inline void msdos_int_67h_70h()
                   16282: {
                   16283:        if(!support_ems) {
                   16284:                REG8(AH) = 0x84;
                   16285:        } else if(REG8(AL) == 0x00) {
                   16286:                REG8(AL) = 0x00;
                   16287:                REG8(AH) = 0x00;
                   16288:        } else if(REG8(AL) == 0x01) {
                   16289:                REG8(AL) = 0x00;
                   16290: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   16291:                REG8(AH) = 0x00;
                   16292:        } else {
                   16293:                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));
                   16294:                REG8(AH) = 0x8f;
                   16295:        }
                   16296: }
                   16297: 
1.1.1.30  root     16298: inline void msdos_int_67h_deh()
                   16299: {
1.1.1.63  root     16300: #if defined(SUPPORT_VCPI)
                   16301:        if(!support_ems) {
                   16302:                REG8(AH) = 0x84;
                   16303:        } else if(REG8(AL) == 0x00) {
                   16304:                REG8(AH) = 0x00;
                   16305:                REG16(BX) = 0x0100;
                   16306:        } else if(REG8(AL) == 0x01) {
                   16307:                REG8(AH) = 0x00;
                   16308:                // from DOSBox
                   16309:                for(int ct = 0; ct < 0xff; ct++) {
                   16310:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x00) = 0x67;            // access bits
                   16311:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x01) = ct * 0x10;       // mapping
                   16312:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x03) = 0x00;
                   16313:                }
                   16314:                for(int ct = 0xff; ct < 0x100; ct++) {
                   16315:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x00) = 0x67;            // access bits
                   16316:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x01) = (ct - 0xff) * 0x10 + 0x1100;     // mapping
                   16317:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x03) = 0x00;
                   16318:                }
                   16319:                REG16(DI) += 0x400;             // advance pointer by 0x100*4
                   16320:                
                   16321:                // Set up three descriptor table entries
                   16322:                UINT32 cbseg_low  = (DUMMY_TOP & 0x00ffff) << 16;
                   16323:                UINT32 cbseg_high = (DUMMY_TOP & 0x1f0000) >> 16;
                   16324:                // Descriptor 1 (code segment, callback segment)
                   16325:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x0000ffff | cbseg_low ;
                   16326:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = 0x00009a00 | cbseg_high;
                   16327:                // Descriptor 2 (data segment, full access)
                   16328:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x08) = 0x0000ffff;
                   16329:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c) = 0x00009200;
                   16330:                // Descriptor 3 (full access)
                   16331:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10) = 0x0000ffff;
                   16332:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x14) = 0x00009200;
                   16333:                // Offset in code segment of protected mode entry point
                   16334:                REG32(EBX) = 0x2a; // fffc:002a
                   16335:        } else if(REG8(AL) == 0x02) {
                   16336:                REG8(AH) = 0x00;
                   16337:                REG32(EDX) = (MAX_MEM - 1) & 0xfffff000;
                   16338:        } else if(REG8(AL) == 0x03) {
                   16339:                REG8(AH) = 0x00;
                   16340:                REG32(EDX) = 0;
                   16341:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16342:                        if(emb_handle->handle == 0) {
                   16343:                                REG32(EDX) += emb_handle->size_kb;
                   16344:                        }
                   16345:                }
                   16346:                REG32(EDX) /= 4;
                   16347:        } else if(REG8(AL) == 0x04) {
                   16348:                emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(4);
                   16349:                if(emb_handle != NULL) {
                   16350:                        REG8(AH) = 0x00;
                   16351:                        REG32(EDX) = emb_handle->address;
                   16352:                }
                   16353:        } else if(REG8(AL) == 0x05) {
                   16354:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16355:                        if(emb_handle->handle != 0 && emb_handle->address == REG32(EDX)) {
                   16356:                                REG8(AH) = 0x00;
                   16357:                                msdos_xms_free_emb_handle(emb_handle);
                   16358:                                break;
                   16359:                        }
                   16360:                }
                   16361:        } else if(REG8(AL) == 0x06) {
                   16362:                REG8(AH) = 0x00;
                   16363:                REG32(EDX) = REG16(CX) << 12;
                   16364:        } else if(REG8(AL) == 0x07) {
                   16365:                REG8(AH) = 0x00;
                   16366:                REG32(EBX) = m_cr[0];
                   16367:        } else if(REG8(AL) == 0x08) {
                   16368:                REG8(AH) = 0x00;
                   16369:                for(int i = 0; i < 8; i++) {
                   16370:                        *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i) = m_dr[i];
                   16371:                }
                   16372:        } else if(REG8(AL) == 0x09) {
                   16373:                REG8(AH) = 0x00;
                   16374:                for(int i = 0; i < 8; i++) {
                   16375:                        m_dr[i] = *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i);
                   16376:                }
                   16377:        } else if(REG8(AL) == 0x0a) {
                   16378:                REG8(AH) = 0x00;
                   16379:                REG16(BX) = pic[0].icw2;
                   16380:                REG16(CX) = pic[1].icw2;
                   16381:        } else if(REG8(AL) == 0x0b) {
                   16382:                REG8(AH) = 0x00;
                   16383:                pic[0].icw2 = REG8(BL);
                   16384:                pic[1].icw2 = REG8(CL);
                   16385:        } else if(REG8(AL) == 0x0c) {
                   16386:                // from DOSBox
                   16387:                m_IF = 0;
                   16388:                m_CPL = 0;
                   16389:                
                   16390:                // Read data from ESI (linear address)
                   16391:                UINT32 new_cr3      = *(UINT32 *)(mem + REG32(ESI) + 0x00);
                   16392:                UINT32 new_gdt_addr = *(UINT32 *)(mem + REG32(ESI) + 0x04);
                   16393:                UINT32 new_idt_addr = *(UINT32 *)(mem + REG32(ESI) + 0x08);
                   16394:                UINT16 new_ldt      = *(UINT16 *)(mem + REG32(ESI) + 0x0c);
                   16395:                UINT16 new_tr       = *(UINT16 *)(mem + REG32(ESI) + 0x0e);
                   16396:                UINT32 new_eip      = *(UINT32 *)(mem + REG32(ESI) + 0x10);
                   16397:                UINT16 new_cs       = *(UINT16 *)(mem + REG32(ESI) + 0x14);
                   16398:                
                   16399:                // Get GDT and IDT entries
                   16400:                UINT16 new_gdt_limit = *(UINT16 *)(mem + new_gdt_addr + 0);
                   16401:                UINT32 new_gdt_base  = *(UINT32 *)(mem + new_gdt_addr + 2);
                   16402:                UINT16 new_idt_limit = *(UINT16 *)(mem + new_idt_addr + 0);
                   16403:                UINT32 new_idt_base  = *(UINT32 *)(mem + new_idt_addr + 2);
                   16404:                
                   16405:                // Switch to protected mode, paging enabled if necessary
                   16406:                if(new_cr3 != 0) {
                   16407:                        m_cr[0] |= 0x80000000;
                   16408:                }
                   16409:                m_cr[3] = new_cr3;
                   16410:                
                   16411:                *(UINT8 *)(mem + new_gdt_base + (new_tr & 0xfff8) + 5) &= 0xfd;
                   16412:                
                   16413:                // Load tables and initialize segment registers
                   16414:                m_gdtr.limit = new_gdt_limit;
                   16415:                m_gdtr.base = new_gdt_base;
                   16416:                m_idtr.limit = new_idt_limit;
                   16417:                m_idtr.base = new_idt_base;
                   16418:                
1.1.1.64! root     16419:                i386_sreg_load(0x00, DS, NULL);
        !          16420:                i386_sreg_load(0x00, ES, NULL);
        !          16421:                i386_sreg_load(0x00, FS, NULL);
        !          16422:                i386_sreg_load(0x00, GS, NULL);
1.1.1.63  root     16423:                
                   16424: //             i386_set_a20_line(1);
                   16425:                
                   16426:                /* Switch to protected mode */
                   16427:                m_VM = m_NT = 0;
                   16428:                m_IOP1 = m_IOP2 = 1;
                   16429:                
                   16430:                i386_jmp_far(new_cs, new_eip);
                   16431:        } else {
                   16432:                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));
                   16433:                REG8(AH) = 0x8f;
                   16434:        }
                   16435: #else
1.1.1.30  root     16436:        REG8(AH) = 0x84;
1.1.1.63  root     16437: #endif
1.1.1.30  root     16438: }
                   16439: 
1.1.1.19  root     16440: #ifdef SUPPORT_XMS
                   16441: 
1.1.1.32  root     16442: void msdos_xms_init()
1.1.1.26  root     16443: {
1.1.1.30  root     16444:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16445:        emb_handle_top->address = EMB_TOP;
                   16446:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     16447:        xms_a20_local_enb_count = 0;
                   16448: }
                   16449: 
1.1.1.32  root     16450: void msdos_xms_finish()
                   16451: {
                   16452:        msdos_xms_release();
                   16453: }
                   16454: 
                   16455: void msdos_xms_release()
1.1.1.30  root     16456: {
                   16457:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   16458:                emb_handle_t *next_handle = emb_handle->next;
                   16459:                free(emb_handle);
                   16460:                emb_handle = next_handle;
                   16461:        }
                   16462: }
                   16463: 
                   16464: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   16465: {
                   16466:        if(handle != 0) {
                   16467:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16468:                        if(emb_handle->handle == handle) {
                   16469:                                return(emb_handle);
                   16470:                        }
                   16471:                }
                   16472:        }
                   16473:        return(NULL);
                   16474: }
                   16475: 
                   16476: int msdos_xms_get_unused_emb_handle_id()
                   16477: {
                   16478:        for(int handle = 1;; handle++) {
                   16479:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   16480:                        return(handle);
                   16481:                }
                   16482:        }
                   16483:        return(0);
                   16484: }
                   16485: 
                   16486: int msdos_xms_get_unused_emb_handle_count()
                   16487: {
                   16488:        int count = 64; //255;
                   16489:        
                   16490:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16491:                if(emb_handle->handle != 0) {
                   16492:                        if(--count == 1) {
                   16493:                                break;
                   16494:                        }
                   16495:                }
                   16496:        }
                   16497:        return(count);
                   16498: }
                   16499: 
                   16500: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   16501: {
                   16502:        if(emb_handle->size_kb > size_kb) {
                   16503:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16504:                
                   16505:                new_handle->address = emb_handle->address + size_kb * 1024;
                   16506:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   16507:                emb_handle->size_kb = size_kb;
                   16508:                
                   16509:                new_handle->prev = emb_handle;
                   16510:                new_handle->next = emb_handle->next;
                   16511:                if(emb_handle->next != NULL) {
                   16512:                        emb_handle->next->prev = new_handle;
                   16513:                }
                   16514:                emb_handle->next = new_handle;
                   16515:        }
                   16516: }
                   16517: 
                   16518: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   16519: {
                   16520:        emb_handle_t *next_handle = emb_handle->next;
                   16521:        
                   16522:        if(next_handle != NULL) {
                   16523:                emb_handle->size_kb += next_handle->size_kb;
                   16524:                
                   16525:                if(next_handle->next != NULL) {
                   16526:                        next_handle->next->prev = emb_handle;
                   16527:                }
                   16528:                emb_handle->next = next_handle->next;
                   16529:                free(next_handle);
                   16530:        }
                   16531: }
                   16532: 
                   16533: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   16534: {
                   16535:        emb_handle_t *target_handle = NULL;
                   16536:        
                   16537:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16538:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   16539:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   16540:                                target_handle = emb_handle;
                   16541:                        }
                   16542:                }
                   16543:        }
                   16544:        if(target_handle != NULL) {
                   16545:                if(target_handle->size_kb > size_kb) {
                   16546:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   16547:                }
                   16548: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16549:                return(target_handle);
                   16550:        }
                   16551:        return(NULL);
                   16552: }
                   16553: 
                   16554: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   16555: {
                   16556:        emb_handle_t *prev_handle = emb_handle->prev;
                   16557:        emb_handle_t *next_handle = emb_handle->next;
                   16558:        
                   16559:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   16560:                msdos_xms_combine_emb_handles(prev_handle);
                   16561:                emb_handle = prev_handle;
                   16562:        }
                   16563:        if(next_handle != NULL && next_handle->handle == 0) {
                   16564:                msdos_xms_combine_emb_handles(emb_handle);
                   16565:        }
                   16566:        emb_handle->handle = 0;
                   16567: }
                   16568: 
1.1.1.19  root     16569: inline void msdos_call_xms_00h()
                   16570: {
1.1.1.29  root     16571: #if defined(HAS_I386)
                   16572:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.63  root     16573:        REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
                   16574: //     REG16(BX) = 0x035f; // V3.95 (Driver Revision)
1.1.1.29  root     16575: #else
                   16576:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   16577:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   16578: #endif
                   16579: //     REG16(DX) = 0x0000; // HMA does not exist
                   16580:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     16581: }
                   16582: 
                   16583: inline void msdos_call_xms_01h()
                   16584: {
1.1.1.29  root     16585:        if(REG8(AL) == 0x40) {
                   16586:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   16587:                // DX=KB free extended memory returned by last call of function 08h
                   16588:                REG16(AX) = 0x0000;
                   16589:                REG8(BL) = 0x91;
                   16590:                REG16(DX) = xms_dx_after_call_08h;
                   16591:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16592:                REG16(AX) = 0x0000;
                   16593:                REG8(BL) = 0x81; // Vdisk was detected
                   16594: #ifdef SUPPORT_HMA
                   16595:        } else if(is_hma_used_by_int_2fh) {
                   16596:                REG16(AX) = 0x0000;
                   16597:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16598:        } else if(is_hma_used_by_xms) {
                   16599:                REG16(AX) = 0x0000;
                   16600:                REG8(BL) = 0x91; // HMA is already in use
                   16601:        } else {
                   16602:                REG16(AX) = 0x0001;
                   16603:                is_hma_used_by_xms = true;
                   16604: #else
                   16605:        } else {
                   16606:                REG16(AX) = 0x0000;
                   16607:                REG8(BL) = 0x91; // HMA is already in use
                   16608: #endif
                   16609:        }
1.1.1.19  root     16610: }
                   16611: 
                   16612: inline void msdos_call_xms_02h()
                   16613: {
1.1.1.29  root     16614:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16615:                REG16(AX) = 0x0000;
                   16616:                REG8(BL) = 0x81; // Vdisk was detected
                   16617: #ifdef SUPPORT_HMA
                   16618:        } else if(is_hma_used_by_int_2fh) {
                   16619:                REG16(AX) = 0x0000;
                   16620:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16621:        } else if(!is_hma_used_by_xms) {
                   16622:                REG16(AX) = 0x0000;
                   16623:                REG8(BL) = 0x93; // HMA is not allocated
                   16624:        } else {
                   16625:                REG16(AX) = 0x0001;
                   16626:                is_hma_used_by_xms = false;
                   16627:                // restore first free mcb in high memory area
                   16628:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16629: #else
                   16630:        } else {
                   16631:                REG16(AX) = 0x0000;
                   16632:                REG8(BL) = 0x91; // HMA is already in use
                   16633: #endif
                   16634:        }
1.1.1.19  root     16635: }
                   16636: 
                   16637: inline void msdos_call_xms_03h()
                   16638: {
                   16639:        i386_set_a20_line(1);
                   16640:        REG16(AX) = 0x0001;
                   16641:        REG8(BL) = 0x00;
                   16642: }
                   16643: 
                   16644: inline void msdos_call_xms_04h()
                   16645: {
1.1.1.21  root     16646:        i386_set_a20_line(0);
                   16647:        REG16(AX) = 0x0001;
                   16648:        REG8(BL) = 0x00;
1.1.1.19  root     16649: }
                   16650: 
                   16651: inline void msdos_call_xms_05h()
                   16652: {
                   16653:        i386_set_a20_line(1);
                   16654:        REG16(AX) = 0x0001;
                   16655:        REG8(BL) = 0x00;
1.1.1.21  root     16656:        xms_a20_local_enb_count++;
1.1.1.19  root     16657: }
                   16658: 
                   16659: void msdos_call_xms_06h()
                   16660: {
1.1.1.21  root     16661:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16662:                if(--xms_a20_local_enb_count == 0) {
                   16663:                        i386_set_a20_line(0);
                   16664:                        REG16(AX) = 0x0001;
                   16665:                        REG8(BL) = 0x00;
                   16666:                } else {
                   16667:                        REG16(AX) = 0x0000;
                   16668:                        REG8(BL) = 0x94;
                   16669:                }
1.1.1.21  root     16670:        } else {
1.1.1.45  root     16671:                i386_set_a20_line(0);
1.1.1.21  root     16672:                REG16(AX) = 0x0001;
                   16673:                REG8(BL) = 0x00;
1.1.1.19  root     16674:        }
                   16675: }
                   16676: 
                   16677: inline void msdos_call_xms_07h()
                   16678: {
                   16679:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16680:        REG8(BL) = 0x00;
                   16681: }
                   16682: 
                   16683: inline void msdos_call_xms_08h()
                   16684: {
1.1.1.45  root     16685:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16686:        
1.1.1.30  root     16687:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16688:                if(emb_handle->handle == 0) {
1.1.1.45  root     16689:                        if(eax < emb_handle->size_kb) {
                   16690:                                eax = emb_handle->size_kb;
1.1.1.19  root     16691:                        }
1.1.1.45  root     16692:                        edx += emb_handle->size_kb;
1.1.1.19  root     16693:                }
                   16694:        }
1.1.1.45  root     16695:        if(eax > 65535) {
                   16696:                eax = 65535;
                   16697:        }
                   16698:        if(edx > 65535) {
                   16699:                edx = 65535;
                   16700:        }
                   16701:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16702:                REG8(BL) = 0xa0;
                   16703:        } else {
                   16704:                REG8(BL) = 0x00;
                   16705:        }
1.1.1.45  root     16706: #if defined(HAS_I386)
                   16707:        REG32(EAX) = eax;
                   16708:        REG32(EDX) = edx;
                   16709: #else
                   16710:        REG16(AX) = (UINT16)eax;
                   16711:        REG16(DX) = (UINT16)edx;
                   16712: #endif
1.1.1.29  root     16713:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16714: }
                   16715: 
1.1.1.30  root     16716: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16717: {
1.1.1.30  root     16718:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16719:        
                   16720:        if(emb_handle != NULL) {
                   16721:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16722:                
                   16723:                REG16(AX) = 0x0001;
                   16724:                REG16(DX) = emb_handle->handle;
                   16725:                REG8(BL) = 0x00;
                   16726:        } else {
                   16727:                REG16(AX) = REG16(DX) = 0x0000;
                   16728:                REG8(BL) = 0xa0;
1.1.1.19  root     16729:        }
1.1.1.30  root     16730: }
                   16731: 
                   16732: inline void msdos_call_xms_09h()
                   16733: {
                   16734:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16735: }
                   16736: 
                   16737: inline void msdos_call_xms_0ah()
                   16738: {
1.1.1.30  root     16739:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16740:        
                   16741:        if(emb_handle == NULL) {
1.1.1.19  root     16742:                REG16(AX) = 0x0000;
                   16743:                REG8(BL) = 0xa2;
1.1.1.45  root     16744: //     } else if(emb_handle->lock > 0) {
                   16745: //             REG16(AX) = 0x0000;
                   16746: //             REG8(BL) = 0xab;
1.1.1.19  root     16747:        } else {
1.1.1.30  root     16748:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16749:                
                   16750:                REG16(AX) = 0x0001;
                   16751:                REG8(BL) = 0x00;
                   16752:        }
                   16753: }
                   16754: 
                   16755: inline void msdos_call_xms_0bh()
                   16756: {
                   16757:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16758:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16759:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16760:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16761:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16762:        
                   16763:        UINT8 *src_buffer, *dest_buffer;
                   16764:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16765:        emb_handle_t *emb_handle;
1.1.1.19  root     16766:        
                   16767:        if(src_handle == 0) {
                   16768:                src_buffer = mem;
                   16769:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16770:                src_addr_max = MAX_MEM;
                   16771:        } else {
1.1.1.30  root     16772:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16773:                        REG16(AX) = 0x0000;
                   16774:                        REG8(BL) = 0xa3;
                   16775:                        return;
                   16776:                }
1.1.1.30  root     16777:                src_buffer = mem + emb_handle->address;
                   16778:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16779:        }
                   16780:        if(dest_handle == 0) {
                   16781:                dest_buffer = mem;
                   16782:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16783:                dest_addr_max = MAX_MEM;
                   16784:        } else {
1.1.1.30  root     16785:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16786:                        REG16(AX) = 0x0000;
                   16787:                        REG8(BL) = 0xa5;
                   16788:                        return;
                   16789:                }
1.1.1.30  root     16790:                dest_buffer = mem + emb_handle->address;
                   16791:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16792:        }
                   16793:        for(int i = 0; i < copy_length; i++) {
                   16794:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16795:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16796:                } else {
                   16797:                        break;
                   16798:                }
                   16799:        }
                   16800:        REG16(AX) = 0x0001;
                   16801:        REG8(BL) = 0x00;
                   16802: }
                   16803: 
                   16804: inline void msdos_call_xms_0ch()
                   16805: {
1.1.1.30  root     16806:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16807:        
                   16808:        if(emb_handle == NULL) {
1.1.1.19  root     16809:                REG16(AX) = 0x0000;
                   16810:                REG8(BL) = 0xa2;
                   16811:        } else {
1.1.1.45  root     16812:                if(emb_handle->lock < 255) {
                   16813:                        emb_handle->lock++;
                   16814:                }
1.1.1.19  root     16815:                REG16(AX) = 0x0001;
1.1.1.30  root     16816:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16817:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16818:        }
                   16819: }
                   16820: 
                   16821: inline void msdos_call_xms_0dh()
                   16822: {
1.1.1.30  root     16823:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16824:        
                   16825:        if(emb_handle == NULL) {
1.1.1.19  root     16826:                REG16(AX) = 0x0000;
                   16827:                REG8(BL) = 0xa2;
1.1.1.30  root     16828:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16829:                REG16(AX) = 0x0000;
                   16830:                REG8(BL) = 0xaa;
                   16831:        } else {
1.1.1.30  root     16832:                emb_handle->lock--;
1.1.1.19  root     16833:                REG16(AX) = 0x0001;
                   16834:                REG8(BL) = 0x00;
                   16835:        }
                   16836: }
                   16837: 
                   16838: inline void msdos_call_xms_0eh()
                   16839: {
1.1.1.30  root     16840:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16841:        
                   16842:        if(emb_handle == NULL) {
1.1.1.19  root     16843:                REG16(AX) = 0x0000;
                   16844:                REG8(BL) = 0xa2;
                   16845:        } else {
                   16846:                REG16(AX) = 0x0001;
1.1.1.30  root     16847:                REG8(BH) = emb_handle->lock;
                   16848:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16849:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16850:        }
                   16851: }
                   16852: 
1.1.1.30  root     16853: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16854: {
1.1.1.30  root     16855:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16856:        
                   16857:        if(emb_handle == NULL) {
1.1.1.19  root     16858:                REG16(AX) = 0x0000;
                   16859:                REG8(BL) = 0xa2;
1.1.1.30  root     16860:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16861:                REG16(AX) = 0x0000;
                   16862:                REG8(BL) = 0xab;
                   16863:        } else {
1.1.1.30  root     16864:                if(emb_handle->size_kb < size_kb) {
                   16865:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16866:                                msdos_xms_combine_emb_handles(emb_handle);
                   16867:                                if(emb_handle->size_kb > size_kb) {
                   16868:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16869:                                }
                   16870:                        } else {
                   16871:                                int old_handle = emb_handle->handle;
                   16872:                                int old_size_kb = emb_handle->size_kb;
                   16873:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16874:                                
                   16875:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16876:                                msdos_xms_free_emb_handle(emb_handle);
                   16877:                                
                   16878:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16879:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16880:                                }
                   16881:                                emb_handle->handle = old_handle;
                   16882:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16883:                                free(buffer);
                   16884:                        }
                   16885:                } else if(emb_handle->size_kb > size_kb) {
                   16886:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16887:                }
                   16888:                if(emb_handle->size_kb != size_kb) {
                   16889:                        REG16(AX) = 0x0000;
                   16890:                        REG8(BL) = 0xa0;
                   16891:                } else {
                   16892:                        REG16(AX) = 0x0001;
                   16893:                        REG8(BL) = 0x00;
                   16894:                }
1.1.1.19  root     16895:        }
                   16896: }
                   16897: 
1.1.1.30  root     16898: inline void msdos_call_xms_0fh()
                   16899: {
                   16900:        msdos_call_xms_0fh(REG16(BX));
                   16901: }
                   16902: 
1.1.1.19  root     16903: inline void msdos_call_xms_10h()
                   16904: {
                   16905:        int seg;
                   16906:        
                   16907:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16908:                REG16(AX) = 0x0001;
                   16909:                REG16(BX) = seg;
                   16910:        } else {
                   16911:                REG16(AX) = 0x0000;
                   16912:                REG8(BL) = 0xb0;
                   16913:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16914:        }
                   16915: }
                   16916: 
                   16917: inline void msdos_call_xms_11h()
                   16918: {
                   16919:        int mcb_seg = REG16(DX) - 1;
                   16920:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16921:        
                   16922:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16923:                msdos_mem_free(REG16(DX));
                   16924:                REG16(AX) = 0x0001;
                   16925:                REG8(BL) = 0x00;
                   16926:        } else {
                   16927:                REG16(AX) = 0x0000;
                   16928:                REG8(BL) = 0xb2;
                   16929:        }
                   16930: }
                   16931: 
                   16932: inline void msdos_call_xms_12h()
                   16933: {
                   16934:        int mcb_seg = REG16(DX) - 1;
                   16935:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16936:        int max_paragraphs;
                   16937:        
                   16938:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16939:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16940:                        REG16(AX) = 0x0001;
                   16941:                        REG8(BL) = 0x00;
                   16942:                } else {
                   16943:                        REG16(AX) = 0x0000;
                   16944:                        REG8(BL) = 0xb0;
                   16945:                        REG16(DX) = max_paragraphs;
                   16946:                }
                   16947:        } else {
                   16948:                REG16(AX) = 0x0000;
                   16949:                REG8(BL) = 0xb2;
                   16950:        }
                   16951: }
                   16952: 
1.1.1.29  root     16953: #if defined(HAS_I386)
                   16954: 
                   16955: inline void msdos_call_xms_88h()
                   16956: {
                   16957:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16958:        
1.1.1.30  root     16959:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16960:                if(emb_handle->handle == 0) {
                   16961:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16962:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16963:                        }
1.1.1.30  root     16964:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16965:                }
                   16966:        }
                   16967:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16968:                REG8(BL) = 0xa0;
                   16969:        } else {
                   16970:                REG8(BL) = 0x00;
                   16971:        }
                   16972:        REG32(ECX) = EMB_END - 1;
                   16973: }
                   16974: 
                   16975: inline void msdos_call_xms_89h()
                   16976: {
1.1.1.30  root     16977:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16978: }
                   16979: 
                   16980: inline void msdos_call_xms_8eh()
                   16981: {
1.1.1.30  root     16982:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16983:        
                   16984:        if(emb_handle == NULL) {
1.1.1.29  root     16985:                REG16(AX) = 0x0000;
                   16986:                REG8(BL) = 0xa2;
                   16987:        } else {
                   16988:                REG16(AX) = 0x0001;
1.1.1.30  root     16989:                REG8(BH) = emb_handle->lock;
                   16990:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16991:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16992:        }
                   16993: }
                   16994: 
                   16995: inline void msdos_call_xms_8fh()
                   16996: {
1.1.1.30  root     16997:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16998: }
                   16999: 
                   17000: #endif
1.1.1.19  root     17001: #endif
                   17002: 
1.1.1.26  root     17003: UINT16 msdos_get_equipment()
                   17004: {
                   17005:        static UINT16 equip = 0;
                   17006:        
                   17007:        if(equip == 0) {
                   17008: #ifdef SUPPORT_FPU
                   17009:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   17010: #endif
                   17011:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   17012:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   17013: //             equip |= (1 << 8);      // 0 if DMA installed
                   17014:                equip |= (2 << 9);      // number of serial ports
                   17015:                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     17016:                
                   17017:                // check only A: and B: if it is floppy drive
                   17018:                int n = 0;
                   17019:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     17020:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   17021:                                n++;
1.1.1.28  root     17022:                        }
                   17023:                }
                   17024:                if(n != 0) {
                   17025:                        equip |= (1 << 0);      // floppy disk(s) installed
                   17026:                        n--;
                   17027:                        equip |= (n << 6);      // number of floppies installed less 1
                   17028:                }
                   17029: //             if(joyGetNumDevs() != 0) {
                   17030: //                     equip |= (1 << 12);     // game port installed
                   17031: //             }
1.1.1.26  root     17032:        }
                   17033:        return(equip);
                   17034: }
                   17035: 
1.1       root     17036: void msdos_syscall(unsigned num)
                   17037: {
1.1.1.22  root     17038: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     17039:        if(num == 0x08 || num == 0x1c) {
                   17040:                // don't log the timer interrupts
1.1.1.45  root     17041: //             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     17042:        } else if(num == 0x30) {
                   17043:                // dummy interrupt for call 0005h (call near)
                   17044:                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     17045:        } else if(num == 0x65) {
1.1.1.22  root     17046:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     17047:                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     17048:        } else if(num == 0x66) {
1.1.1.22  root     17049:                // dummy interrupt for XMS (call far)
1.1.1.33  root     17050:                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     17051:        } else if(num >= 0x68 && num <= 0x6f) {
1.1.1.45  root     17052:                // dummy interrupt
1.1.1.22  root     17053:        } else {
1.1.1.33  root     17054:                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     17055:        }
                   17056: #endif
1.1.1.36  root     17057:        // update cursor position
                   17058:        if(cursor_moved) {
                   17059:                pcbios_update_cursor_position();
                   17060:                cursor_moved = false;
                   17061:        }
1.1.1.50  root     17062: #ifdef USE_SERVICE_THREAD
                   17063:        // this is called from dummy loop to wait until a serive that waits input is done
                   17064:        if(!in_service)
                   17065: #endif
1.1.1.33  root     17066:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     17067:        
1.1       root     17068:        switch(num) {
                   17069:        case 0x00:
1.1.1.28  root     17070:                try {
                   17071:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17072:                        error("division by zero\n");
                   17073:                } catch(...) {
                   17074:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   17075:                }
1.1       root     17076:                break;
                   17077:        case 0x04:
1.1.1.28  root     17078:                try {
                   17079:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17080:                        error("overflow\n");
                   17081:                } catch(...) {
                   17082:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   17083:                }
1.1       root     17084:                break;
                   17085:        case 0x06:
                   17086:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     17087:                if(!ignore_illegal_insn) {
1.1.1.28  root     17088:                        try {
                   17089:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17090:                                error("illegal instruction\n");
                   17091:                        } catch(...) {
                   17092:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   17093:                        }
1.1.1.14  root     17094:                } else {
                   17095: #if defined(HAS_I386)
1.1.1.39  root     17096:                        m_eip = m_int6h_skip_eip;
                   17097: #elif defined(HAS_I286)
                   17098:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     17099: #else
1.1.1.39  root     17100:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     17101: #endif
                   17102:                }
1.1       root     17103:                break;
1.1.1.33  root     17104:        case 0x09:
                   17105:                // ctrl-break is pressed
                   17106:                if(raise_int_1bh) {
                   17107: #if defined(HAS_I386)
                   17108:                        m_ext = 0; // not an external interrupt
                   17109:                        i386_trap(0x1b, 1, 0);
                   17110:                        m_ext = 1;
                   17111: #else
                   17112:                        PREFIX86(_interrupt)(0x1b);
                   17113: #endif
                   17114:                        raise_int_1bh = false;
                   17115:                }
1.1.1.8   root     17116:        case 0x08:
1.1.1.14  root     17117: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     17118:        case 0x0b:
                   17119:        case 0x0c:
                   17120:        case 0x0d:
                   17121:        case 0x0e:
                   17122:        case 0x0f:
                   17123:                // EOI
                   17124:                pic[0].isr &= ~(1 << (num - 0x08));
                   17125:                pic_update();
                   17126:                break;
1.1       root     17127:        case 0x10:
                   17128:                // PC BIOS - Video
1.1.1.14  root     17129:                if(!restore_console_on_exit) {
1.1.1.15  root     17130:                        change_console_size(scr_width, scr_height);
1.1       root     17131:                }
1.1.1.3   root     17132:                m_CF = 0;
1.1       root     17133:                switch(REG8(AH)) {
1.1.1.16  root     17134:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     17135:                case 0x01: pcbios_int_10h_01h(); break;
                   17136:                case 0x02: pcbios_int_10h_02h(); break;
                   17137:                case 0x03: pcbios_int_10h_03h(); break;
                   17138:                case 0x05: pcbios_int_10h_05h(); break;
                   17139:                case 0x06: pcbios_int_10h_06h(); break;
                   17140:                case 0x07: pcbios_int_10h_07h(); break;
                   17141:                case 0x08: pcbios_int_10h_08h(); break;
                   17142:                case 0x09: pcbios_int_10h_09h(); break;
                   17143:                case 0x0a: pcbios_int_10h_0ah(); break;
                   17144:                case 0x0b: break;
1.1.1.40  root     17145:                case 0x0c: pcbios_int_10h_0ch(); break;
                   17146:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     17147:                case 0x0e: pcbios_int_10h_0eh(); break;
                   17148:                case 0x0f: pcbios_int_10h_0fh(); break;
                   17149:                case 0x10: break;
1.1.1.14  root     17150:                case 0x11: pcbios_int_10h_11h(); break;
                   17151:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     17152:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     17153:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     17154:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     17155:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   17156:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     17157:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     17158:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   17159:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     17160:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     17161:                case 0x6f: break;
1.1.1.22  root     17162:                case 0x80: m_CF = 1; break; // unknown
                   17163:                case 0x81: m_CF = 1; break; // unknown
1.1       root     17164:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     17165:                case 0x83: pcbios_int_10h_83h(); break;
                   17166:                case 0x8b: break;
                   17167:                case 0x8c: m_CF = 1; break; // unknown
                   17168:                case 0x8d: m_CF = 1; break; // unknown
                   17169:                case 0x8e: m_CF = 1; break; // unknown
                   17170:                case 0x90: pcbios_int_10h_90h(); break;
                   17171:                case 0x91: pcbios_int_10h_91h(); break;
                   17172:                case 0x92: break;
                   17173:                case 0x93: break;
                   17174:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     17175:                case 0xfa: break; // ega register interface library is not installed
1.1       root     17176:                case 0xfe: pcbios_int_10h_feh(); break;
                   17177:                case 0xff: pcbios_int_10h_ffh(); break;
                   17178:                default:
1.1.1.22  root     17179:                        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));
                   17180:                        m_CF = 1;
1.1       root     17181:                        break;
                   17182:                }
                   17183:                break;
                   17184:        case 0x11:
                   17185:                // PC BIOS - Get Equipment List
1.1.1.26  root     17186:                REG16(AX) = msdos_get_equipment();
1.1       root     17187:                break;
                   17188:        case 0x12:
                   17189:                // PC BIOS - Get Memory Size
1.1.1.33  root     17190:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     17191:                break;
                   17192:        case 0x13:
1.1.1.42  root     17193:                // PC BIOS - Disk I/O
                   17194:                {
                   17195:                        static UINT8 last = 0x00;
                   17196:                        switch(REG8(AH)) {
                   17197:                        case 0x00: pcbios_int_13h_00h(); break;
                   17198:                        case 0x01: // get last status
                   17199:                                REG8(AH) = last;
                   17200:                                break;
                   17201:                        case 0x02: pcbios_int_13h_02h(); break;
                   17202:                        case 0x03: pcbios_int_13h_03h(); break;
                   17203:                        case 0x04: pcbios_int_13h_04h(); break;
                   17204:                        case 0x08: pcbios_int_13h_08h(); break;
                   17205:                        case 0x0a: pcbios_int_13h_02h(); break;
                   17206:                        case 0x0b: pcbios_int_13h_03h(); break;
                   17207:                        case 0x0d: pcbios_int_13h_00h(); break;
                   17208:                        case 0x10: pcbios_int_13h_10h(); break;
                   17209:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     17210:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     17211:                        case 0x05: // format
                   17212:                        case 0x06:
                   17213:                        case 0x07:
                   17214:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   17215:                                m_CF = 1;
                   17216:                                break;
                   17217:                        case 0x09:
                   17218:                        case 0x0c: // seek
                   17219:                        case 0x11: // recalib
                   17220:                        case 0x14:
                   17221:                        case 0x17:
                   17222:                                REG8(AH) = 0x00; // successful completion
                   17223:                                break;
1.1.1.43  root     17224:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   17225:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   17226:                                REG8(AH) = 0x01; // invalid function
                   17227:                                m_CF = 1;
                   17228:                                break;
1.1.1.42  root     17229:                        default:
                   17230:                                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));
                   17231:                                REG8(AH) = 0x01; // invalid function
                   17232:                                m_CF = 1;
                   17233:                                break;
                   17234:                        }
                   17235:                        last = REG8(AH);
                   17236:                }
1.1       root     17237:                break;
                   17238:        case 0x14:
                   17239:                // PC BIOS - Serial I/O
1.1.1.25  root     17240:                switch(REG8(AH)) {
                   17241:                case 0x00: pcbios_int_14h_00h(); break;
                   17242:                case 0x01: pcbios_int_14h_01h(); break;
                   17243:                case 0x02: pcbios_int_14h_02h(); break;
                   17244:                case 0x03: pcbios_int_14h_03h(); break;
                   17245:                case 0x04: pcbios_int_14h_04h(); break;
                   17246:                case 0x05: pcbios_int_14h_05h(); break;
                   17247:                default:
                   17248:                        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));
                   17249:                        break;
                   17250:                }
1.1       root     17251:                break;
                   17252:        case 0x15:
                   17253:                // PC BIOS
1.1.1.3   root     17254:                m_CF = 0;
1.1       root     17255:                switch(REG8(AH)) {
                   17256:                case 0x23: pcbios_int_15h_23h(); break;
                   17257:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     17258:                case 0x41: break;
1.1       root     17259:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.64! root     17260:                case 0x4f: m_CF = 1; break; // from DOSBox
1.1.1.22  root     17261:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     17262:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     17263:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     17264:                case 0x86: pcbios_int_15h_86h(); break;
                   17265:                case 0x87: pcbios_int_15h_87h(); break;
                   17266:                case 0x88: pcbios_int_15h_88h(); break;
                   17267:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     17268:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.64! root     17269:                case 0x90: REG8(AH) = 0x00; break; // from DOSBox
        !          17270:                case 0x91: REG8(AH) = 0x00; break; // from DOSBox
1.1.1.22  root     17271:                case 0xc0: // PS/2 ???
1.1.1.54  root     17272: #ifndef EXT_BIOS_TOP
1.1.1.22  root     17273:                case 0xc1:
1.1.1.54  root     17274: #endif
1.1.1.30  root     17275:                case 0xc3: // PS50+ ???
                   17276:                case 0xc4:
1.1.1.22  root     17277:                        REG8(AH) = 0x86;
                   17278:                        m_CF = 1;
                   17279:                        break;
1.1.1.54  root     17280: #ifdef EXT_BIOS_TOP
                   17281:                case 0xc1: pcbios_int_15h_c1h(); break;
                   17282: #endif
                   17283:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     17284: #if defined(HAS_I386)
1.1       root     17285:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     17286: #endif
1.1       root     17287:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     17288:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     17289:                default:
1.1.1.22  root     17290:                        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));
                   17291:                        REG8(AH) = 0x86;
1.1.1.3   root     17292:                        m_CF = 1;
1.1       root     17293:                        break;
                   17294:                }
                   17295:                break;
                   17296:        case 0x16:
                   17297:                // PC BIOS - Keyboard
1.1.1.3   root     17298:                m_CF = 0;
1.1       root     17299:                switch(REG8(AH)) {
                   17300:                case 0x00: pcbios_int_16h_00h(); break;
                   17301:                case 0x01: pcbios_int_16h_01h(); break;
                   17302:                case 0x02: pcbios_int_16h_02h(); break;
                   17303:                case 0x03: pcbios_int_16h_03h(); break;
                   17304:                case 0x05: pcbios_int_16h_05h(); break;
1.1.1.60  root     17305:                case 0x09: pcbios_int_16h_09h(); break;
                   17306:                case 0x0a: pcbios_int_16h_0ah(); break;
1.1       root     17307:                case 0x10: pcbios_int_16h_00h(); break;
1.1.1.60  root     17308:                case 0x11: pcbios_int_16h_11h(); break;
1.1       root     17309:                case 0x12: pcbios_int_16h_12h(); break;
                   17310:                case 0x13: pcbios_int_16h_13h(); break;
                   17311:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     17312:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     17313:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     17314:                case 0xda: break; // unknown
1.1.1.43  root     17315:                case 0xdb: break; // unknown
1.1.1.22  root     17316:                case 0xff: break; // unknown
1.1       root     17317:                default:
1.1.1.22  root     17318:                        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     17319:                        break;
                   17320:                }
                   17321:                break;
                   17322:        case 0x17:
                   17323:                // PC BIOS - Printer
1.1.1.37  root     17324:                m_CF = 0;
                   17325:                switch(REG8(AH)) {
                   17326:                case 0x00: pcbios_int_17h_00h(); break;
                   17327:                case 0x01: pcbios_int_17h_01h(); break;
                   17328:                case 0x02: pcbios_int_17h_02h(); break;
                   17329:                case 0x03: pcbios_int_17h_03h(); break;
                   17330:                case 0x50: pcbios_int_17h_50h(); break;
                   17331:                case 0x51: pcbios_int_17h_51h(); break;
                   17332:                case 0x52: pcbios_int_17h_52h(); break;
                   17333:                case 0x84: pcbios_int_17h_84h(); break;
                   17334:                case 0x85: pcbios_int_17h_85h(); break;
                   17335:                default:
                   17336:                        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));
                   17337:                        break;
                   17338:                }
1.1       root     17339:                break;
                   17340:        case 0x1a:
                   17341:                // PC BIOS - Timer
1.1.1.3   root     17342:                m_CF = 0;
1.1       root     17343:                switch(REG8(AH)) {
                   17344:                case 0x00: pcbios_int_1ah_00h(); break;
                   17345:                case 0x01: break;
                   17346:                case 0x02: pcbios_int_1ah_02h(); break;
                   17347:                case 0x03: break;
                   17348:                case 0x04: pcbios_int_1ah_04h(); break;
                   17349:                case 0x05: break;
                   17350:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   17351:                case 0x0b: break;
1.1.1.14  root     17352:                case 0x35: break; // Word Perfect Third Party Interface?
                   17353:                case 0x36: break; // Word Perfect Third Party Interface
                   17354:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     17355:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     17356:                case 0xb1: break; // PCI BIOS v2.0c+
                   17357:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     17358:                default:
1.1.1.22  root     17359:                        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     17360:                        break;
                   17361:                }
                   17362:                break;
1.1.1.33  root     17363:        case 0x1b:
                   17364:                mem[0x471] = 0x00;
                   17365:                break;
1.1       root     17366:        case 0x20:
1.1.1.28  root     17367:                try {
                   17368:                        msdos_process_terminate(SREG(CS), retval, 1);
                   17369:                } catch(...) {
                   17370:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   17371:                }
1.1       root     17372:                break;
1.1.1.49  root     17373:        case 0x30:
1.1.1.46  root     17374:                // dummy interrupt for case map routine pointed in the country info
                   17375: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   17376: //                     REG8(AL) = 0x00;
                   17377: //                     break;
                   17378: //             }
1.1       root     17379:        case 0x21:
                   17380:                // MS-DOS System Call
1.1.1.3   root     17381:                m_CF = 0;
1.1.1.28  root     17382:                try {
1.1.1.46  root     17383:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     17384:                        case 0x00: msdos_int_21h_00h(); break;
                   17385:                        case 0x01: msdos_int_21h_01h(); break;
                   17386:                        case 0x02: msdos_int_21h_02h(); break;
                   17387:                        case 0x03: msdos_int_21h_03h(); break;
                   17388:                        case 0x04: msdos_int_21h_04h(); break;
                   17389:                        case 0x05: msdos_int_21h_05h(); break;
                   17390:                        case 0x06: msdos_int_21h_06h(); break;
                   17391:                        case 0x07: msdos_int_21h_07h(); break;
                   17392:                        case 0x08: msdos_int_21h_08h(); break;
                   17393:                        case 0x09: msdos_int_21h_09h(); break;
                   17394:                        case 0x0a: msdos_int_21h_0ah(); break;
                   17395:                        case 0x0b: msdos_int_21h_0bh(); break;
                   17396:                        case 0x0c: msdos_int_21h_0ch(); break;
                   17397:                        case 0x0d: msdos_int_21h_0dh(); break;
                   17398:                        case 0x0e: msdos_int_21h_0eh(); break;
                   17399:                        case 0x0f: msdos_int_21h_0fh(); break;
                   17400:                        case 0x10: msdos_int_21h_10h(); break;
                   17401:                        case 0x11: msdos_int_21h_11h(); break;
                   17402:                        case 0x12: msdos_int_21h_12h(); break;
                   17403:                        case 0x13: msdos_int_21h_13h(); break;
                   17404:                        case 0x14: msdos_int_21h_14h(); break;
                   17405:                        case 0x15: msdos_int_21h_15h(); break;
                   17406:                        case 0x16: msdos_int_21h_16h(); break;
                   17407:                        case 0x17: msdos_int_21h_17h(); break;
                   17408:                        case 0x18: msdos_int_21h_18h(); break;
                   17409:                        case 0x19: msdos_int_21h_19h(); break;
                   17410:                        case 0x1a: msdos_int_21h_1ah(); break;
                   17411:                        case 0x1b: msdos_int_21h_1bh(); break;
                   17412:                        case 0x1c: msdos_int_21h_1ch(); break;
                   17413:                        case 0x1d: msdos_int_21h_1dh(); break;
                   17414:                        case 0x1e: msdos_int_21h_1eh(); break;
                   17415:                        case 0x1f: msdos_int_21h_1fh(); break;
                   17416:                        case 0x20: msdos_int_21h_20h(); break;
                   17417:                        case 0x21: msdos_int_21h_21h(); break;
                   17418:                        case 0x22: msdos_int_21h_22h(); break;
                   17419:                        case 0x23: msdos_int_21h_23h(); break;
                   17420:                        case 0x24: msdos_int_21h_24h(); break;
                   17421:                        case 0x25: msdos_int_21h_25h(); break;
                   17422:                        case 0x26: msdos_int_21h_26h(); break;
                   17423:                        case 0x27: msdos_int_21h_27h(); break;
                   17424:                        case 0x28: msdos_int_21h_28h(); break;
                   17425:                        case 0x29: msdos_int_21h_29h(); break;
                   17426:                        case 0x2a: msdos_int_21h_2ah(); break;
                   17427:                        case 0x2b: msdos_int_21h_2bh(); break;
                   17428:                        case 0x2c: msdos_int_21h_2ch(); break;
                   17429:                        case 0x2d: msdos_int_21h_2dh(); break;
                   17430:                        case 0x2e: msdos_int_21h_2eh(); break;
                   17431:                        case 0x2f: msdos_int_21h_2fh(); break;
                   17432:                        case 0x30: msdos_int_21h_30h(); break;
                   17433:                        case 0x31: msdos_int_21h_31h(); break;
                   17434:                        case 0x32: msdos_int_21h_32h(); break;
                   17435:                        case 0x33: msdos_int_21h_33h(); break;
                   17436:                        case 0x34: msdos_int_21h_34h(); break;
                   17437:                        case 0x35: msdos_int_21h_35h(); break;
                   17438:                        case 0x36: msdos_int_21h_36h(); break;
                   17439:                        case 0x37: msdos_int_21h_37h(); break;
                   17440:                        case 0x38: msdos_int_21h_38h(); break;
                   17441:                        case 0x39: msdos_int_21h_39h(0); break;
                   17442:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   17443:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   17444:                        case 0x3c: msdos_int_21h_3ch(); break;
                   17445:                        case 0x3d: msdos_int_21h_3dh(); break;
                   17446:                        case 0x3e: msdos_int_21h_3eh(); break;
                   17447:                        case 0x3f: msdos_int_21h_3fh(); break;
                   17448:                        case 0x40: msdos_int_21h_40h(); break;
                   17449:                        case 0x41: msdos_int_21h_41h(0); break;
                   17450:                        case 0x42: msdos_int_21h_42h(); break;
                   17451:                        case 0x43: msdos_int_21h_43h(0); break;
                   17452:                        case 0x44: msdos_int_21h_44h(); break;
                   17453:                        case 0x45: msdos_int_21h_45h(); break;
                   17454:                        case 0x46: msdos_int_21h_46h(); break;
                   17455:                        case 0x47: msdos_int_21h_47h(0); break;
                   17456:                        case 0x48: msdos_int_21h_48h(); break;
                   17457:                        case 0x49: msdos_int_21h_49h(); break;
                   17458:                        case 0x4a: msdos_int_21h_4ah(); break;
                   17459:                        case 0x4b: msdos_int_21h_4bh(); break;
                   17460:                        case 0x4c: msdos_int_21h_4ch(); break;
                   17461:                        case 0x4d: msdos_int_21h_4dh(); break;
                   17462:                        case 0x4e: msdos_int_21h_4eh(); break;
                   17463:                        case 0x4f: msdos_int_21h_4fh(); break;
                   17464:                        case 0x50: msdos_int_21h_50h(); break;
                   17465:                        case 0x51: msdos_int_21h_51h(); break;
                   17466:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     17467:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     17468:                        case 0x54: msdos_int_21h_54h(); break;
                   17469:                        case 0x55: msdos_int_21h_55h(); break;
                   17470:                        case 0x56: msdos_int_21h_56h(0); break;
                   17471:                        case 0x57: msdos_int_21h_57h(); break;
                   17472:                        case 0x58: msdos_int_21h_58h(); break;
                   17473:                        case 0x59: msdos_int_21h_59h(); break;
                   17474:                        case 0x5a: msdos_int_21h_5ah(); break;
                   17475:                        case 0x5b: msdos_int_21h_5bh(); break;
                   17476:                        case 0x5c: msdos_int_21h_5ch(); break;
                   17477:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     17478:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     17479:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     17480:                        case 0x60: msdos_int_21h_60h(0); break;
                   17481:                        case 0x61: msdos_int_21h_61h(); break;
                   17482:                        case 0x62: msdos_int_21h_62h(); break;
                   17483:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     17484:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     17485:                        case 0x65: msdos_int_21h_65h(); break;
                   17486:                        case 0x66: msdos_int_21h_66h(); break;
                   17487:                        case 0x67: msdos_int_21h_67h(); break;
                   17488:                        case 0x68: msdos_int_21h_68h(); break;
                   17489:                        case 0x69: msdos_int_21h_69h(); break;
                   17490:                        case 0x6a: msdos_int_21h_6ah(); break;
                   17491:                        case 0x6b: msdos_int_21h_6bh(); break;
                   17492:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     17493:                        case 0x6d: // Find First ROM Program
                   17494:                        case 0x6e: // Find Next ROM Program
                   17495:                        case 0x6f: // Get/Set ROM Scan Start Address
                   17496:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   17497:                                break;
1.1.1.43  root     17498:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     17499:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     17500:                                switch(REG8(AL)) {
                   17501:                                case 0x0d: msdos_int_21h_710dh(); break;
                   17502:                                case 0x39: msdos_int_21h_39h(1); break;
                   17503:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   17504:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     17505:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     17506:                                case 0x43: msdos_int_21h_43h(1); break;
                   17507:                                case 0x47: msdos_int_21h_47h(1); break;
                   17508:                                case 0x4e: msdos_int_21h_714eh(); break;
                   17509:                                case 0x4f: msdos_int_21h_714fh(); break;
                   17510:                                case 0x56: msdos_int_21h_56h(1); break;
                   17511:                                case 0x60: msdos_int_21h_60h(1); break;
                   17512:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   17513:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   17514:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   17515:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   17516:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   17517:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     17518:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     17519:                                case 0xaa: msdos_int_21h_71aah(); break;
                   17520:                                default:
                   17521:                                        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));
                   17522:                                        REG16(AX) = 0x7100;
                   17523:                                        m_CF = 1;
                   17524:                                        break;
                   17525:                                }
                   17526:                                break;
1.1.1.48  root     17527:                        case 0x72: // Windows95 beta - LFN FindClose
                   17528: //                             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));
                   17529:                                REG16(AX) = 0x7200;
                   17530:                                m_CF = 1;
                   17531:                                break;
                   17532:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     17533:                                switch(REG8(AL)) {
                   17534:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     17535:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     17536:                                case 0x02: msdos_int_21h_7302h(); break;
                   17537:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     17538:                                // 0x04: Set DPB to Use for Formatting
                   17539:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     17540:                                default:
                   17541:                                        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));
                   17542:                                        REG16(AX) = 0x7300;
                   17543:                                        m_CF = 1;
                   17544:                                        break;
                   17545:                                }
1.1       root     17546:                                break;
1.1.1.30  root     17547:                        case 0xdb: msdos_int_21h_dbh(); break;
                   17548:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     17549:                        default:
1.1.1.22  root     17550:                                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     17551:                                REG16(AX) = 0x01;
1.1.1.3   root     17552:                                m_CF = 1;
1.1       root     17553:                                break;
                   17554:                        }
1.1.1.28  root     17555:                } catch(int error) {
                   17556:                        REG16(AX) = error;
                   17557:                        m_CF = 1;
                   17558:                } catch(...) {
                   17559:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     17560:                        m_CF = 1;
1.1       root     17561:                }
1.1.1.3   root     17562:                if(m_CF) {
1.1.1.23  root     17563:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     17564:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     17565:                        sda->extended_error_code = REG16(AX);
                   17566:                        switch(sda->extended_error_code) {
                   17567:                        case  4: // Too many open files
                   17568:                        case  8: // Insufficient memory
                   17569:                                sda->error_class = 1; // Out of resource
                   17570:                                break;
                   17571:                        case  5: // Access denied
                   17572:                                sda->error_class = 3; // Authorization
                   17573:                                break;
                   17574:                        case  7: // Memory control block destroyed
                   17575:                                sda->error_class = 4; // Internal
                   17576:                                break;
                   17577:                        case  2: // File not found
                   17578:                        case  3: // Path not found
                   17579:                        case 15: // Invaid drive specified
                   17580:                        case 18: // No more files
                   17581:                                sda->error_class = 8; // Not found
                   17582:                                break;
                   17583:                        case 32: // Sharing violation
                   17584:                        case 33: // Lock violation
                   17585:                                sda->error_class = 10; // Locked
                   17586:                                break;
                   17587: //                     case 16: // Removal of current directory attempted
                   17588:                        case 19: // Attempted write on protected disk
                   17589:                        case 21: // Drive not ready
                   17590: //                     case 29: // Write failure
                   17591: //                     case 30: // Read failure
                   17592: //                     case 82: // Cannot create subdirectory
                   17593:                                sda->error_class = 11; // Media
                   17594:                                break;
                   17595:                        case 80: // File already exists
                   17596:                                sda->error_class = 12; // Already exist
                   17597:                                break;
                   17598:                        default:
                   17599:                                sda->error_class = 13; // Unknown
                   17600:                                break;
                   17601:                        }
                   17602:                        sda->suggested_action = 1; // Retry
                   17603:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     17604:                }
1.1.1.33  root     17605:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     17606:                        // raise int 23h
                   17607: #if defined(HAS_I386)
                   17608:                        m_ext = 0; // not an external interrupt
                   17609:                        i386_trap(0x23, 1, 0);
                   17610:                        m_ext = 1;
                   17611: #else
                   17612:                        PREFIX86(_interrupt)(0x23);
                   17613: #endif
                   17614:                }
1.1       root     17615:                break;
                   17616:        case 0x22:
                   17617:                fatalerror("int 22h (terminate address)\n");
                   17618:        case 0x23:
1.1.1.28  root     17619:                try {
                   17620:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   17621:                } catch(...) {
                   17622:                        fatalerror("failed to terminate the current process by int 23h\n");
                   17623:                }
1.1       root     17624:                break;
                   17625:        case 0x24:
1.1.1.32  root     17626: /*
1.1.1.28  root     17627:                try {
                   17628:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17629:                } catch(...) {
                   17630:                        fatalerror("failed to terminate the current process by int 24h\n");
                   17631:                }
1.1.1.32  root     17632: */
                   17633:                msdos_int_24h();
1.1       root     17634:                break;
                   17635:        case 0x25:
                   17636:                msdos_int_25h();
                   17637:                break;
                   17638:        case 0x26:
                   17639:                msdos_int_26h();
                   17640:                break;
                   17641:        case 0x27:
1.1.1.28  root     17642:                try {
                   17643:                        msdos_int_27h();
                   17644:                } catch(...) {
                   17645:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   17646:                }
1.1       root     17647:                break;
                   17648:        case 0x28:
                   17649:                Sleep(10);
1.1.1.35  root     17650:                REQUEST_HARDWRE_UPDATE();
1.1       root     17651:                break;
                   17652:        case 0x29:
                   17653:                msdos_int_29h();
                   17654:                break;
                   17655:        case 0x2e:
                   17656:                msdos_int_2eh();
                   17657:                break;
                   17658:        case 0x2f:
                   17659:                // multiplex interrupt
                   17660:                switch(REG8(AH)) {
1.1.1.22  root     17661:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     17662:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     17663:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     17664:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17665:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17666:                case 0x14: msdos_int_2fh_14h(); break;
                   17667:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17668:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17669:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17670:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17671:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17672:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17673:                case 0x46: msdos_int_2fh_46h(); break;
                   17674:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17675:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17676:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17677:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17678:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17679:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17680:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17681:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17682:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17683:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17684:                default:
1.1.1.30  root     17685:                        switch(REG8(AL)) {
                   17686:                        case 0x00:
                   17687:                                // This is not installed
                   17688: //                             REG8(AL) = 0x00;
                   17689:                                break;
1.1.1.33  root     17690:                        case 0x01:
1.1.1.42  root     17691:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17692:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17693:                                        break;
                   17694:                                }
1.1.1.33  root     17695:                                // Banyan VINES v4+ is not installed
                   17696:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17697:                                        break;
                   17698:                                }
1.1.1.42  root     17699:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17700:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17701:                                        break;
                   17702:                                }
1.1.1.30  root     17703:                        default:
1.1.1.42  root     17704:                                // NORTON UTILITIES 5.0+
                   17705:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17706:                                        break;
                   17707:                                }
1.1.1.30  root     17708:                                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     17709:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17710:                                m_CF = 1;
                   17711:                                break;
                   17712:                        }
                   17713:                        break;
1.1       root     17714:                }
                   17715:                break;
1.1.1.24  root     17716:        case 0x33:
                   17717:                switch(REG8(AH)) {
                   17718:                case 0x00:
                   17719:                        // Mouse
1.1.1.49  root     17720:                        switch(REG16(AX)) {
                   17721:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17722:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17723:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17724:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17725:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17726:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17727:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17728:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17729:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17730:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17731:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17732:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17733:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17734:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17735:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17736:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17737:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17738:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17739:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17740:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17741:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17742:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17743:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17744:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17745:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17746:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17747:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17748:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17749:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17750:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17751:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17752:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17753:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17754:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17755:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17756:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17757:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17758:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17759:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17760:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17761:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17762:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17763:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17764:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17765:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17766:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17767:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17768:                        case 0x002f: break; // Mouse Hardware Reset
                   17769:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17770:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17771:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17772:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17773:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17774:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17775:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17776:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17777:                        default:
                   17778:                                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));
                   17779:                                break;
                   17780:                        }
                   17781:                        break;
                   17782:                default:
                   17783:                        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));
                   17784:                        break;
                   17785:                }
                   17786:                break;
1.1.1.59  root     17787:        case 0x65:
1.1.1.19  root     17788:                // dummy interrupt for EMS (int 67h)
                   17789:                switch(REG8(AH)) {
                   17790:                case 0x40: msdos_int_67h_40h(); break;
                   17791:                case 0x41: msdos_int_67h_41h(); break;
                   17792:                case 0x42: msdos_int_67h_42h(); break;
                   17793:                case 0x43: msdos_int_67h_43h(); break;
                   17794:                case 0x44: msdos_int_67h_44h(); break;
                   17795:                case 0x45: msdos_int_67h_45h(); break;
                   17796:                case 0x46: msdos_int_67h_46h(); break;
                   17797:                case 0x47: msdos_int_67h_47h(); break;
                   17798:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17799:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17800:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17801:                case 0x4b: msdos_int_67h_4bh(); break;
                   17802:                case 0x4c: msdos_int_67h_4ch(); break;
                   17803:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17804:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17805:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17806:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17807:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17808:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17809:                case 0x53: msdos_int_67h_53h(); break;
                   17810:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17811:                case 0x55: msdos_int_67h_55h(); break;
                   17812:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17813:                case 0x57: msdos_int_67h_57h(); break;
                   17814:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17815:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17816:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17817:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17818:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17819:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17820:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17821:                // 0xde: VCPI
1.1.1.30  root     17822:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17823:                default:
1.1.1.22  root     17824:                        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     17825:                        REG8(AH) = 0x84;
                   17826:                        break;
                   17827:                }
                   17828:                break;
                   17829: #ifdef SUPPORT_XMS
1.1.1.59  root     17830:        case 0x66:
1.1.1.19  root     17831:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17832:                try {
                   17833:                        switch(REG8(AH)) {
                   17834:                        case 0x00: msdos_call_xms_00h(); break;
                   17835:                        case 0x01: msdos_call_xms_01h(); break;
                   17836:                        case 0x02: msdos_call_xms_02h(); break;
                   17837:                        case 0x03: msdos_call_xms_03h(); break;
                   17838:                        case 0x04: msdos_call_xms_04h(); break;
                   17839:                        case 0x05: msdos_call_xms_05h(); break;
                   17840:                        case 0x06: msdos_call_xms_06h(); break;
                   17841:                        case 0x07: msdos_call_xms_07h(); break;
                   17842:                        case 0x08: msdos_call_xms_08h(); break;
                   17843:                        case 0x09: msdos_call_xms_09h(); break;
                   17844:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17845:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17846:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17847:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17848:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17849:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17850:                        case 0x10: msdos_call_xms_10h(); break;
                   17851:                        case 0x11: msdos_call_xms_11h(); break;
                   17852:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17853: #if defined(HAS_I386)
                   17854:                        case 0x88: msdos_call_xms_88h(); break;
                   17855:                        case 0x89: msdos_call_xms_89h(); break;
                   17856:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17857:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17858: #endif
1.1.1.28  root     17859:                        default:
                   17860:                                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));
                   17861:                                REG16(AX) = 0x0000;
                   17862:                                REG8(BL) = 0x80; // function not implemented
                   17863:                                break;
                   17864:                        }
                   17865:                } catch(...) {
1.1.1.19  root     17866:                        REG16(AX) = 0x0000;
1.1.1.28  root     17867:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17868:                }
                   17869:                break;
                   17870: #endif
1.1.1.59  root     17871: /*
                   17872:        case 0x67:
                   17873:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 65h
                   17874:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   17875:                break;
                   17876: */
                   17877:        case 0x69:
1.1.1.24  root     17878:                // irq12 (mouse)
                   17879:                mouse_push_ax = REG16(AX);
                   17880:                mouse_push_bx = REG16(BX);
                   17881:                mouse_push_cx = REG16(CX);
                   17882:                mouse_push_dx = REG16(DX);
                   17883:                mouse_push_si = REG16(SI);
                   17884:                mouse_push_di = REG16(DI);
                   17885:                
1.1.1.43  root     17886:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17887:                        REG16(AX) = mouse.status_irq;
                   17888:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17889:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17890:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17891:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17892:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17893:                        
1.1.1.49  root     17894:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17895:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17896:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17897:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17898:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.59  root     17899:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17900:                        mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17901:                        break;
1.1.1.24  root     17902:                }
1.1.1.43  root     17903:                for(int i = 0; i < 8; i++) {
                   17904:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17905:                                REG16(AX) = mouse.status_irq_alt;
                   17906:                                REG16(BX) = mouse.get_buttons();
                   17907:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17908:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17909:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17910:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17911:                                
1.1.1.49  root     17912:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17913:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17914:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17915:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17916:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.59  root     17917:                                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17918:                                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17919:                                break;
                   17920:                        }
                   17921:                }
1.1.1.59  root     17922:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     17923:                        UINT16 data_1st, data_2nd, data_3rd;
                   17924:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   17925:                        i386_push16(data_1st);
                   17926:                        i386_push16(data_2nd);
                   17927:                        i386_push16(data_3rd);
                   17928:                        i386_push16(0x0000);
                   17929:                        
                   17930:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17931:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
                   17932:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
                   17933:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
                   17934:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
1.1.1.59  root     17935:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   17936:                        mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.54  root     17937:                        break;
                   17938:                }
1.1.1.43  root     17939:                // invalid call addr :-(
1.1.1.49  root     17940:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17941:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17942:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17943:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17944:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.59  root     17945:                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17946:                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.24  root     17947:                break;
1.1.1.59  root     17948:        case 0x6a:
                   17949:                // end of ps/2 mouse bios
                   17950:                i386_pop16();
                   17951:                i386_pop16();
                   17952:                i386_pop16();
                   17953:                i386_pop16();
1.1.1.24  root     17954:        case 0x6b:
                   17955:                // end of irq12 (mouse)
                   17956:                REG16(AX) = mouse_push_ax;
                   17957:                REG16(BX) = mouse_push_bx;
                   17958:                REG16(CX) = mouse_push_cx;
                   17959:                REG16(DX) = mouse_push_dx;
                   17960:                REG16(SI) = mouse_push_si;
                   17961:                REG16(DI) = mouse_push_di;
                   17962:                
                   17963:                // EOI
                   17964:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17965:                        pic[0].isr &= ~(1 << 2); // master
                   17966:                }
                   17967:                pic_update();
                   17968:                break;
                   17969:        case 0x6c:
1.1.1.19  root     17970:                // dummy interrupt for case map routine pointed in the country info
                   17971:                if(REG8(AL) >= 0x80) {
                   17972:                        char tmp[2] = {0};
                   17973:                        tmp[0] = REG8(AL);
                   17974:                        my_strupr(tmp);
                   17975:                        REG8(AL) = tmp[0];
                   17976:                }
                   17977:                break;
1.1.1.27  root     17978:        case 0x6d:
                   17979:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17980:                REG8(AL) = 0x86; // not supported
                   17981:                m_CF = 1;
                   17982:                break;
1.1.1.32  root     17983:        case 0x6e:
                   17984:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17985:                {
                   17986:                        UINT16 code = REG16(AX);
                   17987:                        if(code & 0xf0) {
                   17988:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17989:                        }
                   17990:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17991:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17992:                                        const char *message = NULL;
                   17993:                                        if(active_code_page == 932) {
                   17994:                                                message = param_error_table[i].message_japanese;
                   17995:                                        }
                   17996:                                        if(message == NULL) {
                   17997:                                                message = param_error_table[i].message_english;
                   17998:                                        }
                   17999:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   18000:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   18001:                                        
                   18002:                                        SREG(ES) = WORK_TOP >> 4;
                   18003:                                        i386_load_segment_descriptor(ES);
                   18004:                                        REG16(DI) = 0x0000;
                   18005:                                        break;
                   18006:                                }
                   18007:                        }
                   18008:                }
                   18009:                break;
1.1.1.49  root     18010:        case 0x6f:
                   18011:                // dummy interrupt for end of alter page map and call
                   18012:                {
                   18013:                        UINT16 handles[4], pages[4];
                   18014:                        
                   18015:                        // pop old mapping data in new mapping
                   18016:                        for(int i = 0; i < 4; i++) {
                   18017:                                pages  [3 - i] = i386_pop16();
                   18018:                                handles[3 - i] = i386_pop16();
                   18019:                        }
                   18020:                        
                   18021:                        // restore old mapping
                   18022:                        for(int i = 0; i < 4; i++) {
                   18023:                                UINT16 handle = handles[i];
                   18024:                                UINT16 page   = pages  [i];
                   18025:                                
                   18026:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   18027:                                        ems_map_page(i, handle, page);
                   18028:                                } else {
                   18029:                                        ems_unmap_page(i);
                   18030:                                }
                   18031:                        }
                   18032:                        // do ret_far (pop cs/ip) in old mapping
                   18033:                }
                   18034:                break;
1.1.1.8   root     18035:        case 0x70:
                   18036:        case 0x71:
                   18037:        case 0x72:
                   18038:        case 0x73:
                   18039:        case 0x74:
                   18040:        case 0x75:
                   18041:        case 0x76:
                   18042:        case 0x77:
                   18043:                // EOI
                   18044:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   18045:                        pic[0].isr &= ~(1 << 2); // master
                   18046:                }
                   18047:                pic_update();
                   18048:                break;
1.1       root     18049:        default:
1.1.1.22  root     18050: //             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     18051:                break;
                   18052:        }
                   18053:        
                   18054:        // update cursor position
                   18055:        if(cursor_moved) {
1.1.1.36  root     18056:                pcbios_update_cursor_position();
1.1       root     18057:                cursor_moved = false;
                   18058:        }
                   18059: }
                   18060: 
                   18061: // init
                   18062: 
                   18063: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   18064: {
                   18065:        // init file handler
                   18066:        memset(file_handler, 0, sizeof(file_handler));
                   18067:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   18068:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   18069:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     18070: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18071:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     18072: #else
                   18073:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   18074: #endif
                   18075:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     18076:        }
1.1.1.21  root     18077:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     18078: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   18079:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     18080:        }
1.1       root     18081:        _dup2(0, DUP_STDIN);
                   18082:        _dup2(1, DUP_STDOUT);
                   18083:        _dup2(2, DUP_STDERR);
1.1.1.21  root     18084:        _dup2(3, DUP_STDAUX);
                   18085:        _dup2(4, DUP_STDPRN);
1.1       root     18086:        
1.1.1.24  root     18087:        // init mouse
                   18088:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     18089:        mouse.enabled = true;   // from DOSBox
                   18090:        mouse.hidden = 1;       // hidden in default ???
                   18091:        mouse.old_hidden = 1;   // from DOSBox
                   18092:        mouse.max_position.x = 8 * (scr_width  - 1);
                   18093:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     18094:        mouse.mickey.x = 8;
                   18095:        mouse.mickey.y = 16;
                   18096:        
1.1.1.26  root     18097: #ifdef SUPPORT_XMS
                   18098:        // init xms
                   18099:        msdos_xms_init();
                   18100: #endif
                   18101:        
1.1       root     18102:        // init process
                   18103:        memset(process, 0, sizeof(process));
                   18104:        
1.1.1.13  root     18105:        // init dtainfo
                   18106:        msdos_dta_info_init();
                   18107:        
1.1       root     18108:        // init memory
                   18109:        memset(mem, 0, sizeof(mem));
                   18110:        
                   18111:        // bios data area
1.1.1.23  root     18112:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     18113:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   18114:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.58  root     18115: //     CONSOLE_FONT_INFO cfi;
1.1.1.57  root     18116: //     GetCurrentConsoleFont(hStdout, FALSE, &cfi);
1.1.1.14  root     18117:        
                   18118:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   18119:        text_vram_top_address = TEXT_VRAM_TOP;
                   18120:        text_vram_end_address = text_vram_top_address + regen;
                   18121:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   18122:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     18123:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     18124:        
                   18125:        if(regen > 0x4000) {
                   18126:                regen = 0x8000;
                   18127:                vram_pages = 1;
                   18128:        } else if(regen > 0x2000) {
                   18129:                regen = 0x4000;
                   18130:                vram_pages = 2;
                   18131:        } else if(regen > 0x1000) {
                   18132:                regen = 0x2000;
                   18133:                vram_pages = 4;
                   18134:        } else {
                   18135:                regen = 0x1000;
                   18136:                vram_pages = 8;
                   18137:        }
1.1       root     18138:        
1.1.1.25  root     18139:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   18140:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     18141:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   18142:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     18143:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     18144:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   18145:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     18146: #ifdef EXT_BIOS_TOP
1.1.1.25  root     18147:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     18148: #endif
1.1.1.26  root     18149:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     18150:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     18151:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   18152:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     18153:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     18154:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   18155:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     18156:        *(UINT16 *)(mem + 0x44e) = 0;
                   18157:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     18158:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     18159:        *(UINT8  *)(mem + 0x460) = 7;
                   18160:        *(UINT8  *)(mem + 0x461) = 7;
                   18161:        *(UINT8  *)(mem + 0x462) = 0;
                   18162:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     18163:        *(UINT8  *)(mem + 0x465) = 0x09;
                   18164:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     18165:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     18166:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   18167:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     18168:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
1.1.1.58  root     18169:        *(UINT16 *)(mem + 0x485) = font_height;
1.1.1.14  root     18170:        *(UINT8  *)(mem + 0x487) = 0x60;
                   18171:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     18172: #ifdef EXT_BIOS_TOP
1.1.1.25  root     18173:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     18174: #endif
1.1.1.14  root     18175:        
                   18176:        // initial screen
                   18177:        SMALL_RECT rect;
                   18178:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     18179:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     18180:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   18181:                for(int x = 0; x < scr_width; x++) {
                   18182:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   18183:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   18184:                }
                   18185:        }
1.1       root     18186:        
1.1.1.19  root     18187:        // init mcb
1.1       root     18188:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     18189:        
                   18190:        // iret table
                   18191:        // note: int 2eh vector should address the routine in command.com,
                   18192:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   18193:        // so move iret table into allocated memory block
                   18194:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.58  root     18195:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, (IRET_SIZE + 5 * 128) >> 4);
1.1.1.19  root     18196:        IRET_TOP = seg << 4;
1.1.1.58  root     18197:        seg += (IRET_SIZE + 5 * 128) >> 4;
1.1.1.25  root     18198:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     18199:        
1.1.1.58  root     18200:        // note: SO1 checks int 21h vector and if it aims iret (cfh)
                   18201:        // it is recognized SO1 is not running on MS-DOS environment
                   18202:        for(int i = 0; i < 128; i++) {
                   18203:                // jmp far (IRET_TOP >> 4):(interrupt number)
                   18204:                *(UINT8  *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 0) = 0xea;
                   18205:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 1) = i;
                   18206:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 3) = IRET_TOP >> 4;
                   18207:        }
                   18208:        
1.1.1.19  root     18209:        // dummy xms/ems device
1.1.1.33  root     18210:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     18211:        XMS_TOP = seg << 4;
                   18212:        seg += XMS_SIZE >> 4;
                   18213:        
                   18214:        // environment
1.1.1.33  root     18215:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     18216:        int env_seg = seg;
                   18217:        int ofs = 0;
1.1.1.32  root     18218:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   18219:        char comspec_added = 0;
1.1.1.33  root     18220:        char lastdrive_added = 0;
1.1.1.32  root     18221:        char env_msdos_path[ENV_SIZE] = {0};
                   18222:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     18223:        char prompt_added = 0;
1.1.1.32  root     18224:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     18225:        char tz_added = 0;
1.1.1.45  root     18226:        const char *path, *short_path;
1.1.1.32  root     18227:        
                   18228:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   18229:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18230:                        strcpy(env_append, short_path);
                   18231:                }
                   18232:        }
                   18233:        if((path = getenv("APPEND")) != NULL) {
                   18234:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18235:                        if(env_append[0] != '\0') {
                   18236:                                strcat(env_append, ";");
                   18237:                        }
                   18238:                        strcat(env_append, short_path);
                   18239:                }
                   18240:        }
                   18241:        
                   18242:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   18243:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18244:                        strcpy(comspec_path, short_path);
                   18245:                }
                   18246:        }
                   18247:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   18248:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18249:                        strcpy(comspec_path, short_path);
                   18250:                }
                   18251:        }
1.1       root     18252:        
1.1.1.28  root     18253:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     18254:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18255:                        strcpy(env_msdos_path, short_path);
                   18256:                        strcpy(env_path, short_path);
1.1.1.14  root     18257:                }
                   18258:        }
1.1.1.28  root     18259:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     18260:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18261:                        if(env_path[0] != '\0') {
                   18262:                                strcat(env_path, ";");
                   18263:                        }
                   18264:                        strcat(env_path, short_path);
1.1.1.9   root     18265:                }
                   18266:        }
1.1.1.32  root     18267:        
1.1.1.60  root     18268:        if(GetTempPathA(ENV_SIZE, env_temp) != 0) {
1.1.1.32  root     18269:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     18270:        }
1.1.1.32  root     18271:        for(int i = 0; i < 4; i++) {
                   18272:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   18273:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   18274:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18275:                                strcpy(env_temp, short_path);
                   18276:                                break;
                   18277:                        }
                   18278:                }
1.1.1.24  root     18279:        }
1.1.1.32  root     18280:        
1.1.1.9   root     18281:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     18282:                // lower to upper
1.1.1.28  root     18283:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     18284:                strcpy(tmp, *p);
                   18285:                for(int i = 0;; i++) {
                   18286:                        if(tmp[i] == '=') {
                   18287:                                tmp[i] = '\0';
                   18288:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     18289:                                my_strupr(name);
1.1       root     18290:                                tmp[i] = '=';
                   18291:                                break;
                   18292:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     18293:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     18294:                        }
                   18295:                }
1.1.1.33  root     18296:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   18297:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   18298:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     18299:                        // ignore non standard environments
                   18300:                } else {
1.1.1.33  root     18301:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     18302:                                if(env_append[0] != '\0') {
                   18303:                                        sprintf(tmp, "APPEND=%s", env_append);
                   18304:                                } else {
                   18305:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   18306:                                }
                   18307:                                append_added = 1;
                   18308:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     18309:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     18310:                                comspec_added = 1;
1.1.1.33  root     18311:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   18312:                                char *env = getenv("MSDOS_LASTDRIVE");
                   18313:                                if(env != NULL) {
                   18314:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   18315:                                }
                   18316:                                lastdrive_added = 1;
                   18317:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     18318:                                if(env_msdos_path[0] != '\0') {
                   18319:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   18320:                                } else {
                   18321:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   18322:                                }
1.1.1.33  root     18323:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     18324:                                if(env_path[0] != '\0') {
                   18325:                                        sprintf(tmp, "PATH=%s", env_path);
                   18326:                                } else {
                   18327:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   18328:                                }
1.1.1.32  root     18329:                                path_added = 1;
1.1.1.33  root     18330:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   18331:                                prompt_added = 1;
1.1.1.28  root     18332:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   18333:                                if(env_temp[0] != '\0') {
                   18334:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   18335:                                } else {
                   18336:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   18337:                                }
1.1.1.32  root     18338:                                temp_added = 1;
1.1.1.33  root     18339:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     18340:                                if(env_temp[0] != '\0') {
                   18341:                                        sprintf(tmp, "TMP=%s", env_temp);
                   18342:                                } else {
                   18343:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     18344:                                }
1.1.1.32  root     18345:                                tmp_added = 1;
1.1.1.33  root     18346:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   18347:                                char *env = getenv("MSDOS_TZ");
                   18348:                                if(env != NULL) {
                   18349:                                        sprintf(tmp, "TZ=%s", env);
                   18350:                                }
                   18351:                                tz_added = 1;
1.1       root     18352:                        }
                   18353:                        int len = strlen(tmp);
1.1.1.14  root     18354:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     18355:                                fatalerror("too many environments\n");
                   18356:                        }
                   18357:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   18358:                        ofs += len + 1;
                   18359:                }
                   18360:        }
1.1.1.32  root     18361:        if(!append_added && env_append[0] != '\0') {
                   18362:                #define SET_ENV(name, value) { \
                   18363:                        char tmp[ENV_SIZE]; \
                   18364:                        sprintf(tmp, "%s=%s", name, value); \
                   18365:                        int len = strlen(tmp); \
                   18366:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   18367:                                fatalerror("too many environments\n"); \
                   18368:                        } \
                   18369:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   18370:                        ofs += len + 1; \
                   18371:                }
                   18372:                SET_ENV("APPEND", env_append);
                   18373:        }
                   18374:        if(!comspec_added) {
                   18375:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   18376:        }
1.1.1.33  root     18377:        if(!lastdrive_added) {
                   18378:                SET_ENV("LASTDRIVE", "Z");
                   18379:        }
1.1.1.32  root     18380:        if(!path_added) {
                   18381:                SET_ENV("PATH", env_path);
                   18382:        }
1.1.1.33  root     18383:        if(!prompt_added) {
                   18384:                SET_ENV("PROMPT", "$P$G");
                   18385:        }
1.1.1.32  root     18386:        if(!temp_added) {
                   18387:                SET_ENV("TEMP", env_temp);
                   18388:        }
                   18389:        if(!tmp_added) {
                   18390:                SET_ENV("TMP", env_temp);
                   18391:        }
1.1.1.33  root     18392:        if(!tz_added) {
                   18393:                TIME_ZONE_INFORMATION tzi;
                   18394:                HKEY hKey, hSubKey;
                   18395:                char tzi_std_name[64];
                   18396:                char tz_std[8] = "GMT";
                   18397:                char tz_dlt[8] = "GST";
                   18398:                char tz_value[32];
                   18399:                
                   18400:                // timezone name from GetTimeZoneInformation may not be english
                   18401:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   18402:                setlocale(LC_CTYPE, "");
                   18403:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   18404:                
                   18405:                // get english timezone name from registry
1.1.1.60  root     18406:                if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
1.1.1.33  root     18407:                        for(DWORD i = 0; !tz_added; i++) {
                   18408:                                char reg_name[256], sub_key[1024], std_name[256];
                   18409:                                DWORD size;
                   18410:                                FILETIME ftTime;
1.1.1.60  root     18411:                                LONG result = RegEnumKeyExA(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
1.1.1.33  root     18412:                                
                   18413:                                if(result == ERROR_SUCCESS) {
                   18414:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
1.1.1.60  root     18415:                                        if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   18416:                                                if(RegQueryValueExA(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
1.1.1.33  root     18417:                                                        // search english timezone name from table
1.1.1.37  root     18418:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     18419:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   18420:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   18421:                                                                                if(tz_table[j].std != NULL) {
                   18422:                                                                                        strcpy(tz_std, tz_table[j].std);
                   18423:                                                                                }
                   18424:                                                                                if(tz_table[j].dlt != NULL) {
                   18425:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   18426:                                                                                }
                   18427:                                                                                tz_added = 1;
                   18428:                                                                                break;
                   18429:                                                                        }
                   18430:                                                                }
                   18431:                                                        }
                   18432:                                                }
                   18433:                                                RegCloseKey(hSubKey);
                   18434:                                        }
                   18435:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   18436:                                        break;
                   18437:                                }
                   18438:                        }
                   18439:                        RegCloseKey(hKey);
                   18440:                }
                   18441:                if((tzi.Bias % 60) != 0) {
                   18442:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   18443:                } else {
                   18444:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   18445:                }
                   18446:                if(daylight) {
                   18447:                        strcat(tz_value, tz_dlt);
                   18448:                }
                   18449:                SET_ENV("TZ", tz_value);
                   18450:        }
1.1       root     18451:        seg += (ENV_SIZE >> 4);
                   18452:        
                   18453:        // psp
1.1.1.33  root     18454:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     18455:        current_psp = seg;
1.1.1.35  root     18456:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   18457:        psp->parent_psp = current_psp;
1.1       root     18458:        seg += (PSP_SIZE >> 4);
                   18459:        
1.1.1.19  root     18460:        // first free mcb in conventional memory
1.1.1.33  root     18461:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     18462:        first_mcb = seg;
                   18463:        
1.1.1.19  root     18464:        // dummy mcb to link to umb
1.1.1.33  root     18465: #if 0
1.1.1.39  root     18466:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     18467: #else
1.1.1.39  root     18468:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     18469: #endif
1.1.1.19  root     18470:        
                   18471:        // first free mcb in upper memory block
1.1.1.8   root     18472:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     18473:        
1.1.1.29  root     18474: #ifdef SUPPORT_HMA
                   18475:        // first free mcb in high memory area
                   18476:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   18477: #endif
                   18478:        
1.1.1.26  root     18479:        // interrupt vector
1.1.1.58  root     18480:        for(int i = 0; i < 256; i++) {
                   18481:                // 00-07: CPU exception handler
                   18482:                // 08-0F: IRQ 0-7
                   18483:                // 10-1F: PC BIOS
                   18484:                // 20-3F: MS-DOS system call
                   18485:                // 70-77: IRQ 8-15
                   18486:                *(UINT16 *)(mem + 4 * i + 0) = (i <= 0x3f || (i >= 0x70 && i <= 0x77)) ? (IRET_SIZE + 5 * i) : i;
1.1.1.26  root     18487:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   18488:        }
1.1.1.49  root     18489:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   18490:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18491:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   18492:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   18493:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   18494:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     18495:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   18496:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18497:        
1.1.1.29  root     18498:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     18499:        static const struct {
                   18500:                UINT16 attributes;
                   18501:                char *dev_name;
                   18502:        } dummy_devices[] = {
                   18503:                {0x8013, "CON     "},
                   18504:                {0x8000, "AUX     "},
                   18505:                {0xa0c0, "PRN     "},
                   18506:                {0x8008, "CLOCK$  "},
                   18507:                {0x8000, "COM1    "},
                   18508:                {0xa0c0, "LPT1    "},
                   18509:                {0xa0c0, "LPT2    "},
                   18510:                {0xa0c0, "LPT3    "},
                   18511:                {0x8000, "COM2    "},
                   18512:                {0x8000, "COM3    "},
                   18513:                {0x8000, "COM4    "},
1.1.1.30  root     18514: //             {0xc000, "CONFIG$ "},
                   18515:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     18516:        };
                   18517:        static const UINT8 dummy_device_routine[] = {
                   18518:                // from NUL device of Windows 98 SE
                   18519:                // or word ptr ES:[BX+03],0100
                   18520:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   18521:                // retf
                   18522:                0xcb,
                   18523:        };
1.1.1.29  root     18524:        device_t *last = NULL;
1.1.1.32  root     18525:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     18526:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     18527:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   18528:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18529:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     18530:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   18531:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     18532:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     18533:                last = device;
                   18534:        }
                   18535:        if(last != NULL) {
                   18536:                last->next_driver.w.l = 0;
                   18537:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     18538:        }
1.1.1.29  root     18539:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     18540:        
1.1.1.25  root     18541:        // dos info
                   18542:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   18543:        dos_info->magic_word = 1;
                   18544:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   18545:        dos_info->first_dpb.w.l = 0;
                   18546:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   18547:        dos_info->first_sft.w.l = 0;
                   18548:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     18549:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     18550:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18551:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     18552:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   18553:        dos_info->max_sector_len = 512;
                   18554:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   18555:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   18556:        dos_info->cds.w.l = 0;
                   18557:        dos_info->cds.w.h = CDS_TOP >> 4;
                   18558:        dos_info->fcb_table.w.l = 0;
                   18559:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   18560:        dos_info->last_drive = 'Z' - 'A' + 1;
                   18561:        dos_info->buffers_x = 20;
                   18562:        dos_info->buffers_y = 0;
                   18563:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     18564:        dos_info->nul_device.next_driver.w.l = 22;
                   18565:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     18566:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     18567:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   18568:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18569:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   18570:        dos_info->disk_buf_heads.w.l = 0;
                   18571:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     18572:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     18573:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   18574:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     18575:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     18576:        
                   18577:        char *env;
                   18578:        if((env = getenv("LASTDRIVE")) != NULL) {
                   18579:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18580:                        dos_info->last_drive = env[0] - 'A' + 1;
                   18581:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18582:                        dos_info->last_drive = env[0] - 'a' + 1;
                   18583:                }
                   18584:        }
                   18585:        if((env = getenv("windir")) != NULL) {
                   18586:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18587:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   18588:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18589:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   18590:                }
                   18591:        }
                   18592: #if defined(HAS_I386)
                   18593:        dos_info->i386_or_later = 1;
                   18594: #else
                   18595:        dos_info->i386_or_later = 0;
                   18596: #endif
                   18597:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   18598:        
1.1.1.27  root     18599:        // ems (int 67h) and xms
1.1.1.25  root     18600:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   18601:        xms_device->next_driver.w.l = 0xffff;
                   18602:        xms_device->next_driver.w.h = 0xffff;
                   18603:        xms_device->attributes = 0xc000;
1.1.1.29  root     18604:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   18605:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18606:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   18607:        
1.1.1.59  root     18608:        mem[XMS_TOP + 0x12] = 0xcd;     // int 65h (dummy)
                   18609:        mem[XMS_TOP + 0x13] = 0x65;
1.1.1.26  root     18610:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     18611: #ifdef SUPPORT_XMS
                   18612:        if(support_xms) {
1.1.1.59  root     18613:                mem[XMS_TOP + 0x15] = 0xcd;     // int 66h (dummy)
                   18614:                mem[XMS_TOP + 0x16] = 0x66;
1.1.1.26  root     18615:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     18616:        } else
                   18617: #endif
1.1.1.26  root     18618:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     18619:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     18620:        
1.1.1.26  root     18621:        // irq12 routine (mouse)
1.1.1.59  root     18622:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 69h (dummy)
                   18623:        mem[DUMMY_TOP + 0x01] = 0x69;
1.1.1.49  root     18624:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   18625:        mem[DUMMY_TOP + 0x03] = 0xff;
                   18626:        mem[DUMMY_TOP + 0x04] = 0xff;
                   18627:        mem[DUMMY_TOP + 0x05] = 0xff;
                   18628:        mem[DUMMY_TOP + 0x06] = 0xff;
1.1.1.59  root     18629: //     mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   18630: //     mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.49  root     18631:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   18632:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   18633:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     18634:        
1.1.1.27  root     18635:        // case map routine
1.1.1.49  root     18636:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   18637:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   18638:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     18639:        
                   18640:        // font read routine
1.1.1.49  root     18641:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   18642:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   18643:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     18644:        
1.1.1.32  root     18645:        // error message read routine
1.1.1.49  root     18646:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   18647:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   18648:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     18649:        
1.1.1.35  root     18650:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     18651:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   18652:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   18653:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   18654:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   18655:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     18656:        
1.1.1.50  root     18657:        // irq0 routine (system timer)
1.1.1.49  root     18658:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   18659:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   18660:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   18661:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   18662:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   18663:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   18664:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   18665:        
                   18666:        // alter page map and call routine
                   18667:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   18668:        mem[DUMMY_TOP + 0x20] = 0xff;
                   18669:        mem[DUMMY_TOP + 0x21] = 0xff;
                   18670:        mem[DUMMY_TOP + 0x22] = 0xff;
                   18671:        mem[DUMMY_TOP + 0x23] = 0xff;
                   18672:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   18673:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   18674:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     18675:        
1.1.1.50  root     18676:        // call int 29h routine
                   18677:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   18678:        mem[DUMMY_TOP + 0x28] = 0x29;
                   18679:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   18680:        
1.1.1.63  root     18681:        // VCPI entry point
                   18682:        mem[DUMMY_TOP + 0x2a] = 0xcd;   // int 65h
                   18683:        mem[DUMMY_TOP + 0x2b] = 0x65;
                   18684:        mem[DUMMY_TOP + 0x2c] = 0xcb;   // retf
                   18685:        
1.1.1.26  root     18686:        // boot routine
1.1.1.59  root     18687:        mem[0xffff0 + 0x00] = 0xf4;     // halt to exit MS-DOS Player
                   18688: #if 1
                   18689:        mem[0xffff0 + 0x05] = '0';      // rom date (same as DOSBox)
                   18690:        mem[0xffff0 + 0x06] = '1';
                   18691:        mem[0xffff0 + 0x07] = '/';
                   18692:        mem[0xffff0 + 0x08] = '0';
                   18693:        mem[0xffff0 + 0x09] = '1';
                   18694:        mem[0xffff0 + 0x0a] = '/';
                   18695:        mem[0xffff0 + 0x0b] = '9';
                   18696:        mem[0xffff0 + 0x0c] = '2';
                   18697:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
                   18698:        mem[0xffff0 + 0x0f] = 0x55;     // signature
                   18699: #else
                   18700:        mem[0xffff0 + 0x05] = '0';      // rom date (same as Windows 98 SE)
1.1.1.49  root     18701:        mem[0xffff0 + 0x06] = '2';
                   18702:        mem[0xffff0 + 0x07] = '/';
                   18703:        mem[0xffff0 + 0x08] = '2';
                   18704:        mem[0xffff0 + 0x09] = '2';
                   18705:        mem[0xffff0 + 0x0a] = '/';
                   18706:        mem[0xffff0 + 0x0b] = '0';
                   18707:        mem[0xffff0 + 0x0c] = '6';
1.1.1.59  root     18708:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
1.1.1.49  root     18709:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.59  root     18710: #endif
1.1.1.24  root     18711:        
1.1       root     18712:        // param block
                   18713:        // + 0: param block (22bytes)
                   18714:        // +24: fcb1/2 (20bytes)
                   18715:        // +44: command tail (128bytes)
                   18716:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18717:        param->env_seg = 0;
                   18718:        param->cmd_line.w.l = 44;
                   18719:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18720:        param->fcb1.w.l = 24;
                   18721:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18722:        param->fcb2.w.l = 24;
                   18723:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18724:        
                   18725:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18726:        
                   18727:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18728:        if(argc > 1) {
                   18729:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18730:                for(int i = 2; i < argc; i++) {
                   18731:                        char tmp[128];
                   18732:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18733:                        strcpy(cmd_line->cmd, tmp);
                   18734:                }
                   18735:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18736:        } else {
                   18737:                cmd_line->len = 0;
                   18738:        }
                   18739:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18740:        
                   18741:        // system file table
1.1.1.21  root     18742:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18743:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18744:        
1.1.1.19  root     18745:        // disk buffer header (from DOSBox)
                   18746:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18747:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18748:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18749:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18750:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18751:        
1.1       root     18752:        // fcb table
                   18753:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18754:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18755:        
1.1.1.41  root     18756:        // drive parameter block
1.1.1.42  root     18757:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18758:                // may be a floppy drive
1.1.1.44  root     18759:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18760:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18761:                cds->drive_attrib = 0x4000;     // physical drive
                   18762:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18763:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18764:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18765:                cds->bs_offset = 2;
                   18766:                
1.1.1.41  root     18767:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18768:                dpb->drive_num = i;
                   18769:                dpb->unit_num = i;
1.1.1.43  root     18770:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18771:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18772:        }
                   18773:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18774:                msdos_cds_update(i);
1.1.1.42  root     18775:                UINT16 seg, ofs;
                   18776:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18777:        }
                   18778:        
1.1.1.17  root     18779:        // nls stuff
                   18780:        msdos_nls_tables_init();
1.1       root     18781:        
                   18782:        // execute command
1.1.1.28  root     18783:        try {
1.1.1.52  root     18784:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18785:                        fatalerror("'%s' not found\n", argv[0]);
                   18786:                }
                   18787:        } catch(...) {
                   18788:                // we should not reach here :-(
                   18789:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18790:        }
                   18791:        retval = 0;
                   18792:        return(0);
                   18793: }
                   18794: 
                   18795: #define remove_std_file(path) { \
                   18796:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18797:        if(fd != -1) { \
                   18798:                _lseek(fd, 0, SEEK_END); \
                   18799:                int size = _tell(fd); \
                   18800:                _close(fd); \
                   18801:                if(size == 0) { \
                   18802:                        remove(path); \
                   18803:                } \
                   18804:        } \
                   18805: }
                   18806: 
                   18807: void msdos_finish()
                   18808: {
                   18809:        for(int i = 0; i < MAX_FILES; i++) {
                   18810:                if(file_handler[i].valid) {
                   18811:                        _close(i);
                   18812:                }
                   18813:        }
1.1.1.21  root     18814: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18815:        remove_std_file("stdaux.txt");
1.1.1.21  root     18816: #endif
1.1.1.30  root     18817: #ifdef SUPPORT_XMS
                   18818:        msdos_xms_finish();
                   18819: #endif
1.1       root     18820:        msdos_dbcs_table_finish();
                   18821: }
                   18822: 
                   18823: /* ----------------------------------------------------------------------------
                   18824:        PC/AT hardware emulation
                   18825: ---------------------------------------------------------------------------- */
                   18826: 
                   18827: void hardware_init()
                   18828: {
1.1.1.3   root     18829:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18830:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18831:        m_IF = 1;
1.1.1.3   root     18832: #if defined(HAS_I386)
1.1       root     18833:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18834:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18835: #endif
                   18836:        i386_set_a20_line(0);
1.1.1.14  root     18837:        
1.1.1.19  root     18838:        ems_init();
1.1.1.25  root     18839:        dma_init();
1.1       root     18840:        pic_init();
1.1.1.25  root     18841:        pio_init();
1.1.1.8   root     18842: #ifdef PIT_ALWAYS_RUNNING
                   18843:        pit_init();
                   18844: #else
1.1       root     18845:        pit_active = 0;
                   18846: #endif
1.1.1.25  root     18847:        sio_init();
1.1.1.8   root     18848:        cmos_init();
                   18849:        kbd_init();
1.1       root     18850: }
                   18851: 
1.1.1.10  root     18852: void hardware_finish()
                   18853: {
                   18854: #if defined(HAS_I386)
                   18855:        vtlb_free(m_vtlb);
                   18856: #endif
1.1.1.19  root     18857:        ems_finish();
1.1.1.37  root     18858:        pio_finish();
1.1.1.25  root     18859:        sio_finish();
1.1.1.10  root     18860: }
                   18861: 
1.1.1.28  root     18862: void hardware_release()
                   18863: {
                   18864:        // release hardware resources when this program will be terminated abnormally
                   18865: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18866:        if(fp_debug_log != NULL) {
                   18867:                fclose(fp_debug_log);
                   18868:                fp_debug_log = NULL;
1.1.1.28  root     18869:        }
                   18870: #endif
                   18871: #if defined(HAS_I386)
                   18872:        vtlb_free(m_vtlb);
                   18873: #endif
                   18874:        ems_release();
1.1.1.37  root     18875:        pio_release();
1.1.1.28  root     18876:        sio_release();
                   18877: }
                   18878: 
1.1       root     18879: void hardware_run()
                   18880: {
1.1.1.22  root     18881: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18882:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18883:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18884: #endif
1.1.1.51  root     18885: #ifdef USE_DEBUGGER
                   18886:        m_int_num = -1;
                   18887: #endif
1.1.1.54  root     18888:        while(!m_exit) {
1.1.1.50  root     18889:                hardware_run_cpu();
1.1       root     18890:        }
1.1.1.22  root     18891: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18892:        if(fp_debug_log != NULL) {
                   18893:                fclose(fp_debug_log);
                   18894:                fp_debug_log = NULL;
1.1.1.28  root     18895:        }
1.1.1.22  root     18896: #endif
1.1       root     18897: }
                   18898: 
1.1.1.50  root     18899: inline void hardware_run_cpu()
                   18900: {
                   18901: #if defined(HAS_I386)
                   18902:        CPU_EXECUTE_CALL(i386);
                   18903:        if(m_eip != m_prev_eip) {
                   18904:                idle_ops++;
                   18905:        }
                   18906: #else
                   18907:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18908:        if(m_pc != m_prevpc) {
                   18909:                idle_ops++;
                   18910:        }
                   18911: #endif
                   18912: #ifdef USE_DEBUGGER
                   18913:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18914:        if(m_int_num >= 0) {
                   18915:                unsigned num = (unsigned)m_int_num;
                   18916:                m_int_num = -1;
                   18917:                msdos_syscall(num);
                   18918:        }
                   18919: #endif
                   18920:        if(++update_ops == UPDATE_OPS) {
                   18921:                update_ops = 0;
                   18922:                hardware_update();
                   18923:        }
                   18924: }
                   18925: 
1.1       root     18926: void hardware_update()
                   18927: {
1.1.1.8   root     18928:        static UINT32 prev_time = 0;
                   18929:        UINT32 cur_time = timeGetTime();
                   18930:        
                   18931:        if(prev_time != cur_time) {
                   18932:                // update pit and raise irq0
                   18933: #ifndef PIT_ALWAYS_RUNNING
                   18934:                if(pit_active)
                   18935: #endif
                   18936:                {
                   18937:                        if(pit_run(0, cur_time)) {
                   18938:                                pic_req(0, 0, 1);
                   18939:                        }
                   18940:                        pit_run(1, cur_time);
                   18941:                        pit_run(2, cur_time);
                   18942:                }
1.1.1.24  root     18943:                
1.1.1.25  root     18944:                // update sio and raise irq4/3
1.1.1.29  root     18945:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18946:                        sio_update(c);
                   18947:                }
                   18948:                
1.1.1.24  root     18949:                // update keyboard and mouse
1.1.1.14  root     18950:                static UINT32 prev_tick = 0;
                   18951:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     18952:                
1.1.1.14  root     18953:                if(prev_tick != cur_tick) {
                   18954:                        // update keyboard flags
                   18955:                        UINT8 state;
1.1.1.24  root     18956:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   18957:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   18958:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   18959:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   18960:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   18961:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   18962:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   18963:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     18964:                        mem[0x417] = state;
                   18965:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   18966:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   18967:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   18968:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     18969: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   18970: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     18971:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   18972:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   18973:                        mem[0x418] = state;
                   18974:                        
1.1.1.24  root     18975:                        // update console input if needed
1.1.1.34  root     18976:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     18977:                                update_console_input();
                   18978:                        }
1.1.1.57  root     18979:                        if(!(kbd_status & 1)) {
                   18980:                                if(key_buf_data != NULL) {
                   18981: #ifdef USE_SERVICE_THREAD
                   18982:                                        EnterCriticalSection(&key_buf_crit_sect);
                   18983: #endif
                   18984:                                        if(!key_buf_data->empty()) {
                   18985:                                                kbd_data = key_buf_data->read();
                   18986:                                                kbd_status |= 1;
                   18987:                                                key_changed = true;
                   18988:                                        }
                   18989: #ifdef USE_SERVICE_THREAD
                   18990:                                        LeaveCriticalSection(&key_buf_crit_sect);
                   18991: #endif
                   18992:                                }
                   18993:                        }
1.1.1.24  root     18994:                        
1.1.1.57  root     18995:                        // raise irq1 if key is pressed/released or key buffer is not empty
1.1.1.56  root     18996:                        if(!key_changed) {
1.1.1.55  root     18997: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     18998:                                EnterCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     18999: #endif
1.1.1.57  root     19000:                                if(!pcbios_is_key_buffer_empty()) {
                   19001: /*
                   19002:                                        if(!(kbd_status & 1)) {
                   19003:                                                UINT16 head = *(UINT16 *)(mem + 0x41a);
                   19004:                                                UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   19005:                                                if(head != tail) {
                   19006:                                                        int key_char = mem[0x400 + (head++)];
                   19007:                                                        int key_scan = mem[0x400 + (head++)];
                   19008:                                                        kbd_data = key_char ? key_char : key_scan;
                   19009:                                                        kbd_status |= 1;
                   19010:                                                }
                   19011:                                        }
                   19012: */
                   19013:                                        key_changed = true;
                   19014:                                }
1.1.1.55  root     19015: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     19016:                                LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     19017: #endif
1.1.1.56  root     19018:                        }
                   19019:                        if(key_changed) {
1.1.1.8   root     19020:                                pic_req(0, 1, 1);
1.1.1.56  root     19021:                                key_changed = false;
1.1.1.24  root     19022:                        }
                   19023:                        
                   19024:                        // raise irq12 if mouse status is changed
1.1.1.59  root     19025:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     19026:                                mouse.status_irq = 0; // ???
                   19027:                                mouse.status_irq_alt = 0; // ???
                   19028:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
                   19029:                                mouse.status = 0;
                   19030:                                pic_req(1, 4, 1);
1.1.1.59  root     19031:                        } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     19032:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   19033:                                mouse.status_irq_alt = 0; // ???
1.1.1.54  root     19034:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     19035:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     19036:                                pic_req(1, 4, 1);
                   19037:                        } else {
                   19038:                                for(int i = 0; i < 8; i++) {
                   19039:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   19040:                                                mouse.status_irq = 0; // ???
                   19041:                                                mouse.status_irq_alt = 0;
1.1.1.54  root     19042:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     19043:                                                for(int j = 0; j < 8; j++) {
                   19044:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   19045:                                                                mouse.status_irq_alt |=  (1 << j);
                   19046:                                                                mouse.status_alt     &= ~(1 << j);
                   19047:                                                        }
                   19048:                                                }
                   19049:                                                pic_req(1, 4, 1);
                   19050:                                                break;
                   19051:                                        }
                   19052:                                }
1.1.1.8   root     19053:                        }
1.1.1.24  root     19054:                        
1.1.1.60  root     19055:                        prev_tick = cur_tick;
                   19056:                }
                   19057:                
                   19058:                // update cursor size/position by crtc
                   19059:                if(crtc_changed[10] != 0 || crtc_changed[11] != 0) {
                   19060:                        int size = (int)(crtc_regs[11] & 7) - (int)(crtc_regs[10] & 7) + 1;
                   19061:                        if(!((crtc_regs[10] & 0x20) != 0 || size < 0)) {
                   19062:                                ci_new.bVisible = TRUE;
                   19063:                                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
1.1.1.59  root     19064:                        } else {
1.1.1.60  root     19065:                                ci_new.bVisible = FALSE;
                   19066:                        }
                   19067:                        crtc_changed[10] = crtc_changed[11] = 0;
                   19068:                }
                   19069:                if(crtc_changed[14] != 0 || crtc_changed[15] != 0) {
                   19070:                        if(cursor_moved) {
                   19071:                                pcbios_update_cursor_position();
                   19072:                                cursor_moved = false;
1.1.1.59  root     19073:                        }
1.1.1.60  root     19074:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   19075:                        int position = crtc_regs[14] * 256 + crtc_regs[15];
                   19076:                        int width = *(UINT16 *)(mem + 0x44a);
                   19077:                        COORD co;
                   19078:                        co.X = position % width;
                   19079:                        co.Y = position / width + scr_top;
                   19080:                        SetConsoleCursorPosition(hStdout, co);
1.1.1.59  root     19081:                        
1.1.1.60  root     19082:                        crtc_changed[14] = crtc_changed[15] = 0;
                   19083:                        cursor_moved_by_crtc = true;
                   19084:                }
                   19085:                
                   19086:                // update cursor info
                   19087:                if(!is_cursor_blink_off()) {
                   19088:                        ci_new.bVisible = TRUE;
                   19089:                }
                   19090:                if(!(ci_old.dwSize == ci_new.dwSize && ci_old.bVisible == ci_new.bVisible)) {
                   19091:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   19092:                        SetConsoleCursorInfo(hStdout, &ci_new);
1.1.1.8   root     19093:                }
1.1.1.60  root     19094:                ci_old = ci_new;
1.1.1.24  root     19095:                
1.1.1.19  root     19096:                // update daily timer counter
                   19097:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     19098:                
1.1.1.8   root     19099:                prev_time = cur_time;
1.1       root     19100:        }
                   19101: }
                   19102: 
1.1.1.19  root     19103: // ems
                   19104: 
                   19105: void ems_init()
                   19106: {
                   19107:        memset(ems_handles, 0, sizeof(ems_handles));
                   19108:        memset(ems_pages, 0, sizeof(ems_pages));
                   19109:        free_ems_pages = MAX_EMS_PAGES;
                   19110: }
                   19111: 
                   19112: void ems_finish()
                   19113: {
1.1.1.28  root     19114:        ems_release();
                   19115: }
                   19116: 
                   19117: void ems_release()
                   19118: {
1.1.1.31  root     19119:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     19120:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     19121:                        free(ems_handles[i].buffer);
                   19122:                        ems_handles[i].buffer = NULL;
                   19123:                }
                   19124:        }
                   19125: }
                   19126: 
                   19127: void ems_allocate_pages(int handle, int pages)
                   19128: {
                   19129:        if(pages > 0) {
                   19130:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   19131:        } else {
                   19132:                ems_handles[handle].buffer = NULL;
                   19133:        }
                   19134:        ems_handles[handle].pages = pages;
                   19135:        ems_handles[handle].allocated = true;
                   19136:        free_ems_pages -= pages;
                   19137: }
                   19138: 
                   19139: void ems_reallocate_pages(int handle, int pages)
                   19140: {
                   19141:        if(ems_handles[handle].allocated) {
                   19142:                if(ems_handles[handle].pages != pages) {
                   19143:                        UINT8 *new_buffer = NULL;
                   19144:                        
                   19145:                        if(pages > 0) {
                   19146:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   19147:                        }
1.1.1.32  root     19148:                        if(ems_handles[handle].buffer != NULL) {
                   19149:                                if(new_buffer != NULL) {
1.1.1.19  root     19150:                                        if(pages > ems_handles[handle].pages) {
                   19151:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   19152:                                        } else {
                   19153:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   19154:                                        }
                   19155:                                }
                   19156:                                free(ems_handles[handle].buffer);
                   19157:                                ems_handles[handle].buffer = NULL;
                   19158:                        }
                   19159:                        free_ems_pages += ems_handles[handle].pages;
                   19160:                        
                   19161:                        ems_handles[handle].buffer = new_buffer;
                   19162:                        ems_handles[handle].pages = pages;
                   19163:                        free_ems_pages -= pages;
                   19164:                }
                   19165:        } else {
                   19166:                ems_allocate_pages(handle, pages);
                   19167:        }
                   19168: }
                   19169: 
                   19170: void ems_release_pages(int handle)
                   19171: {
                   19172:        if(ems_handles[handle].allocated) {
1.1.1.32  root     19173:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     19174:                        free(ems_handles[handle].buffer);
                   19175:                        ems_handles[handle].buffer = NULL;
                   19176:                }
                   19177:                free_ems_pages += ems_handles[handle].pages;
                   19178:                ems_handles[handle].allocated = false;
                   19179:        }
                   19180: }
                   19181: 
                   19182: void ems_map_page(int physical, int handle, int logical)
                   19183: {
                   19184:        if(ems_pages[physical].mapped) {
                   19185:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   19186:                        return;
                   19187:                }
                   19188:                ems_unmap_page(physical);
                   19189:        }
1.1.1.32  root     19190:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     19191:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   19192:        }
                   19193:        ems_pages[physical].handle = handle;
                   19194:        ems_pages[physical].page = logical;
                   19195:        ems_pages[physical].mapped = true;
                   19196: }
                   19197: 
                   19198: void ems_unmap_page(int physical)
                   19199: {
                   19200:        if(ems_pages[physical].mapped) {
                   19201:                int handle = ems_pages[physical].handle;
                   19202:                int logical = ems_pages[physical].page;
                   19203:                
1.1.1.32  root     19204:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     19205:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   19206:                }
                   19207:                ems_pages[physical].mapped = false;
                   19208:        }
                   19209: }
                   19210: 
1.1.1.25  root     19211: // dma
1.1       root     19212: 
1.1.1.25  root     19213: void dma_init()
1.1       root     19214: {
1.1.1.26  root     19215:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     19216:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     19217: //             for(int ch = 0; ch < 4; ch++) {
                   19218: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   19219: //             }
1.1.1.25  root     19220:                dma_reset(c);
                   19221:        }
1.1       root     19222: }
                   19223: 
1.1.1.25  root     19224: void dma_reset(int c)
1.1       root     19225: {
1.1.1.25  root     19226:        dma[c].low_high = false;
                   19227:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   19228:        dma[c].mask = 0xff;
                   19229: }
                   19230: 
                   19231: void dma_write(int c, UINT32 addr, UINT8 data)
                   19232: {
                   19233:        int ch = (addr >> 1) & 3;
                   19234:        UINT8 bit = 1 << (data & 3);
                   19235:        
                   19236:        switch(addr & 0x0f) {
                   19237:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19238:                if(dma[c].low_high) {
                   19239:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     19240:                } else {
1.1.1.25  root     19241:                        dma[c].ch[ch].bareg.b.l = data;
                   19242:                }
                   19243:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19244:                dma[c].low_high = !dma[c].low_high;
                   19245:                break;
                   19246:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19247:                if(dma[c].low_high) {
                   19248:                        dma[c].ch[ch].bcreg.b.h = data;
                   19249:                } else {
                   19250:                        dma[c].ch[ch].bcreg.b.l = data;
                   19251:                }
                   19252:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19253:                dma[c].low_high = !dma[c].low_high;
                   19254:                break;
                   19255:        case 0x08:
                   19256:                // command register
                   19257:                dma[c].cmd = data;
                   19258:                break;
                   19259:        case 0x09:
                   19260:                // dma[c].request register
                   19261:                if(data & 4) {
                   19262:                        if(!(dma[c].req & bit)) {
                   19263:                                dma[c].req |= bit;
                   19264: //                             dma_run(c, ch);
                   19265:                        }
                   19266:                } else {
                   19267:                        dma[c].req &= ~bit;
                   19268:                }
                   19269:                break;
                   19270:        case 0x0a:
                   19271:                // single mask register
                   19272:                if(data & 4) {
                   19273:                        dma[c].mask |= bit;
                   19274:                } else {
                   19275:                        dma[c].mask &= ~bit;
                   19276:                }
                   19277:                break;
                   19278:        case 0x0b:
                   19279:                // mode register
                   19280:                dma[c].ch[data & 3].mode = data;
                   19281:                break;
                   19282:        case 0x0c:
                   19283:                dma[c].low_high = false;
                   19284:                break;
                   19285:        case 0x0d:
                   19286:                // clear master
                   19287:                dma_reset(c);
                   19288:                break;
                   19289:        case 0x0e:
                   19290:                // clear mask register
                   19291:                dma[c].mask = 0;
                   19292:                break;
                   19293:        case 0x0f:
                   19294:                // all mask register
                   19295:                dma[c].mask = data & 0x0f;
                   19296:                break;
                   19297:        }
                   19298: }
                   19299: 
                   19300: UINT8 dma_read(int c, UINT32 addr)
                   19301: {
                   19302:        int ch = (addr >> 1) & 3;
                   19303:        UINT8 val = 0xff;
                   19304:        
                   19305:        switch(addr & 0x0f) {
                   19306:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19307:                if(dma[c].low_high) {
                   19308:                        val = dma[c].ch[ch].areg.b.h;
                   19309:                } else {
                   19310:                        val = dma[c].ch[ch].areg.b.l;
                   19311:                }
                   19312:                dma[c].low_high = !dma[c].low_high;
                   19313:                return(val);
                   19314:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19315:                if(dma[c].low_high) {
                   19316:                        val = dma[c].ch[ch].creg.b.h;
                   19317:                } else {
                   19318:                        val = dma[c].ch[ch].creg.b.l;
                   19319:                }
                   19320:                dma[c].low_high = !dma[c].low_high;
                   19321:                return(val);
                   19322:        case 0x08:
                   19323:                // status register
                   19324:                val = (dma[c].req << 4) | dma[c].tc;
                   19325:                dma[c].tc = 0;
                   19326:                return(val);
                   19327:        case 0x0d:
1.1.1.26  root     19328:                // temporary register (intel 82374 does not support)
1.1.1.25  root     19329:                return(dma[c].tmp & 0xff);
1.1.1.26  root     19330:        case 0x0f:
                   19331:                // mask register (intel 82374 does support)
                   19332:                return(dma[c].mask);
1.1.1.25  root     19333:        }
                   19334:        return(0xff);
                   19335: }
                   19336: 
                   19337: void dma_page_write(int c, int ch, UINT8 data)
                   19338: {
                   19339:        dma[c].ch[ch].pagereg = data;
                   19340: }
                   19341: 
                   19342: UINT8 dma_page_read(int c, int ch)
                   19343: {
                   19344:        return(dma[c].ch[ch].pagereg);
                   19345: }
                   19346: 
                   19347: void dma_run(int c, int ch)
                   19348: {
                   19349:        UINT8 bit = 1 << ch;
                   19350:        
                   19351:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   19352:                // execute dma
                   19353:                while(dma[c].req & bit) {
                   19354:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   19355:                                // memory -> memory
                   19356:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   19357:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   19358:                                
                   19359:                                if(c == 0) {
                   19360:                                        dma[c].tmp = read_byte(saddr);
                   19361:                                        write_byte(daddr, dma[c].tmp);
                   19362:                                } else {
                   19363:                                        dma[c].tmp = read_word(saddr << 1);
                   19364:                                        write_word(daddr << 1, dma[c].tmp);
                   19365:                                }
                   19366:                                if(!(dma[c].cmd & 0x02)) {
                   19367:                                        if(dma[c].ch[0].mode & 0x20) {
                   19368:                                                dma[c].ch[0].areg.w--;
                   19369:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   19370:                                                        dma[c].ch[0].pagereg--;
                   19371:                                                }
                   19372:                                        } else {
                   19373:                                                dma[c].ch[0].areg.w++;
                   19374:                                                if(dma[c].ch[0].areg.w == 0) {
                   19375:                                                        dma[c].ch[0].pagereg++;
                   19376:                                                }
                   19377:                                        }
                   19378:                                }
                   19379:                                if(dma[c].ch[1].mode & 0x20) {
                   19380:                                        dma[c].ch[1].areg.w--;
                   19381:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   19382:                                                dma[c].ch[1].pagereg--;
                   19383:                                        }
                   19384:                                } else {
                   19385:                                        dma[c].ch[1].areg.w++;
                   19386:                                        if(dma[c].ch[1].areg.w == 0) {
                   19387:                                                dma[c].ch[1].pagereg++;
                   19388:                                        }
                   19389:                                }
                   19390:                                
                   19391:                                // check dma condition
                   19392:                                if(dma[c].ch[0].creg.w-- == 0) {
                   19393:                                        if(dma[c].ch[0].mode & 0x10) {
                   19394:                                                // self initialize
                   19395:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   19396:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   19397:                                        } else {
                   19398: //                                             dma[c].mask |= bit;
                   19399:                                        }
                   19400:                                }
                   19401:                                if(dma[c].ch[1].creg.w-- == 0) {
                   19402:                                        // terminal count
                   19403:                                        if(dma[c].ch[1].mode & 0x10) {
                   19404:                                                // self initialize
                   19405:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   19406:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   19407:                                        } else {
                   19408:                                                dma[c].mask |= bit;
                   19409:                                        }
                   19410:                                        dma[c].req &= ~bit;
                   19411:                                        dma[c].tc |= bit;
                   19412:                                }
                   19413:                        } else {
                   19414:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   19415:                                
                   19416:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   19417:                                        // verify
                   19418:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   19419:                                        // io -> memory
                   19420:                                        if(c == 0) {
                   19421:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   19422:                                                write_byte(addr, dma[c].tmp);
                   19423:                                        } else {
                   19424:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   19425:                                                write_word(addr << 1, dma[c].tmp);
                   19426:                                        }
                   19427:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   19428:                                        // memory -> io
                   19429:                                        if(c == 0) {
                   19430:                                                dma[c].tmp = read_byte(addr);
                   19431:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   19432:                                        } else {
                   19433:                                                dma[c].tmp = read_word(addr << 1);
                   19434:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   19435:                                        }
                   19436:                                }
                   19437:                                if(dma[c].ch[ch].mode & 0x20) {
                   19438:                                        dma[c].ch[ch].areg.w--;
                   19439:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   19440:                                                dma[c].ch[ch].pagereg--;
                   19441:                                        }
                   19442:                                } else {
                   19443:                                        dma[c].ch[ch].areg.w++;
                   19444:                                        if(dma[c].ch[ch].areg.w == 0) {
                   19445:                                                dma[c].ch[ch].pagereg++;
                   19446:                                        }
                   19447:                                }
                   19448:                                
                   19449:                                // check dma condition
                   19450:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   19451:                                        // terminal count
                   19452:                                        if(dma[c].ch[ch].mode & 0x10) {
                   19453:                                                // self initialize
                   19454:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19455:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19456:                                        } else {
                   19457:                                                dma[c].mask |= bit;
                   19458:                                        }
                   19459:                                        dma[c].req &= ~bit;
                   19460:                                        dma[c].tc |= bit;
                   19461:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   19462:                                        // single mode
                   19463:                                        break;
                   19464:                                }
                   19465:                        }
                   19466:                }
                   19467:        }
                   19468: }
                   19469: 
                   19470: // pic
                   19471: 
                   19472: void pic_init()
                   19473: {
                   19474:        memset(pic, 0, sizeof(pic));
                   19475:        pic[0].imr = pic[1].imr = 0xff;
                   19476:        
                   19477:        // from bochs bios
                   19478:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   19479:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   19480:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   19481:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   19482:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   19483:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   19484:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   19485:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   19486:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   19487: }
                   19488: 
                   19489: void pic_write(int c, UINT32 addr, UINT8 data)
                   19490: {
                   19491:        if(addr & 1) {
                   19492:                if(pic[c].icw2_r) {
                   19493:                        // icw2
                   19494:                        pic[c].icw2 = data;
                   19495:                        pic[c].icw2_r = 0;
                   19496:                } else if(pic[c].icw3_r) {
                   19497:                        // icw3
                   19498:                        pic[c].icw3 = data;
                   19499:                        pic[c].icw3_r = 0;
                   19500:                } else if(pic[c].icw4_r) {
                   19501:                        // icw4
                   19502:                        pic[c].icw4 = data;
                   19503:                        pic[c].icw4_r = 0;
                   19504:                } else {
                   19505:                        // ocw1
1.1       root     19506:                        pic[c].imr = data;
                   19507:                }
                   19508:        } else {
                   19509:                if(data & 0x10) {
                   19510:                        // icw1
                   19511:                        pic[c].icw1 = data;
                   19512:                        pic[c].icw2_r = 1;
                   19513:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   19514:                        pic[c].icw4_r = data & 1;
                   19515:                        pic[c].irr = 0;
                   19516:                        pic[c].isr = 0;
                   19517:                        pic[c].imr = 0;
                   19518:                        pic[c].prio = 0;
                   19519:                        if(!(pic[c].icw1 & 1)) {
                   19520:                                pic[c].icw4 = 0;
                   19521:                        }
                   19522:                        pic[c].ocw3 = 0;
                   19523:                } else if(data & 8) {
                   19524:                        // ocw3
                   19525:                        if(!(data & 2)) {
                   19526:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   19527:                        }
                   19528:                        if(!(data & 0x40)) {
                   19529:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   19530:                        }
                   19531:                        pic[c].ocw3 = data;
                   19532:                } else {
                   19533:                        // ocw2
                   19534:                        int level = 0;
                   19535:                        if(data & 0x40) {
                   19536:                                level = data & 7;
                   19537:                        } else {
                   19538:                                if(!pic[c].isr) {
                   19539:                                        return;
                   19540:                                }
                   19541:                                level = pic[c].prio;
                   19542:                                while(!(pic[c].isr & (1 << level))) {
                   19543:                                        level = (level + 1) & 7;
                   19544:                                }
                   19545:                        }
                   19546:                        if(data & 0x80) {
                   19547:                                pic[c].prio = (level + 1) & 7;
                   19548:                        }
                   19549:                        if(data & 0x20) {
                   19550:                                pic[c].isr &= ~(1 << level);
                   19551:                        }
                   19552:                }
                   19553:        }
                   19554:        pic_update();
                   19555: }
                   19556: 
                   19557: UINT8 pic_read(int c, UINT32 addr)
                   19558: {
                   19559:        if(addr & 1) {
                   19560:                return(pic[c].imr);
                   19561:        } else {
                   19562:                // polling mode is not supported...
                   19563:                //if(pic[c].ocw3 & 4) {
                   19564:                //      return ???;
                   19565:                //}
                   19566:                if(pic[c].ocw3 & 1) {
                   19567:                        return(pic[c].isr);
                   19568:                } else {
                   19569:                        return(pic[c].irr);
                   19570:                }
                   19571:        }
                   19572: }
                   19573: 
                   19574: void pic_req(int c, int level, int signal)
                   19575: {
                   19576:        if(signal) {
                   19577:                pic[c].irr |= (1 << level);
                   19578:        } else {
                   19579:                pic[c].irr &= ~(1 << level);
                   19580:        }
                   19581:        pic_update();
                   19582: }
                   19583: 
                   19584: int pic_ack()
                   19585: {
                   19586:        // ack (INTA=L)
                   19587:        pic[pic_req_chip].isr |= pic_req_bit;
                   19588:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   19589:        if(pic_req_chip > 0) {
                   19590:                // update isr and irr of master
                   19591:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   19592:                pic[pic_req_chip - 1].isr |= slave;
                   19593:                pic[pic_req_chip - 1].irr &= ~slave;
                   19594:        }
                   19595:        //if(pic[pic_req_chip].icw4 & 1) {
                   19596:                // 8086 mode
                   19597:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   19598:        //} else {
                   19599:        //      // 8080 mode
                   19600:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   19601:        //      if(pic[pic_req_chip].icw1 & 4) {
                   19602:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   19603:        //      } else {
                   19604:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   19605:        //      }
                   19606:        //      vector = 0xcd | (addr << 8);
                   19607:        //}
                   19608:        if(pic[pic_req_chip].icw4 & 2) {
                   19609:                // auto eoi
                   19610:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   19611:        }
                   19612:        return(vector);
                   19613: }
                   19614: 
                   19615: void pic_update()
                   19616: {
                   19617:        for(int c = 0; c < 2; c++) {
                   19618:                UINT8 irr = pic[c].irr;
                   19619:                if(c + 1 < 2) {
                   19620:                        // this is master
                   19621:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   19622:                                // request from slave
                   19623:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   19624:                        }
                   19625:                }
                   19626:                irr &= (~pic[c].imr);
                   19627:                if(!irr) {
                   19628:                        break;
                   19629:                }
                   19630:                if(!(pic[c].ocw3 & 0x20)) {
                   19631:                        irr |= pic[c].isr;
                   19632:                }
                   19633:                int level = pic[c].prio;
                   19634:                UINT8 bit = 1 << level;
                   19635:                while(!(irr & bit)) {
                   19636:                        level = (level + 1) & 7;
                   19637:                        bit = 1 << level;
                   19638:                }
                   19639:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   19640:                        // check slave
                   19641:                        continue;
                   19642:                }
                   19643:                if(pic[c].isr & bit) {
                   19644:                        break;
                   19645:                }
                   19646:                // interrupt request
                   19647:                pic_req_chip = c;
                   19648:                pic_req_level = level;
                   19649:                pic_req_bit = bit;
1.1.1.3   root     19650:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     19651:                return;
                   19652:        }
1.1.1.3   root     19653:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     19654: }
1.1       root     19655: 
1.1.1.25  root     19656: // pio
                   19657: 
                   19658: void pio_init()
                   19659: {
1.1.1.38  root     19660: //     bool conv_mode = (GetConsoleCP() == 932);
                   19661:        
1.1.1.26  root     19662:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     19663:        
1.1.1.25  root     19664:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     19665:                pio[c].stat = 0xdf;
1.1.1.25  root     19666:                pio[c].ctrl = 0x0c;
1.1.1.38  root     19667: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     19668:        }
                   19669: }
                   19670: 
1.1.1.37  root     19671: void pio_finish()
                   19672: {
                   19673:        pio_release();
                   19674: }
                   19675: 
                   19676: void pio_release()
                   19677: {
                   19678:        for(int c = 0; c < 2; c++) {
                   19679:                if(pio[c].fp != NULL) {
1.1.1.38  root     19680:                        if(pio[c].jis_mode) {
                   19681:                                fputc(0x1c, pio[c].fp);
                   19682:                                fputc(0x2e, pio[c].fp);
                   19683:                        }
1.1.1.37  root     19684:                        fclose(pio[c].fp);
                   19685:                        pio[c].fp = NULL;
                   19686:                }
                   19687:        }
                   19688: }
                   19689: 
1.1.1.25  root     19690: void pio_write(int c, UINT32 addr, UINT8 data)
                   19691: {
                   19692:        switch(addr & 3) {
                   19693:        case 0:
                   19694:                pio[c].data = data;
                   19695:                break;
                   19696:        case 2:
1.1.1.37  root     19697:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   19698:                        // strobe H -> L
                   19699:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   19700:                                // auto feed
                   19701:                                printer_out(c, 0x0d);
                   19702:                                printer_out(c, 0x0a);
                   19703:                        } else {
                   19704:                                printer_out(c, pio[c].data);
                   19705:                        }
                   19706:                        pio[c].stat &= ~0x40; // set ack
                   19707:                }
1.1.1.25  root     19708:                pio[c].ctrl = data;
                   19709:                break;
                   19710:        }
                   19711: }
                   19712: 
                   19713: UINT8 pio_read(int c, UINT32 addr)
                   19714: {
                   19715:        switch(addr & 3) {
                   19716:        case 0:
1.1.1.37  root     19717:                if(pio[c].ctrl & 0x20) {
                   19718:                        // input mode
                   19719:                        return(0xff);
                   19720:                }
1.1.1.25  root     19721:                return(pio[c].data);
                   19722:        case 1:
1.1.1.37  root     19723:                {
                   19724:                        UINT8 stat = pio[c].stat;
                   19725:                        pio[c].stat |= 0x40; // clear ack
                   19726:                        return(stat);
                   19727:                }
1.1.1.25  root     19728:        case 2:
                   19729:                return(pio[c].ctrl);
                   19730:        }
                   19731:        return(0xff);
                   19732: }
                   19733: 
1.1.1.37  root     19734: void printer_out(int c, UINT8 data)
                   19735: {
                   19736:        SYSTEMTIME time;
1.1.1.38  root     19737:        bool jis_mode = false;
1.1.1.37  root     19738:        
                   19739:        GetLocalTime(&time);
                   19740:        
                   19741:        if(pio[c].fp != NULL) {
                   19742:                // if at least 1000ms passed from last written, close the current file
                   19743:                FILETIME ftime1;
                   19744:                FILETIME ftime2;
                   19745:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   19746:                SystemTimeToFileTime(&time, &ftime2);
                   19747:                INT64 *time1 = (INT64 *)&ftime1;
                   19748:                INT64 *time2 = (INT64 *)&ftime2;
                   19749:                INT64 msec = (*time2 - *time1) / 10000;
                   19750:                
                   19751:                if(msec >= 1000) {
1.1.1.38  root     19752:                        if(pio[c].jis_mode) {
                   19753:                                fputc(0x1c, pio[c].fp);
                   19754:                                fputc(0x2e, pio[c].fp);
                   19755:                                jis_mode = true;
                   19756:                        }
1.1.1.37  root     19757:                        fclose(pio[c].fp);
                   19758:                        pio[c].fp = NULL;
                   19759:                }
                   19760:        }
                   19761:        if(pio[c].fp == NULL) {
                   19762:                // create a new file in the temp folder
                   19763:                char file_name[MAX_PATH];
                   19764:                
                   19765:                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);
1.1.1.60  root     19766:                if(GetTempPathA(MAX_PATH, pio[c].path)) {
1.1.1.37  root     19767:                        strcat(pio[c].path, file_name);
                   19768:                } else {
                   19769:                        strcpy(pio[c].path, file_name);
                   19770:                }
1.1.1.38  root     19771:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     19772:        }
                   19773:        if(pio[c].fp != NULL) {
1.1.1.38  root     19774:                if(jis_mode) {
                   19775:                        fputc(0x1c, pio[c].fp);
                   19776:                        fputc(0x26, pio[c].fp);
                   19777:                }
1.1.1.37  root     19778:                fputc(data, pio[c].fp);
1.1.1.38  root     19779:                
                   19780:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   19781:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   19782:                        UINT8 buffer[4];
                   19783:                        fseek(pio[c].fp, 0, SEEK_SET);
                   19784:                        fread(buffer, 4, 1, pio[c].fp);
                   19785:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   19786:                                fclose(pio[c].fp);
                   19787:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   19788:                        }
                   19789:                }
1.1.1.37  root     19790:                pio[c].time = time;
                   19791:        }
                   19792: }
                   19793: 
1.1       root     19794: // pit
                   19795: 
1.1.1.22  root     19796: #define PIT_FREQ 1193182ULL
1.1       root     19797: #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)
                   19798: 
                   19799: void pit_init()
                   19800: {
1.1.1.8   root     19801:        memset(pit, 0, sizeof(pit));
1.1       root     19802:        for(int ch = 0; ch < 3; ch++) {
                   19803:                pit[ch].count = 0x10000;
                   19804:                pit[ch].ctrl_reg = 0x34;
                   19805:                pit[ch].mode = 3;
                   19806:        }
                   19807:        
                   19808:        // from bochs bios
                   19809:        pit_write(3, 0x34);
                   19810:        pit_write(0, 0x00);
                   19811:        pit_write(0, 0x00);
                   19812: }
                   19813: 
                   19814: void pit_write(int ch, UINT8 val)
                   19815: {
1.1.1.8   root     19816: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19817:        if(!pit_active) {
                   19818:                pit_active = 1;
                   19819:                pit_init();
                   19820:        }
1.1.1.8   root     19821: #endif
1.1       root     19822:        switch(ch) {
                   19823:        case 0:
                   19824:        case 1:
                   19825:        case 2:
                   19826:                // write count register
                   19827:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19828:                        if(pit[ch].ctrl_reg & 0x10) {
                   19829:                                pit[ch].low_write = 1;
                   19830:                        }
                   19831:                        if(pit[ch].ctrl_reg & 0x20) {
                   19832:                                pit[ch].high_write = 1;
                   19833:                        }
                   19834:                }
                   19835:                if(pit[ch].low_write) {
                   19836:                        pit[ch].count_reg = val;
                   19837:                        pit[ch].low_write = 0;
                   19838:                } else if(pit[ch].high_write) {
                   19839:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19840:                                pit[ch].count_reg = val << 8;
                   19841:                        } else {
                   19842:                                pit[ch].count_reg |= val << 8;
                   19843:                        }
                   19844:                        pit[ch].high_write = 0;
                   19845:                }
                   19846:                // start count
1.1.1.8   root     19847:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19848:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19849:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19850:                                pit[ch].prev_time = timeGetTime();
                   19851:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19852:                        }
                   19853:                }
                   19854:                break;
                   19855:        case 3: // ctrl reg
                   19856:                if((val & 0xc0) == 0xc0) {
                   19857:                        // i8254 read-back command
                   19858:                        for(ch = 0; ch < 3; ch++) {
                   19859:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19860:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19861:                                        pit[ch].status_latched = 1;
                   19862:                                }
                   19863:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19864:                                        pit_latch_count(ch);
                   19865:                                }
                   19866:                        }
                   19867:                        break;
                   19868:                }
                   19869:                ch = (val >> 6) & 3;
                   19870:                if(val & 0x30) {
1.1.1.35  root     19871:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19872:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19873:                        pit[ch].count_latched = 0;
                   19874:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19875:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19876:                        pit[ch].ctrl_reg = val;
                   19877:                        // stop count
1.1.1.8   root     19878:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19879:                        pit[ch].count_reg = 0;
                   19880:                } else if(!pit[ch].count_latched) {
                   19881:                        pit_latch_count(ch);
                   19882:                }
                   19883:                break;
                   19884:        }
                   19885: }
                   19886: 
                   19887: UINT8 pit_read(int ch)
                   19888: {
1.1.1.8   root     19889: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19890:        if(!pit_active) {
                   19891:                pit_active = 1;
                   19892:                pit_init();
                   19893:        }
1.1.1.8   root     19894: #endif
1.1       root     19895:        switch(ch) {
                   19896:        case 0:
                   19897:        case 1:
                   19898:        case 2:
                   19899:                if(pit[ch].status_latched) {
                   19900:                        pit[ch].status_latched = 0;
                   19901:                        return(pit[ch].status);
                   19902:                }
                   19903:                // if not latched, through current count
                   19904:                if(!pit[ch].count_latched) {
                   19905:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19906:                                pit_latch_count(ch);
                   19907:                        }
                   19908:                }
                   19909:                // return latched count
                   19910:                if(pit[ch].low_read) {
                   19911:                        pit[ch].low_read = 0;
                   19912:                        if(!pit[ch].high_read) {
                   19913:                                pit[ch].count_latched = 0;
                   19914:                        }
                   19915:                        return(pit[ch].latch & 0xff);
                   19916:                } else if(pit[ch].high_read) {
                   19917:                        pit[ch].high_read = 0;
                   19918:                        pit[ch].count_latched = 0;
                   19919:                        return((pit[ch].latch >> 8) & 0xff);
                   19920:                }
                   19921:        }
                   19922:        return(0xff);
                   19923: }
                   19924: 
1.1.1.8   root     19925: int pit_run(int ch, UINT32 cur_time)
1.1       root     19926: {
1.1.1.8   root     19927:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19928:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19929:                pit[ch].prev_time = pit[ch].expired_time;
                   19930:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19931:                if(cur_time >= pit[ch].expired_time) {
                   19932:                        pit[ch].prev_time = cur_time;
                   19933:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19934:                }
1.1.1.8   root     19935:                return(1);
1.1       root     19936:        }
1.1.1.8   root     19937:        return(0);
1.1       root     19938: }
                   19939: 
                   19940: void pit_latch_count(int ch)
                   19941: {
1.1.1.8   root     19942:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19943:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19944:                pit_run(ch, cur_time);
                   19945:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19946:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19947:                
                   19948:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19949:                        // decrement counter in 1msec period
                   19950:                        if(pit[ch].next_latch == 0) {
                   19951:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   19952:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19953:                        }
                   19954:                        if(pit[ch].latch > pit[ch].next_latch) {
                   19955:                                pit[ch].latch--;
                   19956:                        }
                   19957:                } else {
                   19958:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   19959:                        pit[ch].next_latch = 0;
                   19960:                }
1.1.1.8   root     19961:        } else {
                   19962:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     19963:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     19964:        }
                   19965:        pit[ch].count_latched = 1;
                   19966:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   19967:                // lower byte
                   19968:                pit[ch].low_read = 1;
                   19969:                pit[ch].high_read = 0;
                   19970:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19971:                // upper byte
                   19972:                pit[ch].low_read = 0;
                   19973:                pit[ch].high_read = 1;
                   19974:        } else {
                   19975:                // lower -> upper
1.1.1.14  root     19976:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     19977:        }
                   19978: }
                   19979: 
1.1.1.8   root     19980: int pit_get_expired_time(int ch)
1.1       root     19981: {
1.1.1.22  root     19982:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   19983:        UINT64 val = pit[ch].accum >> 10;
                   19984:        pit[ch].accum -= val << 10;
                   19985:        return((val != 0) ? val : 1);
1.1.1.8   root     19986: }
                   19987: 
1.1.1.25  root     19988: // sio
                   19989: 
                   19990: void sio_init()
                   19991: {
1.1.1.26  root     19992:        memset(sio, 0, sizeof(sio));
                   19993:        memset(sio_mt, 0, sizeof(sio_mt));
                   19994:        
1.1.1.29  root     19995:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     19996:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19997:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   19998:                
                   19999:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   20000:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     20001:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   20002:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     20003:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   20004:                sio[c].irq_identify = 0x01;     // no pending irq
                   20005:                
                   20006:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   20007:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   20008:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   20009:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   20010:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   20011:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   20012:                
1.1.1.26  root     20013:                if(sio_port_number[c] != 0) {
1.1.1.25  root     20014:                        sio[c].channel = c;
                   20015:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   20016:                }
                   20017:        }
                   20018: }
                   20019: 
                   20020: void sio_finish()
                   20021: {
1.1.1.29  root     20022:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     20023:                if(sio_mt[c].hThread != NULL) {
                   20024:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   20025:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     20026:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     20027:                }
                   20028:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   20029:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   20030:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   20031:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   20032:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   20033:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     20034:        }
                   20035:        sio_release();
                   20036: }
                   20037: 
                   20038: void sio_release()
                   20039: {
1.1.1.29  root     20040:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     20041:                // sio_thread() may access the resources :-(
1.1.1.32  root     20042:                bool running = (sio_mt[c].hThread != NULL);
                   20043:                
                   20044:                if(running) {
                   20045:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   20046:                }
                   20047:                if(sio[c].send_buffer != NULL) {
                   20048:                        sio[c].send_buffer->release();
                   20049:                        delete sio[c].send_buffer;
                   20050:                        sio[c].send_buffer = NULL;
                   20051:                }
                   20052:                if(running) {
                   20053:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   20054:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   20055:                }
                   20056:                if(sio[c].recv_buffer != NULL) {
                   20057:                        sio[c].recv_buffer->release();
                   20058:                        delete sio[c].recv_buffer;
                   20059:                        sio[c].recv_buffer = NULL;
                   20060:                }
                   20061:                if(running) {
                   20062:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     20063:                }
1.1.1.25  root     20064:        }
                   20065: }
                   20066: 
                   20067: void sio_write(int c, UINT32 addr, UINT8 data)
                   20068: {
                   20069:        switch(addr & 7) {
                   20070:        case 0:
                   20071:                if(sio[c].selector & 0x80) {
                   20072:                        if(sio[c].divisor.b.l != data) {
                   20073:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20074:                                sio[c].divisor.b.l = data;
                   20075:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20076:                        }
                   20077:                } else {
                   20078:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20079:                        if(sio[c].send_buffer != NULL) {
                   20080:                                sio[c].send_buffer->write(data);
                   20081:                        }
1.1.1.25  root     20082:                        // transmitter holding/shift registers are not empty
                   20083:                        sio[c].line_stat_buf &= ~0x60;
                   20084:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   20085:                        
                   20086:                        if(sio[c].irq_enable & 0x02) {
                   20087:                                sio_update_irq(c);
                   20088:                        }
                   20089:                }
                   20090:                break;
                   20091:        case 1:
                   20092:                if(sio[c].selector & 0x80) {
                   20093:                        if(sio[c].divisor.b.h != data) {
                   20094:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20095:                                sio[c].divisor.b.h = data;
                   20096:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20097:                        }
                   20098:                } else {
                   20099:                        if(sio[c].irq_enable != data) {
                   20100:                                sio[c].irq_enable = data;
                   20101:                                sio_update_irq(c);
                   20102:                        }
                   20103:                }
                   20104:                break;
                   20105:        case 3:
                   20106:                {
                   20107:                        UINT8 line_ctrl = data & 0x3f;
                   20108:                        bool set_brk = ((data & 0x40) != 0);
                   20109:                        
                   20110:                        if(sio[c].line_ctrl != line_ctrl) {
                   20111:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20112:                                sio[c].line_ctrl = line_ctrl;
                   20113:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20114:                        }
                   20115:                        if(sio[c].set_brk != set_brk) {
                   20116:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   20117:                                sio[c].set_brk = set_brk;
                   20118:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   20119:                        }
                   20120:                }
                   20121:                sio[c].selector = data;
                   20122:                break;
                   20123:        case 4:
                   20124:                {
                   20125:                        bool set_dtr = ((data & 0x01) != 0);
                   20126:                        bool set_rts = ((data & 0x02) != 0);
                   20127:                        
                   20128:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     20129: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     20130:                                sio[c].set_dtr = set_dtr;
                   20131:                                sio[c].set_rts = set_rts;
1.1.1.26  root     20132: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   20133:                                
                   20134:                                bool state_changed = false;
                   20135:                                
                   20136:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20137:                                if(set_dtr) {
                   20138:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   20139:                                } else {
                   20140:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   20141:                                }
                   20142:                                if(set_rts) {
                   20143:                                        sio[c].modem_stat |= 0x10;      // cts on
                   20144:                                } else {
                   20145:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   20146:                                }
                   20147:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   20148:                                        if(!(sio[c].modem_stat & 0x02)) {
                   20149:                                                if(sio[c].irq_enable & 0x08) {
                   20150:                                                        state_changed = true;
                   20151:                                                }
                   20152:                                                sio[c].modem_stat |= 0x02;
                   20153:                                        }
                   20154:                                }
                   20155:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   20156:                                        if(!(sio[c].modem_stat & 0x01)) {
                   20157:                                                if(sio[c].irq_enable & 0x08) {
                   20158:                                                        state_changed = true;
                   20159:                                                }
                   20160:                                                sio[c].modem_stat |= 0x01;
                   20161:                                        }
                   20162:                                }
                   20163:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   20164:                                
                   20165:                                if(state_changed) {
                   20166:                                        sio_update_irq(c);
                   20167:                                }
1.1.1.25  root     20168:                        }
                   20169:                }
                   20170:                sio[c].modem_ctrl = data;
                   20171:                break;
                   20172:        case 7:
                   20173:                sio[c].scratch = data;
                   20174:                break;
                   20175:        }
                   20176: }
                   20177: 
                   20178: UINT8 sio_read(int c, UINT32 addr)
                   20179: {
                   20180:        switch(addr & 7) {
                   20181:        case 0:
                   20182:                if(sio[c].selector & 0x80) {
                   20183:                        return(sio[c].divisor.b.l);
                   20184:                } else {
                   20185:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20186:                        UINT8 data = 0;
                   20187:                        if(sio[c].recv_buffer != NULL) {
                   20188:                                data = sio[c].recv_buffer->read();
                   20189:                        }
1.1.1.25  root     20190:                        // data is not ready
                   20191:                        sio[c].line_stat_buf &= ~0x01;
                   20192:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20193:                        
                   20194:                        if(sio[c].irq_enable & 0x01) {
                   20195:                                sio_update_irq(c);
                   20196:                        }
                   20197:                        return(data);
                   20198:                }
                   20199:        case 1:
                   20200:                if(sio[c].selector & 0x80) {
                   20201:                        return(sio[c].divisor.b.h);
                   20202:                } else {
                   20203:                        return(sio[c].irq_enable);
                   20204:                }
                   20205:        case 2:
                   20206:                return(sio[c].irq_identify);
                   20207:        case 3:
                   20208:                return(sio[c].selector);
                   20209:        case 4:
                   20210:                return(sio[c].modem_ctrl);
                   20211:        case 5:
                   20212:                {
                   20213:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   20214:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   20215:                        sio[c].line_stat_err = 0x00;
                   20216:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   20217:                        
                   20218:                        bool state_changed = false;
                   20219:                        
                   20220:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   20221:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20222:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     20223:                                        // transmitter holding register will be empty first
                   20224:                                        if(sio[c].irq_enable & 0x02) {
                   20225:                                                state_changed = true;
                   20226:                                        }
                   20227:                                        sio[c].line_stat_buf |= 0x20;
                   20228:                                }
                   20229:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   20230:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   20231:                                // transmitter shift register will be empty later
                   20232:                                sio[c].line_stat_buf |= 0x40;
                   20233:                        }
                   20234:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   20235:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20236:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     20237:                                        // data is ready
                   20238:                                        if(sio[c].irq_enable & 0x01) {
                   20239:                                                state_changed = true;
                   20240:                                        }
                   20241:                                        sio[c].line_stat_buf |= 0x01;
                   20242:                                }
                   20243:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20244:                        }
                   20245:                        if(state_changed) {
                   20246:                                sio_update_irq(c);
                   20247:                        }
                   20248:                        return(val);
                   20249:                }
                   20250:        case 6:
                   20251:                {
                   20252:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   20253:                        UINT8 val = sio[c].modem_stat;
                   20254:                        sio[c].modem_stat &= 0xf0;
                   20255:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   20256:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   20257:                        
                   20258:                        if(sio[c].modem_ctrl & 0x10) {
                   20259:                                // loop-back
                   20260:                                val &= 0x0f;
                   20261:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   20262:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   20263:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   20264:                        }
                   20265:                        return(val);
                   20266:                }
                   20267:        case 7:
                   20268:                return(sio[c].scratch);
                   20269:        }
                   20270:        return(0xff);
                   20271: }
                   20272: 
                   20273: void sio_update(int c)
                   20274: {
                   20275:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   20276:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20277:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     20278:                        // transmitter holding/shift registers will be empty
                   20279:                        sio[c].line_stat_buf |= 0x60;
                   20280:                }
                   20281:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   20282:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   20283:                // transmitter shift register will be empty
                   20284:                sio[c].line_stat_buf |= 0x40;
                   20285:        }
                   20286:        if(!(sio[c].line_stat_buf & 0x01)) {
                   20287:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20288:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     20289:                        // data is ready
                   20290:                        sio[c].line_stat_buf |= 0x01;
                   20291:                }
                   20292:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20293:        }
                   20294:        sio_update_irq(c);
                   20295: }
                   20296: 
                   20297: void sio_update_irq(int c)
                   20298: {
                   20299:        int level = -1;
                   20300:        
                   20301:        if(sio[c].irq_enable & 0x08) {
                   20302:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20303:                if((sio[c].modem_stat & 0x0f) != 0) {
                   20304:                        level = 0;
                   20305:                }
                   20306:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20307:        }
                   20308:        if(sio[c].irq_enable & 0x02) {
                   20309:                if(sio[c].line_stat_buf & 0x20) {
                   20310:                        level = 1;
                   20311:                }
                   20312:        }
                   20313:        if(sio[c].irq_enable & 0x01) {
                   20314:                if(sio[c].line_stat_buf & 0x01) {
                   20315:                        level = 2;
                   20316:                }
                   20317:        }
                   20318:        if(sio[c].irq_enable & 0x04) {
                   20319:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   20320:                if(sio[c].line_stat_err != 0) {
                   20321:                        level = 3;
                   20322:                }
                   20323:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   20324:        }
1.1.1.29  root     20325:        
                   20326:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     20327:        if(level != -1) {
                   20328:                sio[c].irq_identify = level << 1;
1.1.1.29  root     20329:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     20330:        } else {
                   20331:                sio[c].irq_identify = 1;
1.1.1.29  root     20332:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     20333:        }
                   20334: }
                   20335: 
                   20336: DWORD WINAPI sio_thread(void *lpx)
                   20337: {
                   20338:        volatile sio_t *p = (sio_t *)lpx;
                   20339:        sio_mt_t *q = &sio_mt[p->channel];
                   20340:        
                   20341:        char name[] = "COM1";
1.1.1.26  root     20342:        name[3] = '0' + sio_port_number[p->channel];
                   20343:        HANDLE hComm = NULL;
                   20344:        COMMPROP commProp;
                   20345:        DCB dcb;
                   20346:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   20347:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   20348:        
1.1.1.60  root     20349:        if((hComm = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
1.1.1.26  root     20350:                if(GetCommProperties(hComm, &commProp)) {
                   20351:                        dwSettableBaud = commProp.dwSettableBaud;
                   20352:                }
1.1.1.25  root     20353:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     20354: //             EscapeCommFunction(hComm, SETRTS);
                   20355: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     20356:                
1.1.1.54  root     20357:                while(!m_exit) {
1.1.1.25  root     20358:                        // setup comm port
                   20359:                        bool comm_state_changed = false;
                   20360:                        
                   20361:                        EnterCriticalSection(&q->csLineCtrl);
                   20362:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   20363:                                p->prev_divisor = p->divisor.w;
                   20364:                                p->prev_line_ctrl = p->line_ctrl;
                   20365:                                comm_state_changed = true;
                   20366:                        }
                   20367:                        LeaveCriticalSection(&q->csLineCtrl);
                   20368:                        
                   20369:                        if(comm_state_changed) {
1.1.1.26  root     20370:                                if(GetCommState(hComm, &dcb)) {
                   20371: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   20372:                                        DWORD baud = 115200 / p->prev_divisor;
                   20373:                                        dcb.BaudRate = 9600; // default
                   20374:                                        
                   20375:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   20376:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   20377:                                        // 134.5bps is not supported ???
                   20378: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   20379:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   20380:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   20381:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   20382:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   20383:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   20384:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   20385:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   20386:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   20387:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   20388: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   20389: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   20390: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   20391:                                        
                   20392:                                        switch(p->prev_line_ctrl & 0x03) {
                   20393:                                        case 0x00: dcb.ByteSize = 5; break;
                   20394:                                        case 0x01: dcb.ByteSize = 6; break;
                   20395:                                        case 0x02: dcb.ByteSize = 7; break;
                   20396:                                        case 0x03: dcb.ByteSize = 8; break;
                   20397:                                        }
                   20398:                                        switch(p->prev_line_ctrl & 0x04) {
                   20399:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   20400:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   20401:                                        }
                   20402:                                        switch(p->prev_line_ctrl & 0x38) {
                   20403:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   20404:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   20405:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   20406:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   20407:                                        default:   dcb.Parity = NOPARITY;    break;
                   20408:                                        }
                   20409:                                        dcb.fBinary = TRUE;
                   20410:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   20411:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   20412:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   20413:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   20414:                                        dcb.fTXContinueOnXoff = TRUE;
                   20415:                                        dcb.fOutX = dcb.fInX = FALSE;
                   20416:                                        dcb.fErrorChar = FALSE;
                   20417:                                        dcb.fNull = FALSE;
                   20418:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   20419:                                        dcb.fAbortOnError = FALSE;
                   20420:                                        
                   20421:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     20422:                                }
                   20423:                                
                   20424:                                // check again to apply all comm state changes
                   20425:                                Sleep(10);
                   20426:                                continue;
                   20427:                        }
                   20428:                        
                   20429:                        // set comm pins
                   20430:                        bool change_brk = false;
1.1.1.26  root     20431: //                     bool change_rts = false;
                   20432: //                     bool change_dtr = false;
1.1.1.25  root     20433:                        
                   20434:                        EnterCriticalSection(&q->csModemCtrl);
                   20435:                        if(p->prev_set_brk != p->set_brk) {
                   20436:                                p->prev_set_brk = p->set_brk;
                   20437:                                change_brk = true;
                   20438:                        }
1.1.1.26  root     20439: //                     if(p->prev_set_rts != p->set_rts) {
                   20440: //                             p->prev_set_rts = p->set_rts;
                   20441: //                             change_rts = true;
                   20442: //                     }
                   20443: //                     if(p->prev_set_dtr != p->set_dtr) {
                   20444: //                             p->prev_set_dtr = p->set_dtr;
                   20445: //                             change_dtr = true;
                   20446: //                     }
1.1.1.25  root     20447:                        LeaveCriticalSection(&q->csModemCtrl);
                   20448:                        
                   20449:                        if(change_brk) {
1.1.1.26  root     20450:                                static UINT32 clear_time = 0;
                   20451:                                if(p->prev_set_brk) {
                   20452:                                        EscapeCommFunction(hComm, SETBREAK);
                   20453:                                        clear_time = timeGetTime() + 200;
                   20454:                                } else {
                   20455:                                        // keep break for at least 200msec
                   20456:                                        UINT32 cur_time = timeGetTime();
                   20457:                                        if(clear_time > cur_time) {
                   20458:                                                Sleep(clear_time - cur_time);
                   20459:                                        }
                   20460:                                        EscapeCommFunction(hComm, CLRBREAK);
                   20461:                                }
1.1.1.25  root     20462:                        }
1.1.1.26  root     20463: //                     if(change_rts) {
                   20464: //                             if(p->prev_set_rts) {
                   20465: //                                     EscapeCommFunction(hComm, SETRTS);
                   20466: //                             } else {
                   20467: //                                     EscapeCommFunction(hComm, CLRRTS);
                   20468: //                             }
                   20469: //                     }
                   20470: //                     if(change_dtr) {
                   20471: //                             if(p->prev_set_dtr) {
                   20472: //                                     EscapeCommFunction(hComm, SETDTR);
                   20473: //                             } else {
                   20474: //                                     EscapeCommFunction(hComm, CLRDTR);
                   20475: //                             }
                   20476: //                     }
1.1.1.25  root     20477:                        
                   20478:                        // get comm pins
                   20479:                        DWORD dwModemStat = 0;
                   20480:                        
                   20481:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   20482:                                EnterCriticalSection(&q->csModemStat);
                   20483:                                if(dwModemStat & MS_RLSD_ON) {
                   20484:                                        p->modem_stat |= 0x80;
                   20485:                                } else {
                   20486:                                        p->modem_stat &= ~0x80;
                   20487:                                }
                   20488:                                if(dwModemStat & MS_RING_ON) {
                   20489:                                        p->modem_stat |= 0x40;
                   20490:                                } else {
                   20491:                                        p->modem_stat &= ~0x40;
                   20492:                                }
1.1.1.26  root     20493: //                             if(dwModemStat & MS_DSR_ON) {
                   20494: //                                     p->modem_stat |= 0x20;
                   20495: //                             } else {
                   20496: //                                     p->modem_stat &= ~0x20;
                   20497: //                             }
                   20498: //                             if(dwModemStat & MS_CTS_ON) {
                   20499: //                                     p->modem_stat |= 0x10;
                   20500: //                             } else {
                   20501: //                                     p->modem_stat &= ~0x10;
                   20502: //                             }
1.1.1.25  root     20503:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   20504:                                        p->modem_stat |= 0x08;
                   20505:                                }
                   20506:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   20507:                                        p->modem_stat |= 0x04;
                   20508:                                }
1.1.1.26  root     20509: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   20510: //                                     p->modem_stat |= 0x02;
                   20511: //                             }
                   20512: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   20513: //                                     p->modem_stat |= 0x01;
                   20514: //                             }
1.1.1.25  root     20515:                                LeaveCriticalSection(&q->csModemStat);
                   20516:                        }
                   20517:                        
                   20518:                        // send data
                   20519:                        DWORD dwSend = 0;
                   20520:                        
                   20521:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     20522:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     20523:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   20524:                        }
                   20525:                        LeaveCriticalSection(&q->csSendData);
                   20526:                        
                   20527:                        if(dwSend != 0) {
                   20528:                                DWORD dwWritten = 0;
                   20529:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   20530:                        }
                   20531:                        
                   20532:                        // get line status and recv data
                   20533:                        DWORD dwLineStat = 0;
                   20534:                        COMSTAT comStat;
                   20535:                        
                   20536:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   20537:                                EnterCriticalSection(&q->csLineStat);
                   20538:                                if(dwLineStat & CE_BREAK) {
                   20539:                                        p->line_stat_err |= 0x10;
                   20540:                                }
                   20541:                                if(dwLineStat & CE_FRAME) {
                   20542:                                        p->line_stat_err |= 0x08;
                   20543:                                }
                   20544:                                if(dwLineStat & CE_RXPARITY) {
                   20545:                                        p->line_stat_err |= 0x04;
                   20546:                                }
                   20547:                                if(dwLineStat & CE_OVERRUN) {
                   20548:                                        p->line_stat_err |= 0x02;
                   20549:                                }
                   20550:                                LeaveCriticalSection(&q->csLineStat);
                   20551:                                
                   20552:                                if(comStat.cbInQue != 0) {
                   20553:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20554:                                        DWORD dwRecv = 0;
                   20555:                                        if(p->recv_buffer != NULL) {
                   20556:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   20557:                                        }
1.1.1.25  root     20558:                                        LeaveCriticalSection(&q->csRecvData);
                   20559:                                        
                   20560:                                        if(dwRecv != 0) {
                   20561:                                                DWORD dwRead = 0;
                   20562:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   20563:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20564:                                                        if(p->recv_buffer != NULL) {
                   20565:                                                                for(int i = 0; i < dwRead; i++) {
                   20566:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   20567:                                                                }
1.1.1.25  root     20568:                                                        }
                   20569:                                                        LeaveCriticalSection(&q->csRecvData);
                   20570:                                                }
                   20571:                                        }
                   20572:                                }
                   20573:                        }
                   20574:                        Sleep(10);
                   20575:                }
                   20576:                CloseHandle(hComm);
                   20577:        }
                   20578:        return 0;
                   20579: }
                   20580: 
1.1.1.8   root     20581: // cmos
                   20582: 
                   20583: void cmos_init()
                   20584: {
                   20585:        memset(cmos, 0, sizeof(cmos));
                   20586:        cmos_addr = 0;
1.1       root     20587:        
1.1.1.8   root     20588:        // from DOSBox
                   20589:        cmos_write(0x0a, 0x26);
                   20590:        cmos_write(0x0b, 0x02);
                   20591:        cmos_write(0x0d, 0x80);
1.1       root     20592: }
                   20593: 
1.1.1.8   root     20594: void cmos_write(int addr, UINT8 val)
1.1       root     20595: {
1.1.1.8   root     20596:        cmos[addr & 0x7f] = val;
                   20597: }
                   20598: 
                   20599: #define CMOS_GET_TIME() { \
                   20600:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   20601:        if(prev_sec != cur_sec) { \
                   20602:                GetLocalTime(&time); \
                   20603:                prev_sec = cur_sec; \
                   20604:        } \
1.1       root     20605: }
1.1.1.8   root     20606: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     20607: 
1.1.1.8   root     20608: UINT8 cmos_read(int addr)
1.1       root     20609: {
1.1.1.8   root     20610:        static SYSTEMTIME time;
                   20611:        static UINT32 prev_sec = 0;
1.1       root     20612:        
1.1.1.8   root     20613:        switch(addr & 0x7f) {
                   20614:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   20615:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   20616:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   20617:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   20618:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   20619:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   20620:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   20621: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   20622:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   20623:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   20624:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   20625:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20626:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20627:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20628:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20629:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     20630:        }
1.1.1.8   root     20631:        return(cmos[addr & 0x7f]);
1.1       root     20632: }
                   20633: 
1.1.1.7   root     20634: // kbd (a20)
                   20635: 
                   20636: void kbd_init()
                   20637: {
1.1.1.8   root     20638:        kbd_data = kbd_command = 0;
1.1.1.7   root     20639:        kbd_status = 0x18;
                   20640: }
                   20641: 
                   20642: UINT8 kbd_read_data()
                   20643: {
1.1.1.57  root     20644:        UINT8 data = kbd_data;
                   20645:        kbd_data = 0;
1.1.1.8   root     20646:        kbd_status &= ~1;
1.1.1.57  root     20647:        return(data);
1.1.1.7   root     20648: }
                   20649: 
                   20650: void kbd_write_data(UINT8 val)
                   20651: {
                   20652:        switch(kbd_command) {
                   20653:        case 0xd1:
                   20654:                i386_set_a20_line((val >> 1) & 1);
                   20655:                break;
                   20656:        }
                   20657:        kbd_command = 0;
1.1.1.8   root     20658:        kbd_status &= ~8;
1.1.1.7   root     20659: }
                   20660: 
                   20661: UINT8 kbd_read_status()
                   20662: {
                   20663:        return(kbd_status);
                   20664: }
                   20665: 
                   20666: void kbd_write_command(UINT8 val)
                   20667: {
                   20668:        switch(val) {
                   20669:        case 0xd0:
                   20670:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     20671:                kbd_status |= 1;
1.1.1.7   root     20672:                break;
                   20673:        case 0xdd:
                   20674:                i386_set_a20_line(0);
                   20675:                break;
                   20676:        case 0xdf:
                   20677:                i386_set_a20_line(1);
                   20678:                break;
1.1.1.26  root     20679:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   20680:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     20681:                if(!(val & 1)) {
1.1.1.8   root     20682:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     20683:                                // reset pic
                   20684:                                pic_init();
                   20685:                                pic[0].irr = pic[1].irr = 0x00;
                   20686:                                pic[0].imr = pic[1].imr = 0xff;
                   20687:                        }
                   20688:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     20689:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   20690:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   20691:                        i386_jmp_far(selector, address);
1.1.1.7   root     20692:                }
                   20693:                i386_set_a20_line((val >> 1) & 1);
                   20694:                break;
                   20695:        }
                   20696:        kbd_command = val;
1.1.1.8   root     20697:        kbd_status |= 8;
1.1.1.7   root     20698: }
                   20699: 
1.1.1.9   root     20700: // vga
                   20701: 
                   20702: UINT8 vga_read_status()
                   20703: {
                   20704:        // 60hz
                   20705:        static const int period[3] = {16, 17, 17};
                   20706:        static int index = 0;
                   20707:        UINT32 time = timeGetTime() % period[index];
                   20708:        
                   20709:        index = (index + 1) % 3;
1.1.1.14  root     20710:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     20711: }
                   20712: 
1.1       root     20713: // i/o bus
                   20714: 
1.1.1.29  root     20715: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   20716: //#define SW1US_PATCH
                   20717: 
1.1.1.25  root     20718: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     20719: #ifdef USE_DEBUGGER
1.1.1.25  root     20720: {
1.1.1.33  root     20721:        if(now_debugging) {
                   20722:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20723:                        if(in_break_point.table[i].status == 1) {
                   20724:                                if(addr == in_break_point.table[i].addr) {
                   20725:                                        in_break_point.hit = i + 1;
                   20726:                                        now_suspended = true;
                   20727:                                        break;
                   20728:                                }
                   20729:                        }
                   20730:                }
1.1.1.25  root     20731:        }
1.1.1.33  root     20732:        return(debugger_read_io_byte(addr));
1.1.1.25  root     20733: }
1.1.1.33  root     20734: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     20735: #endif
1.1       root     20736: {
1.1.1.33  root     20737:        UINT8 val = 0xff;
                   20738:        
1.1       root     20739:        switch(addr) {
1.1.1.29  root     20740: #ifdef SW1US_PATCH
                   20741:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20742:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     20743:                val = sio_read(0, addr - 1);
                   20744:                break;
1.1.1.29  root     20745: #else
1.1.1.25  root     20746:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20747:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     20748:                val = dma_read(0, addr);
                   20749:                break;
1.1.1.29  root     20750: #endif
1.1.1.25  root     20751:        case 0x20: case 0x21:
1.1.1.33  root     20752:                val = pic_read(0, addr);
                   20753:                break;
1.1.1.25  root     20754:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     20755:                val = pit_read(addr & 0x03);
                   20756:                break;
1.1.1.7   root     20757:        case 0x60:
1.1.1.33  root     20758:                val = kbd_read_data();
                   20759:                break;
1.1.1.9   root     20760:        case 0x61:
1.1.1.33  root     20761:                val = system_port;
                   20762:                break;
1.1.1.7   root     20763:        case 0x64:
1.1.1.33  root     20764:                val = kbd_read_status();
                   20765:                break;
1.1       root     20766:        case 0x71:
1.1.1.33  root     20767:                val = cmos_read(cmos_addr);
                   20768:                break;
1.1.1.25  root     20769:        case 0x81:
1.1.1.33  root     20770:                val = dma_page_read(0, 2);
                   20771:                break;
1.1.1.25  root     20772:        case 0x82:
1.1.1.33  root     20773:                val = dma_page_read(0, 3);
                   20774:                break;
1.1.1.25  root     20775:        case 0x83:
1.1.1.33  root     20776:                val = dma_page_read(0, 1);
                   20777:                break;
1.1.1.25  root     20778:        case 0x87:
1.1.1.33  root     20779:                val = dma_page_read(0, 0);
                   20780:                break;
1.1.1.25  root     20781:        case 0x89:
1.1.1.33  root     20782:                val = dma_page_read(1, 2);
                   20783:                break;
1.1.1.25  root     20784:        case 0x8a:
1.1.1.33  root     20785:                val = dma_page_read(1, 3);
                   20786:                break;
1.1.1.25  root     20787:        case 0x8b:
1.1.1.33  root     20788:                val = dma_page_read(1, 1);
                   20789:                break;
1.1.1.25  root     20790:        case 0x8f:
1.1.1.33  root     20791:                val = dma_page_read(1, 0);
                   20792:                break;
1.1       root     20793:        case 0x92:
1.1.1.33  root     20794:                val = (m_a20_mask >> 19) & 2;
                   20795:                break;
1.1.1.25  root     20796:        case 0xa0: case 0xa1:
1.1.1.33  root     20797:                val = pic_read(1, addr);
                   20798:                break;
1.1.1.25  root     20799:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20800:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20801:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20802:                break;
1.1.1.37  root     20803:        case 0x278: case 0x279: case 0x27a:
                   20804:                val = pio_read(1, addr);
                   20805:                break;
1.1.1.29  root     20806:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20807:                val = sio_read(3, addr);
                   20808:                break;
1.1.1.25  root     20809:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20810:                val = sio_read(1, addr);
                   20811:                break;
1.1.1.25  root     20812:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20813:                val = pio_read(0, addr);
                   20814:                break;
1.1.1.25  root     20815:        case 0x3ba: case 0x3da:
1.1.1.33  root     20816:                val = vga_read_status();
                   20817:                break;
1.1.1.37  root     20818:        case 0x3bc: case 0x3bd: case 0x3be:
                   20819:                val = pio_read(2, addr);
                   20820:                break;
1.1.1.58  root     20821:        case 0x3d5:
                   20822:                if(crtc_addr < 16) {
                   20823:                        val = crtc_regs[crtc_addr];
                   20824:                }
                   20825:                break;
1.1.1.29  root     20826:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20827:                val = sio_read(2, addr);
                   20828:                break;
1.1.1.25  root     20829:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20830:                val = sio_read(0, addr);
                   20831:                break;
1.1       root     20832:        default:
1.1.1.33  root     20833: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20834:                break;
                   20835:        }
1.1.1.33  root     20836: #ifdef ENABLE_DEBUG_IOPORT
                   20837:        if(fp_debug_log != NULL) {
                   20838:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20839:        }
                   20840: #endif
                   20841:        return(val);
1.1       root     20842: }
                   20843: 
                   20844: UINT16 read_io_word(offs_t addr)
                   20845: {
                   20846:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20847: }
                   20848: 
1.1.1.33  root     20849: #ifdef USE_DEBUGGER
                   20850: UINT16 debugger_read_io_word(offs_t addr)
                   20851: {
                   20852:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20853: }
                   20854: #endif
                   20855: 
1.1       root     20856: UINT32 read_io_dword(offs_t addr)
                   20857: {
                   20858:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20859: }
                   20860: 
1.1.1.33  root     20861: #ifdef USE_DEBUGGER
                   20862: UINT32 debugger_read_io_dword(offs_t addr)
                   20863: {
                   20864:        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));
                   20865: }
                   20866: #endif
                   20867: 
1.1       root     20868: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20869: #ifdef USE_DEBUGGER
                   20870: {
                   20871:        if(now_debugging) {
                   20872:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20873:                        if(out_break_point.table[i].status == 1) {
                   20874:                                if(addr == out_break_point.table[i].addr) {
                   20875:                                        out_break_point.hit = i + 1;
                   20876:                                        now_suspended = true;
                   20877:                                        break;
                   20878:                                }
                   20879:                        }
                   20880:                }
                   20881:        }
                   20882:        debugger_write_io_byte(addr, val);
                   20883: }
                   20884: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20885: #endif
1.1       root     20886: {
1.1.1.25  root     20887: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20888:        if(fp_debug_log != NULL) {
1.1.1.43  root     20889: #ifdef USE_SERVICE_THREAD
                   20890:                if(addr != 0xf7)
                   20891: #endif
1.1.1.33  root     20892:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20893:        }
                   20894: #endif
1.1       root     20895:        switch(addr) {
1.1.1.29  root     20896: #ifdef SW1US_PATCH
                   20897:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20898:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20899:                sio_write(0, addr - 1, val);
                   20900:                break;
                   20901: #else
1.1.1.25  root     20902:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20903:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20904:                dma_write(0, addr, val);
                   20905:                break;
1.1.1.29  root     20906: #endif
1.1.1.25  root     20907:        case 0x20: case 0x21:
1.1       root     20908:                pic_write(0, addr, val);
                   20909:                break;
1.1.1.25  root     20910:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20911:                pit_write(addr & 0x03, val);
                   20912:                break;
1.1.1.7   root     20913:        case 0x60:
                   20914:                kbd_write_data(val);
                   20915:                break;
1.1.1.9   root     20916:        case 0x61:
                   20917:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20918:                        // beep on
                   20919: //                     MessageBeep(-1);
                   20920:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20921:                        // beep off
                   20922:                }
                   20923:                system_port = val;
                   20924:                break;
1.1       root     20925:        case 0x64:
1.1.1.7   root     20926:                kbd_write_command(val);
1.1       root     20927:                break;
                   20928:        case 0x70:
                   20929:                cmos_addr = val;
                   20930:                break;
                   20931:        case 0x71:
1.1.1.8   root     20932:                cmos_write(cmos_addr, val);
1.1       root     20933:                break;
1.1.1.25  root     20934:        case 0x81:
                   20935:                dma_page_write(0, 2, val);
                   20936:        case 0x82:
                   20937:                dma_page_write(0, 3, val);
                   20938:        case 0x83:
                   20939:                dma_page_write(0, 1, val);
                   20940:        case 0x87:
                   20941:                dma_page_write(0, 0, val);
                   20942:        case 0x89:
                   20943:                dma_page_write(1, 2, val);
                   20944:        case 0x8a:
                   20945:                dma_page_write(1, 3, val);
                   20946:        case 0x8b:
                   20947:                dma_page_write(1, 1, val);
                   20948:        case 0x8f:
                   20949:                dma_page_write(1, 0, val);
1.1       root     20950:        case 0x92:
1.1.1.7   root     20951:                i386_set_a20_line((val >> 1) & 1);
1.1       root     20952:                break;
1.1.1.25  root     20953:        case 0xa0: case 0xa1:
1.1       root     20954:                pic_write(1, addr, val);
                   20955:                break;
1.1.1.25  root     20956:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20957:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     20958:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     20959:                break;
1.1.1.35  root     20960: #ifdef USE_SERVICE_THREAD
                   20961:        case 0xf7:
                   20962:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     20963:                if(in_service && cursor_moved) {
                   20964:                        // update cursor position before service is done
                   20965:                        pcbios_update_cursor_position();
                   20966:                        cursor_moved = false;
                   20967:                }
1.1.1.35  root     20968:                finish_service_loop();
                   20969:                break;
                   20970: #endif
1.1.1.37  root     20971:        case 0x278: case 0x279: case 0x27a:
                   20972:                pio_write(1, addr, val);
                   20973:                break;
1.1.1.29  root     20974:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   20975:                sio_write(3, addr, val);
                   20976:                break;
1.1.1.25  root     20977:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   20978:                sio_write(1, addr, val);
                   20979:                break;
                   20980:        case 0x378: case 0x379: case 0x37a:
                   20981:                pio_write(0, addr, val);
                   20982:                break;
1.1.1.37  root     20983:        case 0x3bc: case 0x3bd: case 0x3be:
                   20984:                pio_write(2, addr, val);
                   20985:                break;
1.1.1.58  root     20986:        case 0x3d4:
                   20987:                crtc_addr = val;
                   20988:                break;
                   20989:        case 0x3d5:
                   20990:                if(crtc_addr < 16) {
                   20991:                        if(crtc_regs[crtc_addr] != val) {
                   20992:                                crtc_regs[crtc_addr] = val;
                   20993:                                crtc_changed[crtc_addr] = 1;
                   20994:                        }
                   20995:                }
                   20996:                break;
1.1.1.29  root     20997:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   20998:                sio_write(2, addr, val);
                   20999:                break;
1.1.1.25  root     21000:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   21001:                sio_write(0, addr, val);
                   21002:                break;
1.1       root     21003:        default:
1.1.1.33  root     21004: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     21005:                break;
                   21006:        }
                   21007: }
                   21008: 
                   21009: void write_io_word(offs_t addr, UINT16 val)
                   21010: {
                   21011:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   21012:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   21013: }
                   21014: 
1.1.1.33  root     21015: #ifdef USE_DEBUGGER
                   21016: void debugger_write_io_word(offs_t addr, UINT16 val)
                   21017: {
                   21018:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   21019:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   21020: }
                   21021: #endif
                   21022: 
1.1       root     21023: void write_io_dword(offs_t addr, UINT32 val)
                   21024: {
                   21025:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   21026:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   21027:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   21028:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   21029: }
1.1.1.33  root     21030: 
                   21031: #ifdef USE_DEBUGGER
                   21032: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   21033: {
                   21034:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   21035:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   21036:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   21037:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   21038: }
                   21039: #endif

unix.superglobalmegacorp.com

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