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

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"
1.1.1.66! root      774:        #include "mame/lib/softfloat/fsincos.c"
1.1.1.10  root      775:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      776:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      777: #elif defined(HAS_I286)
1.1.1.10  root      778:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      779: #else
1.1.1.10  root      780:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      781: #endif
1.1.1.33  root      782: #ifdef USE_DEBUGGER
1.1.1.10  root      783:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      784: #endif
                    785: 
1.1.1.3   root      786: #if defined(HAS_I386)
                    787:        #define SREG(x)                         m_sreg[x].selector
                    788:        #define SREG_BASE(x)                    m_sreg[x].base
                    789:        int cpu_type, cpu_step;
1.1.1.64  root      790:        #define i386_get_flags()                get_flags()
                    791:        #define i386_set_flags(x)               set_flags(x)
1.1.1.3   root      792: #else
                    793:        #define REG8(x)                         m_regs.b[x]
                    794:        #define REG16(x)                        m_regs.w[x]
                    795:        #define SREG(x)                         m_sregs[x]
                    796:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      797:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      798:        #define m_CF                            m_CarryVal
                    799:        #define m_a20_mask                      AMASK
                    800:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
1.1.1.64  root      801:        void i386_sreg_load(UINT16 selector, UINT8 reg, bool *fault)
                    802:        {
                    803: #if defined(HAS_I286)
                    804:                i80286_data_descriptor(reg, selector);
                    805: #else
                    806:                m_sregs[reg] = selector;
                    807:                m_base[reg] = SegBase(reg);
                    808: #endif
                    809:        }
                    810:        #define i386_get_flags()                CompressFlags()
                    811:        #define i386_set_flags(x)               ExpandFlags(x)
1.1.1.3   root      812:        #if defined(HAS_I286)
                    813:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    814:        #else
                    815:                #define i386_set_a20_line(x)
                    816:        #endif
                    817:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    818: #endif
1.1       root      819: 
                    820: void i386_jmp_far(UINT16 selector, UINT32 address)
                    821: {
1.1.1.3   root      822: #if defined(HAS_I386)
1.1       root      823:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      824:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      825:        } else {
1.1.1.3   root      826:                SREG(CS) = selector;
                    827:                m_performed_intersegment_jump = 1;
                    828:                i386_load_segment_descriptor(CS);
                    829:                m_eip = address;
                    830:                CHANGE_PC(m_eip);
1.1       root      831:        }
1.1.1.3   root      832: #elif defined(HAS_I286)
                    833:        i80286_code_descriptor(selector, address, 1);
                    834: #else
                    835:        SREG(CS) = selector;
                    836:        i386_load_segment_descriptor(CS);
                    837:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    838: #endif
1.1       root      839: }
                    840: 
1.1.1.24  root      841: void i386_call_far(UINT16 selector, UINT32 address)
                    842: {
                    843: #if defined(HAS_I386)
                    844:        if(PROTECTED_MODE && !V8086_MODE) {
                    845:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    846:        } else {
                    847:                PUSH16(SREG(CS));
                    848:                PUSH16(m_eip);
                    849:                SREG(CS) = selector;
                    850:                m_performed_intersegment_jump = 1;
                    851:                i386_load_segment_descriptor(CS);
                    852:                m_eip = address;
                    853:                CHANGE_PC(m_eip);
                    854:        }
                    855: #else
                    856:        UINT16 ip = m_pc - SREG_BASE(CS);
                    857:        UINT16 cs = SREG(CS);
                    858: #if defined(HAS_I286)
                    859:        i80286_code_descriptor(selector, address, 2);
                    860: #else
                    861:        SREG(CS) = selector;
                    862:        i386_load_segment_descriptor(CS);
                    863:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    864: #endif
                    865:        PUSH(cs);
                    866:        PUSH(ip);
                    867:        CHANGE_PC(m_pc);
                    868: #endif
                    869: }
1.1.1.49  root      870: 
                    871: void i386_push16(UINT16 value)
                    872: {
                    873: #if defined(HAS_I386)
                    874:        PUSH16(value);
                    875: #else
                    876:        PUSH(value);
1.1.1.35  root      877: #endif
1.1.1.49  root      878: }
                    879: 
                    880: UINT16 i386_pop16()
                    881: {
                    882: #if defined(HAS_I386)
                    883:        return POP16();
                    884: #else
                    885:        UINT16 value;
                    886:        POP(value);
                    887:        return value;
                    888: #endif
                    889: }
1.1.1.24  root      890: 
1.1.1.29  root      891: UINT16 i386_read_stack()
                    892: {
                    893: #if defined(HAS_I386)
                    894:        UINT32 ea, new_esp;
                    895:        if( STACK_32BIT ) {
                    896:                new_esp = REG32(ESP) + 2;
                    897:                ea = i386_translate(SS, new_esp - 2, 0);
                    898:        } else {
                    899:                new_esp = REG16(SP) + 2;
                    900:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    901:        }
                    902:        return READ16(ea);
                    903: #else
                    904:        UINT16 sp = m_regs.w[SP] + 2;
                    905:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    906: #endif
                    907: }
                    908: 
1.1.1.53  root      909: void i386_write_stack(UINT16 value)
                    910: {
                    911: #if defined(HAS_I386)
                    912:        UINT32 ea, new_esp;
                    913:        if( STACK_32BIT ) {
                    914:                new_esp = REG32(ESP) + 2;
                    915:                ea = i386_translate(SS, new_esp - 2, 0);
                    916:        } else {
                    917:                new_esp = REG16(SP) + 2;
                    918:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    919:        }
                    920:        WRITE16(ea, value);
                    921: #else
                    922:        UINT16 sp = m_regs.w[SP] + 2;
                    923:        WriteWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK), value);
                    924: #endif
                    925: }
                    926: 
1.1       root      927: /* ----------------------------------------------------------------------------
1.1.1.33  root      928:        debugger
                    929: ---------------------------------------------------------------------------- */
                    930: 
                    931: #ifdef USE_DEBUGGER
                    932: #define TELNET_BLUE      0x0004 // text color contains blue.
                    933: #define TELNET_GREEN     0x0002 // text color contains green.
                    934: #define TELNET_RED       0x0001 // text color contains red.
                    935: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    936: 
                    937: int svr_socket = 0;
                    938: int cli_socket = 0;
                    939: 
1.1.1.55  root      940: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
1.1.1.33  root      941: 
                    942: void debugger_init()
                    943: {
                    944:        now_debugging = false;
                    945:        now_going = false;
                    946:        now_suspended = false;
                    947:        force_suspend = false;
                    948:        
                    949:        memset(&break_point, 0, sizeof(break_point_t));
                    950:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    951:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    952:        memset(&in_break_point, 0, sizeof(break_point_t));
                    953:        memset(&out_break_point, 0, sizeof(break_point_t));
                    954:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    955: }
                    956: 
1.1.1.45  root      957: void telnet_send(const char *string)
1.1.1.33  root      958: {
                    959:        char buffer[8192], *ptr;
                    960:        strcpy(buffer, string);
                    961:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    962:                char tmp[8192];
                    963:                *ptr = '\0';
                    964:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    965:                strcpy(buffer, tmp);
                    966:        }
                    967:        
                    968:        int len = strlen(buffer), res;
                    969:        ptr = buffer;
                    970:        while(len > 0) {
                    971:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    972:                        len -= res;
                    973:                        ptr += res;
                    974:                }
                    975:        }
                    976: }
                    977: 
                    978: void telnet_command(const char *format, ...)
                    979: {
                    980:        char buffer[1024];
                    981:        va_list ap;
                    982:        va_start(ap, format);
                    983:        vsprintf(buffer, format, ap);
                    984:        va_end(ap);
                    985:        
                    986:        telnet_send(buffer);
                    987: }
                    988: 
                    989: void telnet_printf(const char *format, ...)
                    990: {
                    991:        char buffer[1024];
                    992:        va_list ap;
                    993:        va_start(ap, format);
                    994:        vsprintf(buffer, format, ap);
                    995:        va_end(ap);
                    996:        
                    997:        if(fp_debugger != NULL) {
                    998:                fprintf(fp_debugger, "%s", buffer);
                    999:        }
                   1000:        telnet_send(buffer);
                   1001: }
                   1002: 
                   1003: bool telnet_gets(char *str, int n)
                   1004: {
                   1005:        char buffer[1024];
                   1006:        int ptr = 0;
                   1007:        
                   1008:        telnet_command("\033[12l"); // local echo on
                   1009:        telnet_command("\033[2l");  // key unlock
                   1010:        
1.1.1.54  root     1011:        while(!m_exit) {
1.1.1.33  root     1012:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1013:                
                   1014:                if(len > 0 && buffer[0] != 0xff) {
                   1015:                        for(int i = 0; i < len; i++) {
                   1016:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1017:                                        str[ptr] = 0;
                   1018:                                        telnet_command("\033[2h");  // key lock
                   1019:                                        telnet_command("\033[12h"); // local echo off
1.1.1.54  root     1020:                                        return(!m_exit);
1.1.1.33  root     1021:                                } else if(buffer[i] == 0x08) {
                   1022:                                        if(ptr > 0) {
                   1023:                                                telnet_command("\033[0K"); // erase from cursor position
                   1024:                                                ptr--;
                   1025:                                        } else {
                   1026:                                                telnet_command("\033[1C"); // move cursor forward
                   1027:                                        }
                   1028:                                } else if(ptr < n - 1) {
1.1.1.37  root     1029:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root     1030:                                                str[ptr++] = buffer[i];
                   1031:                                        }
                   1032:                                } else {
                   1033:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                   1034:                                }
                   1035:                        }
                   1036:                } else if(len == -1) {
                   1037:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1038:                                return(false);
                   1039:                        }
                   1040:                } else if(len == 0) {
                   1041:                        return(false);
                   1042:                }
                   1043:                Sleep(10);
                   1044:        }
1.1.1.54  root     1045:        return(!m_exit);
1.1.1.33  root     1046: }
                   1047: 
                   1048: bool telnet_kbhit()
                   1049: {
                   1050:        char buffer[1024];
                   1051:        
1.1.1.54  root     1052:        if(!m_exit) {
1.1.1.33  root     1053:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1054:                
                   1055:                if(len > 0) {
                   1056:                        for(int i = 0; i < len; i++) {
                   1057:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                   1058:                                        return(true);
                   1059:                                }
                   1060:                        }
                   1061:                } else if(len == 0) {
                   1062:                        return(true); // disconnected
                   1063:                }
                   1064:        }
                   1065:        return(false);
                   1066: }
                   1067: 
                   1068: bool telnet_disconnected()
                   1069: {
                   1070:        char buffer[1024];
                   1071:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                   1072:        
                   1073:        if(len == 0) {
                   1074:                return(true);
                   1075:        } else if(len == -1) {
                   1076:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                   1077:                        return(true);
                   1078:                }
                   1079:        }
                   1080:        return(false);
                   1081: }
                   1082: 
                   1083: void telnet_set_color(int color)
                   1084: {
                   1085:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                   1086: }
                   1087: 
                   1088: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                   1089: {
                   1090: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                   1091:        UINT8 ops[16];
                   1092:        for(int i = 0; i < 16; i++) {
                   1093:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                   1094:        }
                   1095:        UINT8 *oprom = ops;
                   1096:        
                   1097: #if defined(HAS_I386)
                   1098:        if(m_operand_size) {
                   1099:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                   1100:        } else
                   1101: #endif
                   1102:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1103: }
                   1104: 
                   1105: void debugger_regs_info(char *buffer)
                   1106: {
1.1.1.64  root     1107:        UINT32 flags = i386_get_flags();
                   1108:        
1.1.1.33  root     1109: #if defined(HAS_I386)
                   1110:        if(m_operand_size) {
                   1111:                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",
                   1112:                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),
                   1113:                PROTECTED_MODE ? "PE" : "--",
                   1114:                (flags & 0x40000) ? 'A' : '-',
                   1115:                (flags & 0x20000) ? 'V' : '-',
                   1116:                (flags & 0x10000) ? 'R' : '-',
                   1117:                (flags & 0x04000) ? 'N' : '-',
                   1118:                (flags & 0x02000) ? '1' : '0',
                   1119:                (flags & 0x01000) ? '1' : '0',
                   1120:                (flags & 0x00800) ? 'O' : '-',
                   1121:                (flags & 0x00400) ? 'D' : '-',
                   1122:                (flags & 0x00200) ? 'I' : '-',
                   1123:                (flags & 0x00100) ? 'T' : '-',
                   1124:                (flags & 0x00080) ? 'S' : '-',
                   1125:                (flags & 0x00040) ? 'Z' : '-',
                   1126:                (flags & 0x00010) ? 'A' : '-',
                   1127:                (flags & 0x00004) ? 'P' : '-',
                   1128:                (flags & 0x00001) ? 'C' : '-');
                   1129:        } else {
                   1130: #endif
                   1131:                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",
                   1132:                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),
                   1133: #if defined(HAS_I386)
                   1134:                PROTECTED_MODE ? "PE" : "--",
                   1135: #else
                   1136:                "--",
                   1137: #endif
                   1138:                (flags & 0x40000) ? 'A' : '-',
                   1139:                (flags & 0x20000) ? 'V' : '-',
                   1140:                (flags & 0x10000) ? 'R' : '-',
                   1141:                (flags & 0x04000) ? 'N' : '-',
                   1142:                (flags & 0x02000) ? '1' : '0',
                   1143:                (flags & 0x01000) ? '1' : '0',
                   1144:                (flags & 0x00800) ? 'O' : '-',
                   1145:                (flags & 0x00400) ? 'D' : '-',
                   1146:                (flags & 0x00200) ? 'I' : '-',
                   1147:                (flags & 0x00100) ? 'T' : '-',
                   1148:                (flags & 0x00080) ? 'S' : '-',
                   1149:                (flags & 0x00040) ? 'Z' : '-',
                   1150:                (flags & 0x00010) ? 'A' : '-',
                   1151:                (flags & 0x00004) ? 'P' : '-',
                   1152:                (flags & 0x00001) ? 'C' : '-');
                   1153: #if defined(HAS_I386)
                   1154:        }
                   1155: #endif
                   1156: }
                   1157: 
                   1158: void debugger_process_info(char *buffer)
                   1159: {
                   1160:        UINT16 psp_seg = current_psp;
                   1161:        process_t *process;
                   1162:        bool check[0x10000] = {0};
                   1163:        
                   1164:        buffer[0] = '\0';
                   1165:        
                   1166:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1167:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1168:                char *file = process->module_path, *s;
                   1169:                char tmp[8192];
                   1170:                
                   1171:                while((s = strstr(file, "\\")) != NULL) {
                   1172:                        file = s + 1;
                   1173:                }
                   1174:                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));
                   1175:                strcat(tmp, buffer);
                   1176:                strcpy(buffer, tmp);
                   1177:                
                   1178:                check[psp_seg] = true;
                   1179:                psp_seg = psp->parent_psp;
                   1180:        }
                   1181: }
                   1182: 
                   1183: UINT32 debugger_get_val(const char *str)
                   1184: {
                   1185:        char tmp[1024];
                   1186:        
                   1187:        if(str == NULL || strlen(str) == 0) {
                   1188:                return(0);
                   1189:        }
                   1190:        strcpy(tmp, str);
                   1191:        
                   1192:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1193:                // ank
                   1194:                return(tmp[1] & 0xff);
                   1195:        } else if(tmp[0] == '%') {
                   1196:                // decimal
                   1197:                return(strtoul(tmp + 1, NULL, 10));
                   1198:        }
                   1199:        return(strtoul(tmp, NULL, 16));
                   1200: }
                   1201: 
                   1202: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1203: {
                   1204:        char tmp[1024], *s;
                   1205:        
                   1206:        if(str == NULL || strlen(str) == 0) {
                   1207:                return(val);
                   1208:        }
                   1209:        strcpy(tmp, str);
                   1210:        
                   1211:        if((s = strstr(tmp, ":")) != NULL) {
                   1212:                // 0000:0000
                   1213:                *s = '\0';
                   1214:                return(debugger_get_val(tmp));
                   1215:        }
                   1216:        return(val);
                   1217: }
                   1218: 
                   1219: UINT32 debugger_get_ofs(const char *str)
                   1220: {
                   1221:        char tmp[1024], *s;
                   1222:        
                   1223:        if(str == NULL || strlen(str) == 0) {
                   1224:                return(0);
                   1225:        }
                   1226:        strcpy(tmp, str);
                   1227:        
                   1228:        if((s = strstr(tmp, ":")) != NULL) {
                   1229:                // 0000:0000
                   1230:                return(debugger_get_val(s + 1));
                   1231:        }
                   1232:        return(debugger_get_val(tmp));
                   1233: }
                   1234: 
                   1235: void debugger_main()
                   1236: {
                   1237:        telnet_command("\033[20h"); // cr-lf
                   1238:        
                   1239:        force_suspend = true;
                   1240:        now_going = false;
                   1241:        now_debugging = true;
                   1242:        Sleep(100);
                   1243:        
1.1.1.54  root     1244:        if(!m_exit && !now_suspended) {
1.1.1.33  root     1245:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1246:                telnet_printf("waiting until cpu is suspended...\n");
                   1247:        }
1.1.1.54  root     1248:        while(!m_exit && !now_suspended) {
1.1.1.33  root     1249:                if(telnet_disconnected()) {
                   1250:                        break;
                   1251:                }
                   1252:                Sleep(10);
                   1253:        }
                   1254:        
                   1255:        char buffer[8192];
                   1256:        
                   1257:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1258:        debugger_process_info(buffer);
                   1259:        telnet_printf("%s", buffer);
                   1260:        debugger_regs_info(buffer);
                   1261:        telnet_printf("%s", buffer);
                   1262:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1263:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1264:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1265:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1266:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1267:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1268:        
                   1269:        #define MAX_COMMAND_LEN 64
                   1270:        
                   1271:        char command[MAX_COMMAND_LEN + 1];
                   1272:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1273:        
                   1274:        UINT32 data_seg = SREG(DS);
                   1275:        UINT32 data_ofs = 0;
                   1276:        UINT32 dasm_seg = SREG(CS);
                   1277:        UINT32 dasm_ofs = m_eip;
                   1278:        
1.1.1.54  root     1279:        while(!m_exit) {
1.1.1.33  root     1280:                telnet_printf("- ");
                   1281:                command[0] = '\0';
                   1282:                
                   1283:                if(fi_debugger != NULL) {
                   1284:                        while(command[0] == '\0') {
                   1285:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1286:                                        break;
                   1287:                                }
                   1288:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1289:                                        command[strlen(command) - 1] = '\0';
                   1290:                                }
                   1291:                        }
                   1292:                        if(command[0] != '\0') {
                   1293:                                telnet_command("%s\n", command);
                   1294:                        }
                   1295:                }
                   1296:                if(command[0] == '\0') {
                   1297:                        if(!telnet_gets(command, sizeof(command))) {
                   1298:                                break;
                   1299:                        }
                   1300:                }
                   1301:                if(command[0] == '\0') {
                   1302:                        strcpy(command, prev_command);
                   1303:                } else {
                   1304:                        strcpy(prev_command, command);
                   1305:                }
                   1306:                if(fp_debugger != NULL) {
                   1307:                        fprintf(fp_debugger, "%s\n", command);
                   1308:                }
                   1309:                
1.1.1.54  root     1310:                if(!m_exit && command[0] != 0) {
1.1.1.33  root     1311:                        char *params[32], *token = NULL;
                   1312:                        int num = 0;
                   1313:                        
                   1314:                        if((token = strtok(command, " ")) != NULL) {
                   1315:                                params[num++] = token;
                   1316:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1317:                                        params[num++] = token;
                   1318:                                }
                   1319:                        }
                   1320:                        if(stricmp(params[0], "D") == 0) {
                   1321:                                if(num <= 3) {
                   1322:                                        if(num >= 2) {
                   1323:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1324:                                                data_ofs = debugger_get_ofs(params[1]);
                   1325:                                        }
                   1326:                                        UINT32 end_seg = data_seg;
                   1327:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1328:                                        if(num == 3) {
                   1329:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1330:                                                end_ofs = debugger_get_ofs(params[2]);
                   1331:                                        }
                   1332:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1333:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1334: //                                     bool is_sjis = false;
1.1.1.33  root     1335:                                        
                   1336:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1337:                                                if((addr & 0x0f) == 0) {
                   1338:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1339:                                                                data_seg += 0x1000;
                   1340:                                                                data_ofs -= 0x10000;
                   1341:                                                        }
                   1342:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1343:                                                        memset(buffer, 0, sizeof(buffer));
                   1344:                                                }
                   1345:                                                if(addr < start_addr || addr > end_addr) {
                   1346:                                                        telnet_printf("   ");
                   1347:                                                        buffer[addr & 0x0f] = ' ';
                   1348:                                                } else {
                   1349:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1350:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1351: //                                                     if(is_sjis) {
1.1.1.33  root     1352: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1353: //                                                             is_sjis = false;
1.1.1.33  root     1354: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1355: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1356: //                                                             is_sjis = true;
1.1.1.33  root     1357: //                                                     } else
                   1358:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1359:                                                                buffer[addr & 0x0f] = data;
                   1360:                                                        } else {
                   1361:                                                                buffer[addr & 0x0f] = '.';
                   1362:                                                        }
                   1363:                                                }
                   1364:                                                if((addr & 0x0f) == 0x0f) {
                   1365:                                                        telnet_printf("  %s\n", buffer);
                   1366:                                                }
                   1367:                                        }
                   1368:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1369:                                                data_seg += 0x1000;
                   1370:                                                data_ofs -= 0x10000;
                   1371:                                        }
                   1372:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1373:                                } else {
                   1374:                                        telnet_printf("invalid parameter number\n");
                   1375:                                }
                   1376:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1377:                                if(num >= 3) {
                   1378:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1379:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1380:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1381:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1382:                                        }
                   1383:                                } else {
                   1384:                                        telnet_printf("invalid parameter number\n");
                   1385:                                }
                   1386:                        } else if(stricmp(params[0], "EW") == 0) {
                   1387:                                if(num >= 3) {
                   1388:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1389:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1390:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1391:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1392:                                        }
                   1393:                                } else {
                   1394:                                        telnet_printf("invalid parameter number\n");
                   1395:                                }
                   1396:                        } else if(stricmp(params[0], "ED") == 0) {
                   1397:                                if(num >= 3) {
                   1398:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1399:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1400:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1401:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1402:                                        }
                   1403:                                } else {
                   1404:                                        telnet_printf("invalid parameter number\n");
                   1405:                                }
                   1406:                        } else if(stricmp(params[0], "EA") == 0) {
                   1407:                                if(num >= 3) {
                   1408:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1409:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1410:                                        strcpy(buffer, prev_command);
                   1411:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1412:                                                int len = strlen(token);
                   1413:                                                for(int i = 0; i < len; i++) {
                   1414:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1415:                                                }
                   1416:                                        } else {
                   1417:                                                telnet_printf("invalid parameter\n");
                   1418:                                        }
                   1419:                                } else {
                   1420:                                        telnet_printf("invalid parameter number\n");
                   1421:                                }
                   1422:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1423:                                if(num == 2) {
                   1424:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1425:                                } else {
                   1426:                                        telnet_printf("invalid parameter number\n");
                   1427:                                }
                   1428:                        } else if(stricmp(params[0], "IW") == 0) {
                   1429:                                if(num == 2) {
                   1430:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1431:                                } else {
                   1432:                                        telnet_printf("invalid parameter number\n");
                   1433:                                }
                   1434:                        } else if(stricmp(params[0], "ID") == 0) {
                   1435:                                if(num == 2) {
                   1436:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1437:                                } else {
                   1438:                                        telnet_printf("invalid parameter number\n");
                   1439:                                }
                   1440:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1441:                                if(num == 3) {
                   1442:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1443:                                } else {
                   1444:                                        telnet_printf("invalid parameter number\n");
                   1445:                                }
                   1446:                        } else if(stricmp(params[0], "OW") == 0) {
                   1447:                                if(num == 3) {
                   1448:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1449:                                } else {
                   1450:                                        telnet_printf("invalid parameter number\n");
                   1451:                                }
                   1452:                        } else if(stricmp(params[0], "OD") == 0) {
                   1453:                                if(num == 3) {
                   1454:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1455:                                } else {
                   1456:                                        telnet_printf("invalid parameter number\n");
                   1457:                                }
                   1458:                        } else if(stricmp(params[0], "R") == 0) {
                   1459:                                if(num == 1) {
                   1460:                                        debugger_regs_info(buffer);
                   1461:                                        telnet_printf("%s", buffer);
                   1462:                                } else if(num == 3) {
                   1463: #if defined(HAS_I386)
                   1464:                                        if(stricmp(params[1], "EAX") == 0) {
                   1465:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1466:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1467:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1468:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1469:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1470:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1471:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1472:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1473:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1474:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1475:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1476:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1477:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1478:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1479:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1480:                                        } else
                   1481: #endif
                   1482:                                        if(stricmp(params[1], "AX") == 0) {
                   1483:                                                REG16(AX) = debugger_get_val(params[2]);
                   1484:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1485:                                                REG16(BX) = debugger_get_val(params[2]);
                   1486:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1487:                                                REG16(CX) = debugger_get_val(params[2]);
                   1488:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1489:                                                REG16(DX) = debugger_get_val(params[2]);
                   1490:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1491:                                                REG16(SP) = debugger_get_val(params[2]);
                   1492:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1493:                                                REG16(BP) = debugger_get_val(params[2]);
                   1494:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1495:                                                REG16(SI) = debugger_get_val(params[2]);
                   1496:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1497:                                                REG16(DI) = debugger_get_val(params[2]);
                   1498:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1499: #if defined(HAS_I386)
                   1500:                                                if(m_operand_size) {
                   1501:                                                        m_eip = debugger_get_val(params[2]);
                   1502:                                                } else {
                   1503:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1504:                                                }
                   1505:                                                CHANGE_PC(m_eip);
                   1506: #else
                   1507:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1508:                                                CHANGE_PC(m_pc);
                   1509: #endif
                   1510:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1511:                                                REG8(AL) = debugger_get_val(params[2]);
                   1512:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1513:                                                REG8(AH) = debugger_get_val(params[2]);
                   1514:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1515:                                                REG8(BL) = debugger_get_val(params[2]);
                   1516:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1517:                                                REG8(BH) = debugger_get_val(params[2]);
                   1518:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1519:                                                REG8(CL) = debugger_get_val(params[2]);
                   1520:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1521:                                                REG8(CH) = debugger_get_val(params[2]);
                   1522:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1523:                                                REG8(DL) = debugger_get_val(params[2]);
                   1524:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1525:                                                REG8(DH) = debugger_get_val(params[2]);
                   1526:                                        } else {
                   1527:                                                telnet_printf("unknown register %s\n", params[1]);
                   1528:                                        }
                   1529:                                } else {
                   1530:                                        telnet_printf("invalid parameter number\n");
                   1531:                                }
1.1.1.60  root     1532:                        } else if(stricmp(params[0], "S") == 0) {
1.1.1.33  root     1533:                                if(num >= 4) {
                   1534:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1535:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1536:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1537:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1538:                                        UINT8 list[32];
                   1539:                                        
                   1540:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1541:                                                list[j] = debugger_get_val(params[i]);
                   1542:                                        }
                   1543:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1544:                                                bool found = true;
                   1545:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1546:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1547:                                                                found = false;
                   1548:                                                                break;
                   1549:                                                        }
                   1550:                                                }
                   1551:                                                if(found) {
                   1552:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1553:                                                }
                   1554:                                                if((cur_ofs += 1) > 0xffff) {
                   1555:                                                        cur_seg += 0x1000;
                   1556:                                                        cur_ofs -= 0x10000;
                   1557:                                                }
                   1558:                                        }
                   1559:                                } else {
                   1560:                                        telnet_printf("invalid parameter number\n");
                   1561:                                }
                   1562:                        } else if(stricmp(params[0], "U") == 0) {
                   1563:                                if(num <= 3) {
                   1564:                                        if(num >= 2) {
                   1565:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1566:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1567:                                        }
                   1568:                                        if(num == 3) {
                   1569:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1570:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1571:                                                
                   1572:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1573:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1574:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1575:                                                        for(int i = 0; i < len; i++) {
                   1576:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1577:                                                        }
                   1578:                                                        for(int i = len; i < 8; i++) {
                   1579:                                                                telnet_printf("  ");
                   1580:                                                        }
                   1581:                                                        telnet_printf("  %s\n", buffer);
                   1582:                                                        if((dasm_ofs += len) > 0xffff) {
                   1583:                                                                dasm_seg += 0x1000;
                   1584:                                                                dasm_ofs -= 0x10000;
                   1585:                                                        }
                   1586:                                                }
                   1587:                                        } else {
                   1588:                                                for(int i = 0; i < 16; i++) {
                   1589:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1590:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1591:                                                        for(int i = 0; i < len; i++) {
                   1592:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1593:                                                        }
                   1594:                                                        for(int i = len; i < 8; i++) {
                   1595:                                                                telnet_printf("  ");
                   1596:                                                        }
                   1597:                                                        telnet_printf("  %s\n", buffer);
                   1598:                                                        if((dasm_ofs += len) > 0xffff) {
                   1599:                                                                dasm_seg += 0x1000;
                   1600:                                                                dasm_ofs -= 0x10000;
                   1601:                                                        }
                   1602:                                                }
                   1603:                                        }
                   1604:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1605:                                } else {
                   1606:                                        telnet_printf("invalid parameter number\n");
                   1607:                                }
                   1608:                        } else if(stricmp(params[0], "H") == 0) {
                   1609:                                if(num == 3) {
                   1610:                                        UINT32 l = debugger_get_val(params[1]);
                   1611:                                        UINT32 r = debugger_get_val(params[2]);
                   1612:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1613:                                } else {
                   1614:                                        telnet_printf("invalid parameter number\n");
                   1615:                                }
                   1616:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1617:                                break_point_t *break_point_ptr;
                   1618:                                #define GET_BREAK_POINT_PTR() { \
1.1.1.58  root     1619:                                        if(params[0][0] == 'R' || params[0][0] == 'r') { \
1.1.1.33  root     1620:                                                break_point_ptr = &rd_break_point; \
1.1.1.58  root     1621:                                        } else if(params[0][0] == 'W' || params[0][0] == 'w') { \
1.1.1.33  root     1622:                                                break_point_ptr = &wr_break_point; \
1.1.1.58  root     1623:                                        } else if(params[0][0] == 'I' || params[0][0] == 'i') { \
1.1.1.33  root     1624:                                                break_point_ptr = &in_break_point; \
1.1.1.58  root     1625:                                        } else if(params[0][0] == 'O' || params[0][0] == 'o') { \
1.1.1.33  root     1626:                                                break_point_ptr = &out_break_point; \
                   1627:                                        } else { \
                   1628:                                                break_point_ptr = &break_point; \
                   1629:                                        } \
                   1630:                                }
                   1631:                                GET_BREAK_POINT_PTR();
                   1632:                                if(num == 2) {
1.1.1.58  root     1633:                                        UINT32 seg = 0;
                   1634:                                        if(params[0][0] == 'R' || params[0][0] == 'r' || params[0][0] == 'W' || params[0][0] == 'w') {
                   1635:                                                seg = debugger_get_seg(params[1], data_seg);
                   1636:                                        } else {
                   1637:                                                seg = debugger_get_seg(params[1], SREG(CS));
                   1638:                                        }
1.1.1.33  root     1639:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1640:                                        bool found = false;
                   1641:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1642:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1643:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1644:                                                        break_point_ptr->table[i].seg = seg;
                   1645:                                                        break_point_ptr->table[i].ofs = ofs;
                   1646:                                                        break_point_ptr->table[i].status = 1;
                   1647:                                                        found = true;
                   1648:                                                }
                   1649:                                        }
                   1650:                                        if(!found) {
                   1651:                                                telnet_printf("too many break points\n");
                   1652:                                        }
                   1653:                                } else {
                   1654:                                        telnet_printf("invalid parameter number\n");
                   1655:                                }
                   1656:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1657:                                break_point_t *break_point_ptr;
                   1658:                                GET_BREAK_POINT_PTR();
                   1659:                                if(num == 2) {
                   1660:                                        UINT32 addr = debugger_get_val(params[1]);
                   1661:                                        bool found = false;
                   1662:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1663:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1664:                                                        break_point_ptr->table[i].addr = addr;
                   1665:                                                        break_point_ptr->table[i].status = 1;
                   1666:                                                        found = true;
                   1667:                                                }
                   1668:                                        }
                   1669:                                        if(!found) {
                   1670:                                                telnet_printf("too many break points\n");
                   1671:                                        }
                   1672:                                } else {
                   1673:                                        telnet_printf("invalid parameter number\n");
                   1674:                                }
                   1675:                        } 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) {
                   1676:                                break_point_t *break_point_ptr;
                   1677:                                GET_BREAK_POINT_PTR();
                   1678:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1679:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1680:                                } else if(num >= 2) {
                   1681:                                        for(int i = 1; i < num; i++) {
                   1682:                                                int index = debugger_get_val(params[i]);
                   1683:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1684:                                                        telnet_printf("invalid index %x\n", index);
                   1685:                                                } else {
                   1686:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1687:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1688:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1689:                                                        break_point_ptr->table[index - 1].status = 0;
                   1690:                                                }
                   1691:                                        }
                   1692:                                } else {
                   1693:                                        telnet_printf("invalid parameter number\n");
                   1694:                                }
                   1695:                        } 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 ||
                   1696:                                  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) {
                   1697:                                break_point_t *break_point_ptr;
                   1698:                                GET_BREAK_POINT_PTR();
                   1699:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1700:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1701:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1702:                                                if(break_point_ptr->table[i].status != 0) {
                   1703:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1704:                                                }
                   1705:                                        }
                   1706:                                } else if(num >= 2) {
                   1707:                                        for(int i = 1; i < num; i++) {
                   1708:                                                int index = debugger_get_val(params[i]);
                   1709:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1710:                                                        telnet_printf("invalid index %x\n", index);
                   1711:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1712:                                                        telnet_printf("break point %x is null\n", index);
                   1713:                                                } else {
                   1714:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1715:                                                }
                   1716:                                        }
                   1717:                                } else {
                   1718:                                        telnet_printf("invalid parameter number\n");
                   1719:                                }
                   1720:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1721:                                break_point_t *break_point_ptr;
                   1722:                                GET_BREAK_POINT_PTR();
                   1723:                                if(num == 1) {
                   1724:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1725:                                                if(break_point_ptr->table[i].status) {
                   1726:                                                        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);
                   1727:                                                }
                   1728:                                        }
                   1729:                                } else {
                   1730:                                        telnet_printf("invalid parameter number\n");
                   1731:                                }
                   1732:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1733:                                break_point_t *break_point_ptr;
                   1734:                                GET_BREAK_POINT_PTR();
                   1735:                                if(num == 1) {
                   1736:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1737:                                                if(break_point_ptr->table[i].status) {
                   1738:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1739:                                                }
                   1740:                                        }
                   1741:                                } else {
                   1742:                                        telnet_printf("invalid parameter number\n");
                   1743:                                }
                   1744:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1745:                                if(num >= 2 && num <= 4) {
                   1746:                                        int int_num = debugger_get_val(params[1]);
                   1747:                                        UINT8 ah = 0, ah_registered = 0;
                   1748:                                        UINT8 al = 0, al_registered = 0;
                   1749:                                        if(num >= 3) {
                   1750:                                                ah = debugger_get_val(params[2]);
                   1751:                                                ah_registered = 1;
                   1752:                                        }
                   1753:                                        if(num == 4) {
                   1754:                                                al = debugger_get_val(params[3]);
                   1755:                                                al_registered = 1;
                   1756:                                        }
                   1757:                                        bool found = false;
                   1758:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1759:                                                if(int_break_point.table[i].status == 0 || (
                   1760:                                                   int_break_point.table[i].int_num == int_num &&
                   1761:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1762:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1763:                                                        int_break_point.table[i].int_num = int_num;
                   1764:                                                        int_break_point.table[i].ah = ah;
                   1765:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1766:                                                        int_break_point.table[i].al = al;
                   1767:                                                        int_break_point.table[i].al_registered = al_registered;
                   1768:                                                        int_break_point.table[i].status = 1;
                   1769:                                                        found = true;
                   1770:                                                }
                   1771:                                        }
                   1772:                                        if(!found) {
                   1773:                                                telnet_printf("too many break points\n");
                   1774:                                        }
                   1775:                                } else {
                   1776:                                        telnet_printf("invalid parameter number\n");
                   1777:                                }
                   1778:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1779:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1780:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1781:                                } else if(num >= 2) {
                   1782:                                        for(int i = 1; i < num; i++) {
                   1783:                                                int index = debugger_get_val(params[i]);
                   1784:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1785:                                                        telnet_printf("invalid index %x\n", index);
                   1786:                                                } else {
                   1787:                                                        int_break_point.table[index - 1].int_num = 0;
                   1788:                                                        int_break_point.table[index - 1].ah = 0;
                   1789:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1790:                                                        int_break_point.table[index - 1].al = 0;
                   1791:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1792:                                                        int_break_point.table[index - 1].status = 0;
                   1793:                                                }
                   1794:                                        }
                   1795:                                } else {
                   1796:                                        telnet_printf("invalid parameter number\n");
                   1797:                                }
                   1798:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1799:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1800:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1801:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1802:                                                if(int_break_point.table[i].status != 0) {
                   1803:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1804:                                                }
                   1805:                                        }
                   1806:                                } else if(num >= 2) {
                   1807:                                        for(int i = 1; i < num; i++) {
                   1808:                                                int index = debugger_get_val(params[i]);
                   1809:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1810:                                                        telnet_printf("invalid index %x\n", index);
                   1811:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1812:                                                        telnet_printf("break point %x is null\n", index);
                   1813:                                                } else {
                   1814:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1815:                                                }
                   1816:                                        }
                   1817:                                } else {
                   1818:                                        telnet_printf("invalid parameter number\n");
                   1819:                                }
                   1820:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1821:                                if(num == 1) {
                   1822:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1823:                                                if(int_break_point.table[i].status) {
                   1824:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1825:                                                        if(int_break_point.table[i].ah_registered) {
                   1826:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1827:                                                        }
                   1828:                                                        if(int_break_point.table[i].al_registered) {
                   1829:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1830:                                                        }
                   1831:                                                        telnet_printf("\n");
                   1832:                                                }
                   1833:                                        }
                   1834:                                } else {
                   1835:                                        telnet_printf("invalid parameter number\n");
                   1836:                                }
                   1837:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1838:                                if(num == 1 || num == 2) {
                   1839:                                        break_point_t break_point_stored;
                   1840:                                        bool break_points_stored = false;
                   1841:                                        
                   1842:                                        if(stricmp(params[0], "P") == 0) {
                   1843:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1844:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1845:                                                break_points_stored = true;
                   1846:                                                
                   1847:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1848:                                                break_point.table[0].status = 1;
                   1849:                                        } else if(num >= 2) {
                   1850:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1851:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1852:                                                break_points_stored = true;
                   1853:                                                
                   1854:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1855:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1856:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1857:                                                break_point.table[0].seg = seg;
                   1858:                                                break_point.table[0].ofs = ofs;
                   1859:                                                break_point.table[0].status = 1;
                   1860:                                        }
                   1861:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1862:                                        now_going = true;
                   1863:                                        now_suspended = false;
                   1864:                                        
                   1865:                                        telnet_command("\033[2l"); // key unlock
1.1.1.54  root     1866:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1867:                                                if(telnet_kbhit()) {
                   1868:                                                        break;
                   1869:                                                }
                   1870:                                                Sleep(10);
                   1871:                                        }
                   1872:                                        now_going = false;
                   1873:                                        telnet_command("\033[2h"); // key lock
                   1874:                                        
                   1875:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1876:                                                Sleep(100);
1.1.1.54  root     1877:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1878:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1879:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1880:                                                }
                   1881:                                        }
1.1.1.54  root     1882:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1883:                                                if(telnet_disconnected()) {
                   1884:                                                        break;
                   1885:                                                }
                   1886:                                                Sleep(10);
                   1887:                                        }
                   1888:                                        dasm_seg = SREG(CS);
                   1889:                                        dasm_ofs = m_eip;
                   1890:                                        
                   1891:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1892:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1893:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1894:                                        
                   1895:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1896:                                        debugger_regs_info(buffer);
                   1897:                                        telnet_printf("%s", buffer);
                   1898:                                        
                   1899:                                        if(break_point.hit) {
                   1900:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1901:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1902:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1903:                                                }
                   1904:                                        } else if(rd_break_point.hit) {
                   1905:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1906:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1907:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1908:                                                m_prev_cs, m_prev_eip);
                   1909:                                        } else if(wr_break_point.hit) {
                   1910:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1911:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1912:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1913:                                                m_prev_cs, m_prev_eip);
                   1914:                                        } else if(in_break_point.hit) {
                   1915:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1916:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1917:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1918:                                                m_prev_cs, m_prev_eip);
                   1919:                                        } else if(out_break_point.hit) {
                   1920:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1921:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1922:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1923:                                                m_prev_cs, m_prev_eip);
                   1924:                                        } else if(int_break_point.hit) {
                   1925:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1926:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1927:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1928:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1929:                                                }
                   1930:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1931:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1932:                                                }
                   1933:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1934:                                        } else {
                   1935:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1936:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1937:                                        }
                   1938:                                        if(break_points_stored) {
                   1939:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1940:                                        }
                   1941:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1942:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1943:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1944:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1945:                                } else {
                   1946:                                        telnet_printf("invalid parameter number\n");
                   1947:                                }
                   1948:                        } else if(stricmp(params[0], "T") == 0) {
                   1949:                                if(num == 1 || num == 2) {
                   1950:                                        int steps = 1;
                   1951:                                        if(num >= 2) {
                   1952:                                                steps = debugger_get_val(params[1]);
                   1953:                                        }
                   1954:                                        
                   1955:                                        telnet_command("\033[2l"); // key unlock
                   1956:                                        while(steps-- > 0) {
                   1957:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1958:                                                now_going = false;
                   1959:                                                now_suspended = false;
                   1960:                                                
1.1.1.54  root     1961:                                                while(!m_exit && !now_suspended) {
1.1.1.33  root     1962:                                                        if(telnet_disconnected()) {
                   1963:                                                                break;
                   1964:                                                        }
                   1965:                                                        Sleep(10);
                   1966:                                                }
                   1967:                                                dasm_seg = SREG(CS);
                   1968:                                                dasm_ofs = m_eip;
                   1969:                                                
                   1970:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1971:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1972:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1973:                                                
                   1974:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1975:                                                debugger_regs_info(buffer);
                   1976:                                                telnet_printf("%s", buffer);
                   1977:                                                
                   1978:                                                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()) {
                   1979:                                                        break;
                   1980:                                                }
                   1981:                                        }
                   1982:                                        telnet_command("\033[2h"); // key lock
                   1983:                                        
                   1984:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1985:                                                Sleep(100);
1.1.1.54  root     1986:                                                if(!m_exit && !now_suspended) {
1.1.1.33  root     1987:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1988:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1989:                                                }
                   1990:                                        }
1.1.1.54  root     1991:                                        while(!m_exit && !now_suspended) {
1.1.1.33  root     1992:                                                if(telnet_disconnected()) {
                   1993:                                                        break;
                   1994:                                                }
                   1995:                                                Sleep(10);
                   1996:                                        }
                   1997:                                        if(break_point.hit) {
                   1998:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1999:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   2000:                                        } else if(rd_break_point.hit) {
                   2001:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2002:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   2003:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   2004:                                                m_prev_cs, m_prev_eip);
                   2005:                                        } else if(wr_break_point.hit) {
                   2006:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2007:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   2008:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   2009:                                                m_prev_cs, m_prev_eip);
                   2010:                                        } else if(in_break_point.hit) {
                   2011:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2012:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   2013:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   2014:                                                m_prev_cs, m_prev_eip);
                   2015:                                        } else if(out_break_point.hit) {
                   2016:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2017:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   2018:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   2019:                                                m_prev_cs, m_prev_eip);
                   2020:                                        } else if(int_break_point.hit) {
                   2021:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2022:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   2023:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   2024:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   2025:                                                }
                   2026:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   2027:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   2028:                                                }
                   2029:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   2030:                                        } else if(steps > 0) {
                   2031:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   2032:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   2033:                                        }
                   2034:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   2035:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   2036:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   2037:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   2038:                                } else {
                   2039:                                        telnet_printf("invalid parameter number\n");
                   2040:                                }
                   2041:                        } else if(stricmp(params[0], "Q") == 0) {
                   2042:                                break;
                   2043:                        } else if(stricmp(params[0], "X") == 0) {
                   2044:                                debugger_process_info(buffer);
                   2045:                                telnet_printf("%s", buffer);
                   2046:                        } else if(stricmp(params[0], ">") == 0) {
                   2047:                                if(num == 2) {
                   2048:                                        if(fp_debugger != NULL) {
                   2049:                                                fclose(fp_debugger);
                   2050:                                                fp_debugger = NULL;
                   2051:                                        }
                   2052:                                        fp_debugger = fopen(params[1], "w");
                   2053:                                } else {
                   2054:                                        telnet_printf("invalid parameter number\n");
                   2055:                                }
                   2056:                        } else if(stricmp(params[0], "<") == 0) {
                   2057:                                if(num == 2) {
                   2058:                                        if(fi_debugger != NULL) {
                   2059:                                                fclose(fi_debugger);
                   2060:                                                fi_debugger = NULL;
                   2061:                                        }
                   2062:                                        fi_debugger = fopen(params[1], "r");
                   2063:                                } else {
                   2064:                                        telnet_printf("invalid parameter number\n");
                   2065:                                }
                   2066:                        } else if(stricmp(params[0], "?") == 0) {
                   2067:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   2068:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   2069:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   2070:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   2071:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   2072:                                
                   2073:                                telnet_printf("R - show registers\n");
                   2074:                                telnet_printf("R <reg> <value> - edit register\n");
                   2075:                                telnet_printf("S <start> <end> <list> - search\n");
                   2076:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   2077:                                
                   2078:                                telnet_printf("H <value> <value> - hexadd\n");
                   2079:                                
                   2080:                                telnet_printf("BP <address> - set breakpoint\n");
                   2081:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   2082:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   2083:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   2084:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   2085:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   2086:                                
                   2087:                                telnet_printf("G - go (press enter key to break)\n");
                   2088:                                telnet_printf("G <address> - go and break at address\n");
                   2089:                                telnet_printf("P - trace one opcode (step over)\n");
                   2090:                                telnet_printf("T [<count>] - trace (step in)\n");
                   2091:                                telnet_printf("Q - quit\n");
                   2092:                                telnet_printf("X - show dos process info\n");
                   2093:                                
                   2094:                                telnet_printf("> <filename> - output logfile\n");
                   2095:                                telnet_printf("< <filename> - input commands from file\n");
                   2096:                                
                   2097:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   2098:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   2099:                        } else {
                   2100:                                telnet_printf("unknown command %s\n", params[0]);
                   2101:                        }
                   2102:                }
                   2103:        }
                   2104:        if(fp_debugger != NULL) {
                   2105:                fclose(fp_debugger);
                   2106:                fp_debugger = NULL;
                   2107:        }
                   2108:        if(fi_debugger != NULL) {
                   2109:                fclose(fi_debugger);
                   2110:                fi_debugger = NULL;
                   2111:        }
                   2112:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2113:        closesocket(cli_socket);
                   2114: }
                   2115: 
                   2116: const char *debugger_get_ttermpro_path()
                   2117: {
                   2118:        static char path[MAX_PATH] = {0};
                   2119:        
                   2120:        if(getenv("ProgramFiles")) {
                   2121:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2122:        }
                   2123:        return(path);
                   2124: }
                   2125: 
                   2126: const char *debugger_get_ttermpro_x86_path()
                   2127: {
                   2128:        static char path[MAX_PATH] = {0};
                   2129:        
                   2130:        if(getenv("ProgramFiles(x86)")) {
                   2131:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2132:        }
                   2133:        return(path);
                   2134: }
                   2135: 
                   2136: const char *debugger_get_putty_path()
                   2137: {
                   2138:        static char path[MAX_PATH] = {0};
                   2139:        
                   2140:        if(getenv("ProgramFiles")) {
                   2141:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2142:        }
                   2143:        return(path);
                   2144: }
                   2145: 
                   2146: const char *debugger_get_putty_x86_path()
                   2147: {
                   2148:        static char path[MAX_PATH] = {0};
                   2149:        
                   2150:        if(getenv("ProgramFiles(x86)")) {
                   2151:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2152:        }
                   2153:        return(path);
                   2154: }
                   2155: 
                   2156: const char *debugger_get_telnet_path()
                   2157: {
                   2158:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2159:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2160:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2161:        // and 64bit version of telnet.exe will be installed in System32.
                   2162:        static char path[MAX_PATH] = {0};
                   2163:        
                   2164:        if(getenv("windir") != NULL) {
                   2165:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2166:        }
                   2167:        return(path);
                   2168: }
                   2169: 
                   2170: DWORD WINAPI debugger_thread(LPVOID)
                   2171: {
                   2172:        WSADATA was_data;
                   2173:        struct sockaddr_in svr_addr;
                   2174:        struct sockaddr_in cli_addr;
                   2175:        int cli_addr_len = sizeof(cli_addr);
                   2176:        int port = 23;
                   2177:        int bind_stat = SOCKET_ERROR;
                   2178:        struct timeval timeout;
                   2179:        
                   2180:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2181:        
                   2182:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2183:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2184:                svr_addr.sin_family = AF_INET;
                   2185:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2186:                
1.1.1.54  root     2187:                while(!m_exit && port < 10000) {
1.1.1.33  root     2188:                        svr_addr.sin_port = htons(port);
                   2189:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2190:                                break;
                   2191:                        } else {
                   2192:                                port = (port == 23) ? 9000 : (port + 1);
                   2193:                        }
                   2194:                }
                   2195:                if(bind_stat == 0) {
                   2196:                        timeout.tv_sec = 1;
                   2197:                        timeout.tv_usec = 0;
1.1.1.45  root     2198:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2199:                        
                   2200:                        listen(svr_socket, 1);
                   2201:                        
                   2202:                        char command[MAX_PATH] = {0};
1.1.1.60  root     2203:                        STARTUPINFOA si;
1.1.1.33  root     2204:                        PROCESS_INFORMATION pi;
                   2205:                        
                   2206:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2207:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2208:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2209:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2210:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2211:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2212:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2213:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2214:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2215:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2216:                        }
                   2217:                        if(command[0] != '\0') {
1.1.1.60  root     2218:                                memset(&si, 0, sizeof(STARTUPINFOA));
1.1.1.33  root     2219:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
1.1.1.60  root     2220:                                CreateProcessA(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
1.1.1.33  root     2221:                        }
                   2222:                        
1.1.1.54  root     2223:                        while(!m_exit) {
1.1.1.33  root     2224:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2225:                                        u_long val = 1;
                   2226:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2227:                                        debugger_main();
                   2228:                                }
                   2229:                        }
                   2230:                }
                   2231:        }
                   2232:        WSACleanup();
                   2233:        return(0);
                   2234: }
                   2235: #endif
                   2236: 
                   2237: /* ----------------------------------------------------------------------------
1.1       root     2238:        main
                   2239: ---------------------------------------------------------------------------- */
                   2240: 
1.1.1.28  root     2241: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2242: {
                   2243:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2244:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2245: #ifdef USE_SERVICE_THREAD
                   2246:                        EnterCriticalSection(&key_buf_crit_sect);
                   2247: #endif
1.1.1.51  root     2248:                        pcbios_clear_key_buffer();
1.1.1.35  root     2249: #ifdef USE_SERVICE_THREAD
                   2250:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2251: #endif
1.1.1.33  root     2252:                }
                   2253: //             key_code = key_recv = 0;
1.1.1.28  root     2254:                return TRUE;
                   2255:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2256:                return TRUE;
                   2257:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2258:                // this program will be terminated abnormally, do minimum end process
                   2259:                exit_handler();
                   2260:                exit(1);
                   2261:        }
                   2262:        return FALSE;
                   2263: }
                   2264: 
                   2265: void exit_handler()
                   2266: {
                   2267:        if(temp_file_created) {
1.1.1.60  root     2268:                DeleteFileA(temp_file_path);
1.1.1.28  root     2269:                temp_file_created = false;
                   2270:        }
                   2271:        if(key_buf_char != NULL) {
                   2272:                key_buf_char->release();
                   2273:                delete key_buf_char;
                   2274:                key_buf_char = NULL;
                   2275:        }
                   2276:        if(key_buf_scan != NULL) {
                   2277:                key_buf_scan->release();
                   2278:                delete key_buf_scan;
                   2279:                key_buf_scan = NULL;
                   2280:        }
1.1.1.57  root     2281:        if(key_buf_data != NULL) {
                   2282:                key_buf_data->release();
                   2283:                delete key_buf_data;
                   2284:                key_buf_data = NULL;
                   2285:        }
1.1.1.32  root     2286: #ifdef SUPPORT_XMS
                   2287:        msdos_xms_release();
                   2288: #endif
1.1.1.28  root     2289:        hardware_release();
                   2290: }
                   2291: 
1.1.1.35  root     2292: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2293: DWORD WINAPI vram_thread(LPVOID)
                   2294: {
1.1.1.54  root     2295:        while(!m_exit) {
1.1.1.28  root     2296:                EnterCriticalSection(&vram_crit_sect);
                   2297:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2298:                        vram_flush_char();
                   2299:                }
                   2300:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2301:                        vram_flush_attr();
                   2302:                }
                   2303:                vram_last_length_char = vram_length_char;
                   2304:                vram_last_length_attr = vram_length_attr;
                   2305:                LeaveCriticalSection(&vram_crit_sect);
                   2306:                // this is about half the maximum keyboard repeat rate - any
                   2307:                // lower tends to be jerky, any higher misses updates
                   2308:                Sleep(15);
                   2309:        }
                   2310:        return 0;
                   2311: }
                   2312: #endif
                   2313: 
1.1.1.45  root     2314: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2315: {
                   2316:        UINT8 header[0x400];
                   2317:        
                   2318:        long position = ftell(fp);
                   2319:        fseek(fp, 0, SEEK_SET);
                   2320:        fread(header, sizeof(header), 1, fp);
                   2321:        fseek(fp, position, SEEK_SET);
                   2322:        
                   2323:        try {
                   2324:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2325:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2326:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2327:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2328:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2329:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2330:                
                   2331:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2332:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2333:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2334:                                return(sectionHeader->PointerToRawData);
                   2335:                        }
                   2336:                }
                   2337:        } catch(...) {
                   2338:        }
                   2339:        return(0);
                   2340: }
                   2341: 
1.1.1.10  root     2342: bool is_started_from_command_prompt()
                   2343: {
1.1.1.58  root     2344:        bool result = false;
1.1.1.60  root     2345:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.58  root     2346:        
1.1.1.18  root     2347:        if(hLibrary) {
                   2348:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2349:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2350:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
1.1.1.58  root     2351:                if(lpfnGetConsoleProcessList) { // Windows XP or later
1.1.1.18  root     2352:                        DWORD pl;
1.1.1.58  root     2353:                        result = (lpfnGetConsoleProcessList(&pl, 1) > 1);
1.1.1.18  root     2354:                        FreeLibrary(hLibrary);
1.1.1.58  root     2355:                        return(result);
1.1.1.18  root     2356:                }
                   2357:                FreeLibrary(hLibrary);
                   2358:        }
                   2359:        
                   2360:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2361:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2362:                DWORD dwParentProcessID = 0;
                   2363:                PROCESSENTRY32 pe32;
                   2364:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2365:                if(Process32First(hSnapshot, &pe32)) {
                   2366:                        do {
                   2367:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2368:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2369:                                        break;
                   2370:                                }
                   2371:                        } while(Process32Next(hSnapshot, &pe32));
                   2372:                }
                   2373:                CloseHandle(hSnapshot);
                   2374:                if(dwParentProcessID != 0) {
                   2375:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2376:                        if(hProcess != NULL) {
                   2377:                                HMODULE hMod;
                   2378:                                DWORD cbNeeded;
                   2379:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2380:                                        char module_name[MAX_PATH];
1.1.1.60  root     2381:                                        if(GetModuleBaseNameA(hProcess, hMod, module_name, sizeof(module_name))) {
1.1.1.58  root     2382:                                                result = (_strnicmp(module_name, "cmd.exe", 7) == 0);
1.1.1.18  root     2383:                                        }
                   2384:                                }
                   2385:                                CloseHandle(hProcess);
                   2386:                        }
                   2387:                }
                   2388:        }
1.1.1.58  root     2389:        return(result);
1.1.1.14  root     2390: }
                   2391: 
                   2392: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2393: {
1.1.1.60  root     2394:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.14  root     2395:        
1.1.1.60  root     2396:        if(hLibrary) {
                   2397:                typedef ULONGLONG (WINAPI* VerSetConditionMaskFunction)(ULONGLONG, DWORD, BYTE);
                   2398:                typedef BOOL(WINAPI* VerifyVersionInfoFunction)(LPOSVERSIONINFOEXA, DWORD, DWORDLONG);
                   2399:                
                   2400:                VerSetConditionMaskFunction lpfnVerSetConditionMask = reinterpret_cast<VerSetConditionMaskFunction>(::GetProcAddress(hLibrary, "VerSetConditionMask"));
                   2401:                VerifyVersionInfoFunction lpfnVerifyVersionInfo = reinterpret_cast<VerifyVersionInfoFunction>(::GetProcAddress(hLibrary, "VerifyVersionInfoA"));
                   2402:                
                   2403:                if(lpfnVerSetConditionMask && lpfnVerifyVersionInfo) { // Windows 2000 or later
                   2404:                        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
                   2405:                        OSVERSIONINFOEXA osvi;
                   2406:                        DWORDLONG dwlConditionMask = 0;
                   2407:                        int op = VER_GREATER_EQUAL;
                   2408:                        
                   2409:                        // Initialize the OSVERSIONINFOEXA structure.
                   2410:                        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEXA));
                   2411:                        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXA);
                   2412:                        osvi.dwMajorVersion = dwMajorVersion;
                   2413:                        osvi.dwMinorVersion = dwMinorVersion;
                   2414:                        osvi.wServicePackMajor = wServicePackMajor;
                   2415:                        osvi.wServicePackMinor = wServicePackMinor;
                   2416:                        
                   2417:                         // Initialize the condition mask.
                   2418:                        #define MY_VER_SET_CONDITION(_m_,_t_,_c_) ((_m_)=lpfnVerSetConditionMask((_m_),(_t_),(_c_)))
                   2419:                        
                   2420:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2421:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2422:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2423:                        MY_VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2424:                        
                   2425:                        // Perform the test.
                   2426:                        BOOL result = lpfnVerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2427:                        FreeLibrary(hLibrary);
                   2428:                        return(result);
                   2429:                }
                   2430:                FreeLibrary(hLibrary);
                   2431:        }
                   2432:        
                   2433:        OSVERSIONINFOA osvi;
                   2434:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
                   2435:        
                   2436:        if(GetVersionExA((LPOSVERSIONINFOA)&osvi)) {
                   2437:                if(osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) {
                   2438:                        return(false);
                   2439:                } else if(osvi.dwMajorVersion > dwMajorVersion) {
                   2440:                        return(true);
                   2441:                } else if(osvi.dwMajorVersion < dwMajorVersion) {
                   2442:                        return(false);
                   2443:                } else if(osvi.dwMinorVersion > dwMinorVersion) {
                   2444:                        return(true);
                   2445:                } else if(osvi.dwMinorVersion < dwMinorVersion) {
                   2446:                        return(false);
                   2447:                }
                   2448:                // FIXME: check wServicePackMajor and wServicePackMinor :-(
                   2449:                return(true);
                   2450:        }
                   2451:        return(false);
1.1.1.14  root     2452: }
                   2453: 
1.1.1.61  root     2454: HWND get_console_window_handle()
1.1.1.58  root     2455: {
1.1.1.61  root     2456:        static HWND hwndFound = 0;
                   2457:        
                   2458:        if(hwndFound == 0) {
                   2459:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   2460:                char pszNewWindowTitle[1024];
                   2461:                char pszOldWindowTitle[1024];
                   2462:                
                   2463:                GetConsoleTitleA(pszOldWindowTitle, 1024);
                   2464:                wsprintfA(pszNewWindowTitle, "%d/%d", GetTickCount(), GetCurrentProcessId());
                   2465:                SetConsoleTitleA(pszNewWindowTitle);
                   2466:                Sleep(100);
                   2467:                hwndFound = FindWindowA(NULL, pszNewWindowTitle);
                   2468:                SetConsoleTitleA(pszOldWindowTitle);
                   2469:        }
                   2470:        return hwndFound;
                   2471: }
                   2472: 
                   2473: HDC get_console_window_device_context()
                   2474: {
                   2475:        return GetDC(get_console_window_handle());
                   2476: }
                   2477: 
                   2478: bool get_console_font_size(int *width, int *height)
                   2479: {
                   2480:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.58  root     2481:        bool result = false;
                   2482:        
1.1.1.62  root     2483:        if(is_winxp_or_later) {
                   2484:                HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
                   2485:                if(hLibrary) {
                   2486:                        typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
                   2487:                        GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
                   2488:                        if(lpfnGetCurrentConsoleFont) { // Windows XP or later
                   2489:                                CONSOLE_FONT_INFO fi;
                   2490:                                if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi) != 0) {
                   2491:                                        *width  = fi.dwFontSize.X;
                   2492:                                        *height = fi.dwFontSize.Y;
                   2493:                                        result = true;
                   2494:                                }
1.1.1.58  root     2495:                        }
1.1.1.62  root     2496:                        FreeLibrary(hLibrary);
                   2497:                }
                   2498:        } else {
                   2499:                CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2500:                RECT rect;
                   2501:                if(GetConsoleScreenBufferInfo(hStdout, &csbi) && GetClientRect(get_console_window_handle(), &rect)) {
                   2502:                        int cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
                   2503:                        int rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2504:                        *width  = rect.right / cols;
                   2505:                        *height = rect.bottom / rows;
                   2506:                        result = true;
1.1.1.58  root     2507:                }
                   2508:        }
                   2509:        return(result);
                   2510: }
                   2511: 
1.1.1.61  root     2512: bool set_console_font_size(int width, int height)
1.1.1.56  root     2513: {
                   2514:        // http://d.hatena.ne.jp/aharisu/20090427/1240852598
1.1.1.61  root     2515:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.56  root     2516:        bool result = false;
1.1.1.60  root     2517:        HMODULE hLibrary = LoadLibraryA("Kernel32.dll");
1.1.1.56  root     2518:        
                   2519:        if(hLibrary) {
1.1.1.62  root     2520:                CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2521:                RECT rect;
                   2522:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   2523:                int cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
                   2524:                int rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2525:                
1.1.1.56  root     2526:                typedef BOOL (WINAPI* GetConsoleFontInfoFunction)(HANDLE, BOOL, DWORD, PCONSOLE_FONT_INFO);
                   2527:                typedef DWORD (WINAPI* GetNumberOfConsoleFontsFunction)(VOID);
1.1.1.60  root     2528:                typedef COORD (WINAPI* GetConsoleFontSizeFunction)(HANDLE, DWORD);
1.1.1.56  root     2529:                typedef BOOL (WINAPI* SetConsoleFontFunction)(HANDLE, DWORD);
                   2530:                typedef BOOL (WINAPI* GetCurrentConsoleFontFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFO);
1.1.1.61  root     2531:                typedef BOOL (WINAPI* GetCurrentConsoleFontExFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
                   2532:                typedef BOOL (WINAPI* SetCurrentConsoleFontExFunction)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
1.1.1.56  root     2533:                
                   2534:                GetConsoleFontInfoFunction lpfnGetConsoleFontInfo = reinterpret_cast<GetConsoleFontInfoFunction>(::GetProcAddress(hLibrary, "GetConsoleFontInfo"));
                   2535:                GetNumberOfConsoleFontsFunction lpfnGetNumberOfConsoleFonts = reinterpret_cast<GetNumberOfConsoleFontsFunction>(::GetProcAddress(hLibrary, "GetNumberOfConsoleFonts"));
1.1.1.60  root     2536:                GetConsoleFontSizeFunction lpfnGetConsoleFontSize = reinterpret_cast<GetConsoleFontSizeFunction>(::GetProcAddress(hLibrary, "GetConsoleFontSize"));
1.1.1.56  root     2537:                SetConsoleFontFunction lpfnSetConsoleFont = reinterpret_cast<SetConsoleFontFunction>(::GetProcAddress(hLibrary, "SetConsoleFont"));
                   2538:                GetCurrentConsoleFontFunction lpfnGetCurrentConsoleFont = reinterpret_cast<GetCurrentConsoleFontFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFont"));
1.1.1.61  root     2539:                GetCurrentConsoleFontExFunction lpfnGetCurrentConsoleFontEx = reinterpret_cast<GetCurrentConsoleFontExFunction>(::GetProcAddress(hLibrary, "GetCurrentConsoleFontEx"));
                   2540:                SetCurrentConsoleFontExFunction lpfnSetCurrentConsoleFontEx = reinterpret_cast<SetCurrentConsoleFontExFunction>(::GetProcAddress(hLibrary, "SetCurrentConsoleFontEx"));
1.1.1.56  root     2541:                
1.1.1.62  root     2542:                if(lpfnGetConsoleFontInfo && lpfnGetNumberOfConsoleFonts && lpfnSetConsoleFont) { // Windows 2000 or later
1.1.1.56  root     2543:                        DWORD dwFontNum = lpfnGetNumberOfConsoleFonts();
1.1.1.61  root     2544:                        if(dwFontNum) {
                   2545:                                CONSOLE_FONT_INFO* fonts = (CONSOLE_FONT_INFO*)malloc(sizeof(CONSOLE_FONT_INFO) * dwFontNum);
                   2546:                                lpfnGetConsoleFontInfo(hStdout, FALSE, dwFontNum, fonts);
                   2547:                                for(int i = 0; i < dwFontNum; i++) {
                   2548:                                        fonts[i].dwFontSize = lpfnGetConsoleFontSize(hStdout, fonts[i].nFont);
                   2549:                                        if(fonts[i].dwFontSize.X == width && fonts[i].dwFontSize.Y == height) {
1.1.1.62  root     2550:                                                if(lpfnSetConsoleFont(hStdout, fonts[i].nFont)) {
                   2551:                                                        if(is_winxp_or_later && lpfnGetCurrentConsoleFont) { // Windows XP or later
                   2552:                                                                CONSOLE_FONT_INFO fi;
                   2553:                                                                if(lpfnGetCurrentConsoleFont(hStdout, FALSE, &fi)) {
                   2554:                                                                        if(fonts[i].dwFontSize.X == fi.dwFontSize.X && fonts[i].dwFontSize.Y == fi.dwFontSize.Y) {
                   2555:                                                                                result = true;
                   2556:                                                                                break;
                   2557:                                                                        }
                   2558:                                                                }
                   2559:                                                        } else {
                   2560:                                                                Sleep(10);
                   2561:                                                                if(GetClientRect(get_console_window_handle(), &rect)) {
                   2562:                                                                        if(fonts[i].dwFontSize.X * cols == rect.right && fonts[i].dwFontSize.Y * rows == rect.bottom) {
                   2563:                                                                                result = true;
                   2564:                                                                                break;
                   2565:                                                                        }
                   2566:                                                                }
1.1.1.58  root     2567:                                                        }
                   2568:                                                }
1.1.1.61  root     2569:                                        }
                   2570:                                }
                   2571:                                free(fonts);
                   2572:                        } else if(lpfnGetCurrentConsoleFontEx && lpfnSetCurrentConsoleFontEx) {
                   2573:                                // for Windows10 enhanced command prompt
                   2574:                                CONSOLE_FONT_INFOEX fi_old, fi_new;
                   2575:                                fi_old.cbSize = sizeof(CONSOLE_FONT_INFOEX);
                   2576:                                if(lpfnGetCurrentConsoleFontEx(hStdout, FALSE, &fi_old)) {
                   2577:                                        fi_new = fi_old;
                   2578:                                        fi_new.dwFontSize.X = width;
                   2579:                                        fi_new.dwFontSize.Y = height;
                   2580:                                        if(lpfnSetCurrentConsoleFontEx(hStdout, FALSE, &fi_new)) {
                   2581:                                                lpfnGetCurrentConsoleFontEx(hStdout, FALSE, &fi_new);
                   2582:                                                if(fi_new.dwFontSize.X == width && fi_new.dwFontSize.Y == height) {
                   2583:                                                        result = true;
                   2584:                                                } else {
                   2585:                                                        lpfnSetCurrentConsoleFontEx(hStdout, FALSE, &fi_old);
1.1.1.58  root     2586:                                                }
                   2587:                                        }
1.1.1.57  root     2588:                                }
1.1.1.56  root     2589:                        }
                   2590:                }
                   2591:                FreeLibrary(hLibrary);
                   2592:        }
                   2593:        return(result);
                   2594: }
                   2595: 
1.1.1.59  root     2596: bool is_cursor_blink_off()
                   2597: {
                   2598:        static int result = -1;
                   2599:        HKEY hKey;
                   2600:        char chData[64];
                   2601:        DWORD dwSize = sizeof(chData);
                   2602:        
                   2603:        if(result == -1) {
                   2604:                result = 0;
1.1.1.60  root     2605:                if(RegOpenKeyExA(HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   2606:                        if(RegQueryValueExA(hKey, "CursorBlinkRate", NULL, NULL, (LPBYTE)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.59  root     2607:                                if(strncmp(chData, "-1", 2) == 0) {
                   2608:                                        result = 1;
                   2609:                                }
                   2610:                        }
                   2611:                        RegCloseKey(hKey);
                   2612:                }
                   2613:        }
                   2614:        return(result != 0);
                   2615: }
                   2616: 
1.1.1.27  root     2617: void get_sio_port_numbers()
                   2618: {
                   2619:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2620:        HDEVINFO hDevInfo = 0;
                   2621:        HKEY hKey = 0;
1.1.1.60  root     2622:        if((hDevInfo = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
1.1.1.27  root     2623:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2624:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2625:                                char chData[256];
                   2626:                                DWORD dwType = 0;
                   2627:                                DWORD dwSize = sizeof(chData);
                   2628:                                int port_number = 0;
                   2629:                                
1.1.1.60  root     2630:                                if(RegQueryValueExA(hKey, "PortName", NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
1.1.1.27  root     2631:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2632:                                                port_number = atoi(chData + 3);
                   2633:                                        }
                   2634:                                }
                   2635:                                RegCloseKey(hKey);
                   2636:                                
1.1.1.29  root     2637:                                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     2638:                                        continue;
                   2639:                                }
                   2640:                                if(sio_port_number[0] == 0) {
                   2641:                                        sio_port_number[0] = port_number;
                   2642:                                } else if(sio_port_number[1] == 0) {
                   2643:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2644:                                } else if(sio_port_number[2] == 0) {
                   2645:                                        sio_port_number[2] = port_number;
                   2646:                                } else if(sio_port_number[3] == 0) {
                   2647:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2648:                                }
1.1.1.29  root     2649:                                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     2650:                                        break;
                   2651:                                }
                   2652:                        }
                   2653:                }
                   2654:        }
                   2655: }
                   2656: 
1.1.1.28  root     2657: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2658: 
1.1       root     2659: int main(int argc, char *argv[], char *envp[])
                   2660: {
1.1.1.9   root     2661:        int arg_offset = 0;
                   2662:        int standard_env = 0;
1.1.1.14  root     2663:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2664:        bool get_console_info_success = false;
1.1.1.56  root     2665:        bool get_console_font_success = false;
1.1.1.28  root     2666:        bool screen_size_changed = false;
                   2667:        
1.1.1.60  root     2668:        char path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2669:        GetModuleFileNameA(NULL, path, MAX_PATH);
                   2670:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1       root     2671:        
1.1.1.27  root     2672:        char dummy_argv_0[] = "msdos.exe";
                   2673:        char dummy_argv_1[MAX_PATH];
                   2674:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2675:        char new_exec_file[MAX_PATH];
                   2676:        bool convert_cmd_file = false;
1.1.1.28  root     2677:        unsigned int code_page = 0;
1.1.1.27  root     2678:        
                   2679:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2680:                // check if command file is embedded to this execution file
                   2681:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2682:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2683:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2684:                if(offset != 0) {
1.1.1.30  root     2685:                        UINT8 buffer[16];
1.1.1.28  root     2686:                        fseek(fp, offset, SEEK_SET);
                   2687:                        fread(buffer, sizeof(buffer), 1, fp);
                   2688:                        
                   2689:                        // restore flags
                   2690:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2691:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2692:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2693:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2694:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2695:                        if((buffer[0] & 0x20) != 0) {
                   2696:                                get_sio_port_numbers();
                   2697:                        }
                   2698:                        if((buffer[0] & 0x40) != 0) {
                   2699:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2700:                                support_ems = true;
1.1.1.30  root     2701:                        }
1.1.1.27  root     2702: #ifdef SUPPORT_XMS
1.1.1.30  root     2703:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2704:                                support_xms = true;
                   2705:                        }
1.1.1.30  root     2706: #endif
1.1.1.28  root     2707:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2708:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2709:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2710:                        }
                   2711:                        if(buffer[5] != 0) {
1.1.1.30  root     2712:                                dos_major_version = buffer[5];
                   2713:                                dos_minor_version = buffer[6];
                   2714:                        }
                   2715:                        if(buffer[7] != 0) {
                   2716:                                win_major_version = buffer[7];
                   2717:                                win_minor_version = buffer[8];
1.1.1.28  root     2718:                        }
1.1.1.30  root     2719:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2720:                                SetConsoleCP(code_page);
                   2721:                                SetConsoleOutputCP(code_page);
                   2722:                        }
1.1.1.30  root     2723:                        int name_len = buffer[11];
                   2724:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2725:                        
                   2726:                        // restore command file name
                   2727:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2728:                        fread(dummy_argv_1, name_len, 1, fp);
                   2729:                        
                   2730:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2731:                                // if original command file exists, create a temporary file name
1.1.1.60  root     2732:                                if(GetTempFileNameA(".", "DOS", 0, dummy_argv_1) != 0) {
1.1.1.28  root     2733:                                        // create a temporary command file in the current director
1.1.1.60  root     2734:                                        DeleteFileA(dummy_argv_1);
1.1.1.27  root     2735:                                } else {
1.1.1.28  root     2736:                                        // create a temporary command file in the temporary folder
1.1.1.60  root     2737:                                        GetTempPathA(MAX_PATH, path);
                   2738:                                        if(GetTempFileNameA(path, "DOS", 0, dummy_argv_1) != 0) {
                   2739:                                                DeleteFileA(dummy_argv_1);
1.1.1.28  root     2740:                                        } else {
                   2741:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2742:                                        }
1.1.1.27  root     2743:                                }
1.1.1.28  root     2744:                                // check the command file type
                   2745:                                fread(buffer, 2, 1, fp);
                   2746:                                fseek(fp, -2, SEEK_CUR);
                   2747:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2748:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2749:                                } else {
                   2750:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2751:                                }
                   2752:                        }
1.1.1.28  root     2753:                        
                   2754:                        // restore command file
                   2755:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2756:                        for(int i = 0; i < file_len; i++) {
                   2757:                                fputc(fgetc(fp), fo);
                   2758:                        }
                   2759:                        fclose(fo);
                   2760:                        
1.1.1.60  root     2761:                        GetFullPathNameA(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
1.1.1.28  root     2762:                        temp_file_created = true;
1.1.1.60  root     2763:                        SetFileAttributesA(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
1.1.1.28  root     2764:                        
                   2765:                        // adjust argc/argv
                   2766:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2767:                                dummy_argv[i + 1] = argv[i];
                   2768:                        }
                   2769:                        argc++;
                   2770:                        argv = dummy_argv;
1.1.1.27  root     2771:                }
                   2772:                fclose(fp);
                   2773:        }
1.1.1.9   root     2774:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2775:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2776:                        stay_busy = true;
                   2777:                        arg_offset++;
1.1.1.27  root     2778:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2779:                        if(argv[i][2] != '\0') {
                   2780:                                strcpy(new_exec_file, &argv[i][2]);
                   2781:                        } else {
                   2782:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2783:                        }
                   2784:                        convert_cmd_file = true;
                   2785:                        arg_offset++;
1.1.1.28  root     2786:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2787:                        if(IS_NUMERIC(argv[i][2])) {
                   2788:                                code_page = atoi(&argv[i][2]);
                   2789:                        } else {
                   2790:                                code_page = GetConsoleCP();
                   2791:                        }
                   2792:                        arg_offset++;
1.1.1.25  root     2793:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2794:                        no_windows = true;
                   2795:                        arg_offset++;
                   2796:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2797:                        standard_env = 1;
                   2798:                        arg_offset++;
1.1.1.14  root     2799:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2800:                        ignore_illegal_insn = true;
                   2801:                        arg_offset++;
                   2802:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2803:                        limit_max_memory = true;
                   2804:                        arg_offset++;
                   2805:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51  root     2806:                        int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
                   2807:                        if(result == 1) {
                   2808:                                buf_width = 0;
                   2809:                        } else if(result != 2) {
1.1.1.17  root     2810:                                buf_width = buf_height = 0;
                   2811:                        }
                   2812:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2813:                                buf_width = 80;
                   2814:                        }
                   2815:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2816:                                buf_height = 25;
                   2817:                        }
1.1.1.14  root     2818:                        arg_offset++;
1.1.1.25  root     2819:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2820:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2821:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2822:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2823:                                        sio_port_number[1] = atoi(p1 + 1);
                   2824:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2825:                                                sio_port_number[2] = atoi(p2 + 1);
                   2826:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2827:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2828:                                                }
                   2829:                                        }
1.1.1.25  root     2830:                                }
1.1.1.29  root     2831:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2832:                        }
1.1.1.29  root     2833:                        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     2834:                                get_sio_port_numbers();
1.1.1.25  root     2835:                        }
                   2836:                        arg_offset++;
1.1.1.9   root     2837:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2838:                        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     2839:                                dos_major_version = argv[i][2] - '0';
                   2840:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2841:                        }
                   2842:                        arg_offset++;
                   2843:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2844:                        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]))) {
                   2845:                                win_major_version = argv[i][2] - '0';
                   2846:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2847:                        }
                   2848:                        arg_offset++;
1.1.1.25  root     2849:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2850:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2851:                        support_ems = true;
                   2852: #ifdef SUPPORT_XMS
                   2853:                        support_xms = true;
                   2854: #endif
                   2855:                        arg_offset++;
1.1.1.9   root     2856:                } else {
                   2857:                        break;
                   2858:                }
                   2859:        }
                   2860:        if(argc < 2 + arg_offset) {
1.1       root     2861: #ifdef _WIN64
1.1.1.14  root     2862:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2863: #else
1.1.1.14  root     2864:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2865: #endif
1.1.1.25  root     2866:                fprintf(stderr,
1.1.1.28  root     2867:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2868:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2869:                        "\n"
                   2870:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2871: #ifdef _WIN64
1.1.1.27  root     2872:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2873: #else
1.1.1.27  root     2874:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2875: #endif
1.1.1.28  root     2876:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2877:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2878:                        "\t-e\tuse a reduced environment block\n"
                   2879:                        "\t-i\tignore invalid instructions\n"
                   2880:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2881:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2882:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2883:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2884:                        "\t-w\tset the Windows version\n"
1.1.1.63  root     2885: #if defined(SUPPORT_VCPI)
                   2886:                        "\t-x\tenable LIM EMS, VCPI, and XMS\n"
                   2887: #elif defined(SUPPORT_XMS)
                   2888:                        "\t-x\tenable LIM EMS and XMS\n"
1.1.1.19  root     2889: #else
1.1.1.28  root     2890:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2891: #endif
                   2892:                );
1.1.1.10  root     2893:                
                   2894:                if(!is_started_from_command_prompt()) {
                   2895:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2896:                        while(!_kbhit()) {
                   2897:                                Sleep(10);
                   2898:                        }
                   2899:                }
1.1.1.20  root     2900: #ifdef _DEBUG
                   2901:                _CrtDumpMemoryLeaks();
                   2902: #endif
1.1       root     2903:                return(EXIT_FAILURE);
                   2904:        }
1.1.1.27  root     2905:        if(convert_cmd_file) {
                   2906:                retval = EXIT_FAILURE;
1.1.1.28  root     2907:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2908:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2909:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2910:                        
1.1.1.28  root     2911:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2912:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2913:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2914:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2915:                        } else {
1.1.1.28  root     2916:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2917:                                if(offset != 0) {
                   2918:                                        UINT8 buffer[14];
                   2919:                                        fseek(fp, offset, SEEK_SET);
                   2920:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2921:                                        memset(path, 0, sizeof(path));
                   2922:                                        fread(path, buffer[9], 1, fp);
                   2923:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2924:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2925:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2926:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2927:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2928:                                } else {
                   2929:                                        // read pe header of msdos.exe
                   2930:                                        UINT8 header[0x400];
                   2931:                                        fseek(fp, 0, SEEK_SET);
                   2932:                                        fread(header, sizeof(header), 1, fp);
                   2933:                                        
                   2934:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2935:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2936:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2937:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2938:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2939:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2940:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2941:                                        
                   2942:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2943:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2944:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2945:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2946:                                        if(dwExtraLastSectionBytes != 0) {
                   2947:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2948:                                                dwLastSectionSize += dwRemain;
                   2949:                                        }
                   2950:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2951:                                        
                   2952:                                        // store msdos.exe
                   2953:                                        fseek(fp, 0, SEEK_SET);
                   2954:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2955:                                                if((data = fgetc(fp)) != EOF) {
                   2956:                                                        fputc(data, fo);
                   2957:                                                } else {
                   2958:                                                        // we should not reach here :-(
                   2959:                                                        fputc(0, fo);
                   2960:                                                }
                   2961:                                        }
                   2962:                                        
                   2963:                                        // store options
                   2964:                                        UINT8 flags = 0;
                   2965:                                        if(stay_busy) {
                   2966:                                                flags |= 0x01;
                   2967:                                        }
                   2968:                                        if(no_windows) {
                   2969:                                                flags |= 0x02;
                   2970:                                        }
                   2971:                                        if(standard_env) {
                   2972:                                                flags |= 0x04;
                   2973:                                        }
                   2974:                                        if(ignore_illegal_insn) {
                   2975:                                                flags |= 0x08;
                   2976:                                        }
                   2977:                                        if(limit_max_memory) {
                   2978:                                                flags |= 0x10;
                   2979:                                        }
1.1.1.29  root     2980:                                        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     2981:                                                flags |= 0x20;
                   2982:                                        }
                   2983:                                        if(support_ems) {
                   2984:                                                flags |= 0x40;
                   2985:                                        }
1.1.1.30  root     2986: #ifdef SUPPORT_XMS
                   2987:                                        if(support_xms) {
                   2988:                                                flags |= 0x80;
                   2989:                                        }
                   2990: #endif
1.1.1.28  root     2991:                                        
                   2992:                                        fputc(flags, fo);
                   2993:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2994:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2995:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2996:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2997:                                        fputc(dos_major_version, fo);
                   2998:                                        fputc(dos_minor_version, fo);
                   2999:                                        fputc(win_major_version, fo);
                   3000:                                        fputc(win_minor_version, fo);
1.1.1.28  root     3001:                                        fputc((code_page >> 0) & 0xff, fo);
                   3002:                                        fputc((code_page >> 8) & 0xff, fo);
                   3003:                                        
                   3004:                                        // store command file info
1.1.1.60  root     3005:                                        GetFullPathNameA(argv[arg_offset + 1], MAX_PATH, full, &name);
1.1.1.28  root     3006:                                        int name_len = strlen(name);
                   3007:                                        fseek(fs, 0, SEEK_END);
                   3008:                                        long file_size = ftell(fs);
                   3009:                                        
                   3010:                                        fputc(name_len, fo);
                   3011:                                        fputc((file_size >>  0) & 0xff, fo);
                   3012:                                        fputc((file_size >>  8) & 0xff, fo);
                   3013:                                        fputc((file_size >> 16) & 0xff, fo);
                   3014:                                        fputc((file_size >> 24) & 0xff, fo);
                   3015:                                        fwrite(name, name_len, 1, fo);
                   3016:                                        
                   3017:                                        // store command file
                   3018:                                        fseek(fs, 0, SEEK_SET);
                   3019:                                        for(int i = 0; i < file_size; i++) {
                   3020:                                                if((data = fgetc(fs)) != EOF) {
                   3021:                                                        fputc(data, fo);
                   3022:                                                } else {
                   3023:                                                        // we should not reach here :-(
                   3024:                                                        fputc(0, fo);
                   3025:                                                }
                   3026:                                        }
                   3027:                                        
                   3028:                                        // store padding data and update pe header
1.1.1.29  root     3029:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   3030:                                        coffHeader->NumberOfSections++;
                   3031:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   3032:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   3033:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   3034:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   3035:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     3036:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   3037:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   3038:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     3039:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     3040:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   3041:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     3042:                                                        if(i < 2) {
                   3043:                                                                fputc(padding[i & 15], fo);
                   3044:                                                        } else {
                   3045:                                                                fputc(padding[(i - 2) & 15], fo);
                   3046:                                                        }
1.1.1.28  root     3047:                                                }
                   3048:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   3049:                                        }
                   3050:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   3051:                                        
                   3052:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   3053:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   3054:                                        if(dwExtraNewSectionBytes != 0) {
                   3055:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   3056:                                                dwNewSectionSize += dwRemain;
                   3057:                                        }
                   3058:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   3059:                                        
                   3060:                                        fseek(fo, 0, SEEK_SET);
                   3061:                                        fwrite(header, sizeof(header), 1, fo);
                   3062:                                        
                   3063:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   3064:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     3065:                                }
                   3066:                        }
                   3067:                        if(fp != NULL) {
                   3068:                                fclose(fp);
                   3069:                        }
                   3070:                        if(fs != NULL) {
                   3071:                                fclose(fs);
                   3072:                        }
                   3073:                        if(fo != NULL) {
                   3074:                                fclose(fo);
                   3075:                        }
                   3076:                }
                   3077: #ifdef _DEBUG
                   3078:                _CrtDumpMemoryLeaks();
                   3079: #endif
                   3080:                return(retval);
                   3081:        }
1.1       root     3082:        
1.1.1.62  root     3083:        is_winxp_or_later = is_greater_windows_version(5, 1, 0, 0);
1.1.1.54  root     3084:        is_xp_64_or_later = is_greater_windows_version(5, 2, 0, 0);
1.1.1.14  root     3085:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   3086:        
1.1.1.23  root     3087:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     3088:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     3089:        CONSOLE_CURSOR_INFO ci;
1.1.1.61  root     3090:        UINT input_cp = GetConsoleCP();
                   3091:        UINT output_cp = GetConsoleOutputCP();
                   3092:        int multibyte_cp = _getmbcp();
1.1.1.23  root     3093:        
1.1.1.28  root     3094:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     3095:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.59  root     3096:        ci_old = ci_new = ci;
1.1.1.24  root     3097:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1.1.61  root     3098:        get_console_font_success = get_console_font_size(&font_width, &font_height);
1.1       root     3099:        
1.1.1.14  root     3100:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   3101:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   3102:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3103:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     3104:                }
                   3105:        }
1.1.1.28  root     3106:        if(get_console_info_success) {
1.1.1.12  root     3107:                scr_width = csbi.dwSize.X;
1.1.1.14  root     3108:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   3109:                
1.1.1.28  root     3110:                // v-text shadow buffer size must be lesser than 0x7fd0
                   3111:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     3112:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   3113:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   3114:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     3115:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     3116:                                scr_width = 80;
                   3117:                                scr_height = 25;
                   3118:                        }
1.1.1.28  root     3119:                        screen_size_changed = true;
1.1.1.14  root     3120:                }
1.1.1.12  root     3121:        } else {
                   3122:                // for a proof (not a console)
                   3123:                scr_width = 80;
                   3124:                scr_height = 25;
                   3125:        }
1.1.1.14  root     3126:        scr_buf_size.X = scr_width;
                   3127:        scr_buf_size.Y = scr_height;
                   3128:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   3129:        scr_top = csbi.srWindow.Top;
1.1       root     3130:        cursor_moved = false;
1.1.1.59  root     3131:        cursor_moved_by_crtc = false;
1.1       root     3132:        
1.1.1.54  root     3133:        SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
                   3134:        
1.1.1.35  root     3135: #ifdef USE_SERVICE_THREAD
                   3136:        InitializeCriticalSection(&input_crit_sect);
                   3137:        InitializeCriticalSection(&key_buf_crit_sect);
                   3138:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     3139:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     3140: #endif
1.1.1.50  root     3141:        
1.1.1.25  root     3142:        key_buf_char = new FIFO(256);
                   3143:        key_buf_scan = new FIFO(256);
1.1.1.57  root     3144:        key_buf_data = new FIFO(256);
1.1       root     3145:        
                   3146:        hardware_init();
                   3147:        
1.1.1.33  root     3148: #ifdef USE_DEBUGGER
                   3149:        debugger_init();
                   3150: #endif
                   3151:        
1.1.1.9   root     3152:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     3153:                retval = EXIT_FAILURE;
                   3154:        } else {
1.1.1.27  root     3155: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     3156:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   3157: #endif
                   3158:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   3159:                
1.1.1.28  root     3160:                if(screen_size_changed) {
1.1.1.24  root     3161:                        change_console_size(scr_width, scr_height);
                   3162:                }
1.1.1.8   root     3163:                TIMECAPS caps;
                   3164:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   3165:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     3166: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3167:                InitializeCriticalSection(&vram_crit_sect);
                   3168:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   3169: #endif
1.1.1.33  root     3170: #ifdef USE_DEBUGGER
                   3171:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   3172:                // wait until telnet client starts and connects to me
                   3173:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   3174:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   3175:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   3176:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   3177:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   3178:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   3179:                                Sleep(100);
                   3180:                        }
                   3181:                }
                   3182: #endif
1.1       root     3183:                hardware_run();
1.1.1.35  root     3184: #ifdef USE_VRAM_THREAD
1.1.1.14  root     3185:                vram_flush();
                   3186:                DeleteCriticalSection(&vram_crit_sect);
                   3187: #endif
1.1.1.24  root     3188:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     3189:                
1.1.1.24  root     3190:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.61  root     3191:                hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   3192:                
                   3193:                // restore console settings
                   3194:                _setmbcp(multibyte_cp);
                   3195:                SetConsoleCP(input_cp);
                   3196:                SetConsoleOutputCP(multibyte_cp);
                   3197:                
1.1.1.28  root     3198:                if(get_console_info_success) {
1.1.1.12  root     3199:                        if(restore_console_on_exit) {
1.1.1.14  root     3200:                                // window can't be bigger than buffer,
                   3201:                                // buffer can't be smaller than window,
                   3202:                                // so make a tiny window,
                   3203:                                // set the required buffer,
                   3204:                                // then set the required window
1.1.1.61  root     3205:                                CONSOLE_SCREEN_BUFFER_INFO cur_csbi;
1.1.1.14  root     3206:                                SMALL_RECT rect;
1.1.1.61  root     3207:                                GetConsoleScreenBufferInfo(hStdout, &cur_csbi);
                   3208:                                int min_width  = min(cur_csbi.srWindow.Right - cur_csbi.srWindow.Left + 1, csbi.srWindow.Right - csbi.srWindow.Left + 1);
                   3209:                                int min_height = min(cur_csbi.srWindow.Bottom - cur_csbi.srWindow.Top + 1, csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
                   3210:                                
                   3211:                                SET_RECT(rect, 0, cur_csbi.srWindow.Top, min_width - 1, cur_csbi.srWindow.Top + min_height - 1);
1.1.1.14  root     3212:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     3213:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     3214:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.61  root     3215:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3216:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3217:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3218:                                }
                   3219:                        }
                   3220:                }
                   3221:                if(get_console_font_success) {
                   3222:                        set_console_font_size(font_width, font_height);
                   3223:                }
                   3224:                if(get_console_info_success) {
                   3225:                        if(restore_console_on_exit) {
                   3226:                                SMALL_RECT rect;
                   3227:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
                   3228:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
                   3229:                                if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3230:                                        SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3231:                                        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3232:                                }
1.1.1.12  root     3233:                        }
1.1.1.14  root     3234:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   3235:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     3236:                }
1.1.1.64  root     3237:                if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
                   3238:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
                   3239:                } else {
                   3240:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   3241:                }
1.1.1.24  root     3242:                
1.1       root     3243:                msdos_finish();
1.1.1.14  root     3244:                
                   3245:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     3246:        }
1.1.1.35  root     3247:        if(temp_file_created) {
1.1.1.60  root     3248:                DeleteFileA(temp_file_path);
1.1.1.35  root     3249:                temp_file_created = false;
                   3250:        }
1.1.1.10  root     3251:        hardware_finish();
                   3252:        
1.1.1.28  root     3253:        if(key_buf_char != NULL) {
                   3254:                key_buf_char->release();
                   3255:                delete key_buf_char;
                   3256:                key_buf_char = NULL;
                   3257:        }
                   3258:        if(key_buf_scan != NULL) {
                   3259:                key_buf_scan->release();
                   3260:                delete key_buf_scan;
                   3261:                key_buf_scan = NULL;
                   3262:        }
1.1.1.57  root     3263:        if(key_buf_data != NULL) {
                   3264:                key_buf_data->release();
                   3265:                delete key_buf_data;
                   3266:                key_buf_data = NULL;
                   3267:        }
1.1.1.35  root     3268: #ifdef USE_SERVICE_THREAD
                   3269:        DeleteCriticalSection(&input_crit_sect);
                   3270:        DeleteCriticalSection(&key_buf_crit_sect);
                   3271:        DeleteCriticalSection(&putch_crit_sect);
                   3272: #endif
1.1.1.20  root     3273: #ifdef _DEBUG
                   3274:        _CrtDumpMemoryLeaks();
                   3275: #endif
1.1       root     3276:        return(retval);
                   3277: }
                   3278: 
1.1.1.20  root     3279: /* ----------------------------------------------------------------------------
                   3280:        console
                   3281: ---------------------------------------------------------------------------- */
                   3282: 
1.1.1.14  root     3283: void change_console_size(int width, int height)
1.1.1.12  root     3284: {
1.1.1.23  root     3285:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     3286:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   3287:        SMALL_RECT rect;
                   3288:        COORD co;
                   3289:        
                   3290:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     3291:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   3292:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
1.1.1.60  root     3293:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
1.1.1.14  root     3294:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3295:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3296:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   3297:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
1.1.1.60  root     3298:                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     3299:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.60  root     3300:                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     3301:                }
                   3302:        }
1.1.1.14  root     3303:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     3304:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     3305:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     3306:                SetConsoleCursorPosition(hStdout, co);
                   3307:                cursor_moved = true;
1.1.1.59  root     3308:                cursor_moved_by_crtc = false;
1.1.1.12  root     3309:        }
1.1.1.14  root     3310:        
                   3311:        // window can't be bigger than buffer,
                   3312:        // buffer can't be smaller than window,
                   3313:        // so make a tiny window,
                   3314:        // set the required buffer,
                   3315:        // then set the required window
1.1.1.61  root     3316:        int min_width  = min(csbi.srWindow.Right - csbi.srWindow.Left + 1, width);
                   3317:        int min_height = min(csbi.srWindow.Bottom - csbi.srWindow.Top + 1, height);
                   3318:        
                   3319:        SET_RECT(rect, 0, csbi.srWindow.Top, min_width - 1, csbi.srWindow.Top + min_height - 1);
1.1.1.12  root     3320:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     3321:        co.X = width;
                   3322:        co.Y = height;
1.1.1.12  root     3323:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     3324:        SET_RECT(rect, 0, 0, width - 1, height - 1);
1.1.1.61  root     3325:        if(!SetConsoleWindowInfo(hStdout, TRUE, &rect)) {
                   3326:                SetWindowPos(get_console_window_handle(), NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
                   3327:                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   3328:        }
1.1.1.14  root     3329:        
                   3330:        scr_width = scr_buf_size.X = width;
                   3331:        scr_height = scr_buf_size.Y = height;
                   3332:        scr_top = 0;
                   3333:        
                   3334:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   3335:        
                   3336:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     3337:        text_vram_end_address = text_vram_top_address + regen;
                   3338:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   3339:        
1.1.1.14  root     3340:        if(regen > 0x4000) {
                   3341:                regen = 0x8000;
                   3342:                vram_pages = 1;
                   3343:        } else if(regen > 0x2000) {
                   3344:                regen = 0x4000;
                   3345:                vram_pages = 2;
                   3346:        } else if(regen > 0x1000) {
                   3347:                regen = 0x2000;
                   3348:                vram_pages = 4;
                   3349:        } else {
                   3350:                regen = 0x1000;
                   3351:                vram_pages = 8;
                   3352:        }
1.1.1.15  root     3353:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   3354:        *(UINT16 *)(mem + 0x44c) = regen;
                   3355:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   3356:        
1.1.1.24  root     3357:        mouse.min_position.x = 0;
                   3358:        mouse.min_position.y = 0;
1.1.1.34  root     3359:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3360:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3361:        
1.1.1.15  root     3362:        restore_console_on_exit = true;
1.1.1.14  root     3363: }
                   3364: 
                   3365: void clear_scr_buffer(WORD attr)
                   3366: {
                   3367:        for(int y = 0; y < scr_height; y++) {
                   3368:                for(int x = 0; x < scr_width; x++) {
                   3369:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3370:                        SCR_BUF(y,x).Attributes = attr;
                   3371:                }
                   3372:        }
1.1.1.12  root     3373: }
                   3374: 
1.1.1.24  root     3375: bool update_console_input()
1.1       root     3376: {
1.1.1.35  root     3377: #ifdef USE_SERVICE_THREAD
                   3378:        EnterCriticalSection(&input_crit_sect);
                   3379: #endif
1.1.1.23  root     3380:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3381:        DWORD dwNumberOfEvents = 0;
1.1       root     3382:        DWORD dwRead;
                   3383:        INPUT_RECORD ir[16];
1.1.1.24  root     3384:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3385:        bool result = false;
1.1       root     3386:        
1.1.1.8   root     3387:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3388:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3389:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3390:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.59  root     3391:                                        if(ir[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
                   3392:                                                if(mouse.hidden == 0 || (mouse.call_addr_ps2.dw && mouse.enabled_ps2)) {
                   3393:                                                        // NOTE: if restore_console_on_exit, console is not scrolled
                   3394:                                                        if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3395:                                                                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3396:                                                        }
                   3397:                                                        // FIXME: character size is always 8x8 ???
                   3398:                                                        int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3399:                                                        int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3400:                                                        
                   3401:                                                        if(mouse.position.x != x || mouse.position.y != y) {
                   3402:                                                                mouse.position.x = x;
                   3403:                                                                mouse.position.y = y;
                   3404:                                                                mouse.status |= 1;
                   3405:                                                                mouse.status_alt |= 1;
                   3406:                                                        }
1.1.1.34  root     3407:                                                }
1.1.1.59  root     3408:                                        } else if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3409:                                                for(int j = 0; j < MAX_MOUSE_BUTTONS; j++) {
1.1.1.34  root     3410:                                                        static const DWORD bits[] = {
                   3411:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3412:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3413:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3414:                                                        };
1.1.1.59  root     3415:                                                        bool prev_status = mouse.buttons[j].status;
                   3416:                                                        mouse.buttons[j].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[j]) != 0);
1.1.1.34  root     3417:                                                        
1.1.1.59  root     3418:                                                        if(!prev_status && mouse.buttons[j].status) {
                   3419:                                                                mouse.buttons[j].pressed_times++;
                   3420:                                                                mouse.buttons[j].pressed_position.x = mouse.position.x;
                   3421:                                                                mouse.buttons[j].pressed_position.y = mouse.position.x;
                   3422:                                                                if(j < 2) {
                   3423:                                                                        mouse.status_alt |= 2 << (j * 2);
1.1.1.43  root     3424:                                                                }
1.1.1.59  root     3425:                                                                mouse.status |= 2 << (j * 2);
                   3426:                                                        } else if(prev_status && !mouse.buttons[j].status) {
                   3427:                                                                mouse.buttons[j].released_times++;
                   3428:                                                                mouse.buttons[j].released_position.x = mouse.position.x;
                   3429:                                                                mouse.buttons[j].released_position.y = mouse.position.x;
                   3430:                                                                if(j < 2) {
                   3431:                                                                        mouse.status_alt |= 4 << (j * 2);
1.1.1.43  root     3432:                                                                }
1.1.1.59  root     3433:                                                                mouse.status |= 4 << (j * 2);
1.1.1.14  root     3434:                                                        }
                   3435:                                                }
                   3436:                                        }
1.1.1.24  root     3437:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3438:                                        // update keyboard flags in bios data area
1.1.1.35  root     3439:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3440:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3441:                                        } else {
1.1.1.35  root     3442:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3443:                                        }
1.1.1.35  root     3444:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3445:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3446:                                        } else {
1.1.1.35  root     3447:                                                mem[0x417] &= ~0x20;
                   3448:                                        }
                   3449:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3450:                                                mem[0x417] |= 0x10;
                   3451:                                        } else {
                   3452:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3453:                                        }
                   3454:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3455:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3456:                                                        mouse.status_alt |= 0x80;
                   3457:                                                }
1.1.1.33  root     3458:                                                mem[0x417] |= 0x08;
                   3459:                                        } else {
                   3460:                                                mem[0x417] &= ~0x08;
                   3461:                                        }
1.1.1.35  root     3462:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3463:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3464:                                                        mouse.status_alt |= 0x40;
                   3465:                                                }
1.1.1.35  root     3466:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3467:                                        } else {
1.1.1.35  root     3468:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3469:                                        }
                   3470:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3471:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3472:                                                        mouse.status_alt |= 0x20;
                   3473:                                                }
1.1.1.33  root     3474:                                                if(!(mem[0x417] & 0x03)) {
                   3475:                                                        mem[0x417] |= 0x02; // left shift
                   3476:                                                }
                   3477:                                        } else {
                   3478:                                                mem[0x417] &= ~0x03;
                   3479:                                        }
1.1.1.35  root     3480:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3481:                                                mem[0x418] |= 0x02;
                   3482:                                        } else {
                   3483:                                                mem[0x418] &= ~0x02;
                   3484:                                        }
                   3485:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3486:                                                mem[0x418] |= 0x01;
                   3487:                                        } else {
                   3488:                                                mem[0x418] &= ~0x01;
                   3489:                                        }
1.1.1.33  root     3490:                                        
1.1.1.28  root     3491:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.57  root     3492: //                                     kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
                   3493: //                                     kbd_status |= 1;
                   3494:                                        UINT8 tmp_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3495:                                        
                   3496:                                        // update dos key buffer
                   3497:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3498:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3499:                                        UINT8 scn_old = scn;
1.1.1.33  root     3500:                                        
                   3501:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3502:                                                // make
1.1.1.57  root     3503:                                                tmp_data &= 0x7f;
1.1.1.24  root     3504:                                                
1.1.1.33  root     3505:                                                if(chr == 0x00) {
1.1.1.24  root     3506:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3507:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3508:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3509:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3510:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3511:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3512:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3513:                                                                } else if(scn == 0x35) {
                   3514:                                                                        scn = 0xa4;             // keypad /
                   3515:                                                                }
                   3516:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3517:                                                                if(scn == 0x07) {
                   3518:                                                                        chr = 0x1e;     // Ctrl+^
                   3519:                                                                } else if(scn == 0x0c) {
                   3520:                                                                        chr = 0x1f;     // Ctrl+_
                   3521:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3522:                                                                        static const UINT8 ctrl_map[] = {
                   3523:                                                                                0x95,   // keypad /
                   3524:                                                                                0,
                   3525:                                                                                0x96,   // keypad *
                   3526:                                                                                0, 0, 0,
                   3527:                                                                                0x5e,   // F1
                   3528:                                                                                0x5f,   // F2
                   3529:                                                                                0x60,   // F3
                   3530:                                                                                0x61,   // F4
                   3531:                                                                                0x62,   // F5
                   3532:                                                                                0x63,   // F6
                   3533:                                                                                0x64,   // F7
                   3534:                                                                                0x65,   // F8
                   3535:                                                                                0x66,   // F9
                   3536:                                                                                0x67,   // F10
                   3537:                                                                                0,
                   3538:                                                                                0,
                   3539:                                                                                0x77,   // Home
                   3540:                                                                                0x8d,   // Up
                   3541:                                                                                0x84,   // PgUp
                   3542:                                                                                0x8e,   // keypad -
                   3543:                                                                                0x73,   // Left
                   3544:                                                                                0x8f,   // keypad center
                   3545:                                                                                0x74,   // Right
                   3546:                                                                                0x90,   // keyapd +
                   3547:                                                                                0x75,   // End
                   3548:                                                                                0x91,   // Down
                   3549:                                                                                0x76,   // PgDn
                   3550:                                                                                0x92,   // Insert
                   3551:                                                                                0x93,   // Delete
                   3552:                                                                                0, 0, 0,
                   3553:                                                                                0x89,   // F11
                   3554:                                                                                0x8a,   // F12
                   3555:                                                                        };
                   3556:                                                                        scn = ctrl_map[scn - 0x35];
                   3557:                                                                }
                   3558:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3559:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3560:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3561:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3562:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3563:                                                                }
                   3564:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3565:                                                                scn += 0x85 - 0x57;
                   3566:                                                        }
                   3567:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3568:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3569:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3570: #ifdef USE_SERVICE_THREAD
                   3571:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3572: #endif
1.1.1.32  root     3573:                                                                        if(chr == 0) {
1.1.1.51  root     3574:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3575:                                                                        }
1.1.1.51  root     3576:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3577: #ifdef USE_SERVICE_THREAD
                   3578:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3579: #endif
1.1.1.24  root     3580:                                                                }
                   3581:                                                        }
                   3582:                                                } else {
                   3583:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3584:                                                                chr = 0;
                   3585:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3586:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3587:                                                                }
                   3588:                                                        }
1.1.1.32  root     3589:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3590: #ifdef USE_SERVICE_THREAD
                   3591:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3592: #endif
1.1.1.51  root     3593:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3594: #ifdef USE_SERVICE_THREAD
                   3595:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3596: #endif
1.1.1.32  root     3597:                                                        }
1.1.1.24  root     3598:                                                }
1.1.1.57  root     3599:                                        } else {
                   3600:                                                if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3601:                                                        // ctrl-break, ctrl-c
                   3602:                                                        if(scn == 0x46) {
                   3603:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3604: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3605:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3606: #endif
1.1.1.57  root     3607:                                                                        pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3608: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3609:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3610: #endif
1.1.1.57  root     3611:                                                                }
                   3612:                                                                ctrl_break_pressed = true;
                   3613:                                                                mem[0x471] = 0x80;
                   3614:                                                                raise_int_1bh = true;
                   3615:                                                        } else {
                   3616:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3617: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3618:                                                                        EnterCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3619: #endif
1.1.1.57  root     3620:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3621: #ifdef USE_SERVICE_THREAD
1.1.1.57  root     3622:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.35  root     3623: #endif
1.1.1.57  root     3624:                                                                }
                   3625:                                                                ctrl_c_pressed = (scn == 0x2e);
1.1.1.33  root     3626:                                                        }
                   3627:                                                }
                   3628:                                                // break
1.1.1.57  root     3629:                                                tmp_data |= 0x80;
                   3630:                                        }
                   3631:                                        if(!(kbd_status & 1)) {
                   3632:                                                kbd_data = tmp_data;
                   3633:                                                kbd_status |= 1;
                   3634:                                        } else {
                   3635:                                                if(key_buf_data != NULL) {
                   3636: #ifdef USE_SERVICE_THREAD
                   3637:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3638: #endif
                   3639:                                                        key_buf_data->write(tmp_data);
                   3640: #ifdef USE_SERVICE_THREAD
                   3641:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3642: #endif
                   3643:                                                }
1.1       root     3644:                                        }
1.1.1.24  root     3645:                                        result = key_changed = true;
1.1.1.36  root     3646:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3647:                                        cursor_moved = true;
1.1       root     3648:                                }
                   3649:                        }
                   3650:                }
                   3651:        }
1.1.1.35  root     3652: #ifdef USE_SERVICE_THREAD
                   3653:        LeaveCriticalSection(&input_crit_sect);
                   3654: #endif
1.1.1.24  root     3655:        return(result);
1.1.1.8   root     3656: }
                   3657: 
1.1.1.14  root     3658: bool update_key_buffer()
1.1.1.8   root     3659: {
1.1.1.35  root     3660:        if(update_console_input()) {
                   3661:                return(true);
                   3662:        }
                   3663:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3664: #ifdef USE_SERVICE_THREAD
                   3665:                EnterCriticalSection(&key_buf_crit_sect);
                   3666: #endif
1.1.1.55  root     3667:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     3668: #ifdef USE_SERVICE_THREAD
                   3669:                LeaveCriticalSection(&key_buf_crit_sect);
                   3670: #endif
                   3671:                if(!empty) return(true);
                   3672:        }
                   3673:        return(false);
1.1.1.8   root     3674: }
                   3675: 
1.1.1.20  root     3676: /* ----------------------------------------------------------------------------
                   3677:        MS-DOS virtual machine
                   3678: ---------------------------------------------------------------------------- */
                   3679: 
1.1.1.32  root     3680: static const struct {
1.1.1.33  root     3681:        char *name;
                   3682:        DWORD lcid;
                   3683:        char *std;
                   3684:        char *dlt;
                   3685: } tz_table[] = {
                   3686:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3687: //     0       GMT             Greenwich Mean Time             GMT0
                   3688:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3689:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3690:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3691:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3692: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3693:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3694:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3695: //     3       BST             Brazil Standard Time            BST3
                   3696:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3697:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3698:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3699: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3700:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3701: //     3       GST             Greenland Standard Time         GST3
                   3702:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3703: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3704:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3705: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3706:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3707: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3708:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3709:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3710: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3711:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3712:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3713:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3714: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3715:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3716: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3717:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3718: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3719:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3720: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3721:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3722:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3723:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3724: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3725:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3726: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3727:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3728:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3729:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3730: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3731:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3732:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3733: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3734: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3735:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3736: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3737:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3738:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3739: //     11      SST             Samoa Standard Time             SST11
                   3740:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3741: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3742:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3743: //     -10     GST             Guam Standard Time              GST-10
                   3744:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3745: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3746:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3747:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3748:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3749: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3750:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3751:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3752: //     -9      JST             Japan Standard Time             JST-9
                   3753:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3754: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3755:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3756:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3757: //     -8      HKT             Hong Kong Time                  HKT-8
                   3758:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3759: //     -8      CCT             China Coast Time                CCT-8
                   3760:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3761:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3762: //     -8      SST             Singapore Standard Time         SST-8
                   3763:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3764: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3765:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3766:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3767: //     -7:30   JT              Java Standard Time              JST-7:30
                   3768: //     -7      NST             North Sumatra Time              NST-7
                   3769:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3770: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3771:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3772: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3773:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3774: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3775:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3776:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3777: //     -2      EET             Eastern Europe Time             EET-2
                   3778:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3779:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3780:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3781:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3782: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3783:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3784: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3785: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3786: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3787: //     -1      CET     CES     Central European Time           CET-1CES
                   3788:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3789:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3790:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3791:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3792: //     -1      WAT             West African Time               WAT-1
                   3793:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3794:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3795: //     0       UTC             Universal Coordinated Time      UTC0
                   3796:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3797:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3798:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3799:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3800:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3801:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3802: };
                   3803: 
1.1.1.53  root     3804: // FIXME: consider to build on non-Japanese environment :-(
                   3805: // message_japanese string must be in shift-jis
                   3806: 
1.1.1.33  root     3807: static const struct {
1.1.1.32  root     3808:        UINT16 code;
                   3809:        char *message_english;
                   3810:        char *message_japanese;
                   3811: } standard_error_table[] = {
                   3812:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3813:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3814:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3815:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3816:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3817:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3818:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3819:        {0x08,  "Insufficient memory", "������������܂���."},
                   3820:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3821:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3822:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3823:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3824:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3825:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3826:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3827:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3828:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3829:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3830:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3831:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3832:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3833:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3834:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3835:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3836:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3837:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3838:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3839:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3840:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3841:        {0x1F,  "General failure", "�G���[�ł�."},
                   3842:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3843:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3844:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3845:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3846:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3847:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3848:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3849:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3850: /*
                   3851:        {0x32,  "Network request not supported", NULL},
                   3852:        {0x33,  "Remote computer not listening", NULL},
                   3853:        {0x34,  "Duplicate name on network", NULL},
                   3854:        {0x35,  "Network name not found", NULL},
                   3855:        {0x36,  "Network busy", NULL},
                   3856:        {0x37,  "Network device no longer exists", NULL},
                   3857:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3858:        {0x39,  "Network adapter hardware error", NULL},
                   3859:        {0x3A,  "Incorrect response from network", NULL},
                   3860:        {0x3B,  "Unexpected network error", NULL},
                   3861:        {0x3C,  "Incompatible remote adapter", NULL},
                   3862:        {0x3D,  "Print queue full", NULL},
                   3863:        {0x3E,  "Queue not full", NULL},
                   3864:        {0x3F,  "Not enough space to print file", NULL},
                   3865:        {0x40,  "Network name was deleted", NULL},
                   3866:        {0x41,  "Network: Access denied", NULL},
                   3867:        {0x42,  "Network device type incorrect", NULL},
                   3868:        {0x43,  "Network name not found", NULL},
                   3869:        {0x44,  "Network name limit exceeded", NULL},
                   3870:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3871:        {0x46,  "Temporarily paused", NULL},
                   3872:        {0x47,  "Network request not accepted", NULL},
                   3873:        {0x48,  "Network print/disk redirection paused", NULL},
                   3874:        {0x49,  "Network software not installed", NULL},
                   3875:        {0x4A,  "Unexpected adapter close", NULL},
                   3876: */
                   3877:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3878:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3879:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3880:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3881:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3882:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3883:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3884:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3885:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3886:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
1.1.1.53  root     3887: #ifdef SUPPORT_MSCDEX
1.1.1.32  root     3888:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3889:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3890:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3891:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3892:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
1.1.1.53  root     3893: #endif
1.1.1.32  root     3894:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3895:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3896:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3897:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3898:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3899:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3900: };
                   3901: 
                   3902: static const struct {
                   3903:        UINT16 code;
                   3904:        char *message_english;
                   3905:        char *message_japanese;
                   3906: } param_error_table[] = {
                   3907:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3908:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3909:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3910:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3911:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3912:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3913:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3914:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3915:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3916:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3917:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3918: };
                   3919: 
                   3920: static const struct {
                   3921:        UINT16 code;
                   3922:        char *message_english;
                   3923:        char *message_japanese;
                   3924: } critical_error_table[] = {
                   3925:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3926:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3927:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3928:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3929:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3930:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3931:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3932:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3933:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3934:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3935:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3936:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3937:        {0x0C,  "General failure", "�G���[�ł�."},
                   3938:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3939:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3940:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3941:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3942:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3943:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3944:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3945:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3946:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3947: };
                   3948: 
1.1.1.20  root     3949: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3950: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3951: void msdos_putch(UINT8 data);
1.1.1.50  root     3952: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3953: #ifdef USE_SERVICE_THREAD
                   3954: void msdos_putch_tmp(UINT8 data);
                   3955: #endif
1.1.1.45  root     3956: const char *msdos_short_path(const char *path);
1.1.1.44  root     3957: bool msdos_is_valid_drive(int drv);
                   3958: bool msdos_is_removable_drive(int drv);
                   3959: bool msdos_is_cdrom_drive(int drv);
                   3960: bool msdos_is_remote_drive(int drv);
                   3961: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3962: 
1.1       root     3963: // process info
                   3964: 
                   3965: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3966: {
                   3967:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3968:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3969:                        memset(&process[i], 0, sizeof(process_t));
                   3970:                        process[i].psp = psp_seg;
                   3971:                        return(&process[i]);
                   3972:                }
                   3973:        }
                   3974:        fatalerror("too many processes\n");
                   3975:        return(NULL);
                   3976: }
                   3977: 
1.1.1.52  root     3978: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3979: {
                   3980:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3981:                if(process[i].psp == psp_seg) {
                   3982:                        return(&process[i]);
                   3983:                }
                   3984:        }
1.1.1.33  root     3985:        if(show_error) {
                   3986:                fatalerror("invalid psp address\n");
                   3987:        }
1.1       root     3988:        return(NULL);
                   3989: }
                   3990: 
1.1.1.23  root     3991: void msdos_sda_update(int psp_seg)
                   3992: {
                   3993:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3994:        
                   3995:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3996:                if(process[i].psp == psp_seg) {
                   3997:                        sda->switchar = process[i].switchar;
                   3998:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3999:                        sda->current_dta.w.h = process[i].dta.w.h;
                   4000:                        sda->current_psp = process[i].psp;
                   4001:                        break;
                   4002:                }
                   4003:        }
                   4004:        sda->malloc_strategy = malloc_strategy;
                   4005:        sda->return_code = retval;
                   4006:        sda->current_drive = _getdrive();
                   4007: }
                   4008: 
1.1.1.13  root     4009: // dta info
                   4010: 
                   4011: void msdos_dta_info_init()
                   4012: {
1.1.1.14  root     4013:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     4014:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   4015:        }
                   4016: }
                   4017: 
                   4018: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   4019: {
                   4020:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     4021:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   4022:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   4023:                        if(free_dta == NULL) {
1.1.1.13  root     4024:                                free_dta = &dtalist[i];
                   4025:                        }
1.1.1.14  root     4026:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     4027:                        return(&dtalist[i]);
                   4028:                }
                   4029:        }
1.1.1.14  root     4030:        if(free_dta) {
1.1.1.13  root     4031:                free_dta->psp = psp_seg;
                   4032:                free_dta->dta = dta_laddr;
                   4033:                return(free_dta);
                   4034:        }
                   4035:        fatalerror("too many dta\n");
                   4036:        return(NULL);
                   4037: }
                   4038: 
                   4039: void msdos_dta_info_free(UINT16 psp_seg)
                   4040: {
1.1.1.14  root     4041:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   4042:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     4043:                        FindClose(dtalist[i].find_handle);
                   4044:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   4045:                }
                   4046:        }
                   4047: }
                   4048: 
1.1       root     4049: void msdos_cds_update(int drv)
                   4050: {
1.1.1.44  root     4051:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     4052:        
1.1.1.44  root     4053:        memset(cds, 0, 88);
                   4054:        
                   4055:        if(msdos_is_valid_drive(drv)) {
                   4056:                char path[MAX_PATH];
                   4057:                if(msdos_is_remote_drive(drv)) {
                   4058:                        cds->drive_attrib = 0xc000;     // network drive
                   4059:                } else if(msdos_is_subst_drive(drv)) {
                   4060:                        cds->drive_attrib = 0x5000;     // subst drive
                   4061:                } else {
                   4062:                        cds->drive_attrib = 0x4000;     // physical drive
                   4063:                }
                   4064:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   4065:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   4066:                }
                   4067:        }
                   4068:        if(cds->path_name[0] == '\0') {
                   4069:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   4070:        }
                   4071:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   4072:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   4073:        cds->word_1 = cds->word_2 = 0xffff;
                   4074:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   4075:        cds->bs_offset = 2;
                   4076: }
                   4077: 
1.1.1.45  root     4078: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     4079: {
                   4080:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   4081:        
                   4082:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     4083: }
                   4084: 
1.1.1.17  root     4085: // nls information tables
                   4086: 
                   4087: // uppercase table (func 6502h)
                   4088: void msdos_upper_table_update()
                   4089: {
                   4090:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4091:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4092:                UINT8 c[4];
1.1.1.33  root     4093:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4094:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     4095:                c[0] = 0x80 + i;
                   4096:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   4097:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4098:        }
                   4099: }
                   4100: 
1.1.1.23  root     4101: // lowercase table (func 6503h)
                   4102: void msdos_lower_table_update()
                   4103: {
                   4104:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   4105:        for(unsigned i = 0; i < 0x80; ++i) {
                   4106:                UINT8 c[4];
1.1.1.33  root     4107:                *(UINT32 *)c = 0;               // reset internal conversion state
                   4108:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     4109:                c[0] = 0x80 + i;
                   4110:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   4111:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   4112:        }
                   4113: }
                   4114: 
1.1.1.17  root     4115: // filename uppercase table (func 6504h)
                   4116: void msdos_filename_upper_table_init()
                   4117: {
                   4118:        // depended on (file)system, not on active codepage
                   4119:        // temporary solution: just filling data
                   4120:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     4121:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     4122:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   4123:        }
                   4124: }
                   4125: 
                   4126: // filaname terminator table (func 6505h)
                   4127: void msdos_filename_terminator_table_init()
                   4128: {
                   4129:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   4130:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   4131:        
                   4132:        data[2] = 1;            // marker? (permissible character value)
                   4133:        data[3] = 0x00;         // 00h...FFh
                   4134:        data[4] = 0xff;
                   4135:        data[5] = 0;            // marker? (excluded character)
                   4136:        data[6] = 0x00;         // 00h...20h
                   4137:        data[7] = 0x20;
                   4138:        data[8] = 2;            // marker? (illegal characters for filename)
                   4139:        data[9] = (UINT8)strlen(illegal_chars);
                   4140:        memcpy(data + 10, illegal_chars, data[9]);
                   4141:        
                   4142:        // total length
                   4143:        *(UINT16 *)data = (10 - 2) + data[9];
                   4144: }
                   4145: 
                   4146: // collating table (func 6506h)
                   4147: void msdos_collating_table_update()
                   4148: {
                   4149:        // temporary solution: just filling data
                   4150:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     4151:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     4152:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   4153:        }
                   4154: }
                   4155: 
1.1       root     4156: // dbcs
                   4157: 
                   4158: void msdos_dbcs_table_update()
                   4159: {
                   4160:        UINT8 dbcs_data[DBCS_SIZE];
                   4161:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   4162:        
                   4163:        CPINFO info;
                   4164:        GetCPInfo(active_code_page, &info);
                   4165:        
                   4166:        if(info.MaxCharSize != 1) {
                   4167:                for(int i = 0;; i += 2) {
                   4168:                        UINT8 lo = info.LeadByte[i + 0];
                   4169:                        UINT8 hi = info.LeadByte[i + 1];
                   4170:                        dbcs_data[2 + i + 0] = lo;
                   4171:                        dbcs_data[2 + i + 1] = hi;
                   4172:                        if(lo == 0 && hi == 0) {
                   4173:                                dbcs_data[0] = i + 2;
                   4174:                                break;
                   4175:                        }
                   4176:                }
                   4177:        } else {
                   4178:                dbcs_data[0] = 2;       // ???
                   4179:        }
                   4180:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   4181: }
                   4182: 
1.1.1.17  root     4183: void msdos_dbcs_table_finish()
                   4184: {
1.1.1.32  root     4185:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     4186:                _setmbcp(system_code_page);
                   4187:        }
1.1.1.32  root     4188:        if(console_code_page != GetConsoleCP()) {
                   4189:                SetConsoleCP(console_code_page);
                   4190:                SetConsoleOutputCP(console_code_page);
                   4191:        }
1.1.1.17  root     4192: }
                   4193: 
                   4194: void msdos_nls_tables_init()
1.1       root     4195: {
1.1.1.32  root     4196:        active_code_page = console_code_page = GetConsoleCP();
                   4197:        system_code_page = _getmbcp();
                   4198:        
                   4199:        if(active_code_page != system_code_page) {
                   4200:                if(_setmbcp(active_code_page) != 0) {
                   4201:                        active_code_page = system_code_page;
                   4202:                }
                   4203:        }
                   4204:        
1.1.1.17  root     4205:        msdos_upper_table_update();
1.1.1.23  root     4206:        msdos_lower_table_update();
1.1.1.17  root     4207:        msdos_filename_terminator_table_init();
                   4208:        msdos_filename_upper_table_init();
                   4209:        msdos_collating_table_update();
1.1       root     4210:        msdos_dbcs_table_update();
                   4211: }
                   4212: 
1.1.1.17  root     4213: void msdos_nls_tables_update()
1.1       root     4214: {
1.1.1.17  root     4215:        msdos_dbcs_table_update();
                   4216:        msdos_upper_table_update();
1.1.1.23  root     4217:        msdos_lower_table_update();
                   4218: //     msdos_collating_table_update();
1.1       root     4219: }
                   4220: 
                   4221: int msdos_lead_byte_check(UINT8 code)
                   4222: {
                   4223:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   4224:        
                   4225:        for(int i = 0;; i += 2) {
                   4226:                UINT8 lo = dbcs_table[i + 0];
                   4227:                UINT8 hi = dbcs_table[i + 1];
                   4228:                if(lo == 0 && hi == 0) {
                   4229:                        break;
                   4230:                }
                   4231:                if(lo <= code && code <= hi) {
                   4232:                        return(1);
                   4233:                }
                   4234:        }
                   4235:        return(0);
                   4236: }
                   4237: 
1.1.1.20  root     4238: int msdos_ctrl_code_check(UINT8 code)
                   4239: {
1.1.1.22  root     4240:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     4241: }
                   4242: 
1.1.1.36  root     4243: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   4244: {
                   4245:        int is_kanji_1st = 0;
                   4246:        int is_kanji_2nd = 0;
                   4247:        
                   4248:        for(int p = 0;; p++) {
                   4249:                if(is_kanji_1st) {
                   4250:                        is_kanji_1st = 0;
                   4251:                        is_kanji_2nd = 1;
                   4252:                } else if(msdos_lead_byte_check(buf[p])) {
                   4253:                        is_kanji_1st = 1;
                   4254:                }
                   4255:                if(p == n) {
                   4256:                        return(is_kanji_2nd);
                   4257:                }
                   4258:                is_kanji_2nd = 0;
                   4259:        }
                   4260: }
                   4261: 
1.1       root     4262: // file control
                   4263: 
1.1.1.45  root     4264: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     4265: {
                   4266:        static char tmp[MAX_PATH];
                   4267:        
                   4268:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     4269:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     4270:                memcpy(tmp, path + 1, strlen(path) - 2);
                   4271:        } else {
                   4272:                strcpy(tmp, path);
                   4273:        }
                   4274:        return(tmp);
                   4275: }
                   4276: 
1.1.1.45  root     4277: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     4278: {
                   4279:        static char tmp[MAX_PATH];
                   4280:        
                   4281:        strcpy(tmp, path);
1.1.1.45  root     4282:        
                   4283:        // for example "C:\" case, the end separator should not be removed
                   4284:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   4285:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     4286:        }
                   4287:        return(tmp);
                   4288: }
                   4289: 
1.1.1.45  root     4290: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     4291: {
                   4292:        static char tmp[MAX_PATH];
1.1.1.45  root     4293:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     4294:        
                   4295:        if(strlen(tmp_dir) == 0) {
                   4296:                strcpy(tmp, file);
                   4297:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   4298:                sprintf(tmp, "%s%s", tmp_dir, file);
                   4299:        } else {
                   4300:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   4301:        }
                   4302:        return(tmp);
                   4303: }
                   4304: 
1.1.1.45  root     4305: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     4306: {
                   4307:        static char tmp[MAX_PATH];
                   4308:        
                   4309:        if(lfn) {
                   4310:                strcpy(tmp, path);
                   4311:        } else {
                   4312:                // remove space in the path
1.1.1.45  root     4313:                const char *src = path;
                   4314:                char *dst = tmp;
1.1       root     4315:                
                   4316:                while(*src != '\0') {
                   4317:                        if(msdos_lead_byte_check(*src)) {
                   4318:                                *dst++ = *src++;
                   4319:                                *dst++ = *src++;
                   4320:                        } else if(*src != ' ') {
                   4321:                                *dst++ = *src++;
                   4322:                        } else {
                   4323:                                src++;  // skip space
                   4324:                        }
                   4325:                }
                   4326:                *dst = '\0';
                   4327:        }
1.1.1.14  root     4328:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   4329:                // redirect C:\COMMAND.COM to comspec_path
                   4330:                strcpy(tmp, comspec_path);
                   4331:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   4332:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   4333:                static int root_drive_protected = -1;
                   4334:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   4335:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   4336:                
1.1.1.60  root     4337:                if(GetFullPathNameA(tmp, MAX_PATH, temp, &name_temp) != 0 &&
1.1.1.14  root     4338:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   4339:                        strcpy(name, name_temp);
                   4340:                        name_temp[0] = '\0';
                   4341:                        
                   4342:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   4343:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   4344:                                if(root_drive_protected == -1) {
                   4345:                                        FILE *fp = NULL;
                   4346:                                        
                   4347:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   4348:                                        root_drive_protected = 1;
                   4349:                                        try {
                   4350:                                                if((fp = fopen(temp, "w")) != NULL) {
                   4351:                                                        if(fprintf(fp, "TEST") == 4) {
                   4352:                                                                root_drive_protected = 0;
                   4353:                                                        }
                   4354:                                                }
                   4355:                                        } catch(...) {
                   4356:                                        }
                   4357:                                        if(fp != NULL) {
                   4358:                                                fclose(fp);
                   4359:                                        }
                   4360:                                        if(_access(temp, 0) == 0) {
                   4361:                                                remove(temp);
                   4362:                                        }
                   4363:                                }
                   4364:                                if(root_drive_protected == 1) {
1.1.1.60  root     4365:                                        if(GetEnvironmentVariableA("TEMP", temp, MAX_PATH) != 0 ||
                   4366:                                           GetEnvironmentVariableA("TMP",  temp, MAX_PATH) != 0) {
1.1.1.14  root     4367:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   4368:                                        }
                   4369:                                }
                   4370:                        }
                   4371:                }
                   4372:        }
1.1       root     4373:        return(tmp);
                   4374: }
                   4375: 
1.1.1.45  root     4376: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4377: {
1.1.1.32  root     4378:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4379:        static char env_path[ENV_SIZE];
                   4380:        char tmp[ENV_SIZE], *token;
                   4381:        
                   4382:        memset(env_path, 0, sizeof(env_path));
                   4383:        strcpy(tmp, src);
                   4384:        token = my_strtok(tmp, ";");
                   4385:        
                   4386:        while(token != NULL) {
                   4387:                if(token[0] != '\0') {
1.1.1.45  root     4388:                        const char *path = msdos_remove_double_quote(token);
                   4389:                        char short_path[MAX_PATH];
1.1.1.32  root     4390:                        if(path != NULL && strlen(path) != 0) {
                   4391:                                if(env_path[0] != '\0') {
                   4392:                                        strcat(env_path, ";");
                   4393:                                }
1.1.1.60  root     4394:                                if(GetShortPathNameA(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4395:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4396:                                } else {
                   4397:                                        my_strupr(short_path);
1.1.1.32  root     4398:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4399:                                }
                   4400:                        }
                   4401:                }
                   4402:                token = my_strtok(NULL, ";");
                   4403:        }
                   4404:        return(env_path);
                   4405: }
                   4406: 
1.1.1.45  root     4407: bool match(const char *text, const char *pattern)
1.1       root     4408: {
1.1.1.24  root     4409:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4410:        switch(*pattern) {
1.1       root     4411:        case '\0':
                   4412:                return !*text;
                   4413:        case '*':
1.1.1.14  root     4414:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4415:        case '?':
                   4416:                return *text && match(text + 1, pattern + 1);
                   4417:        default:
                   4418:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4419:        }
                   4420: }
                   4421: 
1.1.1.45  root     4422: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4423: {
1.1.1.45  root     4424:        const char *p = NULL;
1.1       root     4425:        
1.1.1.14  root     4426:        if(!*volume) {
                   4427:                return false;
                   4428:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4429:                return msdos_match_volume_label(p + 1, volume);
                   4430:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4431:                return msdos_match_volume_label(p + 1, volume);
                   4432:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4433:                char tmp[MAX_PATH];
                   4434:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4435:                return match(volume, tmp);
1.1       root     4436:        } else {
                   4437:                return match(volume, path);
                   4438:        }
                   4439: }
                   4440: 
1.1.1.45  root     4441: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4442: {
                   4443:        static char tmp[MAX_PATH];
                   4444:        char name[9], ext[4];
                   4445:        
                   4446:        memset(name, 0, sizeof(name));
                   4447:        memcpy(name, fcb->file_name, 8);
                   4448:        strcpy(name, msdos_trimmed_path(name, 0));
                   4449:        
                   4450:        memset(ext, 0, sizeof(ext));
                   4451:        memcpy(ext, fcb->file_name + 8, 3);
                   4452:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4453:        
                   4454:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4455:                strcpy(name, "*");
                   4456:        }
                   4457:        if(ext[0] == '\0') {
                   4458:                strcpy(tmp, name);
                   4459:        } else {
                   4460:                if(strcmp(ext, "???") == 0) {
                   4461:                        strcpy(ext, "*");
                   4462:                }
                   4463:                sprintf(tmp, "%s.%s", name, ext);
                   4464:        }
                   4465:        return(tmp);
                   4466: }
                   4467: 
1.1.1.45  root     4468: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4469: {
1.1.1.60  root     4470:        char tmp[MAX_PATH];
                   4471:        strcpy(tmp, path);
                   4472:        char *ext = my_strchr(tmp, '.');
1.1       root     4473:        
                   4474:        memset(fcb->file_name, 0x20, 8 + 3);
1.1.1.60  root     4475:        if(ext != NULL && tmp[0] != '.') {
1.1       root     4476:                *ext = '\0';
                   4477:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4478:        }
1.1.1.60  root     4479:        memcpy(fcb->file_name, tmp, strlen(tmp));
1.1       root     4480: }
                   4481: 
1.1.1.45  root     4482: const char *msdos_short_path(const char *path)
1.1       root     4483: {
                   4484:        static char tmp[MAX_PATH];
                   4485:        
1.1.1.60  root     4486:        if(GetShortPathNameA(path, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4487:                strcpy(tmp, path);
                   4488:        }
1.1       root     4489:        my_strupr(tmp);
                   4490:        return(tmp);
                   4491: }
                   4492: 
1.1.1.60  root     4493: const char *msdos_short_name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     4494: {
                   4495:        static char tmp[MAX_PATH];
1.1.1.45  root     4496:        
1.1.1.14  root     4497:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4498:                strcpy(tmp, fd->cAlternateFileName);
                   4499:        } else {
                   4500:                strcpy(tmp, fd->cFileName);
                   4501:        }
                   4502:        my_strupr(tmp);
                   4503:        return(tmp);
                   4504: }
                   4505: 
1.1.1.45  root     4506: const char *msdos_short_full_path(const char *path)
1.1       root     4507: {
                   4508:        static char tmp[MAX_PATH];
                   4509:        char full[MAX_PATH], *name;
                   4510:        
1.1.1.14  root     4511:        // Full works with non-existent files, but Short does not
1.1.1.60  root     4512:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1.1.14  root     4513:        *tmp = '\0';
1.1.1.60  root     4514:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0 && name > path) {
1.1.1.14  root     4515:                name[-1] = '\0';
1.1.1.60  root     4516:                DWORD len = GetShortPathNameA(full, tmp, MAX_PATH);
1.1.1.14  root     4517:                if(len == 0) {
                   4518:                        strcpy(tmp, full);
                   4519:                } else {
                   4520:                        tmp[len++] = '\\';
                   4521:                        strcpy(tmp + len, name);
                   4522:                }
                   4523:        }
1.1       root     4524:        my_strupr(tmp);
                   4525:        return(tmp);
                   4526: }
                   4527: 
1.1.1.45  root     4528: const char *msdos_short_full_dir(const char *path)
1.1       root     4529: {
                   4530:        static char tmp[MAX_PATH];
                   4531:        char full[MAX_PATH], *name;
                   4532:        
1.1.1.60  root     4533:        GetFullPathNameA(path, MAX_PATH, full, &name);
1.1       root     4534:        name[-1] = '\0';
1.1.1.60  root     4535:        if(GetShortPathNameA(full, tmp, MAX_PATH) == 0) {
1.1.1.24  root     4536:                strcpy(tmp, full);
                   4537:        }
1.1       root     4538:        my_strupr(tmp);
                   4539:        return(tmp);
                   4540: }
                   4541: 
1.1.1.45  root     4542: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4543: {
1.1.1.45  root     4544:        static char trimmed[MAX_PATH];
                   4545:        
                   4546:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4547: #if 0
                   4548:        // I have forgotten the reason of this routine... :-(
1.1       root     4549:        if(_access(trimmed, 0) != 0) {
                   4550:                process_t *process = msdos_process_info_get(current_psp);
                   4551:                static char tmp[MAX_PATH];
                   4552:                
                   4553:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4554:                if(_access(tmp, 0) == 0) {
                   4555:                        return(tmp);
                   4556:                }
                   4557:        }
1.1.1.14  root     4558: #endif
1.1       root     4559:        return(trimmed);
                   4560: }
                   4561: 
1.1.1.45  root     4562: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4563: {
                   4564:        char full[MAX_PATH], *name;
                   4565:        
1.1.1.60  root     4566:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4567:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4568:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4569:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4570:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4571:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4572:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4573:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4574:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4575:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4576:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4577:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4578:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4579:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4580:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4581:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4582:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4583:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4584:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4585:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4586:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4587:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4588:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4589:                        return(true);
                   4590:                } else if(name != NULL) {
                   4591:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4592:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4593:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4594: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4595:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4596:                                return(true);
                   4597:                        }
                   4598:                }
1.1.1.24  root     4599:        }
                   4600:        return(false);
1.1.1.11  root     4601: }
                   4602: 
1.1.1.45  root     4603: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4604: {
1.1.1.14  root     4605:        char full[MAX_PATH], *name;
1.1.1.8   root     4606:        
1.1.1.60  root     4607:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4608:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4609:        }
                   4610:        return(false);
                   4611: }
                   4612: 
1.1.1.45  root     4613: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4614: {
                   4615:        char full[MAX_PATH], *name;
                   4616:        
1.1.1.60  root     4617:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4618:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4619:                        return(1);
                   4620:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4621:                        return(2);
                   4622:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4623:                        return(3);
                   4624:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4625:                        return(4);
1.1.1.24  root     4626:                }
                   4627:        }
1.1.1.29  root     4628:        return(0);
                   4629: }
                   4630: 
1.1.1.45  root     4631: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4632: {
                   4633:        // 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     4634:        const char *p = NULL;
1.1.1.37  root     4635:        
                   4636:        if((p = strstr(path, ":")) != NULL) {
                   4637:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4638:                
                   4639:                // baud rate
                   4640:                int baud = max(110, min(9600, atoi(p + 1)));
                   4641:                UINT16 divisor = 115200 / baud;
                   4642:                
                   4643:                if((p = strstr(p + 1, ",")) != NULL) {
                   4644:                        // parity
                   4645:                        if(p[1] == 'N' || p[1] == 'n') {
                   4646:                                selector = (selector & ~0x38) | 0x00;
                   4647:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4648:                                selector = (selector & ~0x38) | 0x08;
                   4649:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4650:                                selector = (selector & ~0x38) | 0x18;
                   4651:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4652:                                selector = (selector & ~0x38) | 0x28;
                   4653:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4654:                                selector = (selector & ~0x38) | 0x38;
                   4655:                        }
                   4656:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4657:                                // word length
                   4658:                                if(p[1] == '8') {
                   4659:                                        selector = (selector & ~0x03) | 0x03;
                   4660:                                } else if(p[1] == '7') {
                   4661:                                        selector = (selector & ~0x03) | 0x02;
                   4662:                                } else if(p[1] == '6') {
                   4663:                                        selector = (selector & ~0x03) | 0x01;
                   4664:                                } else if(p[1] == '5') {
                   4665:                                        selector = (selector & ~0x03) | 0x00;
                   4666:                                }
                   4667:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4668:                                        // stop bits
                   4669:                                        float bits = atof(p + 1);
                   4670:                                        if(bits > 1.0F) {
                   4671:                                                selector |= 0x04;
                   4672:                                        } else {
                   4673:                                                selector &= ~0x04;
                   4674:                                        }
                   4675:                                }
                   4676:                        }
                   4677:                }
                   4678:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4679:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4680:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4681:                sio_write(sio_port - 1, 3, selector);
                   4682:        }
                   4683: }
                   4684: 
1.1.1.45  root     4685: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4686: {
                   4687:        char full[MAX_PATH], *name;
                   4688:        
1.1.1.60  root     4689:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1.1.30  root     4690:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4691:                        return(1);
                   4692:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4693:                        return(1);
                   4694:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4695:                        return(2);
                   4696:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4697:                        return(3);
                   4698:                }
                   4699:        }
                   4700:        return(0);
                   4701: }
                   4702: 
1.1.1.44  root     4703: bool msdos_is_valid_drive(int drv)
                   4704: {
                   4705:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4706: }
                   4707: 
                   4708: bool msdos_is_removable_drive(int drv)
                   4709: {
                   4710:        char volume[] = "A:\\";
                   4711:        
                   4712:        volume[0] = 'A' + drv;
                   4713:        
1.1.1.60  root     4714:        return(GetDriveTypeA(volume) == DRIVE_REMOVABLE);
1.1.1.44  root     4715: }
                   4716: 
                   4717: bool msdos_is_cdrom_drive(int drv)
                   4718: {
                   4719:        char volume[] = "A:\\";
                   4720:        
                   4721:        volume[0] = 'A' + drv;
                   4722:        
1.1.1.60  root     4723:        return(GetDriveTypeA(volume) == DRIVE_CDROM);
1.1.1.44  root     4724: }
                   4725: 
                   4726: bool msdos_is_remote_drive(int drv)
                   4727: {
                   4728:        char volume[] = "A:\\";
                   4729:        
                   4730:        volume[0] = 'A' + drv;
                   4731:        
1.1.1.60  root     4732:        return(GetDriveTypeA(volume) == DRIVE_REMOTE);
1.1.1.44  root     4733: }
                   4734: 
                   4735: bool msdos_is_subst_drive(int drv)
                   4736: {
                   4737:        char device[] = "A:", path[MAX_PATH];
                   4738:        
                   4739:        device[0] = 'A' + drv;
                   4740:        
1.1.1.60  root     4741:        if(QueryDosDeviceA(device, path, MAX_PATH)) {
1.1.1.44  root     4742:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4743:                        return(true);
                   4744:                }
                   4745:        }
                   4746:        return(false);
                   4747: }
                   4748: 
1.1.1.45  root     4749: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4750: {
                   4751:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
1.1.1.60  root     4752:        WIN32_FIND_DATAA fd;
1.1.1.24  root     4753:        HANDLE hFind;
                   4754:        
1.1.1.60  root     4755:        if((hFind = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.24  root     4756:                FindClose(hFind);
1.1.1.63  root     4757:                return((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0);
                   4758:        }
                   4759:        return(false);
                   4760: }
                   4761: 
                   4762: bool msdos_is_existing_dir(const char *path)
                   4763: {
                   4764:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4765:        WIN32_FIND_DATAA fd;
                   4766:        HANDLE hFind;
                   4767:        
                   4768:        if((hFind = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
                   4769:                FindClose(hFind);
                   4770:                return((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
1.1.1.24  root     4771:        }
                   4772:        return(false);
1.1.1.8   root     4773: }
                   4774: 
1.1.1.45  root     4775: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4776: {
                   4777:        static char tmp[MAX_PATH];
1.1.1.28  root     4778:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4779:        
1.1.1.28  root     4780:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.60  root     4781:        if(GetFullPathNameA(command_path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4782:                sprintf(file_name, "COMMAND.COM");
                   4783:                if(_access(tmp, 0) == 0) {
                   4784:                        return(tmp);
                   4785:                }
                   4786:        }
1.1.1.28  root     4787:        
                   4788:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.60  root     4789:        if(GetModuleFileNameA(NULL, path, MAX_PATH) != 0 && GetFullPathNameA(path, MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4790:                sprintf(file_name, "COMMAND.COM");
                   4791:                if(_access(tmp, 0) == 0) {
                   4792:                        return(tmp);
                   4793:                }
                   4794:        }
1.1.1.28  root     4795:        
                   4796:        // check if COMMAND.COM is in the current directory
1.1.1.60  root     4797:        if(GetFullPathNameA("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
1.1.1.9   root     4798:                if(_access(tmp, 0) == 0) {
                   4799:                        return(tmp);
                   4800:                }
                   4801:        }
1.1.1.28  root     4802:        
                   4803:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4804:        strcpy(path, env_path);
                   4805:        char *token = my_strtok(path, ";");
1.1.1.9   root     4806:        while(token != NULL) {
1.1.1.14  root     4807:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4808:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4809:                        if(_access(tmp, 0) == 0) {
                   4810:                                return(tmp);
                   4811:                        }
                   4812:                }
                   4813:                token = my_strtok(NULL, ";");
                   4814:        }
                   4815:        return(NULL);
                   4816: }
                   4817: 
1.1.1.14  root     4818: int msdos_drive_number(const char *path)
1.1       root     4819: {
                   4820:        char tmp[MAX_PATH], *name;
                   4821:        
1.1.1.60  root     4822:        if(GetFullPathNameA(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
1.1.1.45  root     4823:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4824:                        return(tmp[0] - 'a');
                   4825:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4826:                        return(tmp[0] - 'A');
                   4827:                }
1.1       root     4828:        }
1.1.1.45  root     4829: //     return(msdos_drive_number("."));
                   4830:        return(_getdrive() - 1);
1.1       root     4831: }
                   4832: 
1.1.1.45  root     4833: const char *msdos_volume_label(const char *path)
1.1       root     4834: {
                   4835:        static char tmp[MAX_PATH];
                   4836:        char volume[] = "A:\\";
                   4837:        
                   4838:        if(path[1] == ':') {
                   4839:                volume[0] = path[0];
                   4840:        } else {
                   4841:                volume[0] = 'A' + _getdrive() - 1;
                   4842:        }
1.1.1.60  root     4843:        if(!GetVolumeInformationA(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
1.1       root     4844:                memset(tmp, 0, sizeof(tmp));
                   4845:        }
                   4846:        return(tmp);
                   4847: }
                   4848: 
1.1.1.45  root     4849: const char *msdos_short_volume_label(const char *label)
1.1       root     4850: {
                   4851:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4852:        const char *src = label;
1.1       root     4853:        int remain = strlen(label);
                   4854:        char *dst_n = tmp;
                   4855:        char *dst_e = tmp + 9;
                   4856:        
                   4857:        strcpy(tmp, "        .   ");
                   4858:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4859:                if(msdos_lead_byte_check(*src)) {
                   4860:                        if(++i == 8) {
                   4861:                                break;
                   4862:                        }
                   4863:                        *dst_n++ = *src++;
                   4864:                        remain--;
                   4865:                }
                   4866:                *dst_n++ = *src++;
                   4867:                remain--;
                   4868:        }
                   4869:        if(remain > 0) {
                   4870:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4871:                        if(msdos_lead_byte_check(*src)) {
                   4872:                                if(++i == 3) {
                   4873:                                        break;
                   4874:                                }
                   4875:                                *dst_e++ = *src++;
                   4876:                                remain--;
                   4877:                        }
                   4878:                        *dst_e++ = *src++;
                   4879:                        remain--;
                   4880:                }
                   4881:                *dst_e = '\0';
                   4882:        } else {
                   4883:                *dst_n = '\0';
                   4884:        }
                   4885:        my_strupr(tmp);
                   4886:        return(tmp);
                   4887: }
                   4888: 
1.1.1.13  root     4889: errno_t msdos_maperr(unsigned long oserrno)
                   4890: {
                   4891:        _doserrno = oserrno;
1.1.1.14  root     4892:        switch(oserrno) {
1.1.1.13  root     4893:        case ERROR_FILE_NOT_FOUND:         // 2
                   4894:        case ERROR_PATH_NOT_FOUND:         // 3
                   4895:        case ERROR_INVALID_DRIVE:          // 15
                   4896:        case ERROR_NO_MORE_FILES:          // 18
                   4897:        case ERROR_BAD_NETPATH:            // 53
                   4898:        case ERROR_BAD_NET_NAME:           // 67
                   4899:        case ERROR_BAD_PATHNAME:           // 161
                   4900:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4901:                return ENOENT;
                   4902:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4903:                return EMFILE;
                   4904:        case ERROR_ACCESS_DENIED:          // 5
                   4905:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4906:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4907:        case ERROR_CANNOT_MAKE:            // 82
                   4908:        case ERROR_FAIL_I24:               // 83
                   4909:        case ERROR_DRIVE_LOCKED:           // 108
                   4910:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4911:        case ERROR_NOT_LOCKED:             // 158
                   4912:        case ERROR_LOCK_FAILED:            // 167
                   4913:                return EACCES;
                   4914:        case ERROR_INVALID_HANDLE:         // 6
                   4915:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4916:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4917:                return EBADF;
                   4918:        case ERROR_ARENA_TRASHED:          // 7
                   4919:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4920:        case ERROR_INVALID_BLOCK:          // 9
                   4921:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4922:                return ENOMEM;
                   4923:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4924:                return E2BIG;
                   4925:        case ERROR_BAD_FORMAT:             // 11
                   4926:                return ENOEXEC;
                   4927:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4928:                return EXDEV;
                   4929:        case ERROR_FILE_EXISTS:            // 80
                   4930:        case ERROR_ALREADY_EXISTS:         // 183
                   4931:                return EEXIST;
                   4932:        case ERROR_NO_PROC_SLOTS:          // 89
                   4933:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4934:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4935:                return EAGAIN;
                   4936:        case ERROR_BROKEN_PIPE:            // 109
                   4937:                return EPIPE;
                   4938:        case ERROR_DISK_FULL:              // 112
                   4939:                return ENOSPC;
                   4940:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4941:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4942:                return ECHILD;
                   4943:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4944:                return ENOTEMPTY;
                   4945:        }
1.1.1.14  root     4946:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4947:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4948:                return EACCES;
                   4949:        }
1.1.1.14  root     4950:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4951:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4952:                return ENOEXEC;
                   4953:        }
                   4954:        return EINVAL;
                   4955: }
                   4956: 
1.1.1.45  root     4957: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4958: {
1.1.1.14  root     4959:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4960:                return(_open(path, oflag));
1.1.1.13  root     4961:        }
1.1.1.14  root     4962:        
                   4963:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4964:        DWORD disposition;
1.1.1.14  root     4965:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4966:        default:
1.1.1.13  root     4967:        case _O_EXCL:
                   4968:                disposition = OPEN_EXISTING;
                   4969:                break;
                   4970:        case _O_CREAT:
                   4971:                disposition = OPEN_ALWAYS;
                   4972:                break;
                   4973:        case _O_CREAT | _O_EXCL:
                   4974:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4975:                disposition = CREATE_NEW;
                   4976:                break;
                   4977:        case _O_TRUNC:
                   4978:        case _O_TRUNC | _O_EXCL:
                   4979:                disposition = TRUNCATE_EXISTING;
                   4980:                break;
                   4981:        case _O_CREAT | _O_TRUNC:
                   4982:                disposition = CREATE_ALWAYS;
                   4983:                break;
                   4984:        }
1.1.1.14  root     4985:        
1.1.1.60  root     4986:        HANDLE h = CreateFileA(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4987:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4988:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4989:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4990:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4991:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.60  root     4992:                h = CreateFileA(path, GENERIC_READ,
1.1.1.13  root     4993:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4994:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4995:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4996:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4997:                        return(-1);
1.1.1.13  root     4998:                }
                   4999:        }
1.1.1.14  root     5000:        
1.1.1.13  root     5001:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     5002:        if(fd == -1) {
1.1.1.13  root     5003:                CloseHandle(h);
                   5004:        }
1.1.1.45  root     5005:        return(fd);
                   5006: }
                   5007: 
                   5008: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   5009: {
                   5010:        int fd = -1;
                   5011:        
                   5012:        *sio_port = *lpt_port = 0;
                   5013:        
                   5014:        if(msdos_is_con_path(path)) {
                   5015:                // MODE.COM opens CON device with read/write mode :-(
                   5016:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   5017:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   5018:                        oflag |= _O_RDONLY;
                   5019:                }
                   5020:                if((fd = msdos_open("CON", oflag)) == -1) {
                   5021: //                     fd = msdos_open("NUL", oflag);
                   5022:                }
                   5023:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   5024:                fd = msdos_open("NUL", oflag);
                   5025:                msdos_set_comm_params(*sio_port, path);
                   5026:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   5027:                fd = msdos_open("NUL", oflag);
                   5028:        } else if(msdos_is_device_path(path)) {
                   5029:                fd = msdos_open("NUL", oflag);
                   5030: //     } else if(oflag & _O_CREAT) {
                   5031: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   5032: //     } else {
                   5033: //             fd = _open(path, oflag);
                   5034:        }
                   5035:        return(fd);
                   5036: }
                   5037: 
                   5038: UINT16 msdos_device_info(const char *path)
                   5039: {
                   5040:        if(msdos_is_con_path(path)) {
                   5041:                return(0x80d3);
                   5042:        } else if(msdos_is_comm_path(path)) {
                   5043:                return(0x80a0);
                   5044:        } else if(msdos_is_prn_path(path)) {
                   5045: //             return(0xa8c0);
                   5046:                return(0x80a0);
                   5047:        } else if(msdos_is_device_path(path)) {
                   5048:                if(strstr(path, "EMMXXXX0") != NULL) {
                   5049:                        return(0xc0c0);
                   5050:                } else if(strstr(path, "MSCD001") != NULL) {
                   5051:                        return(0xc880);
                   5052:                } else {
                   5053:                        return(0x8084);
                   5054:                }
                   5055:        } else {
                   5056:                return(msdos_drive_number(path));
                   5057:        }
1.1.1.13  root     5058: }
                   5059: 
1.1.1.52  root     5060: 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     5061: {
                   5062:        static int id = 0;
                   5063:        char full[MAX_PATH], *name;
                   5064:        
1.1.1.60  root     5065:        if(GetFullPathNameA(path, MAX_PATH, full, &name) != 0) {
1.1       root     5066:                strcpy(file_handler[fd].path, full);
                   5067:        } else {
                   5068:                strcpy(file_handler[fd].path, path);
                   5069:        }
1.1.1.14  root     5070:        // isatty makes no distinction between CON & NUL
                   5071:        // GetFileSize fails on CON, succeeds on NUL
                   5072:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     5073:                if(info == 0x80d3) {
                   5074:                        info = 0x8084;
                   5075:                }
1.1.1.14  root     5076:                atty = 0;
                   5077:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     5078: //             info = msdos_drive_number(".");
                   5079:                info = msdos_drive_number(path);
1.1.1.14  root     5080:        }
1.1       root     5081:        file_handler[fd].valid = 1;
                   5082:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     5083:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     5084:        file_handler[fd].mode = mode;
                   5085:        file_handler[fd].info = info;
                   5086:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     5087:        file_handler[fd].sio_port = sio_port;
                   5088:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     5089:        
                   5090:        // init system file table
                   5091:        if(fd < 20) {
                   5092:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   5093:                
                   5094:                memset(sft, 0, 0x3b);
                   5095:                
                   5096:                *(UINT16 *)(sft + 0x00) = 1;
                   5097:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
1.1.1.60  root     5098:                *(UINT8  *)(sft + 0x04) = GetFileAttributesA(file_handler[fd].path) & 0xff;
1.1.1.21  root     5099:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   5100:                
                   5101:                if(!(file_handler[fd].info & 0x80)) {
                   5102:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   5103:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   5104:                        
                   5105:                        FILETIME time, local;
                   5106:                        HANDLE hHandle;
                   5107:                        WORD dos_date = 0, dos_time = 0;
                   5108:                        DWORD file_size = 0;
                   5109:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   5110:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   5111:                                        FileTimeToLocalFileTime(&time, &local);
                   5112:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   5113:                                }
                   5114:                                file_size = GetFileSize(hHandle, NULL);
                   5115:                        }
                   5116:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   5117:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   5118:                        *(UINT32 *)(sft + 0x11) = file_size;
                   5119:                }
                   5120:                
                   5121:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   5122:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   5123:                my_strupr(fname);
                   5124:                my_strupr(ext);
                   5125:                memset(sft + 0x20, 0x20, 11);
                   5126:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   5127:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   5128:                
                   5129:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   5130:        }
1.1       root     5131: }
                   5132: 
                   5133: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   5134: {
                   5135:        strcpy(file_handler[dst].path, file_handler[src].path);
                   5136:        file_handler[dst].valid = 1;
                   5137:        file_handler[dst].id = file_handler[src].id;
                   5138:        file_handler[dst].atty = file_handler[src].atty;
                   5139:        file_handler[dst].mode = file_handler[src].mode;
                   5140:        file_handler[dst].info = file_handler[src].info;
                   5141:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     5142:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   5143:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     5144: }
                   5145: 
1.1.1.20  root     5146: void msdos_file_handler_close(int fd)
1.1       root     5147: {
                   5148:        file_handler[fd].valid = 0;
1.1.1.21  root     5149:        
                   5150:        if(fd < 20) {
                   5151:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   5152:        }
1.1       root     5153: }
                   5154: 
1.1.1.14  root     5155: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     5156: {
1.1.1.14  root     5157:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   5158:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   5159:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     5160: }
                   5161: 
                   5162: // find file
                   5163: 
                   5164: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   5165: {
                   5166:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5167:                return(0);      // search directory only !!!
                   5168:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   5169:                return(0);
                   5170:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   5171:                return(0);
                   5172:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   5173:                return(0);
                   5174:        } else if((attribute & required_mask) != required_mask) {
                   5175:                return(0);
                   5176:        } else {
                   5177:                return(1);
                   5178:        }
                   5179: }
                   5180: 
1.1.1.60  root     5181: int msdos_find_file_has_8dot3name(WIN32_FIND_DATAA *fd)
1.1.1.13  root     5182: {
1.1.1.14  root     5183:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     5184:                return(1);
1.1.1.13  root     5185:        }
                   5186:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     5187:        if(len > 12) {
1.1.1.42  root     5188:                return(0);
1.1.1.13  root     5189:        }
                   5190:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     5191:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     5192:                return(0);
1.1.1.13  root     5193:        }
1.1.1.42  root     5194:        return(1);
1.1.1.13  root     5195: }
                   5196: 
1.1.1.60  root     5197: void msdos_find_file_conv_local_time(WIN32_FIND_DATAA *fd)
1.1       root     5198: {
                   5199:        FILETIME local;
                   5200:        
                   5201:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   5202:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   5203:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   5204:        
                   5205:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   5206:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   5207:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   5208:        
                   5209:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   5210:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   5211:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   5212: }
                   5213: 
                   5214: // i/o
                   5215: 
                   5216: void msdos_stdio_reopen()
                   5217: {
                   5218:        if(!file_handler[0].valid) {
                   5219:                _dup2(DUP_STDIN, 0);
                   5220:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   5221:        }
                   5222:        if(!file_handler[1].valid) {
                   5223:                _dup2(DUP_STDOUT, 1);
                   5224:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   5225:        }
                   5226:        if(!file_handler[2].valid) {
                   5227:                _dup2(DUP_STDERR, 2);
                   5228:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   5229:        }
1.1.1.21  root     5230:        if(!file_handler[3].valid) {
                   5231:                _dup2(DUP_STDAUX, 3);
                   5232:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   5233:        }
                   5234:        if(!file_handler[4].valid) {
                   5235:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     5236: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   5237:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     5238:        }
                   5239:        for(int i = 0; i < 5; i++) {
                   5240:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   5241:                        msdos_psp_set_file_table(i, i, current_psp);
                   5242:                }
                   5243:        }
1.1       root     5244: }
                   5245: 
1.1.1.37  root     5246: int msdos_read(int fd, void *buffer, unsigned int count)
                   5247: {
                   5248:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5249:                // read from serial port
                   5250:                int read = 0;
                   5251:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5252:                        UINT8 *buf = (UINT8 *)buffer;
                   5253:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5254:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5255:                        DWORD timeout = timeGetTime() + 1000;
                   5256:                        while(read < count) {
                   5257:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   5258:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   5259:                                        timeout = timeGetTime() + 1000;
                   5260:                                } else {
                   5261:                                        if(timeGetTime() > timeout) {
                   5262:                                                break;
                   5263:                                        }
                   5264:                                        Sleep(10);
1.1.1.37  root     5265:                                }
                   5266:                        }
                   5267:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5268:                }
                   5269:                return(read);
                   5270:        }
                   5271:        return(_read(fd, buffer, count));
                   5272: }
                   5273: 
1.1       root     5274: int msdos_kbhit()
                   5275: {
                   5276:        msdos_stdio_reopen();
                   5277:        
1.1.1.20  root     5278:        process_t *process = msdos_process_info_get(current_psp);
                   5279:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5280:        
                   5281:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5282:                // stdin is redirected to file
1.1.1.20  root     5283:                return(eof(fd) == 0);
1.1       root     5284:        }
                   5285:        
                   5286:        // check keyboard status
1.1.1.35  root     5287:        if(key_recv != 0) {
1.1       root     5288:                return(1);
                   5289:        }
1.1.1.35  root     5290:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5291: #ifdef USE_SERVICE_THREAD
                   5292:                EnterCriticalSection(&key_buf_crit_sect);
                   5293: #endif
1.1.1.55  root     5294:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5295: #ifdef USE_SERVICE_THREAD
                   5296:                LeaveCriticalSection(&key_buf_crit_sect);
                   5297: #endif
                   5298:                if(!empty) return(1);
                   5299:        }
                   5300:        return(_kbhit());
1.1       root     5301: }
                   5302: 
                   5303: int msdos_getch_ex(int echo)
                   5304: {
                   5305:        static char prev = 0;
                   5306:        
                   5307:        msdos_stdio_reopen();
                   5308:        
1.1.1.20  root     5309:        process_t *process = msdos_process_info_get(current_psp);
                   5310:        int fd = msdos_psp_get_file_table(0, current_psp);
                   5311:        
                   5312:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5313:                // stdin is redirected to file
                   5314: retry:
                   5315:                char data;
1.1.1.37  root     5316:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     5317:                        char tmp = data;
                   5318:                        if(data == 0x0a) {
                   5319:                                if(prev == 0x0d) {
                   5320:                                        goto retry; // CRLF -> skip LF
                   5321:                                } else {
                   5322:                                        data = 0x0d; // LF only -> CR
                   5323:                                }
                   5324:                        }
                   5325:                        prev = tmp;
                   5326:                        return(data);
                   5327:                }
                   5328:                return(EOF);
                   5329:        }
                   5330:        
                   5331:        // input from console
1.1.1.5   root     5332:        int key_char, key_scan;
1.1.1.33  root     5333:        if(key_recv != 0) {
1.1.1.5   root     5334:                key_char = (key_code >> 0) & 0xff;
                   5335:                key_scan = (key_code >> 8) & 0xff;
                   5336:                key_code >>= 16;
1.1.1.33  root     5337:                key_recv >>= 16;
1.1.1.5   root     5338:        } else {
1.1.1.54  root     5339:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_exit) {
1.1.1.35  root     5340:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   5341: #ifdef USE_SERVICE_THREAD
                   5342:                                EnterCriticalSection(&key_buf_crit_sect);
                   5343: #endif
1.1.1.55  root     5344:                                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     5345: #ifdef USE_SERVICE_THREAD
                   5346:                                LeaveCriticalSection(&key_buf_crit_sect);
                   5347: #endif
                   5348:                                if(!empty) break;
                   5349:                        }
1.1.1.23  root     5350:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   5351:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   5352:                                if(_kbhit()) {
1.1.1.32  root     5353:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5354: #ifdef USE_SERVICE_THREAD
                   5355:                                                EnterCriticalSection(&key_buf_crit_sect);
                   5356: #endif
1.1.1.51  root     5357:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     5358: #ifdef USE_SERVICE_THREAD
                   5359:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   5360: #endif
1.1.1.32  root     5361:                                        }
1.1.1.23  root     5362:                                } else {
                   5363:                                        Sleep(10);
                   5364:                                }
                   5365:                        } else {
                   5366:                                if(!update_key_buffer()) {
                   5367:                                        Sleep(10);
                   5368:                                }
1.1.1.14  root     5369:                        }
                   5370:                }
1.1.1.54  root     5371:                if(m_exit) {
1.1.1.33  root     5372:                        // insert CR to terminate input loops
1.1.1.14  root     5373:                        key_char = 0x0d;
                   5374:                        key_scan = 0;
1.1.1.32  root     5375:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5376: #ifdef USE_SERVICE_THREAD
                   5377:                        EnterCriticalSection(&key_buf_crit_sect);
                   5378: #endif
1.1.1.51  root     5379:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     5380: #ifdef USE_SERVICE_THREAD
                   5381:                        LeaveCriticalSection(&key_buf_crit_sect);
                   5382: #endif
1.1.1.5   root     5383:                }
1.1       root     5384:        }
                   5385:        if(echo && key_char) {
                   5386:                msdos_putch(key_char);
                   5387:        }
                   5388:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5389: }
                   5390: 
                   5391: inline int msdos_getch()
                   5392: {
                   5393:        return(msdos_getch_ex(0));
                   5394: }
                   5395: 
                   5396: inline int msdos_getche()
                   5397: {
                   5398:        return(msdos_getch_ex(1));
                   5399: }
                   5400: 
                   5401: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5402: {
1.1.1.37  root     5403:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5404:                // write to serial port
1.1.1.38  root     5405:                int written = 0;
1.1.1.37  root     5406:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5407:                        UINT8 *buf = (UINT8 *)buffer;
                   5408:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5409:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5410:                        DWORD timeout = timeGetTime() + 1000;
                   5411:                        while(written < count) {
                   5412:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5413:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5414:                                        timeout = timeGetTime() + 1000;
                   5415:                                } else {
                   5416:                                        if(timeGetTime() > timeout) {
                   5417:                                                break;
                   5418:                                        }
                   5419:                                        Sleep(10);
                   5420:                                }
1.1.1.37  root     5421:                        }
                   5422:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5423:                }
1.1.1.38  root     5424:                return(written);
1.1.1.37  root     5425:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5426:                // write to printer port
                   5427:                UINT8 *buf = (UINT8 *)buffer;
                   5428:                for(unsigned int i = 0; i < count; i++) {
                   5429: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5430:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5431:                }
                   5432:                return(count);
                   5433:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5434:                // CR+LF -> LF
1.1.1.37  root     5435:                static int is_cr = 0;
1.1       root     5436:                UINT8 *buf = (UINT8 *)buffer;
                   5437:                for(unsigned int i = 0; i < count; i++) {
                   5438:                        UINT8 data = buf[i];
                   5439:                        if(is_cr) {
                   5440:                                if(data != 0x0a) {
                   5441:                                        UINT8 tmp = 0x0d;
                   5442:                                        _write(1, &tmp, 1);
                   5443:                                }
                   5444:                                _write(1, &data, 1);
                   5445:                                is_cr = 0;
                   5446:                        } else if(data == 0x0d) {
                   5447:                                is_cr = 1;
                   5448:                        } else {
                   5449:                                _write(1, &data, 1);
                   5450:                        }
                   5451:                }
                   5452:                return(count);
                   5453:        }
1.1.1.14  root     5454:        vram_flush();
1.1       root     5455:        return(_write(fd, buffer, count));
                   5456: }
                   5457: 
                   5458: void msdos_putch(UINT8 data)
1.1.1.50  root     5459: {
                   5460:        msdos_stdio_reopen();
                   5461:        
                   5462:        process_t *process = msdos_process_info_get(current_psp);
                   5463:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5464:        
                   5465:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5466:                // stdout is redirected to file
                   5467:                msdos_write(fd, &data, 1);
                   5468:                return;
                   5469:        }
                   5470:        
                   5471:        // call int 29h ?
1.1.1.58  root     5472:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     5473:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5474:                // int 29h is not hooked, no need to call int 29h
                   5475:                msdos_putch_fast(data);
                   5476: #ifdef USE_SERVICE_THREAD
                   5477:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5478:                // XXX: in usually we should not reach here
                   5479:                // this is called from service thread to echo the input
                   5480:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5481:                msdos_putch_fast(data);
                   5482: #endif
1.1.1.51  root     5483:        } else if(in_service_29h) {
1.1.1.50  root     5484:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5485:                msdos_putch_fast(data);
                   5486:        } else {
                   5487:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5488:                in_service_29h = true;
1.1.1.50  root     5489:                try {
                   5490:                        UINT32 tmp_pc = m_pc;
                   5491:                        UINT16 tmp_ax = REG16(AX);
                   5492:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5493:                        
                   5494:                        // call int 29h routine is at fffc:0027
                   5495:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5496:                        REG8(AL) = data;
                   5497:                        
                   5498:                        // run cpu until call int 29h routine is done
1.1.1.54  root     5499:                        while(!m_exit && tmp_pc != m_pc) {
1.1.1.50  root     5500:                                try {
                   5501:                                        hardware_run_cpu();
                   5502:                                } catch(...) {
                   5503:                                }
                   5504:                        }
                   5505:                        REG16(AX) = tmp_ax;
                   5506:                        REG16(BX) = tmp_bx;
                   5507:                } catch(...) {
                   5508:                }
1.1.1.51  root     5509:                in_service_29h = false;
1.1.1.50  root     5510:        }
                   5511: }
                   5512: 
                   5513: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5514: #ifdef USE_SERVICE_THREAD
                   5515: {
                   5516:        EnterCriticalSection(&putch_crit_sect);
                   5517:        msdos_putch_tmp(data);
                   5518:        LeaveCriticalSection(&putch_crit_sect);
                   5519: }
                   5520: void msdos_putch_tmp(UINT8 data)
                   5521: #endif
1.1       root     5522: {
1.1.1.34  root     5523:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5524:        SMALL_RECT rect;
                   5525:        COORD co;
1.1       root     5526:        static int p = 0;
                   5527:        static int is_kanji = 0;
                   5528:        static int is_esc = 0;
                   5529:        static int stored_x;
                   5530:        static int stored_y;
                   5531:        static WORD stored_a;
1.1.1.20  root     5532:        static char tmp[64], out[64];
1.1       root     5533:        
1.1.1.23  root     5534:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5535:        
                   5536:        // output to console
                   5537:        tmp[p++] = data;
                   5538:        
1.1.1.14  root     5539:        vram_flush();
                   5540:        
1.1       root     5541:        if(is_kanji) {
                   5542:                // kanji character
                   5543:                is_kanji = 0;
                   5544:        } else if(is_esc) {
                   5545:                // escape sequense
                   5546:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5547:                        p = is_esc = 0;
                   5548:                } else if(tmp[1] == '=' && p == 4) {
                   5549:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5550:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5551:                        SetConsoleCursorPosition(hStdout, co);
                   5552:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5553:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5554:                        cursor_moved = false;
1.1.1.59  root     5555:                        cursor_moved_by_crtc = false;
1.1       root     5556:                        p = is_esc = 0;
                   5557:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
1.1.1.59  root     5558:                        if(cursor_moved_by_crtc) {
                   5559:                                if(!restore_console_on_exit) {
                   5560:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5561:                                        scr_top = csbi.srWindow.Top;
                   5562:                                }
                   5563:                                co.X = mem[0x450 + REG8(BH) * 2];
                   5564:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5565:                                SetConsoleCursorPosition(hStdout, co);
                   5566:                                cursor_moved_by_crtc = false;
                   5567:                        }
1.1       root     5568:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5569:                        co.X = csbi.dwCursorPosition.X;
                   5570:                        co.Y = csbi.dwCursorPosition.Y;
                   5571:                        WORD wAttributes = csbi.wAttributes;
                   5572:                        
                   5573:                        if(tmp[1] == 'D') {
                   5574:                                co.Y++;
                   5575:                        } else if(tmp[1] == 'E') {
                   5576:                                co.X = 0;
                   5577:                                co.Y++;
                   5578:                        } else if(tmp[1] == 'M') {
                   5579:                                co.Y--;
                   5580:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5581:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5582:                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5583:                                co.X = 0;
                   5584:                                co.Y = csbi.srWindow.Top;
1.1       root     5585:                        } else if(tmp[1] == '[') {
                   5586:                                int param[256], params = 0;
                   5587:                                memset(param, 0, sizeof(param));
                   5588:                                for(int i = 2; i < p; i++) {
                   5589:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5590:                                                param[params] *= 10;
                   5591:                                                param[params] += tmp[i] - '0';
                   5592:                                        } else {
                   5593:                                                params++;
                   5594:                                        }
                   5595:                                }
                   5596:                                if(data == 'A') {
1.1.1.14  root     5597:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5598:                                } else if(data == 'B') {
1.1.1.14  root     5599:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5600:                                } else if(data == 'C') {
1.1.1.14  root     5601:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5602:                                } else if(data == 'D') {
1.1.1.14  root     5603:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5604:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5605:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5606:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5607:                                } else if(data == 'J') {
1.1.1.14  root     5608:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5609:                                        if(param[0] == 0) {
                   5610:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5611:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5612:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5613:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5614:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5615:                                                }
                   5616:                                        } else if(param[0] == 1) {
1.1.1.14  root     5617:                                                if(co.Y > csbi.srWindow.Top) {
                   5618:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
1.1.1.60  root     5619:                                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5620:                                                }
                   5621:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5622:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5623:                                        } else if(param[0] == 2) {
1.1.1.14  root     5624:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5625:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5626:                                                co.X = co.Y = 0;
                   5627:                                        }
                   5628:                                } else if(data == 'K') {
1.1.1.14  root     5629:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5630:                                        if(param[0] == 0) {
                   5631:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5632:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5633:                                        } else if(param[0] == 1) {
                   5634:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.60  root     5635:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5636:                                        } else if(param[0] == 2) {
                   5637:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.60  root     5638:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5639:                                        }
                   5640:                                } else if(data == 'L') {
1.1.1.14  root     5641:                                        if(params == 0) {
                   5642:                                                param[0] = 1;
1.1       root     5643:                                        }
1.1.1.14  root     5644:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5645:                                        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5646:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5647:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5648:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5649:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.60  root     5650:                                        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5651:                                        co.X = 0;
                   5652:                                } else if(data == 'M') {
1.1.1.14  root     5653:                                        if(params == 0) {
                   5654:                                                param[0] = 1;
                   5655:                                        }
                   5656:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5657:                                                clear_scr_buffer(csbi.wAttributes);
                   5658:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5659:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5660:                                        } else {
1.1.1.14  root     5661:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5662:                                                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5663:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     5664:                                                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     5665:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5666:                                        }
                   5667:                                        co.X = 0;
                   5668:                                } else if(data == 'h') {
                   5669:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5670:                                                ci_new.bVisible = FALSE;
1.1       root     5671:                                        }
                   5672:                                } else if(data == 'l') {
                   5673:                                        if(tmp[2] == '>' && tmp[3] == '5') {
1.1.1.60  root     5674:                                                ci_new.bVisible = TRUE;
1.1       root     5675:                                        }
                   5676:                                } else if(data == 'm') {
                   5677:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5678:                                        int reverse = 0, hidden = 0;
                   5679:                                        for(int i = 0; i < params; i++) {
                   5680:                                                if(param[i] == 1) {
                   5681:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5682:                                                } else if(param[i] == 4) {
                   5683:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5684:                                                } else if(param[i] == 7) {
                   5685:                                                        reverse = 1;
                   5686:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5687:                                                        hidden = 1;
                   5688:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5689:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5690:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5691:                                                                param[i] -= 16;
                   5692:                                                        } else {
                   5693:                                                                param[i] -= 30;
                   5694:                                                        }
                   5695:                                                        if(param[i] & 1) {
                   5696:                                                                wAttributes |= FOREGROUND_RED;
                   5697:                                                        }
                   5698:                                                        if(param[i] & 2) {
                   5699:                                                                wAttributes |= FOREGROUND_GREEN;
                   5700:                                                        }
                   5701:                                                        if(param[i] & 4) {
                   5702:                                                                wAttributes |= FOREGROUND_BLUE;
                   5703:                                                        }
                   5704:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5705:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5706:                                                        if((param[i] - 40) & 1) {
                   5707:                                                                wAttributes |= BACKGROUND_RED;
                   5708:                                                        }
                   5709:                                                        if((param[i] - 40) & 2) {
                   5710:                                                                wAttributes |= BACKGROUND_GREEN;
                   5711:                                                        }
                   5712:                                                        if((param[i] - 40) & 4) {
                   5713:                                                                wAttributes |= BACKGROUND_BLUE;
                   5714:                                                        }
                   5715:                                                }
                   5716:                                        }
                   5717:                                        if(reverse) {
                   5718:                                                wAttributes &= ~0xff;
                   5719:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5720:                                        }
                   5721:                                        if(hidden) {
                   5722:                                                wAttributes &= ~0x0f;
                   5723:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5724:                                        }
                   5725:                                } else if(data == 'n') {
                   5726:                                        if(param[0] == 6) {
                   5727:                                                char tmp[16];
                   5728:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5729:                                                int len = strlen(tmp);
1.1.1.32  root     5730:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5731: #ifdef USE_SERVICE_THREAD
                   5732:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5733: #endif
1.1.1.32  root     5734:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5735:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5736:                                                        }
1.1.1.35  root     5737: #ifdef USE_SERVICE_THREAD
                   5738:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5739: #endif
1.1       root     5740:                                                }
                   5741:                                        }
                   5742:                                } else if(data == 's') {
                   5743:                                        stored_x = co.X;
                   5744:                                        stored_y = co.Y;
                   5745:                                        stored_a = wAttributes;
                   5746:                                } else if(data == 'u') {
                   5747:                                        co.X = stored_x;
                   5748:                                        co.Y = stored_y;
                   5749:                                        wAttributes = stored_a;
                   5750:                                }
                   5751:                        }
                   5752:                        if(co.X < 0) {
                   5753:                                co.X = 0;
                   5754:                        } else if(co.X >= csbi.dwSize.X) {
                   5755:                                co.X = csbi.dwSize.X - 1;
                   5756:                        }
1.1.1.14  root     5757:                        if(co.Y < csbi.srWindow.Top) {
                   5758:                                co.Y = csbi.srWindow.Top;
                   5759:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5760:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5761:                        }
                   5762:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5763:                                SetConsoleCursorPosition(hStdout, co);
                   5764:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5765:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5766:                                cursor_moved = false;
                   5767:                        }
                   5768:                        if(wAttributes != csbi.wAttributes) {
                   5769:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5770:                        }
                   5771:                        p = is_esc = 0;
                   5772:                }
                   5773:                return;
                   5774:        } else {
                   5775:                if(msdos_lead_byte_check(data)) {
                   5776:                        is_kanji = 1;
                   5777:                        return;
                   5778:                } else if(data == 0x1b) {
                   5779:                        is_esc = 1;
                   5780:                        return;
                   5781:                }
                   5782:        }
1.1.1.20  root     5783:        
                   5784:        DWORD q = 0, num;
                   5785:        is_kanji = 0;
                   5786:        for(int i = 0; i < p; i++) {
                   5787:                UINT8 c = tmp[i];
                   5788:                if(is_kanji) {
                   5789:                        is_kanji = 0;
                   5790:                } else if(msdos_lead_byte_check(data)) {
                   5791:                        is_kanji = 1;
                   5792:                } else if(msdos_ctrl_code_check(data)) {
                   5793:                        out[q++] = '^';
                   5794:                        c += 'A' - 1;
                   5795:                }
                   5796:                out[q++] = c;
                   5797:        }
1.1.1.59  root     5798:        if(cursor_moved_by_crtc) {
                   5799:                if(!restore_console_on_exit) {
                   5800:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5801:                        scr_top = csbi.srWindow.Top;
                   5802:                }
                   5803:                co.X = mem[0x450 + REG8(BH) * 2];
                   5804:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   5805:                SetConsoleCursorPosition(hStdout, co);
                   5806:                cursor_moved_by_crtc = false;
                   5807:        }
1.1.1.34  root     5808:        if(q == 1 && out[0] == 0x08) {
                   5809:                // back space
                   5810:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5811:                if(csbi.dwCursorPosition.X > 0) {
                   5812:                        co.X = csbi.dwCursorPosition.X - 1;
                   5813:                        co.Y = csbi.dwCursorPosition.Y;
                   5814:                        SetConsoleCursorPosition(hStdout, co);
                   5815:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5816:                        co.X = csbi.dwSize.X - 1;
                   5817:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5818:                        SetConsoleCursorPosition(hStdout, co);
                   5819:                } else {
1.1.1.60  root     5820:                        WriteConsoleA(hStdout, out, q, &num, NULL); // to make sure
1.1.1.34  root     5821:                }
                   5822:        } else {
1.1.1.60  root     5823:                WriteConsoleA(hStdout, out, q, &num, NULL);
1.1.1.34  root     5824:        }
1.1       root     5825:        p = 0;
1.1.1.14  root     5826:        
1.1.1.15  root     5827:        if(!restore_console_on_exit) {
                   5828:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5829:                scr_top = csbi.srWindow.Top;
                   5830:        }
1.1       root     5831:        cursor_moved = true;
                   5832: }
                   5833: 
                   5834: int msdos_aux_in()
                   5835: {
1.1.1.21  root     5836:        msdos_stdio_reopen();
                   5837:        
1.1.1.20  root     5838:        process_t *process = msdos_process_info_get(current_psp);
                   5839:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5840:        
                   5841:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5842:                char data = 0;
1.1.1.37  root     5843:                msdos_read(fd, &data, 1);
1.1       root     5844:                return(data);
                   5845:        } else {
                   5846:                return(EOF);
                   5847:        }
                   5848: }
                   5849: 
                   5850: void msdos_aux_out(char data)
                   5851: {
1.1.1.21  root     5852:        msdos_stdio_reopen();
                   5853:        
1.1.1.20  root     5854:        process_t *process = msdos_process_info_get(current_psp);
                   5855:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5856:        
                   5857:        if(fd < process->max_files && file_handler[fd].valid) {
                   5858:                msdos_write(fd, &data, 1);
1.1       root     5859:        }
                   5860: }
                   5861: 
                   5862: void msdos_prn_out(char data)
                   5863: {
1.1.1.21  root     5864:        msdos_stdio_reopen();
                   5865:        
1.1.1.20  root     5866:        process_t *process = msdos_process_info_get(current_psp);
                   5867:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5868:        
                   5869:        if(fd < process->max_files && file_handler[fd].valid) {
                   5870:                msdos_write(fd, &data, 1);
1.1       root     5871:        }
                   5872: }
                   5873: 
                   5874: // memory control
                   5875: 
1.1.1.52  root     5876: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5877: {
                   5878:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5879:        
                   5880:        mcb->mz = mz;
                   5881:        mcb->psp = psp;
1.1.1.30  root     5882:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5883:        
                   5884:        if(prog_name != NULL) {
                   5885:                memset(mcb->prog_name, 0, 8);
                   5886:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5887:        }
1.1       root     5888:        return(mcb);
                   5889: }
                   5890: 
                   5891: void msdos_mcb_check(mcb_t *mcb)
                   5892: {
                   5893:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5894:                #if 0
                   5895:                        // shutdown now !!!
                   5896:                        fatalerror("broken memory control block\n");
                   5897:                #else
                   5898:                        // return error code and continue
                   5899:                        throw(0x07); // broken memory control block
                   5900:                #endif
1.1       root     5901:        }
                   5902: }
                   5903: 
1.1.1.39  root     5904: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5905: {
                   5906:        int mcb_seg = seg - 1;
                   5907:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5908:        msdos_mcb_check(mcb);
                   5909:        
1.1.1.30  root     5910:        if(mcb->paragraphs > paragraphs) {
1.1       root     5911:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5912:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5913:                
                   5914:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5915:                mcb->mz = 'M';
1.1.1.30  root     5916:                mcb->paragraphs = paragraphs;
1.1       root     5917:        }
                   5918: }
                   5919: 
                   5920: void msdos_mem_merge(int seg)
                   5921: {
                   5922:        int mcb_seg = seg - 1;
                   5923:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5924:        msdos_mcb_check(mcb);
                   5925:        
                   5926:        while(1) {
                   5927:                if(mcb->mz == 'Z') {
                   5928:                        break;
                   5929:                }
1.1.1.30  root     5930:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5931:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5932:                msdos_mcb_check(next_mcb);
                   5933:                
                   5934:                if(next_mcb->psp != 0) {
                   5935:                        break;
                   5936:                }
                   5937:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5938:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5939:        }
                   5940: }
                   5941: 
1.1.1.8   root     5942: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5943: {
                   5944:        while(1) {
                   5945:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5946:                bool last_block;
1.1       root     5947:                
1.1.1.14  root     5948:                if(mcb->psp == 0) {
                   5949:                        msdos_mem_merge(mcb_seg + 1);
                   5950:                } else {
                   5951:                        msdos_mcb_check(mcb);
                   5952:                }
1.1.1.33  root     5953:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5954:                        // check if the next is dummy mcb to link to umb
                   5955:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5956:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5957:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5958:                }
                   5959:                if(!(new_process && !last_block)) {
1.1.1.30  root     5960:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5961:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5962:                                mcb->psp = current_psp;
                   5963:                                return(mcb_seg + 1);
                   5964:                        }
                   5965:                }
                   5966:                if(mcb->mz == 'Z') {
                   5967:                        break;
                   5968:                }
1.1.1.30  root     5969:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5970:        }
                   5971:        return(-1);
                   5972: }
                   5973: 
                   5974: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5975: {
                   5976:        int mcb_seg = seg - 1;
                   5977:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5978:        msdos_mcb_check(mcb);
1.1.1.30  root     5979:        int current_paragraphs = mcb->paragraphs;
1.1       root     5980:        
                   5981:        msdos_mem_merge(seg);
1.1.1.30  root     5982:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5983:                if(max_paragraphs) {
1.1.1.30  root     5984:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5985:                }
1.1       root     5986:                msdos_mem_split(seg, current_paragraphs);
                   5987:                return(-1);
                   5988:        }
                   5989:        msdos_mem_split(seg, paragraphs);
                   5990:        return(0);
                   5991: }
                   5992: 
                   5993: void msdos_mem_free(int seg)
                   5994: {
                   5995:        int mcb_seg = seg - 1;
                   5996:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5997:        msdos_mcb_check(mcb);
                   5998:        
                   5999:        mcb->psp = 0;
                   6000:        msdos_mem_merge(seg);
                   6001: }
                   6002: 
1.1.1.8   root     6003: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     6004: {
                   6005:        int max_paragraphs = 0;
                   6006:        
                   6007:        while(1) {
                   6008:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     6009:                bool last_block;
                   6010:                
1.1       root     6011:                msdos_mcb_check(mcb);
                   6012:                
1.1.1.33  root     6013:                if(!(last_block = (mcb->mz == 'Z'))) {
                   6014:                        // check if the next is dummy mcb to link to umb
                   6015:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   6016:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   6017:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   6018:                }
                   6019:                if(!(new_process && !last_block)) {
1.1.1.30  root     6020:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   6021:                                max_paragraphs = mcb->paragraphs;
1.1       root     6022:                        }
                   6023:                }
                   6024:                if(mcb->mz == 'Z') {
                   6025:                        break;
                   6026:                }
1.1.1.30  root     6027:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     6028:        }
1.1.1.14  root     6029:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     6030: }
                   6031: 
1.1.1.8   root     6032: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   6033: {
                   6034:        int last_seg = -1;
                   6035:        
                   6036:        while(1) {
                   6037:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   6038:                msdos_mcb_check(mcb);
                   6039:                
1.1.1.14  root     6040:                if(mcb->psp == psp) {
1.1.1.8   root     6041:                        last_seg = mcb_seg;
                   6042:                }
1.1.1.14  root     6043:                if(mcb->mz == 'Z') {
                   6044:                        break;
                   6045:                }
1.1.1.30  root     6046:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     6047:        }
                   6048:        return(last_seg);
                   6049: }
                   6050: 
1.1.1.19  root     6051: int msdos_mem_get_umb_linked()
                   6052: {
1.1.1.33  root     6053:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6054:        msdos_mcb_check(mcb);
1.1.1.19  root     6055:        
1.1.1.33  root     6056:        if(mcb->mz == 'M') {
                   6057:                return(-1);
1.1.1.19  root     6058:        }
                   6059:        return(0);
                   6060: }
                   6061: 
1.1.1.33  root     6062: void msdos_mem_link_umb()
1.1.1.19  root     6063: {
1.1.1.33  root     6064:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6065:        msdos_mcb_check(mcb);
1.1.1.19  root     6066:        
1.1.1.33  root     6067:        mcb->mz = 'M';
                   6068:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     6069:        
                   6070:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     6071: }
                   6072: 
1.1.1.33  root     6073: void msdos_mem_unlink_umb()
1.1.1.19  root     6074: {
1.1.1.33  root     6075:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   6076:        msdos_mcb_check(mcb);
1.1.1.19  root     6077:        
1.1.1.33  root     6078:        mcb->mz = 'Z';
                   6079:        mcb->paragraphs = 0;
1.1.1.39  root     6080:        
                   6081:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     6082: }
                   6083: 
1.1.1.29  root     6084: #ifdef SUPPORT_HMA
                   6085: 
                   6086: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   6087: {
                   6088:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6089:        
                   6090:        mcb->ms[0] = 'M';
                   6091:        mcb->ms[1] = 'S';
                   6092:        mcb->owner = owner;
                   6093:        mcb->size = size;
                   6094:        mcb->next = next;
                   6095:        return(mcb);
                   6096: }
                   6097: 
                   6098: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   6099: {
                   6100:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   6101: }
                   6102: 
                   6103: int msdos_hma_mem_split(int offset, int size)
                   6104: {
                   6105:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6106:        
                   6107:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6108:                return(-1);
                   6109:        }
                   6110:        if(mcb->size >= size + 0x10) {
                   6111:                int new_offset = offset + 0x10 + size;
                   6112:                int new_size = mcb->size - 0x10 - size;
                   6113:                
                   6114:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   6115:                mcb->size = size;
                   6116:                mcb->next = new_offset;
                   6117:                return(0);
                   6118:        }
                   6119:        return(-1);
                   6120: }
                   6121: 
                   6122: void msdos_hma_mem_merge(int offset)
                   6123: {
                   6124:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6125:        
                   6126:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6127:                return;
                   6128:        }
                   6129:        while(1) {
                   6130:                if(mcb->next == 0) {
                   6131:                        break;
                   6132:                }
                   6133:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   6134:                
                   6135:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   6136:                        return;
                   6137:                }
                   6138:                if(next_mcb->owner != 0) {
                   6139:                        break;
                   6140:                }
                   6141:                mcb->size += 0x10 + next_mcb->size;
                   6142:                mcb->next = next_mcb->next;
                   6143:        }
                   6144: }
                   6145: 
                   6146: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   6147: {
                   6148:        int offset = 0x10; // first mcb in HMA
                   6149:        
                   6150:        while(1) {
                   6151:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6152:                
                   6153:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6154:                        return(-1);
                   6155:                }
                   6156:                if(mcb->owner == 0) {
                   6157:                        msdos_hma_mem_merge(offset);
                   6158:                }
                   6159:                if(mcb->owner == 0 && mcb->size >= size) {
                   6160:                        msdos_hma_mem_split(offset, size);
                   6161:                        mcb->owner = owner;
                   6162:                        return(offset);
                   6163:                }
                   6164:                if(mcb->next == 0) {
                   6165:                        break;
                   6166:                }
                   6167:                offset = mcb->next;
                   6168:        }
                   6169:        return(-1);
                   6170: }
                   6171: 
                   6172: int msdos_hma_mem_realloc(int offset, int size)
                   6173: {
                   6174:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6175:        
                   6176:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6177:                return(-1);
                   6178:        }
                   6179:        if(mcb->size < size) {
                   6180:                return(-1);
                   6181:        }
                   6182:        msdos_hma_mem_split(offset, size);
                   6183:        return(0);
                   6184: }
                   6185: 
                   6186: void msdos_hma_mem_free(int offset)
                   6187: {
                   6188:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6189:        
                   6190:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   6191:                return;
                   6192:        }
                   6193:        mcb->owner = 0;
                   6194:        msdos_hma_mem_merge(offset);
                   6195: }
                   6196: 
                   6197: int msdos_hma_mem_get_free(int *available_offset)
                   6198: {
                   6199:        int offset = 0x10; // first mcb in HMA
                   6200:        int size = 0;
                   6201:        
                   6202:        while(1) {
                   6203:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   6204:                
                   6205:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   6206:                        return(0);
                   6207:                }
                   6208:                if(mcb->owner == 0 && size < mcb->size) {
                   6209:                        if(available_offset != NULL) {
                   6210:                                *available_offset = offset;
                   6211:                        }
                   6212:                        size = mcb->size;
                   6213:                }
                   6214:                if(mcb->next == 0) {
                   6215:                        break;
                   6216:                }
                   6217:                offset = mcb->next;
                   6218:        }
                   6219:        return(size);
                   6220: }
                   6221: 
                   6222: #endif
                   6223: 
1.1       root     6224: // environment
                   6225: 
1.1.1.45  root     6226: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     6227: {
                   6228:        char *dst = (char *)(mem + (env_seg << 4));
                   6229:        
                   6230:        while(1) {
                   6231:                if(dst[0] == 0) {
                   6232:                        break;
                   6233:                }
                   6234:                dst += strlen(dst) + 1;
                   6235:        }
                   6236:        *dst++ = 0; // end of environment
                   6237:        *dst++ = 1; // top of argv[0]
                   6238:        *dst++ = 0;
                   6239:        memcpy(dst, argv, strlen(argv));
                   6240:        dst += strlen(argv);
                   6241:        *dst++ = 0;
                   6242:        *dst++ = 0;
                   6243: }
                   6244: 
1.1.1.45  root     6245: const char *msdos_env_get_argv(int env_seg)
1.1       root     6246: {
                   6247:        static char env[ENV_SIZE];
                   6248:        char *src = env;
                   6249:        
                   6250:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6251:        while(1) {
                   6252:                if(src[0] == 0) {
                   6253:                        if(src[1] == 1) {
                   6254:                                return(src + 3);
                   6255:                        }
                   6256:                        break;
                   6257:                }
                   6258:                src += strlen(src) + 1;
                   6259:        }
                   6260:        return(NULL);
                   6261: }
                   6262: 
1.1.1.45  root     6263: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     6264: {
                   6265:        static char env[ENV_SIZE];
                   6266:        char *src = env;
                   6267:        
                   6268:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   6269:        while(1) {
                   6270:                if(src[0] == 0) {
                   6271:                        break;
                   6272:                }
                   6273:                int len = strlen(src);
                   6274:                char *n = my_strtok(src, "=");
                   6275:                char *v = src + strlen(n) + 1;
                   6276:                
                   6277:                if(_stricmp(name, n) == 0) {
                   6278:                        return(v);
                   6279:                }
                   6280:                src += len + 1;
                   6281:        }
                   6282:        return(NULL);
                   6283: }
                   6284: 
1.1.1.45  root     6285: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     6286: {
                   6287:        char env[ENV_SIZE];
                   6288:        char *src = env;
                   6289:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     6290:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     6291:        int done = 0;
                   6292:        
                   6293:        memcpy(src, dst, ENV_SIZE);
                   6294:        memset(dst, 0, ENV_SIZE);
                   6295:        while(1) {
                   6296:                if(src[0] == 0) {
                   6297:                        break;
                   6298:                }
                   6299:                int len = strlen(src);
                   6300:                char *n = my_strtok(src, "=");
                   6301:                char *v = src + strlen(n) + 1;
                   6302:                char tmp[1024];
                   6303:                
                   6304:                if(_stricmp(name, n) == 0) {
                   6305:                        sprintf(tmp, "%s=%s", n, value);
                   6306:                        done = 1;
                   6307:                } else {
                   6308:                        sprintf(tmp, "%s=%s", n, v);
                   6309:                }
                   6310:                memcpy(dst, tmp, strlen(tmp));
                   6311:                dst += strlen(tmp) + 1;
                   6312:                src += len + 1;
                   6313:        }
                   6314:        if(!done) {
                   6315:                char tmp[1024];
                   6316:                
                   6317:                sprintf(tmp, "%s=%s", name, value);
                   6318:                memcpy(dst, tmp, strlen(tmp));
                   6319:                dst += strlen(tmp) + 1;
                   6320:        }
                   6321:        if(argv) {
                   6322:                *dst++ = 0; // end of environment
                   6323:                *dst++ = 1; // top of argv[0]
                   6324:                *dst++ = 0;
                   6325:                memcpy(dst, argv, strlen(argv));
                   6326:                dst += strlen(argv);
                   6327:                *dst++ = 0;
                   6328:                *dst++ = 0;
                   6329:        }
                   6330: }
                   6331: 
                   6332: // process
                   6333: 
1.1.1.8   root     6334: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     6335: {
                   6336:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6337:        
                   6338:        memset(psp, 0, PSP_SIZE);
                   6339:        psp->exit[0] = 0xcd;
                   6340:        psp->exit[1] = 0x20;
1.1.1.8   root     6341:        psp->first_mcb = mcb_seg;
1.1.1.46  root     6342: #if 1
1.1.1.49  root     6343:        psp->call5[0] = 0xcd;   // int 30h
                   6344:        psp->call5[1] = 0x30;
1.1.1.46  root     6345:        psp->call5[2] = 0xc3;   // ret
                   6346: #else
                   6347:        psp->call5[0] = 0x8a;   // mov ah, cl
                   6348:        psp->call5[1] = 0xe1;
                   6349:        psp->call5[2] = 0xcd;   // int 21h
                   6350:        psp->call5[3] = 0x21;
                   6351:        psp->call5[4] = 0xc3;   // ret
                   6352: #endif
1.1       root     6353:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   6354:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   6355:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   6356:        psp->parent_psp = parent_psp;
1.1.1.20  root     6357:        if(parent_psp == (UINT16)-1) {
                   6358:                for(int i = 0; i < 20; i++) {
                   6359:                        if(file_handler[i].valid) {
                   6360:                                psp->file_table[i] = i;
                   6361:                        } else {
                   6362:                                psp->file_table[i] = 0xff;
                   6363:                        }
1.1       root     6364:                }
1.1.1.20  root     6365:        } else {
                   6366:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     6367:        }
                   6368:        psp->env_seg = env_seg;
                   6369:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     6370:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     6371:        psp->file_table_size = 20;
                   6372:        psp->file_table_ptr.w.l = 0x18;
                   6373:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     6374:        psp->service[0] = 0xcd;
                   6375:        psp->service[1] = 0x21;
                   6376:        psp->service[2] = 0xcb;
                   6377:        return(psp);
                   6378: }
                   6379: 
1.1.1.20  root     6380: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   6381: {
                   6382:        if(psp_seg && fd < 20) {
                   6383:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6384:                psp->file_table[fd] = value;
                   6385:        }
                   6386: }
                   6387: 
                   6388: int msdos_psp_get_file_table(int fd, int psp_seg)
                   6389: {
                   6390:        if(psp_seg && fd < 20) {
                   6391:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6392:                fd = psp->file_table[fd];
                   6393:        }
                   6394:        return fd;
                   6395: }
                   6396: 
1.1.1.52  root     6397: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     6398: {
                   6399:        // load command file
                   6400:        int fd = -1;
1.1.1.45  root     6401:        int sio_port = 0;
                   6402:        int lpt_port = 0;
1.1       root     6403:        int dos_command = 0;
1.1.1.24  root     6404:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6405:        char pipe_stdin_path[MAX_PATH] = {0};
                   6406:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6407:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6408:        
                   6409:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6410:        int opt_len = mem[opt_ofs];
                   6411:        memset(opt, 0, sizeof(opt));
                   6412:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6413:        
1.1.1.14  root     6414:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6415:                // this is a batch file, run command.com
                   6416:                char tmp[MAX_PATH];
                   6417:                if(opt_len != 0) {
                   6418:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6419:                } else {
                   6420:                        sprintf(tmp, "/C %s", cmd);
                   6421:                }
                   6422:                strcpy(opt, tmp);
                   6423:                opt_len = strlen(opt);
                   6424:                mem[opt_ofs] = opt_len;
                   6425:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6426:                strcpy(command, comspec_path);
                   6427:                strcpy(name_tmp, "COMMAND.COM");
                   6428:        } else {
                   6429:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6430:                        // redirect C:\COMMAND.COM to comspec_path
                   6431:                        strcpy(command, comspec_path);
                   6432:                } else {
                   6433:                        strcpy(command, cmd);
                   6434:                }
1.1.1.60  root     6435:                if(GetFullPathNameA(command, MAX_PATH, path, &name) == 0) {
1.1.1.24  root     6436:                        return(-1);
                   6437:                }
1.1.1.14  root     6438:                memset(name_tmp, 0, sizeof(name_tmp));
                   6439:                strcpy(name_tmp, name);
                   6440:                
                   6441:                // check command.com
1.1.1.38  root     6442:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6443:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6444:                        if(opt_len == 0) {
                   6445: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6446:                                process_t *current_process = NULL;
                   6447:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6448:                                        if(process[i].psp == current_psp) {
                   6449:                                                current_process = &process[i];
                   6450:                                                break;
                   6451:                                        }
                   6452:                                }
                   6453:                                if(current_process != NULL) {
                   6454:                                        param->cmd_line.dw = current_process->dta.dw;
                   6455:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6456:                                        opt_len = mem[opt_ofs];
                   6457:                                        memset(opt, 0, sizeof(opt));
                   6458:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6459:                                }
                   6460:                        }
                   6461:                        for(int i = 0; i < opt_len; i++) {
                   6462:                                if(opt[i] == ' ') {
                   6463:                                        continue;
                   6464:                                }
                   6465:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6466:                                        for(int j = i + 3; j < opt_len; j++) {
                   6467:                                                if(opt[j] == ' ') {
                   6468:                                                        continue;
                   6469:                                                }
                   6470:                                                char *token = my_strtok(opt + j, " ");
                   6471:                                                
1.1.1.38  root     6472:                                                strcpy(command, token);
                   6473:                                                char tmp[MAX_PATH];
                   6474:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6475:                                                strcpy(opt, "");
                   6476:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6477:                                                        if(tmp[i] != ' ') {
                   6478:                                                                strcpy(opt, tmp + i);
                   6479:                                                                break;
                   6480:                                                        }
                   6481:                                                }
                   6482:                                                strcpy(tmp, opt);
1.1.1.38  root     6483:                                                
                   6484:                                                if(al == 0x00) {
1.1.1.39  root     6485:                                                        #define GET_FILE_PATH() { \
                   6486:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6487:                                                                        token++; \
                   6488:                                                                } \
                   6489:                                                                token++; \
                   6490:                                                                while(*token == ' ') { \
                   6491:                                                                        token++; \
                   6492:                                                                } \
                   6493:                                                                char *ptr = token; \
                   6494:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6495:                                                                        ptr++; \
                   6496:                                                                } \
                   6497:                                                                *ptr = '\0'; \
                   6498:                                                        }
                   6499:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6500:                                                                GET_FILE_PATH();
1.1.1.38  root     6501:                                                                strcpy(pipe_stdin_path, token);
                   6502:                                                                strcpy(opt, tmp);
                   6503:                                                        }
1.1.1.39  root     6504:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6505:                                                                GET_FILE_PATH();
1.1.1.38  root     6506:                                                                strcpy(pipe_stdout_path, token);
                   6507:                                                                strcpy(opt, tmp);
                   6508:                                                        }
1.1.1.39  root     6509:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6510:                                                                GET_FILE_PATH();
                   6511:                                                                strcpy(pipe_stderr_path, token);
                   6512:                                                                strcpy(opt, tmp);
                   6513:                                                        }
                   6514:                                                        #undef GET_FILE_PATH
                   6515:                                                        
                   6516:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6517:                                                                *token = '\0';
                   6518:                                                        }
                   6519:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6520:                                                                *token = '\0';
                   6521:                                                        }
                   6522:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6523:                                                                *token = '\0';
                   6524:                                                        }
1.1.1.38  root     6525:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6526:                                                                *token = '\0';
                   6527:                                                        }
                   6528:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6529:                                                                *token = '\0';
                   6530:                                                        }
1.1.1.14  root     6531:                                                }
1.1.1.39  root     6532:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6533:                                                        opt[i] = '\0';
                   6534:                                                }
1.1.1.38  root     6535:                                                opt_len = strlen(opt);
                   6536:                                                mem[opt_ofs] = opt_len;
                   6537:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6538:                                                dos_command = 1;
1.1.1.14  root     6539:                                                break;
1.1       root     6540:                                        }
                   6541:                                }
1.1.1.14  root     6542:                                break;
1.1       root     6543:                        }
                   6544:                }
                   6545:        }
                   6546:        
                   6547:        // load command file
                   6548:        strcpy(path, command);
                   6549:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6550:                sprintf(path, "%s.COM", command);
                   6551:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6552:                        sprintf(path, "%s.EXE", command);
                   6553:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6554:                                sprintf(path, "%s.BAT", command);
                   6555:                                if(_access(path, 0) == 0) {
                   6556:                                        // this is a batch file, run command.com
                   6557:                                        char tmp[MAX_PATH];
                   6558:                                        if(opt_len != 0) {
                   6559:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6560:                                        } else {
                   6561:                                                sprintf(tmp, "/C %s", path);
                   6562:                                        }
                   6563:                                        strcpy(opt, tmp);
                   6564:                                        opt_len = strlen(opt);
                   6565:                                        mem[opt_ofs] = opt_len;
                   6566:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6567:                                        strcpy(path, comspec_path);
                   6568:                                        strcpy(name_tmp, "COMMAND.COM");
                   6569:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6570:                                } else {
                   6571:                                        // search path in parent environments
                   6572:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6573:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6574:                                        if(env != NULL) {
                   6575:                                                char env_path[4096];
                   6576:                                                strcpy(env_path, env);
                   6577:                                                char *token = my_strtok(env_path, ";");
                   6578:                                                
                   6579:                                                while(token != NULL) {
                   6580:                                                        if(strlen(token) != 0) {
                   6581:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6582:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6583:                                                                        break;
                   6584:                                                                }
                   6585:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6586:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6587:                                                                        break;
                   6588:                                                                }
                   6589:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6590:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6591:                                                                        break;
                   6592:                                                                }
                   6593:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6594:                                                                if(_access(path, 0) == 0) {
                   6595:                                                                        // this is a batch file, run command.com
                   6596:                                                                        char tmp[MAX_PATH];
                   6597:                                                                        if(opt_len != 0) {
                   6598:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6599:                                                                        } else {
                   6600:                                                                                sprintf(tmp, "/C %s", path);
                   6601:                                                                        }
                   6602:                                                                        strcpy(opt, tmp);
                   6603:                                                                        opt_len = strlen(opt);
                   6604:                                                                        mem[opt_ofs] = opt_len;
                   6605:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6606:                                                                        strcpy(path, comspec_path);
                   6607:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6608:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6609:                                                                        break;
                   6610:                                                                }
1.1.1.8   root     6611:                                                        }
1.1.1.14  root     6612:                                                        token = my_strtok(NULL, ";");
1.1       root     6613:                                                }
                   6614:                                        }
                   6615:                                }
                   6616:                        }
                   6617:                }
                   6618:        }
                   6619:        if(fd == -1) {
1.1.1.38  root     6620:                // we can not find command.com in the path, so open comspec_path
                   6621:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6622:                        strcpy(command, comspec_path);
                   6623:                        strcpy(path, command);
                   6624:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6625:                }
                   6626:        }
                   6627:        if(fd == -1) {
1.1.1.52  root     6628:                if(!first_process && al == 0 && dos_command) {
1.1       root     6629:                        // may be dos command
                   6630:                        char tmp[MAX_PATH];
1.1.1.52  root     6631:                        if(opt_len != 0) {
                   6632:                                sprintf(tmp, "%s %s", command, opt);
                   6633:                        } else {
                   6634:                                sprintf(tmp, "%s", command);
                   6635:                        }
                   6636:                        retval = system(tmp);
1.1       root     6637:                        return(0);
                   6638:                } else {
                   6639:                        return(-1);
                   6640:                }
                   6641:        }
1.1.1.52  root     6642:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6643:        _read(fd, file_buffer, sizeof(file_buffer));
                   6644:        _close(fd);
                   6645:        
1.1.1.52  root     6646:        // check if this is win32 program
                   6647:        if(!first_process && al == 0) {
                   6648:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
                   6649:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
                   6650:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
                   6651:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
                   6652:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
                   6653:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
                   6654:                                char tmp[MAX_PATH];
                   6655:                                if(opt_len != 0) {
                   6656:                                        sprintf(tmp, "\"%s\" %s", path, opt);
                   6657:                                } else {
                   6658:                                        sprintf(tmp, "\"%s\"", path);
                   6659:                                }
                   6660:                                retval = system(tmp);
                   6661:                                return(0);
                   6662:                        }
                   6663:                }
                   6664:        }
                   6665:        
1.1       root     6666:        // copy environment
1.1.1.29  root     6667:        int umb_linked, env_seg, psp_seg;
1.1       root     6668:        
1.1.1.29  root     6669:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6670:                msdos_mem_unlink_umb();
                   6671:        }
1.1.1.8   root     6672:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6673:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6674:                        if(umb_linked != 0) {
                   6675:                                msdos_mem_link_umb();
                   6676:                        }
                   6677:                        return(-1);
                   6678:                }
1.1       root     6679:        }
                   6680:        if(param->env_seg == 0) {
                   6681:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6682:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6683:        } else {
                   6684:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6685:        }
                   6686:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6687:        
                   6688:        // check exe header
                   6689:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6690:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6691:        UINT16 cs, ss, ip, sp;
                   6692:        
                   6693:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6694:                // memory allocation
                   6695:                int header_size = header->header_size * 16;
                   6696:                int load_size = header->pages * 512 - header_size;
                   6697:                if(header_size + load_size < 512) {
                   6698:                        load_size = 512 - header_size;
                   6699:                }
                   6700:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6701:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6702:                        msdos_mem_free(env_seg);
                   6703:                        return(-1);
                   6704:                }
                   6705:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6706:                if(paragraphs > free_paragraphs) {
                   6707:                        paragraphs = free_paragraphs;
                   6708:                }
1.1.1.8   root     6709:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6710:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6711:                                if(umb_linked != 0) {
                   6712:                                        msdos_mem_link_umb();
                   6713:                                }
                   6714:                                msdos_mem_free(env_seg);
                   6715:                                return(-1);
                   6716:                        }
1.1       root     6717:                }
                   6718:                // relocation
                   6719:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6720:                for(int i = 0; i < header->relocations; i++) {
                   6721:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6722:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6723:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6724:                }
                   6725:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6726:                // segments
                   6727:                cs = header->init_cs + start_seg;
                   6728:                ss = header->init_ss + start_seg;
                   6729:                ip = header->init_ip;
                   6730:                sp = header->init_sp - 2; // for symdeb
                   6731:        } else {
                   6732:                // memory allocation
                   6733:                paragraphs = free_paragraphs;
1.1.1.8   root     6734:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6735:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6736:                                if(umb_linked != 0) {
                   6737:                                        msdos_mem_link_umb();
                   6738:                                }
                   6739:                                msdos_mem_free(env_seg);
                   6740:                                return(-1);
                   6741:                        }
1.1       root     6742:                }
                   6743:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6744:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6745:                // segments
                   6746:                cs = ss = psp_seg;
                   6747:                ip = 0x100;
                   6748:                sp = 0xfffe;
                   6749:        }
1.1.1.29  root     6750:        if(umb_linked != 0) {
                   6751:                msdos_mem_link_umb();
                   6752:        }
1.1       root     6753:        
                   6754:        // create psp
1.1.1.3   root     6755:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6756:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6757:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6758:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6759:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6760:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6761:        
                   6762:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6763:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6764:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6765:        
1.1.1.4   root     6766:        for(int i = 0; i < 8; i++) {
                   6767:                if(name_tmp[i] == '.') {
                   6768:                        mcb_psp->prog_name[i] = '\0';
                   6769:                        break;
                   6770:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6771:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6772:                        i++;
                   6773:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6774:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6775:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6776:                } else {
                   6777:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6778:                }
                   6779:        }
                   6780:        
1.1       root     6781:        // process info
                   6782:        process_t *process = msdos_process_info_create(psp_seg);
                   6783:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6784: #ifdef USE_DEBUGGER
                   6785:        strcpy(process->module_path, path);
                   6786: #endif
1.1       root     6787:        process->dta.w.l = 0x80;
                   6788:        process->dta.w.h = psp_seg;
                   6789:        process->switchar = '/';
                   6790:        process->max_files = 20;
                   6791:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6792:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6793:        process->parent_ds = SREG(DS);
1.1.1.31  root     6794:        process->parent_es = SREG(ES);
1.1       root     6795:        
                   6796:        current_psp = psp_seg;
1.1.1.23  root     6797:        msdos_sda_update(current_psp);
1.1       root     6798:        
                   6799:        if(al == 0x00) {
                   6800:                int_10h_feh_called = int_10h_ffh_called = false;
                   6801:                
1.1.1.38  root     6802:                // pipe
                   6803:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6804: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6805:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6806:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6807:                        } else {
                   6808:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6809:                        }
                   6810:                        if(fd != -1) {
                   6811:                                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     6812:                                psp->file_table[0] = fd;
                   6813:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6814:                        }
                   6815:                }
                   6816:                if(pipe_stdout_path[0] != '\0') {
                   6817:                        if(_access(pipe_stdout_path, 0) == 0) {
1.1.1.60  root     6818:                                SetFileAttributesA(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6819:                                DeleteFileA(pipe_stdout_path);
1.1.1.38  root     6820:                        }
1.1.1.45  root     6821: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6822:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6823:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6824:                        } else {
                   6825:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6826:                        }
                   6827:                        if(fd != -1) {
                   6828:                                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     6829:                                psp->file_table[1] = fd;
                   6830:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6831:                        }
                   6832:                }
1.1.1.39  root     6833:                if(pipe_stderr_path[0] != '\0') {
                   6834:                        if(_access(pipe_stderr_path, 0) == 0) {
1.1.1.60  root     6835:                                SetFileAttributesA(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6836:                                DeleteFileA(pipe_stderr_path);
1.1.1.39  root     6837:                        }
1.1.1.45  root     6838: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6839:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6840:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6841:                        } else {
                   6842:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6843:                        }
                   6844:                        if(fd != -1) {
                   6845:                                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     6846:                                psp->file_table[2] = fd;
                   6847:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6848:                        }
                   6849:                }
1.1.1.38  root     6850:                
1.1       root     6851:                // registers and segments
                   6852:                REG16(AX) = REG16(BX) = 0x00;
                   6853:                REG16(CX) = 0xff;
                   6854:                REG16(DX) = psp_seg;
                   6855:                REG16(SI) = ip;
                   6856:                REG16(DI) = sp;
                   6857:                REG16(SP) = sp;
1.1.1.3   root     6858:                SREG(DS) = SREG(ES) = psp_seg;
                   6859:                SREG(SS) = ss;
                   6860:                i386_load_segment_descriptor(DS);
                   6861:                i386_load_segment_descriptor(ES);
                   6862:                i386_load_segment_descriptor(SS);
1.1       root     6863:                
                   6864:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6865:                i386_jmp_far(cs, ip);
                   6866:        } else if(al == 0x01) {
                   6867:                // copy ss:sp and cs:ip to param block
                   6868:                param->sp = sp;
                   6869:                param->ss = ss;
                   6870:                param->ip = ip;
                   6871:                param->cs = cs;
1.1.1.31  root     6872:                
                   6873:                // the AX value to be passed to the child program is put on top of the child's stack
                   6874:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6875:        }
                   6876:        return(0);
                   6877: }
                   6878: 
                   6879: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6880: {
                   6881:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6882:        
                   6883:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6884:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6885:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6886:        
1.1.1.3   root     6887:        SREG(SS) = psp->stack.w.h;
                   6888:        i386_load_segment_descriptor(SS);
1.1       root     6889:        REG16(SP) = psp->stack.w.l;
                   6890:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6891:        
1.1.1.28  root     6892: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6893:        process_t *current_process = NULL;
                   6894:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6895:                if(process[i].psp == psp_seg) {
                   6896:                        current_process = &process[i];
                   6897:                        break;
                   6898:                }
                   6899:        }
                   6900:        if(current_process == NULL) {
                   6901:                throw(0x1f); // general failure
                   6902:        }
                   6903:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6904:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6905:        if(current_process->called_by_int2eh) {
                   6906:                REG16(AX) = ret;
                   6907:        }
                   6908:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6909:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6910:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6911:        i386_load_segment_descriptor(ES);
1.1       root     6912:        
                   6913:        if(mem_free) {
1.1.1.8   root     6914:                int mcb_seg;
                   6915:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6916:                        msdos_mem_free(mcb_seg + 1);
                   6917:                }
                   6918:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6919:                        msdos_mem_free(mcb_seg + 1);
                   6920:                }
1.1       root     6921:                
                   6922:                for(int i = 0; i < MAX_FILES; i++) {
                   6923:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6924:                                _close(i);
1.1.1.20  root     6925:                                msdos_file_handler_close(i);
                   6926:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6927:                        }
                   6928:                }
1.1.1.13  root     6929:                msdos_dta_info_free(psp_seg);
1.1       root     6930:        }
1.1.1.14  root     6931:        msdos_stdio_reopen();
1.1       root     6932:        
1.1.1.28  root     6933:        memset(current_process, 0, sizeof(process_t));
1.1       root     6934:        
                   6935:        current_psp = psp->parent_psp;
                   6936:        retval = ret;
1.1.1.23  root     6937:        msdos_sda_update(current_psp);
1.1       root     6938: }
                   6939: 
                   6940: // drive
                   6941: 
1.1.1.42  root     6942: int pcbios_update_drive_param(int drive_num, int force_update);
                   6943: 
1.1       root     6944: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6945: {
1.1.1.41  root     6946:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6947:                return(0);
                   6948:        }
1.1.1.42  root     6949:        pcbios_update_drive_param(drive_num, force_update);
                   6950:        
                   6951:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6952:        *seg = DPB_TOP >> 4;
                   6953:        *ofs = sizeof(dpb_t) * drive_num;
                   6954:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6955:        
                   6956:        memset(dpb, 0, sizeof(dpb_t));
                   6957:        
1.1.1.41  root     6958:        dpb->drive_num = drive_num;
                   6959:        dpb->unit_num = drive_num;
1.1.1.42  root     6960:        
                   6961:        if(drive_param->valid) {
                   6962:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6963:                
                   6964:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6965:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6966:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6967:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6968:                switch(geo->MediaType) {
                   6969:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6970:                        dpb->media_type = 0xff;
                   6971:                        break;
                   6972:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6973:                        dpb->media_type = 0xfe;
                   6974:                        break;
                   6975:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6976:                        dpb->media_type = 0xfd;
                   6977:                        break;
                   6978:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6979:                        dpb->media_type = 0xfc;
                   6980:                        break;
                   6981:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6982:                case F3_1Pt2_512:
                   6983:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6984:                case F5_720_512:
                   6985:                        dpb->media_type = 0xf9;
                   6986:                        break;
                   6987:                case FixedMedia:        // hard disk
                   6988:                case RemovableMedia:
                   6989:                case Unknown:
                   6990:                        dpb->media_type = 0xf8;
                   6991:                        break;
                   6992:                default:
                   6993:                        dpb->media_type = 0xf0;
                   6994:                        break;
                   6995:                }
                   6996:        }
1.1.1.41  root     6997:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6998:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6999:        dpb->info_sector = 0xffff;
                   7000:        dpb->backup_boot_sector = 0xffff;
                   7001:        dpb->free_clusters = 0xffff;
                   7002:        dpb->free_search_cluster = 0xffffffff;
                   7003:        
                   7004:        return(drive_param->valid);
1.1       root     7005: }
                   7006: 
                   7007: // pc bios
                   7008: 
1.1.1.35  root     7009: #ifdef USE_SERVICE_THREAD
                   7010: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   7011: {
                   7012: #if defined(HAS_I386)
                   7013:        if(m_SF != 0) {
                   7014:                m_SF = 0;
1.1.1.49  root     7015:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     7016:        } else {
                   7017:                m_SF = 1;
1.1.1.49  root     7018:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     7019:        }
                   7020: #else
                   7021:        if(m_SignVal < 0) {
                   7022:                m_SignVal = 0;
1.1.1.49  root     7023:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     7024:        } else {
                   7025:                m_SignVal = -1;
1.1.1.49  root     7026:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     7027:        }
                   7028: #endif
1.1.1.59  root     7029:        // dummy loop to wait BIOS/DOS service is done is at fffc:0013
1.1.1.49  root     7030:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     7031:        in_service = true;
                   7032:        service_exit = false;
                   7033:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   7034: }
                   7035: 
                   7036: void finish_service_loop()
                   7037: {
                   7038:        if(in_service && service_exit) {
                   7039: #if defined(HAS_I386)
                   7040:                if(m_SF != 0) {
                   7041:                        m_SF = 0;
                   7042:                } else {
                   7043:                        m_SF = 1;
                   7044:                }
                   7045: #else
                   7046:                if(m_SignVal < 0) {
                   7047:                        m_SignVal = 0;
                   7048:                } else {
                   7049:                        m_SignVal = -1;
                   7050:                }
                   7051: #endif
                   7052:                in_service = false;
                   7053:        }
                   7054: }
                   7055: #endif
                   7056: 
1.1.1.19  root     7057: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   7058: {
                   7059:        static unsigned __int64 start_msec_since_midnight = 0;
                   7060:        static unsigned __int64 start_msec_since_hostboot = 0;
                   7061:        
                   7062:        if(start_msec_since_midnight == 0) {
                   7063:                SYSTEMTIME time;
                   7064:                GetLocalTime(&time);
                   7065:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   7066:                start_msec_since_hostboot = cur_msec;
                   7067:        }
                   7068:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   7069:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   7070:        return (UINT32)tick;
                   7071: }
                   7072: 
                   7073: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   7074: {
                   7075:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   7076:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   7077:        
                   7078:        if(prev_tick > next_tick) {
                   7079:                mem[0x470] = 1;
                   7080:        }
                   7081:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   7082: }
                   7083: 
1.1.1.14  root     7084: inline void pcbios_irq0()
                   7085: {
                   7086:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     7087:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     7088: }
                   7089: 
1.1.1.16  root     7090: int pcbios_get_text_vram_address(int page)
1.1       root     7091: {
                   7092:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7093:                return TEXT_VRAM_TOP;
1.1       root     7094:        } else {
1.1.1.14  root     7095:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     7096:        }
                   7097: }
                   7098: 
1.1.1.16  root     7099: int pcbios_get_shadow_buffer_address(int page)
1.1       root     7100: {
1.1.1.14  root     7101:        if(!int_10h_feh_called) {
1.1.1.16  root     7102:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     7103:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7104:                return SHADOW_BUF_TOP;
                   7105:        } else {
1.1.1.14  root     7106:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     7107:        }
                   7108: }
                   7109: 
1.1.1.16  root     7110: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     7111: {
1.1.1.16  root     7112:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     7113: }
                   7114: 
1.1.1.56  root     7115: bool pcbios_set_font_size(int width, int height)
                   7116: {
1.1.1.61  root     7117:        if(set_console_font_size(width, height)) {
                   7118:                *(UINT16 *)(mem + 0x485) = height;
                   7119:                return(true);
                   7120:        }
                   7121:        return(false);
1.1.1.56  root     7122: }
                   7123: 
1.1.1.16  root     7124: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     7125: {
1.1.1.14  root     7126:        // clear the existing screen, not just the new one
                   7127:        int clr_height = max(height, scr_height);
                   7128:        
1.1.1.16  root     7129:        if(scr_width != width || scr_height != height) {
                   7130:                change_console_size(width, height);
1.1.1.14  root     7131:        }
                   7132:        mem[0x462] = 0;
                   7133:        *(UINT16 *)(mem + 0x44e) = 0;
                   7134:        
1.1.1.16  root     7135:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   7136:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   7137:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   7138:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     7139:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7140:        
1.1.1.23  root     7141:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     7142:        if(clr_screen) {
1.1.1.14  root     7143:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   7144:                        mem[ofs++] = 0x20;
                   7145:                        mem[ofs++] = 0x07;
                   7146:                }
                   7147:                
1.1.1.35  root     7148: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7149:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7150: #endif
1.1.1.14  root     7151:                for(int y = 0; y < clr_height; y++) {
                   7152:                        for(int x = 0; x < scr_width; x++) {
                   7153:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7154:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     7155:                        }
                   7156:                }
                   7157:                SMALL_RECT rect;
1.1.1.14  root     7158:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
1.1.1.60  root     7159:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7160:                vram_length_char = vram_last_length_char = 0;
                   7161:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     7162: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7163:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7164: #endif
1.1       root     7165:        }
1.1.1.14  root     7166:        COORD co;
                   7167:        co.X = 0;
                   7168:        co.Y = scr_top;
                   7169:        SetConsoleCursorPosition(hStdout, co);
                   7170:        cursor_moved = true;
1.1.1.59  root     7171:        cursor_moved_by_crtc = false;
1.1.1.14  root     7172:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     7173: }
                   7174: 
1.1.1.36  root     7175: void pcbios_update_cursor_position()
                   7176: {
                   7177:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7178:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   7179:        if(!restore_console_on_exit) {
                   7180:                scr_top = csbi.srWindow.Top;
                   7181:        }
                   7182:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   7183:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   7184: }
                   7185: 
1.1.1.16  root     7186: inline void pcbios_int_10h_00h()
                   7187: {
                   7188:        switch(REG8(AL) & 0x7f) {
                   7189:        case 0x70: // v-text mode
                   7190:        case 0x71: // extended cga v-text mode
                   7191:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   7192:                break;
1.1.1.61  root     7193:        case 0x73:
                   7194:        case 0x03:
                   7195:                change_console_size(80, 25); // for Windows10
                   7196:                pcbios_set_font_size(font_width, font_height);
1.1.1.16  root     7197:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   7198:                break;
                   7199:        }
                   7200:        if(REG8(AL) & 0x80) {
                   7201:                mem[0x487] |= 0x80;
                   7202:        } else {
                   7203:                mem[0x487] &= ~0x80;
                   7204:        }
                   7205:        mem[0x449] = REG8(AL) & 0x7f;
                   7206: }
                   7207: 
1.1       root     7208: inline void pcbios_int_10h_01h()
                   7209: {
1.1.1.13  root     7210:        mem[0x460] = REG8(CL);
                   7211:        mem[0x461] = REG8(CH);
1.1.1.14  root     7212:        
1.1.1.60  root     7213:        int size = (int)(REG8(CL) & 7) - (int)(REG8(CH) & 7) + 1;
                   7214:        
                   7215:        if(!((REG8(CH) & 0x20) != 0 || size < 0)) {
                   7216:                ci_new.bVisible = TRUE;
                   7217:                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
                   7218:        } else {
                   7219:                ci_new.bVisible = FALSE;
                   7220:        }
1.1       root     7221: }
                   7222: 
                   7223: inline void pcbios_int_10h_02h()
                   7224: {
1.1.1.14  root     7225:        // continuously setting the cursor effectively stops it blinking
1.1.1.59  root     7226:        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     7227:                COORD co;
                   7228:                co.X = REG8(DL);
1.1.1.14  root     7229:                co.Y = REG8(DH) + scr_top;
                   7230:                
                   7231:                // some programs hide the cursor by moving it off screen
                   7232:                static bool hidden = false;
1.1.1.23  root     7233:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7234:                
                   7235:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
1.1.1.59  root     7236:                        if(ci_new.bVisible) {
                   7237:                                ci_new.bVisible = FALSE;
1.1.1.14  root     7238:                                hidden = true;
                   7239:                        }
                   7240:                } else if(hidden) {
1.1.1.59  root     7241:                        if(!ci_new.bVisible) {
                   7242:                                ci_new.bVisible = TRUE;
1.1.1.14  root     7243:                        }
                   7244:                        hidden = false;
                   7245:                }
1.1.1.59  root     7246:                cursor_moved_by_crtc = false;
1.1       root     7247:        }
1.1.1.14  root     7248:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   7249:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     7250: }
                   7251: 
                   7252: inline void pcbios_int_10h_03h()
                   7253: {
1.1.1.14  root     7254:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7255:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7256:        REG8(CL) = mem[0x460];
                   7257:        REG8(CH) = mem[0x461];
                   7258: }
                   7259: 
                   7260: inline void pcbios_int_10h_05h()
                   7261: {
1.1.1.14  root     7262:        if(REG8(AL) >= vram_pages) {
                   7263:                return;
                   7264:        }
                   7265:        if(mem[0x462] != REG8(AL)) {
                   7266:                vram_flush();
                   7267:                
1.1.1.23  root     7268:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7269:                SMALL_RECT rect;
1.1.1.14  root     7270:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7271:                ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7272:                
1.1.1.16  root     7273:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     7274:                        for(int x = 0; x < scr_width; x++) {
                   7275:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7276:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7277:                        }
                   7278:                }
1.1.1.16  root     7279:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     7280:                        for(int x = 0; x < scr_width; x++) {
                   7281:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   7282:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     7283:                        }
                   7284:                }
1.1.1.60  root     7285:                WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7286:                
                   7287:                COORD co;
1.1.1.14  root     7288:                co.X = mem[0x450 + REG8(AL) * 2];
                   7289:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   7290:                if(co.Y < scr_top + scr_height) {
                   7291:                        SetConsoleCursorPosition(hStdout, co);
                   7292:                }
1.1.1.59  root     7293:                cursor_moved_by_crtc = false;
1.1       root     7294:        }
1.1.1.14  root     7295:        mem[0x462] = REG8(AL);
                   7296:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   7297:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     7298:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     7299:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     7300:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     7301:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     7302:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     7303: }
                   7304: 
                   7305: inline void pcbios_int_10h_06h()
                   7306: {
1.1.1.14  root     7307:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7308:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7309:                return;
                   7310:        }
                   7311:        vram_flush();
                   7312:        
1.1.1.23  root     7313:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7314:        SMALL_RECT rect;
1.1.1.14  root     7315:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7316:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7317:        
                   7318:        int right = min(REG8(DL), scr_width - 1);
                   7319:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7320:        
                   7321:        if(REG8(AL) == 0) {
1.1.1.14  root     7322:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7323:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7324:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7325:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7326:                        }
                   7327:                }
                   7328:        } else {
1.1.1.14  root     7329:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     7330:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7331:                                if(y2 <= bottom) {
                   7332:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7333:                                } else {
1.1.1.14  root     7334:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7335:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7336:                                }
1.1.1.14  root     7337:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7338:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7339:                        }
                   7340:                }
                   7341:        }
1.1.1.60  root     7342:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7343: }
                   7344: 
                   7345: inline void pcbios_int_10h_07h()
                   7346: {
1.1.1.14  root     7347:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   7348:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   7349:                return;
                   7350:        }
                   7351:        vram_flush();
                   7352:        
1.1.1.23  root     7353:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7354:        SMALL_RECT rect;
1.1.1.14  root     7355:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
1.1.1.60  root     7356:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     7357:        
                   7358:        int right = min(REG8(DL), scr_width - 1);
                   7359:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     7360:        
                   7361:        if(REG8(AL) == 0) {
1.1.1.14  root     7362:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     7363:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7364:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7365:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7366:                        }
                   7367:                }
                   7368:        } else {
1.1.1.14  root     7369:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     7370:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     7371:                                if(y2 >= REG8(CH)) {
                   7372:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     7373:                                } else {
1.1.1.14  root     7374:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   7375:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     7376:                                }
1.1.1.14  root     7377:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   7378:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     7379:                        }
                   7380:                }
                   7381:        }
1.1.1.60  root     7382:        WriteConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     7383: }
                   7384: 
                   7385: inline void pcbios_int_10h_08h()
                   7386: {
                   7387:        COORD co;
                   7388:        DWORD num;
                   7389:        
1.1.1.14  root     7390:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7391:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     7392:        
                   7393:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7394:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7395:                co.Y += scr_top;
                   7396:                vram_flush();
1.1.1.60  root     7397:                ReadConsoleOutputCharacterA(hStdout, scr_char, 1, co, &num);
1.1       root     7398:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   7399:                REG8(AL) = scr_char[0];
                   7400:                REG8(AH) = scr_attr[0];
                   7401:        } else {
1.1.1.16  root     7402:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     7403:        }
                   7404: }
                   7405: 
                   7406: inline void pcbios_int_10h_09h()
                   7407: {
                   7408:        COORD co;
                   7409:        
1.1.1.14  root     7410:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7411:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7412:        
1.1.1.16  root     7413:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7414:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7415:        
                   7416:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7417: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7418:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7419: #endif
1.1.1.16  root     7420:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7421:                while(dest < end) {
                   7422:                        write_text_vram_char(dest - vram, REG8(AL));
                   7423:                        mem[dest++] = REG8(AL);
                   7424:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7425:                        mem[dest++] = REG8(BL);
1.1       root     7426:                }
1.1.1.35  root     7427: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7428:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7429: #endif
1.1       root     7430:        } else {
1.1.1.14  root     7431:                while(dest < end) {
1.1       root     7432:                        mem[dest++] = REG8(AL);
                   7433:                        mem[dest++] = REG8(BL);
                   7434:                }
                   7435:        }
                   7436: }
                   7437: 
                   7438: inline void pcbios_int_10h_0ah()
                   7439: {
                   7440:        COORD co;
                   7441:        
1.1.1.14  root     7442:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7443:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7444:        
1.1.1.16  root     7445:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7446:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7447:        
                   7448:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7449: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7450:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7451: #endif
1.1.1.16  root     7452:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7453:                while(dest < end) {
                   7454:                        write_text_vram_char(dest - vram, REG8(AL));
                   7455:                        mem[dest++] = REG8(AL);
                   7456:                        dest++;
1.1       root     7457:                }
1.1.1.35  root     7458: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7459:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7460: #endif
1.1       root     7461:        } else {
1.1.1.14  root     7462:                while(dest < end) {
1.1       root     7463:                        mem[dest++] = REG8(AL);
                   7464:                        dest++;
                   7465:                }
                   7466:        }
                   7467: }
                   7468: 
1.1.1.40  root     7469: inline void pcbios_int_10h_0ch()
                   7470: {
                   7471:        HDC hdc = get_console_window_device_context();
                   7472:        
                   7473:        if(hdc != NULL) {
                   7474:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7475:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7476:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7477:                
                   7478:                if(REG8(AL) & 0x80) {
                   7479:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7480:                        if(color != CLR_INVALID) {
                   7481:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7482:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7483:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7484:                        }
                   7485:                }
                   7486:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7487:        }
                   7488: }
                   7489: 
                   7490: inline void pcbios_int_10h_0dh()
                   7491: {
                   7492:        HDC hdc = get_console_window_device_context();
                   7493:        BYTE r = 0;
                   7494:        BYTE g = 0;
                   7495:        BYTE b = 0;
                   7496:        
                   7497:        if(hdc != NULL) {
                   7498:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7499:                if(color != CLR_INVALID) {
                   7500:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7501:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7502:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7503:                }
                   7504:        }
                   7505:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7506: }
                   7507: 
1.1       root     7508: inline void pcbios_int_10h_0eh()
                   7509: {
1.1.1.59  root     7510:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   7511:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     7512:        DWORD num;
                   7513:        COORD co;
                   7514:        
1.1.1.59  root     7515:        if(cursor_moved_by_crtc) {
                   7516:                if(!restore_console_on_exit) {
                   7517:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7518:                        scr_top = csbi.srWindow.Top;
                   7519:                }
                   7520:                co.X = mem[0x450 + REG8(BH) * 2];
                   7521:                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
                   7522:                SetConsoleCursorPosition(hStdout, co);
                   7523:                cursor_moved_by_crtc = false;
                   7524:        }
1.1.1.54  root     7525:        co.X = mem[0x450 + mem[0x462] * 2];
                   7526:        co.Y = mem[0x451 + mem[0x462] * 2];
1.1.1.14  root     7527:        
                   7528:        if(REG8(AL) == 7) {
                   7529:                //MessageBeep(-1);
                   7530:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7531:                if(REG8(AL) == 10) {
                   7532:                        vram_flush();
                   7533:                }
1.1.1.60  root     7534:                WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7535:                cursor_moved = true;
                   7536:        } else {
1.1.1.54  root     7537:                int dest = pcbios_get_shadow_buffer_address(mem[0x462], co.X, co.Y);
1.1.1.35  root     7538: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7539:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7540: #endif
1.1.1.54  root     7541:                int vram = pcbios_get_shadow_buffer_address(mem[0x462]);
                   7542:                write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7543: #ifdef USE_VRAM_THREAD
1.1.1.54  root     7544:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7545: #endif
1.1.1.54  root     7546:                
                   7547:                if(++co.X == scr_width) {
                   7548:                        co.X = 0;
                   7549:                        if(++co.Y == scr_height) {
                   7550:                                vram_flush();
1.1.1.60  root     7551:                                WriteConsoleA(hStdout, "\n", 1, &num, NULL);
1.1.1.14  root     7552:                                cursor_moved = true;
                   7553:                        }
                   7554:                }
1.1.1.54  root     7555:                if(!cursor_moved) {
                   7556:                        co.Y += scr_top;
                   7557:                        SetConsoleCursorPosition(hStdout, co);
                   7558:                        cursor_moved = true;
                   7559:                }
1.1.1.14  root     7560:                mem[dest] = REG8(AL);
                   7561:        }
1.1       root     7562: }
                   7563: 
                   7564: inline void pcbios_int_10h_0fh()
                   7565: {
                   7566:        REG8(AL) = mem[0x449];
                   7567:        REG8(AH) = mem[0x44a];
                   7568:        REG8(BH) = mem[0x462];
                   7569: }
                   7570: 
1.1.1.14  root     7571: inline void pcbios_int_10h_11h()
                   7572: {
                   7573:        switch(REG8(AL)) {
1.1.1.58  root     7574:        case 0x00:
                   7575:        case 0x10:
1.1.1.61  root     7576:                if(REG8(BH)) {
                   7577:                        change_console_size(80, 25); // for Windows10
                   7578:                        if(!pcbios_set_font_size(font_width, (int)REG8(BH))) {
                   7579:                                for(int h = min(font_height, (int)REG8(BH)); h <= max(font_height, (int)REG8(BH)); h++) {
                   7580:                                        if(h != (int)REG8(BH)) {
                   7581:                                                if(pcbios_set_font_size(font_width, h)) {
                   7582:                                                        break;
                   7583:                                                }
                   7584:                                        }
                   7585:                                }
                   7586:                        }
1.1.1.58  root     7587:                        pcbios_set_console_size(80, (25 * 16) / REG8(BH), true);
                   7588:                }
                   7589:                break;
1.1.1.16  root     7590:        case 0x01:
1.1.1.14  root     7591:        case 0x11:
1.1.1.61  root     7592:                change_console_size(80, 28); // for Windows10
                   7593:                if(!pcbios_set_font_size(font_width, 14)) {
                   7594:                        for(int h = min(font_height, 14); h <= max(font_height, 14); h++) {
                   7595:                                if(h != 14) {
                   7596:                                        if(pcbios_set_font_size(font_width, h)) {
                   7597:                                                break;
                   7598:                                        }
                   7599:                                }
                   7600:                        }
1.1.1.56  root     7601:                }
1.1.1.61  root     7602:                pcbios_set_console_size(80, 28, true); // 28 = 25 * 16 / 14
1.1.1.14  root     7603:                break;
1.1.1.16  root     7604:        case 0x02:
1.1.1.14  root     7605:        case 0x12:
1.1.1.61  root     7606:                change_console_size(80, 25); // for Windows10
                   7607:                if(!pcbios_set_font_size(8, 8)) {
                   7608:                        bool success = false;
                   7609:                        for(int y = 8; y <= 14; y++) {
                   7610:                                for(int x = min(font_width, 6); x <= max(font_width, 8); x++) {
                   7611:                                        if(pcbios_set_font_size(x, y)) {
                   7612:                                                success = true;
                   7613:                                                break;
                   7614:                                        }
                   7615:                                }
                   7616:                        }
                   7617:                        if(!success) {
                   7618:                                pcbios_set_font_size(font_width, font_height);
                   7619:                        }
1.1.1.56  root     7620:                }
1.1.1.61  root     7621:                pcbios_set_console_size(80, 50, true); // 50 = 25 * 16 / 8
1.1.1.14  root     7622:                break;
1.1.1.16  root     7623:        case 0x04:
1.1.1.14  root     7624:        case 0x14:
1.1.1.61  root     7625:                change_console_size(80, 25); // for Windows10
                   7626:                pcbios_set_font_size(font_width, font_height);
                   7627:                pcbios_set_console_size(80, 25, true);
1.1.1.58  root     7628:                break;
                   7629:        case 0x18:
1.1.1.61  root     7630:                change_console_size(80, 25); // for Windows10
                   7631:                pcbios_set_font_size(font_width, font_height);
                   7632:                pcbios_set_console_size(80, 25, true);
1.1.1.14  root     7633:                break;
                   7634:        case 0x30:
                   7635:                SREG(ES) = 0;
                   7636:                i386_load_segment_descriptor(ES);
                   7637:                REG16(BP) = 0;
                   7638:                REG16(CX) = mem[0x485];
                   7639:                REG8(DL) = mem[0x484];
                   7640:                break;
1.1.1.54  root     7641:        default:
                   7642:                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));
                   7643:                m_CF = 1;
                   7644:                break;
1.1.1.14  root     7645:        }
                   7646: }
                   7647: 
                   7648: inline void pcbios_int_10h_12h()
                   7649: {
1.1.1.16  root     7650:        switch(REG8(BL)) {
                   7651:        case 0x10:
1.1.1.14  root     7652:                REG16(BX) = 0x0003;
                   7653:                REG16(CX) = 0x0009;
1.1.1.16  root     7654:                break;
1.1.1.14  root     7655:        }
                   7656: }
                   7657: 
1.1       root     7658: inline void pcbios_int_10h_13h()
                   7659: {
1.1.1.3   root     7660:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7661:        COORD co;
                   7662:        DWORD num;
                   7663:        
                   7664:        co.X = REG8(DL);
1.1.1.14  root     7665:        co.Y = REG8(DH) + scr_top;
                   7666:        
                   7667:        vram_flush();
1.1       root     7668:        
                   7669:        switch(REG8(AL)) {
                   7670:        case 0x00:
                   7671:        case 0x01:
                   7672:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7673:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7674:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7675:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7676:                        SetConsoleCursorPosition(hStdout, co);
                   7677:                        
                   7678:                        if(csbi.wAttributes != REG8(BL)) {
                   7679:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7680:                        }
1.1.1.60  root     7681:                        WriteConsoleA(hStdout, &mem[ofs], REG16(CX), &num, NULL);
1.1.1.14  root     7682:                        
1.1       root     7683:                        if(csbi.wAttributes != REG8(BL)) {
                   7684:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7685:                        }
                   7686:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7687:                                if(!restore_console_on_exit) {
                   7688:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7689:                                        scr_top = csbi.srWindow.Top;
                   7690:                                }
1.1.1.14  root     7691:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7692:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7693:                                SetConsoleCursorPosition(hStdout, co);
                   7694:                        } else {
                   7695:                                cursor_moved = true;
                   7696:                        }
1.1.1.59  root     7697:                        cursor_moved_by_crtc = false;
1.1       root     7698:                } else {
1.1.1.3   root     7699:                        m_CF = 1;
1.1       root     7700:                }
                   7701:                break;
                   7702:        case 0x02:
                   7703:        case 0x03:
                   7704:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7705:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7706:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7707:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7708:                        SetConsoleCursorPosition(hStdout, co);
                   7709:                        
                   7710:                        WORD wAttributes = csbi.wAttributes;
                   7711:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7712:                                if(wAttributes != mem[ofs + 1]) {
                   7713:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7714:                                        wAttributes = mem[ofs + 1];
                   7715:                                }
1.1.1.60  root     7716:                                WriteConsoleA(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7717:                        }
                   7718:                        if(csbi.wAttributes != wAttributes) {
                   7719:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7720:                        }
                   7721:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7722:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7723:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7724:                                SetConsoleCursorPosition(hStdout, co);
                   7725:                        } else {
                   7726:                                cursor_moved = true;
                   7727:                        }
1.1.1.59  root     7728:                        cursor_moved_by_crtc = false;
1.1       root     7729:                } else {
1.1.1.3   root     7730:                        m_CF = 1;
1.1       root     7731:                }
                   7732:                break;
                   7733:        case 0x10:
                   7734:        case 0x11:
                   7735:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7736:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.60  root     7737:                        ReadConsoleOutputCharacterA(hStdout, scr_char, REG16(CX), co, &num);
1.1       root     7738:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7739:                        for(int i = 0; i < num; i++) {
                   7740:                                mem[ofs++] = scr_char[i];
                   7741:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7742:                                if(REG8(AL) & 0x01) {
1.1       root     7743:                                        mem[ofs++] = 0;
                   7744:                                        mem[ofs++] = 0;
                   7745:                                }
                   7746:                        }
                   7747:                } else {
1.1.1.16  root     7748:                        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     7749:                                mem[ofs++] = mem[src++];
                   7750:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7751:                                if(REG8(AL) & 0x01) {
1.1       root     7752:                                        mem[ofs++] = 0;
                   7753:                                        mem[ofs++] = 0;
                   7754:                                }
1.1.1.14  root     7755:                                if(++co.X == scr_width) {
                   7756:                                        if(++co.Y == scr_height) {
1.1       root     7757:                                                break;
                   7758:                                        }
                   7759:                                        co.X = 0;
                   7760:                                }
                   7761:                        }
                   7762:                }
                   7763:                break;
1.1.1.45  root     7764:        case 0x12: // ???
                   7765:        case 0x13: // ???
1.1       root     7766:        case 0x20:
                   7767:        case 0x21:
                   7768:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7769:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7770:                        int len = min(REG16(CX), scr_width * scr_height);
                   7771:                        for(int i = 0; i < len; i++) {
1.1       root     7772:                                scr_char[i] = mem[ofs++];
                   7773:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7774:                                if(REG8(AL) & 0x01) {
1.1       root     7775:                                        ofs += 2;
                   7776:                                }
                   7777:                        }
1.1.1.60  root     7778:                        WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7779:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7780:                } else {
1.1.1.16  root     7781:                        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     7782:                                mem[dest++] = mem[ofs++];
                   7783:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7784:                                if(REG8(AL) & 0x01) {
1.1       root     7785:                                        ofs += 2;
                   7786:                                }
1.1.1.14  root     7787:                                if(++co.X == scr_width) {
                   7788:                                        if(++co.Y == scr_height) {
1.1       root     7789:                                                break;
                   7790:                                        }
                   7791:                                        co.X = 0;
                   7792:                                }
                   7793:                        }
                   7794:                }
                   7795:                break;
                   7796:        default:
1.1.1.22  root     7797:                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     7798:                m_CF = 1;
1.1       root     7799:                break;
                   7800:        }
                   7801: }
                   7802: 
1.1.1.30  root     7803: inline void pcbios_int_10h_18h()
                   7804: {
                   7805:        switch(REG8(AL)) {
                   7806:        case 0x00:
                   7807:        case 0x01:
                   7808: //             REG8(AL) = 0x86;
                   7809:                REG8(AL) = 0x00;
                   7810:                break;
                   7811:        default:
                   7812:                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));
                   7813:                m_CF = 1;
                   7814:                break;
                   7815:        }
                   7816: }
                   7817: 
1.1.1.14  root     7818: inline void pcbios_int_10h_1ah()
                   7819: {
                   7820:        switch(REG8(AL)) {
                   7821:        case 0x00:
                   7822:                REG8(AL) = 0x1a;
                   7823:                REG8(BL) = 0x08;
                   7824:                REG8(BH) = 0x00;
                   7825:                break;
                   7826:        default:
1.1.1.22  root     7827:                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     7828:                m_CF = 1;
                   7829:                break;
                   7830:        }
                   7831: }
                   7832: 
1.1       root     7833: inline void pcbios_int_10h_1dh()
                   7834: {
                   7835:        switch(REG8(AL)) {
1.1.1.43  root     7836:        case 0x00:
                   7837:                // DOS/V Shift Status Line Control is not supported
                   7838:                m_CF = 1;
                   7839:                break;
1.1       root     7840:        case 0x01:
                   7841:                break;
                   7842:        case 0x02:
                   7843:                REG16(BX) = 0;
                   7844:                break;
                   7845:        default:
1.1.1.22  root     7846:                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));
                   7847:                m_CF = 1;
                   7848:                break;
                   7849:        }
                   7850: }
                   7851: 
                   7852: inline void pcbios_int_10h_4fh()
                   7853: {
                   7854:        switch(REG8(AL)) {
                   7855:        case 0x00:
                   7856:                REG8(AH) = 0x02; // not supported
                   7857:                break;
                   7858:        case 0x01:
                   7859:        case 0x02:
                   7860:        case 0x03:
                   7861:        case 0x04:
                   7862:        case 0x05:
                   7863:        case 0x06:
                   7864:        case 0x07:
                   7865:        case 0x08:
                   7866:        case 0x09:
                   7867:        case 0x0a:
                   7868:        case 0x0b:
                   7869:        case 0x0c:
                   7870:                REG8(AH) = 0x01; // failed
                   7871:                break;
                   7872:        default:
                   7873:                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     7874:                m_CF = 1;
1.1       root     7875:                break;
                   7876:        }
                   7877: }
                   7878: 
                   7879: inline void pcbios_int_10h_82h()
                   7880: {
                   7881:        static UINT8 mode = 0;
                   7882:        
                   7883:        switch(REG8(AL)) {
1.1.1.22  root     7884:        case 0x00:
1.1       root     7885:                if(REG8(BL) != 0xff) {
                   7886:                        mode = REG8(BL);
                   7887:                }
                   7888:                REG8(AL) = mode;
                   7889:                break;
                   7890:        default:
1.1.1.22  root     7891:                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     7892:                m_CF = 1;
1.1       root     7893:                break;
                   7894:        }
                   7895: }
                   7896: 
1.1.1.22  root     7897: inline void pcbios_int_10h_83h()
                   7898: {
                   7899:        static UINT8 mode = 0;
                   7900:        
                   7901:        switch(REG8(AL)) {
                   7902:        case 0x00:
                   7903:                REG16(AX) = 0; // offset???
                   7904:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7905:                i386_load_segment_descriptor(ES);
                   7906:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7907:                break;
                   7908:        default:
                   7909:                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));
                   7910:                m_CF = 1;
                   7911:                break;
                   7912:        }
                   7913: }
                   7914: 
                   7915: inline void pcbios_int_10h_90h()
                   7916: {
                   7917:        REG8(AL) = mem[0x449];
                   7918: }
                   7919: 
                   7920: inline void pcbios_int_10h_91h()
                   7921: {
                   7922:        REG8(AL) = 0x04; // VGA
                   7923: }
                   7924: 
                   7925: inline void pcbios_int_10h_efh()
                   7926: {
                   7927:        REG16(DX) = 0xffff;
                   7928: }
                   7929: 
1.1       root     7930: inline void pcbios_int_10h_feh()
                   7931: {
                   7932:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7933:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7934:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7935:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7936:        }
                   7937:        int_10h_feh_called = true;
                   7938: }
                   7939: 
                   7940: inline void pcbios_int_10h_ffh()
                   7941: {
                   7942:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7943:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7944:                COORD co;
                   7945:                DWORD num;
                   7946:                
1.1.1.14  root     7947:                vram_flush();
                   7948:                
                   7949:                co.X = (REG16(DI) >> 1) % scr_width;
                   7950:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7951:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7952:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7953:                int len;
                   7954:                for(len = 0; ofs < end; len++) {
                   7955:                        scr_char[len] = mem[ofs++];
                   7956:                        scr_attr[len] = mem[ofs++];
                   7957:                }
                   7958:                co.Y += scr_top;
1.1.1.60  root     7959:                WriteConsoleOutputCharacterA(hStdout, scr_char, len, co, &num);
1.1.1.14  root     7960:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7961:        }
                   7962:        int_10h_ffh_called = true;
                   7963: }
                   7964: 
1.1.1.42  root     7965: int pcbios_update_drive_param(int drive_num, int force_update)
                   7966: {
                   7967:        if(drive_num >= 0 && drive_num < 26) {
                   7968:                drive_param_t *drive_param = &drive_params[drive_num];
                   7969:                
                   7970:                if(force_update || !drive_param->initialized) {
                   7971:                        drive_param->valid = 0;
                   7972:                        char dev[64];
                   7973:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7974:                        
1.1.1.60  root     7975:                        HANDLE hFile = CreateFileA(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.42  root     7976:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7977:                                DWORD dwSize;
                   7978:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7979:                                        drive_param->valid = 1;
                   7980:                                }
                   7981:                                CloseHandle(hFile);
                   7982:                        }
                   7983:                        drive_param->initialized = 1;
                   7984:                }
                   7985:                return(drive_param->valid);
                   7986:        }
                   7987:        return(0);
                   7988: }
                   7989: 
                   7990: inline void pcbios_int_13h_00h()
                   7991: {
                   7992:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7993:        
                   7994:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7995:                REG8(AH) = 0x00; // successful completion
                   7996:        } else {
                   7997:                if(REG8(DL) & 0x80) {
                   7998:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7999:                } else {
                   8000:                        REG8(AH) = 0x80; // timeout (not ready)
                   8001:                }
                   8002:                m_CF = 1;
                   8003:        }
                   8004: }
                   8005: 
                   8006: inline void pcbios_int_13h_02h()
                   8007: {
                   8008:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8009:        
                   8010:        if(REG8(AL) == 0) {
                   8011:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8012:                m_CF = 1;
                   8013:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8014:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8015:                m_CF = 1;
                   8016:        } else {
                   8017:                drive_param_t *drive_param = &drive_params[drive_num];
                   8018:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8019:                char dev[64];
                   8020:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8021:                
1.1.1.60  root     8022:                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     8023:                if(hFile == INVALID_HANDLE_VALUE) {
                   8024:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8025:                        m_CF = 1;
                   8026:                } else {
                   8027:                        UINT32 sector_num = REG8(AL);
                   8028:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8029:                        UINT32 head = REG8(DH);
                   8030:                        UINT32 sector = REG8(CL) & 0x3f;
                   8031:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8032:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8033:                        DWORD dwSize;
                   8034:                        
                   8035: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8036: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8037: //                             m_CF = 1;
                   8038: //                     } else 
                   8039:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8040:                                REG8(AH) = 0x04; // sector not found/read error
                   8041:                                m_CF = 1;
                   8042:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8043:                                REG8(AH) = 0x04; // sector not found/read error
                   8044:                                m_CF = 1;
                   8045:                        } else {
                   8046:                                REG8(AH) = 0x00; // successful completion
                   8047:                        }
                   8048:                        CloseHandle(hFile);
                   8049:                }
                   8050:        }
                   8051: }
                   8052: 
                   8053: inline void pcbios_int_13h_03h()
                   8054: {
                   8055:        // this operation may cause serious damage for drives, so support only floppy disk...
                   8056:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8057:        
                   8058:        if(REG8(AL) == 0) {
                   8059:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8060:                m_CF = 1;
                   8061:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8062:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8063:                m_CF = 1;
                   8064:        } else if(!drive_params[drive_num].is_fdd()) {
                   8065:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8066:                m_CF = 1;
                   8067:        } else {
                   8068:                drive_param_t *drive_param = &drive_params[drive_num];
                   8069:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8070:                char dev[64];
                   8071:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8072:                
1.1.1.60  root     8073:                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     8074:                if(hFile == INVALID_HANDLE_VALUE) {
                   8075:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8076:                        m_CF = 1;
                   8077:                } else {
                   8078:                        UINT32 sector_num = REG8(AL);
                   8079:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8080:                        UINT32 head = REG8(DH);
                   8081:                        UINT32 sector = REG8(CL) & 0x3f;
                   8082:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8083:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8084:                        DWORD dwSize;
                   8085:                        
                   8086: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8087: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8088: //                             m_CF = 1;
                   8089: //                     } else 
                   8090:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8091:                                REG8(AH) = 0x04; // sector not found/read error
                   8092:                                m_CF = 1;
                   8093:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8094:                                REG8(AH) = 0x04; // sector not found/read error
                   8095:                                m_CF = 1;
                   8096:                        } else {
                   8097:                                REG8(AH) = 0x00; // successful completion
                   8098:                        }
                   8099:                        CloseHandle(hFile);
                   8100:                }
                   8101:        }
                   8102: }
                   8103: 
                   8104: inline void pcbios_int_13h_04h()
                   8105: {
                   8106:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8107:        
                   8108:        if(REG8(AL) == 0) {
                   8109:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   8110:                m_CF = 1;
                   8111:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   8112:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8113:                m_CF = 1;
                   8114:        } else {
                   8115:                drive_param_t *drive_param = &drive_params[drive_num];
                   8116:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8117:                char dev[64];
                   8118:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   8119:                
1.1.1.60  root     8120:                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     8121:                if(hFile == INVALID_HANDLE_VALUE) {
                   8122:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8123:                        m_CF = 1;
                   8124:                } else {
                   8125:                        UINT32 sector_num = REG8(AL);
                   8126:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   8127:                        UINT32 head = REG8(DH);
                   8128:                        UINT32 sector = REG8(CL) & 0x3f;
                   8129:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   8130:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   8131:                        DWORD dwSize;
                   8132:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   8133:                        
                   8134: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   8135: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   8136: //                             m_CF = 1;
                   8137: //                     } else 
                   8138:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8139:                                REG8(AH) = 0x04; // sector not found/read error
                   8140:                                m_CF = 1;
                   8141:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   8142:                                REG8(AH) = 0x04; // sector not found/read error
                   8143:                                m_CF = 1;
                   8144:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   8145:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   8146:                                m_CF = 1;
                   8147:                        } else {
                   8148:                                REG8(AH) = 0x00; // successful completion
                   8149:                        }
                   8150:                        free(tmp_buffer);
                   8151:                        CloseHandle(hFile);
                   8152:                }
                   8153:        }
                   8154: }
                   8155: 
                   8156: inline void pcbios_int_13h_08h()
                   8157: {
                   8158:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8159:        
                   8160:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8161:                drive_param_t *drive_param = &drive_params[drive_num];
                   8162:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   8163:                
                   8164:                REG16(AX) = 0x0000;
                   8165:                switch(geo->MediaType) {
                   8166:                case F5_360_512:
                   8167:                case F5_320_512:
                   8168:                case F5_320_1024:
                   8169:                case F5_180_512:
                   8170:                case F5_160_512:
                   8171:                        REG8(BL) = 0x01; // 320K/360K disk
                   8172:                        break;
                   8173:                case F5_1Pt2_512:
                   8174:                case F3_1Pt2_512:
                   8175:                case F3_1Pt23_1024:
                   8176:                case F5_1Pt23_1024:
                   8177:                        REG8(BL) = 0x02; // 1.2M disk
                   8178:                        break;
                   8179:                case F3_720_512:
                   8180:                case F3_640_512:
                   8181:                case F5_640_512:
                   8182:                case F5_720_512:
                   8183:                        REG8(BL) = 0x03; // 720K disk
                   8184:                        break;
                   8185:                case F3_1Pt44_512:
                   8186:                        REG8(BL) = 0x04; // 1.44M disk
                   8187:                        break;
                   8188:                case F3_2Pt88_512:
                   8189:                        REG8(BL) = 0x06; // 2.88M disk
                   8190:                        break;
                   8191:                case RemovableMedia:
                   8192:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   8193:                        break;
                   8194:                default:
                   8195:                        REG8(BL) = 0x00; // unknown
                   8196:                        break;
                   8197:                }
                   8198:                if(REG8(DL) & 0x80) {
                   8199:                        switch(GetLogicalDrives() & 0x0c) {
                   8200:                        case 0x00: REG8(DL) = 0x00; break;
                   8201:                        case 0x04:
                   8202:                        case 0x08: REG8(DL) = 0x01; break;
                   8203:                        case 0x0c: REG8(DL) = 0x02; break;
                   8204:                        }
                   8205:                } else {
                   8206:                        switch(GetLogicalDrives() & 0x03) {
                   8207:                        case 0x00: REG8(DL) = 0x00; break;
                   8208:                        case 0x01:
                   8209:                        case 0x02: REG8(DL) = 0x01; break;
                   8210:                        case 0x03: REG8(DL) = 0x02; break;
                   8211:                        }
                   8212:                }
                   8213:                REG8(DH) = drive_param->head_num();
                   8214:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   8215:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   8216:                REG8(CH) = cyl & 0xff;
                   8217:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   8218:        } else {
                   8219:                REG8(AH) = 0x07;
                   8220:                m_CF = 1;
                   8221:        }
                   8222: }
                   8223: 
                   8224: inline void pcbios_int_13h_10h()
                   8225: {
                   8226:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8227:        
                   8228:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8229:                REG8(AH) = 0x00; // successful completion
                   8230:        } else {
                   8231:                if(REG8(DL) & 0x80) {
                   8232:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   8233:                } else {
                   8234:                        REG8(AH) = 0x80; // timeout (not ready)
                   8235:                }
                   8236:                m_CF = 1;
                   8237:        }
                   8238: }
                   8239: 
                   8240: inline void pcbios_int_13h_15h()
                   8241: {
                   8242:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   8243:        
                   8244:        if(pcbios_update_drive_param(drive_num, 1)) {
                   8245:                if(REG8(DL) & 0x80) {
                   8246:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   8247:                } else {
                   8248:                        REG8(AH) = 0x03; // hard disk
                   8249:                }
                   8250:        } else {
                   8251:                REG8(AH) = 0x00; // no such drive
                   8252:        }
                   8253: }
                   8254: 
1.1.1.43  root     8255: inline void pcbios_int_13h_41h()
                   8256: {
                   8257:        if(REG16(BX) == 0x55aa) {
                   8258:                // IBM/MS INT 13 Extensions is not installed
                   8259:                REG8(AH) = 0x01;
                   8260:                m_CF = 1;
                   8261:        } else {
                   8262:                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));
                   8263:                REG8(AH) = 0x01;
                   8264:                m_CF = 1;
                   8265:        }
                   8266: }
                   8267: 
1.1.1.25  root     8268: inline void pcbios_int_14h_00h()
                   8269: {
1.1.1.29  root     8270:        if(REG16(DX) < 4) {
1.1.1.25  root     8271:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   8272:                UINT8 selector = sio_read(REG16(DX), 3);
                   8273:                selector &= ~0x3f;
                   8274:                selector |= REG8(AL) & 0x1f;
                   8275:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   8276:                sio_write(REG16(DX), 3, selector | 0x80);
                   8277:                sio_write(REG16(DX), 0, divisor & 0xff);
                   8278:                sio_write(REG16(DX), 1, divisor >> 8);
                   8279:                sio_write(REG16(DX), 3, selector);
                   8280:                REG8(AH) = sio_read(REG16(DX), 5);
                   8281:                REG8(AL) = sio_read(REG16(DX), 6);
                   8282:        } else {
                   8283:                REG8(AH) = 0x80;
                   8284:        }
                   8285: }
                   8286: 
                   8287: inline void pcbios_int_14h_01h()
                   8288: {
1.1.1.29  root     8289:        if(REG16(DX) < 4) {
1.1.1.25  root     8290:                UINT8 selector = sio_read(REG16(DX), 3);
                   8291:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8292:                sio_write(REG16(DX), 0, REG8(AL));
                   8293:                sio_write(REG16(DX), 3, selector);
                   8294:                REG8(AH) = sio_read(REG16(DX), 5);
                   8295:        } else {
                   8296:                REG8(AH) = 0x80;
                   8297:        }
                   8298: }
                   8299: 
                   8300: inline void pcbios_int_14h_02h()
                   8301: {
1.1.1.29  root     8302:        if(REG16(DX) < 4) {
1.1.1.25  root     8303:                UINT8 selector = sio_read(REG16(DX), 3);
                   8304:                sio_write(REG16(DX), 3, selector & ~0x80);
                   8305:                REG8(AL) = sio_read(REG16(DX), 0);
                   8306:                sio_write(REG16(DX), 3, selector);
                   8307:                REG8(AH) = sio_read(REG16(DX), 5);
                   8308:        } else {
                   8309:                REG8(AH) = 0x80;
                   8310:        }
                   8311: }
                   8312: 
                   8313: inline void pcbios_int_14h_03h()
                   8314: {
1.1.1.29  root     8315:        if(REG16(DX) < 4) {
1.1.1.25  root     8316:                REG8(AH) = sio_read(REG16(DX), 5);
                   8317:                REG8(AL) = sio_read(REG16(DX), 6);
                   8318:        } else {
                   8319:                REG8(AH) = 0x80;
                   8320:        }
                   8321: }
                   8322: 
                   8323: inline void pcbios_int_14h_04h()
                   8324: {
1.1.1.29  root     8325:        if(REG16(DX) < 4) {
1.1.1.25  root     8326:                UINT8 selector = sio_read(REG16(DX), 3);
                   8327:                if(REG8(CH) <= 0x03) {
                   8328:                        selector = (selector & ~0x03) | REG8(CH);
                   8329:                }
                   8330:                if(REG8(BL) == 0x00) {
                   8331:                        selector &= ~0x04;
                   8332:                } else if(REG8(BL) == 0x01) {
                   8333:                        selector |= 0x04;
                   8334:                }
                   8335:                if(REG8(BH) == 0x00) {
                   8336:                        selector = (selector & ~0x38) | 0x00;
                   8337:                } else if(REG8(BH) == 0x01) {
                   8338:                        selector = (selector & ~0x38) | 0x08;
                   8339:                } else if(REG8(BH) == 0x02) {
                   8340:                        selector = (selector & ~0x38) | 0x18;
                   8341:                } else if(REG8(BH) == 0x03) {
                   8342:                        selector = (selector & ~0x38) | 0x28;
                   8343:                } else if(REG8(BH) == 0x04) {
                   8344:                        selector = (selector & ~0x38) | 0x38;
                   8345:                }
                   8346:                if(REG8(AL) == 0x00) {
                   8347:                        selector |= 0x40;
                   8348:                } else if(REG8(AL) == 0x01) {
                   8349:                        selector &= ~0x40;
                   8350:                }
                   8351:                if(REG8(CL) <= 0x0b) {
                   8352:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   8353:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   8354:                        sio_write(REG16(DX), 3, selector | 0x80);
                   8355:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   8356:                        sio_write(REG16(DX), 1, divisor >> 8);
                   8357:                }
                   8358:                sio_write(REG16(DX), 3, selector);
                   8359:                REG8(AH) = sio_read(REG16(DX), 5);
                   8360:                REG8(AL) = sio_read(REG16(DX), 6);
                   8361:        } else {
                   8362:                REG8(AH) = 0x80;
                   8363:        }
                   8364: }
                   8365: 
                   8366: inline void pcbios_int_14h_05h()
                   8367: {
1.1.1.29  root     8368:        if(REG16(DX) < 4) {
1.1.1.25  root     8369:                if(REG8(AL) == 0x00) {
                   8370:                        REG8(BL) = sio_read(REG16(DX), 4);
                   8371:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8372:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8373:                } else if(REG8(AL) == 0x01) {
                   8374:                        sio_write(REG16(DX), 4, REG8(BL));
                   8375:                        REG8(AH) = sio_read(REG16(DX), 5);
                   8376:                        REG8(AL) = sio_read(REG16(DX), 6);
                   8377:                } else {
                   8378:                        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));
                   8379:                }
                   8380:        } else {
                   8381:                REG8(AH) = 0x80;
                   8382:        }
                   8383: }
                   8384: 
1.1       root     8385: inline void pcbios_int_15h_23h()
                   8386: {
                   8387:        switch(REG8(AL)) {
1.1.1.22  root     8388:        case 0x00:
1.1.1.8   root     8389:                REG8(CL) = cmos_read(0x2d);
                   8390:                REG8(CH) = cmos_read(0x2e);
1.1       root     8391:                break;
1.1.1.22  root     8392:        case 0x01:
1.1.1.8   root     8393:                cmos_write(0x2d, REG8(CL));
                   8394:                cmos_write(0x2e, REG8(CH));
1.1       root     8395:                break;
                   8396:        default:
1.1.1.22  root     8397:                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     8398:                REG8(AH) = 0x86;
1.1.1.3   root     8399:                m_CF = 1;
1.1       root     8400:                break;
                   8401:        }
                   8402: }
                   8403: 
                   8404: inline void pcbios_int_15h_24h()
                   8405: {
                   8406:        switch(REG8(AL)) {
1.1.1.22  root     8407:        case 0x00:
1.1.1.3   root     8408:                i386_set_a20_line(0);
1.1       root     8409:                REG8(AH) = 0;
                   8410:                break;
1.1.1.22  root     8411:        case 0x01:
1.1.1.3   root     8412:                i386_set_a20_line(1);
1.1       root     8413:                REG8(AH) = 0;
                   8414:                break;
1.1.1.22  root     8415:        case 0x02:
1.1       root     8416:                REG8(AH) = 0;
1.1.1.3   root     8417:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     8418:                REG16(CX) = 0;
                   8419:                break;
1.1.1.22  root     8420:        case 0x03:
1.1       root     8421:                REG16(AX) = 0;
                   8422:                REG16(BX) = 0;
                   8423:                break;
1.1.1.22  root     8424:        default:
                   8425:                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));
                   8426:                REG8(AH) = 0x86;
                   8427:                m_CF = 1;
                   8428:                break;
1.1       root     8429:        }
                   8430: }
                   8431: 
                   8432: inline void pcbios_int_15h_49h()
                   8433: {
1.1.1.27  root     8434:        REG8(AH) = 0x00;
                   8435:        REG8(BL) = 0x00; // DOS/V
1.1.1.64  root     8436: //     REG8(BL) = 0x01; // standard DBCS DOS (hardware DBCS support)
1.1       root     8437: }
                   8438: 
1.1.1.22  root     8439: inline void pcbios_int_15h_50h()
                   8440: {
                   8441:        switch(REG8(AL)) {
                   8442:        case 0x00:
                   8443:        case 0x01:
                   8444:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8445:                        REG8(AH) = 0x01; // invalid font type in bh
                   8446:                        m_CF = 1;
1.1.1.27  root     8447:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8448:                        REG8(AH) = 0x02; // bl not zero
                   8449:                        m_CF = 1;
                   8450:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8451:                        REG8(AH) = 0x04; // invalid code page
                   8452:                        m_CF = 1;
1.1.1.27  root     8453:                } else if(REG8(AL) == 0x01) {
                   8454:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8455:                        m_CF = 1;
1.1.1.27  root     8456:                } else {
1.1.1.49  root     8457:                        // dummy font read routine is at fffc:000d
                   8458:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8459:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8460:                        REG16(BX) = 0x000d;
1.1.1.27  root     8461:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8462:                }
                   8463:                break;
                   8464:        default:
                   8465:                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));
                   8466:                REG8(AH) = 0x86;
                   8467:                m_CF = 1;
                   8468:                break;
                   8469:        }
                   8470: }
                   8471: 
1.1.1.30  root     8472: inline void pcbios_int_15h_53h()
                   8473: {
                   8474:        switch(REG8(AL)) {
                   8475:        case 0x00:
                   8476:                // APM is not installed
                   8477:                REG8(AH) = 0x86;
                   8478:                m_CF = 1;
                   8479:                break;
                   8480:        default:
                   8481:                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));
                   8482:                REG8(AH) = 0x86;
                   8483:                m_CF = 1;
                   8484:                break;
                   8485:        }
                   8486: }
                   8487: 
1.1.1.43  root     8488: inline void pcbios_int_15h_84h()
                   8489: {
                   8490:        // joystick support (from DOSBox)
                   8491:        switch(REG16(DX)) {
                   8492:        case 0x00:
                   8493:                REG16(AX) = 0x00f0;
                   8494:                REG16(DX) = 0x0201;
                   8495:                m_CF = 1;
                   8496:                break;
                   8497:        case 0x01:
                   8498:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8499:                m_CF = 1;
                   8500:                break;
                   8501:        default:
                   8502:                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));
                   8503:                REG8(AH) = 0x86;
                   8504:                m_CF = 1;
                   8505:                break;
                   8506:        }
                   8507: }
1.1.1.35  root     8508: 
                   8509: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8510: {
                   8511:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8512:        UINT32 msec = usec / 1000;
                   8513:        
1.1.1.54  root     8514:        while(msec && !m_exit) {
1.1.1.14  root     8515:                UINT32 tmp = min(msec, 100);
                   8516:                if(msec - tmp < 10) {
                   8517:                        tmp = msec;
                   8518:                }
                   8519:                Sleep(tmp);
                   8520:                msec -= tmp;
                   8521:        }
1.1.1.35  root     8522:        
                   8523: #ifdef USE_SERVICE_THREAD
                   8524:        service_exit = true;
                   8525: #endif
                   8526:        return(0);
                   8527: }
                   8528: 
                   8529: inline void pcbios_int_15h_86h()
                   8530: {
                   8531:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8532: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8533:                if(!in_service && !in_service_29h) {
                   8534:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8535:                } else {
                   8536: #endif
                   8537:                        pcbios_int_15h_86h_thread(NULL);
                   8538:                        REQUEST_HARDWRE_UPDATE();
                   8539: #ifdef USE_SERVICE_THREAD
                   8540:                }
1.1.1.35  root     8541: #endif
                   8542:        }
1.1       root     8543: }
                   8544: 
                   8545: inline void pcbios_int_15h_87h()
                   8546: {
                   8547:        // copy extended memory (from DOSBox)
                   8548:        int len = REG16(CX) * 2;
1.1.1.3   root     8549:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8550:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8551:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8552:        memcpy(mem + dst, mem + src, len);
                   8553:        REG16(AX) = 0x00;
                   8554: }
                   8555: 
                   8556: inline void pcbios_int_15h_88h()
                   8557: {
1.1.1.17  root     8558:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8559: }
                   8560: 
                   8561: inline void pcbios_int_15h_89h()
                   8562: {
1.1.1.21  root     8563: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8564:        // switch to protected mode (from DOSBox)
                   8565:        write_io_byte(0x20, 0x10);
                   8566:        write_io_byte(0x21, REG8(BH));
                   8567:        write_io_byte(0x21, 0x00);
1.1.1.64  root     8568:        write_io_byte(0x21, 0xff);
1.1       root     8569:        write_io_byte(0xa0, 0x10);
                   8570:        write_io_byte(0xa1, REG8(BL));
                   8571:        write_io_byte(0xa1, 0x00);
1.1.1.64  root     8572:        write_io_byte(0xa1, 0xff);
1.1.1.3   root     8573:        i386_set_a20_line(1);
1.1.1.64  root     8574:        m_gdtr.limit = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x08);
                   8575:        m_gdtr.base  = *(UINT32 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x08 + 0x02) & 0xffffff;
                   8576:        m_idtr.limit = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x10);
                   8577:        m_idtr.base  = *(UINT32 *)(mem + SREG_BASE(ES) + REG16(SI) + 0x10 + 0x02) & 0xffffff;
1.1.1.3   root     8578: #if defined(HAS_I386)
                   8579:        m_cr[0] |= 1;
                   8580: #else
                   8581:        m_msw |= 1;
                   8582: #endif
1.1.1.64  root     8583:        i386_sreg_load(0x18, DS, NULL);
                   8584:        i386_sreg_load(0x20, ES, NULL);
                   8585:        i386_sreg_load(0x28, SS, NULL);
1.1.1.21  root     8586:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1.1.64  root     8587:        REG16(SP) += 6; // clear stack of interrupt frame
                   8588:        UINT32 flags = i386_get_flags();
                   8589:        flags &= ~0x247fd5; // clear CF,PF,AF,ZF,SF,TF,IF,DF,OF,IOPL,NT,AC,ID
                   8590:        i386_set_flags(flags);
1.1       root     8591:        REG16(AX) = 0x00;
1.1.1.21  root     8592:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8593: #else
1.1.1.21  root     8594:        // i86/i186/v30: protected mode is not supported
1.1       root     8595:        REG8(AH) = 0x86;
1.1.1.3   root     8596:        m_CF = 1;
1.1       root     8597: #endif
                   8598: }
                   8599: 
1.1.1.21  root     8600: inline void pcbios_int_15h_8ah()
                   8601: {
                   8602:        UINT32 size = MAX_MEM - 0x100000;
                   8603:        REG16(AX) = size & 0xffff;
                   8604:        REG16(DX) = size >> 16;
                   8605: }
                   8606: 
1.1.1.54  root     8607: #ifdef EXT_BIOS_TOP
                   8608: inline void pcbios_int_15h_c1h()
                   8609: {
                   8610:        SREG(ES) = EXT_BIOS_TOP >> 4;
                   8611:        i386_load_segment_descriptor(ES);
                   8612: }
                   8613: #endif
                   8614: 
                   8615: void pcbios_read_from_ps2_mouse(UINT16 *data_1st, UINT16 *data_2nd, UINT16 *data_3rd)
                   8616: {
                   8617:        // from DOSBox DoPS2Callback()
                   8618:        UINT16 mdat = 0x08;
                   8619:        INT16 xdiff = mouse.position.x - mouse.prev_position.x;
                   8620:        INT16 ydiff = mouse.prev_position.y - mouse.position.y;
                   8621:        
1.1.1.59  root     8622: #if 1
                   8623:        if(xdiff > +16) xdiff = +16;
                   8624:        if(xdiff < -16) xdiff = -16;
                   8625:        if(ydiff > +16) ydiff = +16;
                   8626:        if(ydiff < -16) ydiff = -16;
                   8627: #endif
                   8628:        
1.1.1.54  root     8629:        if(mouse.buttons[0].status) {
                   8630:                mdat |= 0x01;
                   8631:        }
                   8632:        if(mouse.buttons[1].status) {
                   8633:                mdat |= 0x02;
                   8634:        }
                   8635:        mouse.prev_position.x = mouse.position.x;
                   8636:        mouse.prev_position.y = mouse.position.y;
1.1.1.59  root     8637:        
1.1.1.54  root     8638:        if((xdiff > 0xff) || (xdiff < -0xff)) {
                   8639:                mdat |= 0x40;   // x overflow
                   8640:        }
                   8641:        if((ydiff > 0xff) || (ydiff < -0xff)) {
                   8642:                mdat |= 0x80;   // y overflow
                   8643:        }
                   8644:        xdiff %= 256;
                   8645:        ydiff %= 256;
                   8646:        if(xdiff < 0) {
                   8647:                xdiff = (0x100 + xdiff);
                   8648:                mdat |= 0x10;
                   8649:        }
                   8650:        if(ydiff < 0) {
                   8651:                ydiff = (0x100 + ydiff);
                   8652:                mdat |= 0x20;
                   8653:        }
                   8654:        *data_1st = (UINT16)mdat;
                   8655:        *data_2nd = (UINT16)(xdiff % 256);
                   8656:        *data_3rd = (UINT16)(ydiff % 256);
                   8657: }
                   8658: 
                   8659: inline void pcbios_int_15h_c2h()
                   8660: {
                   8661:        static UINT8 sampling_rate = 5;
                   8662:        static UINT8 resolution = 2;
                   8663:        static UINT8 scaling = 1;
                   8664:        
                   8665:        switch(REG8(AL)) {
                   8666:        case 0x00:
1.1.1.59  root     8667:                if(REG8(BH) == 0x00) {
1.1.1.64  root     8668:                        if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
                   8669:                                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
                   8670:                        } else {
                   8671:                                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   8672:                        }
1.1.1.59  root     8673:                        pic[1].imr |= 0x10; // disable irq12
                   8674:                        mouse.enabled_ps2 = false;
                   8675:                        REG8(AH) = 0x00; // successful
                   8676:                } else if(REG8(BH) == 0x01) {
1.1.1.64  root     8677:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~ENABLE_QUICK_EDIT_MODE);
1.1.1.59  root     8678:                        pic[1].imr &= ~0x10; // enable irq12
                   8679:                        mouse.enabled_ps2 = true;
1.1.1.54  root     8680:                        REG8(AH) = 0x00; // successful
                   8681:                } else {
                   8682:                        REG8(AH) = 0x01; // invalid function
                   8683:                        m_CF = 1;
                   8684:                }
                   8685:                break;
                   8686:        case 0x01:
                   8687:                REG8(BH) = 0x00; // device id
                   8688:                REG8(BL) = 0xaa; // mouse
                   8689:        case 0x05:
1.1.1.64  root     8690:                if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
                   8691:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
                   8692:                } else {
                   8693:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   8694:                }
1.1.1.59  root     8695:                pic[1].imr |= 0x10; // disable irq12
                   8696:                mouse.enabled_ps2 = false;
1.1.1.54  root     8697:                sampling_rate = 5;
                   8698:                resolution = 2;
                   8699:                scaling = 1;
                   8700:                REG8(AH) = 0x00; // successful
                   8701:                break;
                   8702:        case 0x02:
                   8703:                sampling_rate = REG8(BH);
                   8704:                REG8(AH) = 0x00; // successful
                   8705:                break;
                   8706:        case 0x03:
                   8707:                resolution = REG8(BH);
                   8708:                REG8(AH) = 0x00; // successful
                   8709:                break;
                   8710:        case 0x04:
                   8711:                REG8(BH) = 0x00; // device id
                   8712:                REG8(AH) = 0x00; // successful
                   8713:                break;
                   8714:        case 0x06:
                   8715:                switch(REG8(BH)) {
                   8716:                case 0x00:
                   8717:                        REG8(BL) = 0x00;
                   8718:                        if(mouse.buttons[1].status) {
                   8719:                                REG8(BL) |= 0x01;
                   8720:                        }
                   8721:                        if(mouse.buttons[0].status) {
                   8722:                                REG8(BL) |= 0x04;
                   8723:                        }
                   8724:                        if(scaling == 2) {
                   8725:                                REG8(BL) |= 0x10;
                   8726:                        }
                   8727:                        REG8(CL) = resolution;
                   8728:                        switch(sampling_rate) {
                   8729:                        case 0:  REG8(DL) =  10; break;
                   8730:                        case 1:  REG8(DL) =  20; break;
                   8731:                        case 2:  REG8(DL) =  40; break;
                   8732:                        case 3:  REG8(DL) =  60; break;
                   8733:                        case 4:  REG8(DL) =  80; break;
                   8734: //                     case 5:  REG8(DL) = 100; break;
                   8735:                        case 6:  REG8(DL) = 200; break;
                   8736:                        default: REG8(DL) = 100; break;
                   8737:                        }
                   8738:                        REG8(AH) = 0x00; // successful
                   8739:                        break;
                   8740:                case 0x01:
                   8741:                case 0x02:
                   8742:                        scaling = REG8(BH);
                   8743:                        REG8(AH) = 0x00; // successful
                   8744:                        break;
                   8745:                default:
                   8746:                        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));
                   8747:                        REG8(AH) = 0x01; // invalid function
                   8748:                        m_CF = 1;
                   8749:                        break;
                   8750:                }
                   8751:                break;
                   8752:        case 0x07: // set device handler addr
                   8753:                mouse.call_addr_ps2.w.l = REG16(BX);
                   8754:                mouse.call_addr_ps2.w.h = SREG(ES);
                   8755:                REG8(AH) = 0x00; // successful
                   8756:                break;
                   8757:        case 0x08:
                   8758:                REG8(AH) = 0x00; // successful
                   8759:                break;
                   8760:        case 0x09:
                   8761:                {
                   8762:                        UINT16 data_1st, data_2nd, data_3rd;
                   8763:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   8764:                        REG8(BL) = (UINT8)(data_1st & 0xff);
                   8765:                        REG8(CL) = (UINT8)(data_2nd & 0xff);
                   8766:                        REG8(DL) = (UINT8)(data_3rd & 0xff);
                   8767:                }
                   8768:                REG8(AH) = 0x00; // successful
                   8769:                break;
                   8770:        default:
                   8771:                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));
                   8772: //             REG8(AH) = 0x86;
                   8773:                REG8(AH) = 0x01; // invalid function
                   8774:                m_CF = 1;
                   8775:                break;
                   8776:        }
                   8777: }
                   8778: 
1.1.1.3   root     8779: #if defined(HAS_I386)
1.1       root     8780: inline void pcbios_int_15h_c9h()
                   8781: {
                   8782:        REG8(AH) = 0x00;
                   8783:        REG8(CH) = cpu_type;
                   8784:        REG8(CL) = cpu_step;
                   8785: }
1.1.1.3   root     8786: #endif
1.1       root     8787: 
                   8788: inline void pcbios_int_15h_cah()
                   8789: {
                   8790:        switch(REG8(AL)) {
1.1.1.22  root     8791:        case 0x00:
1.1       root     8792:                if(REG8(BL) > 0x3f) {
                   8793:                        REG8(AH) = 0x03;
1.1.1.3   root     8794:                        m_CF = 1;
1.1       root     8795:                } else if(REG8(BL) < 0x0e) {
                   8796:                        REG8(AH) = 0x04;
1.1.1.3   root     8797:                        m_CF = 1;
1.1       root     8798:                } else {
1.1.1.8   root     8799:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8800:                }
                   8801:                break;
1.1.1.22  root     8802:        case 0x01:
1.1       root     8803:                if(REG8(BL) > 0x3f) {
                   8804:                        REG8(AH) = 0x03;
1.1.1.3   root     8805:                        m_CF = 1;
1.1       root     8806:                } else if(REG8(BL) < 0x0e) {
                   8807:                        REG8(AH) = 0x04;
1.1.1.3   root     8808:                        m_CF = 1;
1.1       root     8809:                } else {
1.1.1.8   root     8810:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8811:                }
                   8812:                break;
                   8813:        default:
1.1.1.22  root     8814:                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     8815:                REG8(AH) = 0x86;
1.1.1.3   root     8816:                m_CF = 1;
1.1       root     8817:                break;
                   8818:        }
                   8819: }
                   8820: 
1.1.1.22  root     8821: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8822: {
1.1.1.22  root     8823:        switch(REG8(AL)) {
                   8824:        case 0x01:
1.1.1.64  root     8825:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x0100000) >> 10);
1.1.1.22  root     8826:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8827:                break;
1.1.1.64  root     8828: #if defined(HAS_I386)
                   8829:        case 0x20:
1.1.1.65  root     8830:                if(REG32(EDX) == 0x534d4150 && REG32(ECX) >= 20) {
1.1.1.64  root     8831:                        if(REG32(EBX) < 3) {
                   8832:                                UINT32 base = 0, len = 0, type = 0;
                   8833:                                switch(REG32(EBX)) {
                   8834:                                case 0:
                   8835:                                        base = 0x000000;
                   8836:                                        len  = MEMORY_END;
                   8837:                                        type = 1;
                   8838:                                        break;
                   8839:                                case 1:
                   8840:                                        base = DUMMY_TOP;
                   8841:                                        len  = 0x100000 - DUMMY_TOP;
                   8842:                                        type = 2;
                   8843:                                        break;
                   8844:                                case 2:
                   8845:                                        base = 0x100000;
                   8846:                                        len  = MAX_MEM - 0x100000;
                   8847:                                        type = 1;
                   8848:                                        break;
                   8849:                                }
                   8850:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x00) = base;
                   8851:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x04) = 0;
                   8852:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x08) = len;
                   8853:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x0c) = 0;
                   8854:                                *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 0x10) = type;
                   8855:                                
                   8856:                                if(++REG32(EBX) >= 3) {
                   8857:                                        REG32(EBX) = 0;
                   8858:                                }
                   8859:                                REG32(ECX) = 20;
                   8860:                        } else {
                   8861:                                m_CF = 1;
                   8862:                        }
                   8863:                        REG32(EAX) = 0x534d4150;
                   8864:                        break;
                   8865:                }
                   8866:        case 0x81:
                   8867:                REG32(EAX) = REG32(ECX) = ((min(MAX_MEM, 0x1000000) - 0x0100000) >> 10);
                   8868:                REG32(EBX) = REG32(EDX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8869:                break;
1.1.1.17  root     8870: #endif
1.1.1.22  root     8871:        default:
                   8872:                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));
                   8873:                REG8(AH) = 0x86;
                   8874:                m_CF = 1;
                   8875:                break;
                   8876:        }
                   8877: }
1.1.1.17  root     8878: 
1.1.1.55  root     8879: bool pcbios_is_key_buffer_empty()
                   8880: {
                   8881:        return(*(UINT16 *)(mem + 0x41a) == *(UINT16 *)(mem + 0x41c));
                   8882: }
                   8883: 
1.1.1.51  root     8884: void pcbios_clear_key_buffer()
                   8885: {
                   8886:        key_buf_char->clear();
                   8887:        key_buf_scan->clear();
                   8888:        
                   8889:        // update key buffer
                   8890:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8891: }
                   8892: 
                   8893: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8894: {
                   8895:        // update key buffer
                   8896:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8897:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8898:        UINT16 next = tail + 2;
                   8899:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8900:                next = *(UINT16 *)(mem + 0x480);
                   8901:        }
                   8902:        if(next != head) {
                   8903:                *(UINT16 *)(mem + 0x41c) = next;
                   8904:                mem[0x400 + (tail++)] = key_char;
                   8905:                mem[0x400 + (tail++)] = key_scan;
1.1.1.55  root     8906:        } else {
                   8907:                // store to extra key buffer
                   8908:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8909:                        key_buf_char->write(key_char);
                   8910:                        key_buf_scan->write(key_scan);
                   8911:                }
1.1.1.51  root     8912:        }
                   8913: }
                   8914: 
                   8915: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8916: {
                   8917:        // update key buffer
                   8918:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8919:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8920:        UINT16 next = head + 2;
                   8921:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8922:                next = *(UINT16 *)(mem + 0x480);
                   8923:        }
                   8924:        if(head != tail) {
                   8925:                *(UINT16 *)(mem + 0x41a) = next;
1.1.1.55  root     8926:                *key_char = mem[0x400 + (head++)];
                   8927:                *key_scan = mem[0x400 + (head++)];
                   8928:                
                   8929:                // restore from extra key buffer
                   8930:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   8931:                        if(!key_buf_char->empty()) {
                   8932:                                pcbios_set_key_buffer(key_buf_char->read(), key_buf_scan->read());
                   8933:                        }
                   8934:                }
                   8935:                return(true);
                   8936:        } else {
                   8937:                *key_char = 0x00;
                   8938:                *key_scan = 0x00;
                   8939:                return(false);
1.1.1.51  root     8940:        }
                   8941: }
                   8942: 
1.1.1.60  root     8943: bool pcbios_check_key_buffer(int *key_char, int *key_scan)
                   8944: {
                   8945:        // do not remove from key buffer
                   8946:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8947:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8948:        if(head != tail) {
                   8949:                *key_char = mem[0x400 + (head++)];
                   8950:                *key_scan = mem[0x400 + (head++)];
                   8951:                return(true);
                   8952:        } else {
                   8953:                *key_char = 0x00;
                   8954:                *key_scan = 0x00;
                   8955:                return(false);
                   8956:        }
                   8957: }
                   8958: 
1.1.1.33  root     8959: void pcbios_update_key_code(bool wait)
1.1       root     8960: {
1.1.1.32  root     8961:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8962: #ifdef USE_SERVICE_THREAD
                   8963:                EnterCriticalSection(&key_buf_crit_sect);
                   8964: #endif
1.1.1.55  root     8965:                bool empty = pcbios_is_key_buffer_empty();
1.1.1.35  root     8966: #ifdef USE_SERVICE_THREAD
                   8967:                LeaveCriticalSection(&key_buf_crit_sect);
                   8968: #endif
                   8969:                if(empty) {
1.1.1.32  root     8970:                        if(!update_key_buffer()) {
1.1.1.33  root     8971:                                if(wait) {
1.1.1.32  root     8972:                                        Sleep(10);
                   8973:                                } else {
                   8974:                                        maybe_idle();
                   8975:                                }
1.1.1.14  root     8976:                        }
                   8977:                }
1.1.1.34  root     8978:        }
                   8979:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8980: #ifdef USE_SERVICE_THREAD
                   8981:                EnterCriticalSection(&key_buf_crit_sect);
                   8982: #endif
1.1.1.51  root     8983:                int key_char, key_scan;
                   8984:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8985:                        key_code  = key_char << 0;
                   8986:                        key_code |= key_scan << 8;
1.1.1.35  root     8987:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8988:                }
                   8989:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8990:                        key_code |= key_char << 16;
                   8991:                        key_code |= key_scan << 24;
1.1.1.33  root     8992:                        key_recv |= 0xffff0000;
1.1.1.32  root     8993:                }
1.1.1.35  root     8994: #ifdef USE_SERVICE_THREAD
                   8995:                LeaveCriticalSection(&key_buf_crit_sect);
                   8996: #endif
1.1       root     8997:        }
                   8998: }
                   8999: 
1.1.1.35  root     9000: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     9001: {
1.1.1.54  root     9002:        while(key_recv == 0 && !m_exit) {
1.1.1.33  root     9003:                pcbios_update_key_code(true);
1.1       root     9004:        }
1.1.1.33  root     9005:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   9006:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   9007:                        if(REG8(AH) == 0x10) {
                   9008:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   9009:                        } else {
                   9010:                                key_code = ((key_code >> 16) & 0xff00);
                   9011:                        }
                   9012:                        key_recv >>= 16;
1.1       root     9013:                }
                   9014:        }
                   9015:        REG16(AX) = key_code & 0xffff;
                   9016:        key_code >>= 16;
1.1.1.33  root     9017:        key_recv >>= 16;
1.1.1.35  root     9018:        
                   9019: #ifdef USE_SERVICE_THREAD
                   9020:        service_exit = true;
                   9021: #endif
                   9022:        return(0);
                   9023: }
                   9024: 
                   9025: inline void pcbios_int_16h_00h()
                   9026: {
                   9027: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9028:        if(!in_service && !in_service_29h) {
                   9029:                start_service_loop(pcbios_int_16h_00h_thread);
                   9030:        } else {
                   9031: #endif
                   9032:                pcbios_int_16h_00h_thread(NULL);
                   9033:                REQUEST_HARDWRE_UPDATE();
                   9034: #ifdef USE_SERVICE_THREAD
                   9035:        }
1.1.1.35  root     9036: #endif
1.1       root     9037: }
                   9038: 
                   9039: inline void pcbios_int_16h_01h()
                   9040: {
1.1.1.33  root     9041:        if(key_recv == 0) {
                   9042:                pcbios_update_key_code(false);
1.1.1.5   root     9043:        }
1.1.1.33  root     9044:        if(key_recv != 0) {
                   9045:                UINT32 key_code_tmp = key_code;
                   9046:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   9047:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   9048:                                if(REG8(AH) == 0x11) {
                   9049:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   9050:                                } else {
                   9051:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   9052:                                }
                   9053:                        }
1.1       root     9054:                }
1.1.1.5   root     9055:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     9056: #if defined(HAS_I386)
1.1.1.33  root     9057:                m_ZF = 0;
                   9058: #else
                   9059:                m_ZeroVal = 1;
                   9060: #endif
                   9061:        } else {
                   9062: #if defined(HAS_I386)
                   9063:                m_ZF = 1;
1.1.1.3   root     9064: #else
1.1.1.33  root     9065:                m_ZeroVal = 0;
1.1.1.3   root     9066: #endif
1.1.1.33  root     9067:        }
1.1       root     9068: }
                   9069: 
                   9070: inline void pcbios_int_16h_02h()
                   9071: {
                   9072:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   9073:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   9074:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   9075:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   9076:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   9077:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   9078:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   9079:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   9080: }
                   9081: 
                   9082: inline void pcbios_int_16h_03h()
                   9083: {
                   9084:        static UINT16 status = 0;
                   9085:        
                   9086:        switch(REG8(AL)) {
                   9087:        case 0x05:
                   9088:                status = REG16(BX);
                   9089:                break;
                   9090:        case 0x06:
                   9091:                REG16(BX) = status;
                   9092:                break;
                   9093:        default:
1.1.1.3   root     9094:                m_CF = 1;
1.1       root     9095:                break;
                   9096:        }
                   9097: }
                   9098: 
                   9099: inline void pcbios_int_16h_05h()
                   9100: {
1.1.1.32  root     9101:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     9102: #ifdef USE_SERVICE_THREAD
                   9103:                EnterCriticalSection(&key_buf_crit_sect);
                   9104: #endif
1.1.1.51  root     9105:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     9106: #ifdef USE_SERVICE_THREAD
                   9107:                LeaveCriticalSection(&key_buf_crit_sect);
                   9108: #endif
1.1.1.32  root     9109:        }
1.1       root     9110:        REG8(AL) = 0x00;
                   9111: }
                   9112: 
1.1.1.60  root     9113: inline void pcbios_int_16h_09h()
                   9114: {
                   9115:        REG8(AL)  = 0x00;
                   9116: //     REG8(AL) |= 0x01;       // INT 16/AX=0300h supported    (set default delay and rate (PCjr and some PS/2))
                   9117: //     REG8(AL) |= 0x02;       // INT 16/AX=0304h supported    (turn off typematic repeat (PCjr and some PS/2))
                   9118:        REG8(AL) |= 0x04;       // INT 16/AX=0305h supported    (set repeat rate and delay (AT,PS))
                   9119:        REG8(AL) |= 0x08;       // INT 16/AX=0306h supported    (get current typematic rate and delay (newer PS/2s))
                   9120:        REG8(AL) |= 0x10;       // INT 16/AH=0Ah supported      (get keyboard id)
                   9121:        REG8(AL) |= 0x20;       // INT 16/AH=10h-12h supported  (enhanced keyboard support)
                   9122: //     REG8(AL) |= 0x40;       // INT 16/AH=20h-22h supported  (122-key keyboard support)
                   9123: //     REG8(AL) |= 0x80;       // reserved
                   9124: }
                   9125: 
                   9126: inline void pcbios_int_16h_0ah()
                   9127: {
                   9128: //     REG16(BX) = 0x41ab;     // MF2 Keyboard (usually in translate mode)
                   9129:        REG16(BX) = 0x83ab;     // MF2 Keyboard (pass-through mode)
                   9130: }
                   9131: 
                   9132: inline void pcbios_int_16h_11h()
                   9133: {
                   9134:        int key_char, key_scan;
                   9135:        
                   9136: #ifdef USE_SERVICE_THREAD
                   9137:        EnterCriticalSection(&key_buf_crit_sect);
                   9138: #endif
                   9139:        if(pcbios_check_key_buffer(&key_char, &key_scan)) {
                   9140:                REG8(AL) = key_char;
                   9141:                REG8(AH) = key_scan;
                   9142: #if defined(HAS_I386)
                   9143:                m_ZF = 0;
                   9144: #else
                   9145:                m_ZeroVal = 1;
                   9146: #endif
                   9147:        } else {
                   9148: #if defined(HAS_I386)
                   9149:                m_ZF = 1;
                   9150: #else
                   9151:                m_ZeroVal = 0;
                   9152: #endif
                   9153:        }
                   9154: #ifdef USE_SERVICE_THREAD
                   9155:        LeaveCriticalSection(&key_buf_crit_sect);
                   9156: #endif
                   9157: }
                   9158: 
1.1       root     9159: inline void pcbios_int_16h_12h()
                   9160: {
                   9161:        pcbios_int_16h_02h();
                   9162:        
                   9163:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   9164:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   9165:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   9166:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   9167:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   9168:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   9169:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   9170:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   9171: }
                   9172: 
                   9173: inline void pcbios_int_16h_13h()
                   9174: {
                   9175:        static UINT16 status = 0;
                   9176:        
                   9177:        switch(REG8(AL)) {
                   9178:        case 0x00:
                   9179:                status = REG16(DX);
                   9180:                break;
                   9181:        case 0x01:
                   9182:                REG16(DX) = status;
                   9183:                break;
                   9184:        default:
1.1.1.22  root     9185:                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     9186:                m_CF = 1;
1.1       root     9187:                break;
                   9188:        }
                   9189: }
                   9190: 
                   9191: inline void pcbios_int_16h_14h()
                   9192: {
                   9193:        static UINT8 status = 0;
                   9194:        
                   9195:        switch(REG8(AL)) {
                   9196:        case 0x00:
                   9197:        case 0x01:
                   9198:                status = REG8(AL);
                   9199:                break;
                   9200:        case 0x02:
                   9201:                REG8(AL) = status;
                   9202:                break;
                   9203:        default:
1.1.1.22  root     9204:                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     9205:                m_CF = 1;
1.1       root     9206:                break;
                   9207:        }
                   9208: }
                   9209: 
1.1.1.24  root     9210: inline void pcbios_int_16h_55h()
                   9211: {
                   9212:        switch(REG8(AL)) {
                   9213:        case 0x00:
                   9214:                // keyboard tsr is not present
                   9215:                break;
                   9216:        case 0xfe:
                   9217:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   9218:                break;
                   9219:        case 0xff:
                   9220:                break;
                   9221:        default:
                   9222:                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));
                   9223:                m_CF = 1;
                   9224:                break;
                   9225:        }
                   9226: }
                   9227: 
1.1.1.30  root     9228: inline void pcbios_int_16h_6fh()
                   9229: {
                   9230:        switch(REG8(AL)) {
                   9231:        case 0x00:
                   9232:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   9233:                break;
                   9234:        default:
                   9235:                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));
                   9236:                m_CF = 1;
                   9237:                break;
                   9238:        }
                   9239: }
                   9240: 
1.1.1.37  root     9241: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   9242: {
                   9243:        UINT8 hi = jis >> 8;
                   9244:        UINT8 lo = jis & 0xff;
                   9245:        
                   9246:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   9247:        hi = (hi - 0x21) / 2 + 0x81;
                   9248:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   9249:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   9250:        
                   9251:        return((hi << 8) + lo);
                   9252: }
                   9253: 
                   9254: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   9255: {
                   9256:        UINT8 hi = sjis >> 8;
                   9257:        UINT8 lo = sjis & 0xff;
                   9258:        
                   9259:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   9260:                return(0x2121);
                   9261:        }
                   9262:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   9263:                return(0x2121);
                   9264:        }
                   9265:        if(hi >= 0xf0 && hi <= 0xf3) {
                   9266:                // gaiji
                   9267:                if(lo >= 0x40 && lo <= 0x7e) {
                   9268:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   9269:                }
                   9270:                if(lo >= 0x80 && lo <= 0x9e) {
                   9271:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   9272:                }
                   9273:                if(lo >= 0x9f && lo <= 0xfc) {
                   9274:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   9275:                }
                   9276:        }
                   9277:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   9278:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   9279:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   9280:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   9281:        
                   9282:        return((hi << 8) + lo);
                   9283: }
                   9284: 
1.1.1.38  root     9285: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   9286: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   9287: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     9288: 
                   9289: void pcbios_printer_out(int c, UINT8 data)
                   9290: {
                   9291:        if(pio[c].conv_mode) {
                   9292:                if(pio[c].sjis_hi != 0) {
                   9293:                        if(!pio[c].jis_mode) {
                   9294:                                printer_out(c, 0x1c);
                   9295:                                printer_out(c, 0x26);
                   9296:                                pio[c].jis_mode = true;
                   9297:                        }
                   9298:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   9299:                        printer_out(c, jis >> 8);
                   9300:                        printer_out(c, jis & 0xff);
                   9301:                        pio[c].sjis_hi = 0;
                   9302:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   9303:                        printer_out(c, data);
                   9304:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9305:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9306:                        }
                   9307:                        pio[c].esc_len++;
                   9308:                        
                   9309:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9310:                        case 0x33: // 1Bh 33h XX
                   9311:                        case 0x4a: // 1Bh 4Ah XX
                   9312:                        case 0x4e: // 1Bh 4Eh XX
                   9313:                        case 0x51: // 1Bh 51h XX
                   9314:                        case 0x55: // 1Bh 55h XX
                   9315:                        case 0x6c: // 1Bh 6Ch XX
                   9316:                        case 0x71: // 1Bh 71h XX
                   9317:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     9318:                                if(pio[c].esc_len == 3) {
                   9319:                                        pio[c].esc_buf[0] = 0x00;
                   9320:                                }
                   9321:                                break;
1.1.1.38  root     9322:                        case 0x24: // 1Bh 24h XX XX
                   9323:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     9324:                                if(pio[c].esc_len == 4) {
                   9325:                                        pio[c].esc_buf[0] = 0x00;
                   9326:                                }
                   9327:                                break;
1.1.1.38  root     9328:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     9329:                                if(pio[c].esc_len >= 3) {
                   9330:                                        switch(pio[c].esc_buf[2]) {
                   9331:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   9332:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   9333:                                                        pio[c].esc_buf[0] = 0x00;
                   9334:                                                }
                   9335:                                                break;
                   9336:                                        case 32: case 33: case 38: case 39: case 40:
                   9337:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   9338:                                                        pio[c].esc_buf[0] = 0x00;
                   9339:                                                }
                   9340:                                                break;
1.1.1.38  root     9341:                                        case 71: case 72: case 73:
                   9342:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   9343:                                                        pio[c].esc_buf[0] = 0x00;
                   9344:                                                }
                   9345:                                                break;
1.1.1.37  root     9346:                                        default:
                   9347:                                                pio[c].esc_buf[0] = 0x00;
                   9348:                                                break;
                   9349:                                        }
                   9350:                                }
                   9351:                                break;
1.1.1.38  root     9352:                        case 0x40: // 1Bh 40h
1.1.1.37  root     9353:                                if(pio[c].jis_mode) {
                   9354:                                        printer_out(c, 0x1c);
                   9355:                                        printer_out(c, 0x2e);
                   9356:                                        pio[c].jis_mode = false;
                   9357:                                }
                   9358:                                pio[c].esc_buf[0] = 0x00;
                   9359:                                break;
1.1.1.38  root     9360:                        case 0x42: // 1Bh 42h data 00h
                   9361:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9362:                                if(pio[c].esc_len >= 3 && data == 0) {
                   9363:                                        pio[c].esc_buf[0] = 0x00;
                   9364:                                }
                   9365:                                break;
1.1.1.38  root     9366:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     9367:                                if(pio[c].esc_len >= 3 && data != 0) {
                   9368:                                        pio[c].esc_buf[0] = 0x00;
                   9369:                                }
                   9370:                                break;
1.1.1.38  root     9371:                        default: // 1Bh XX
1.1.1.37  root     9372:                                pio[c].esc_buf[0] = 0x00;
                   9373:                                break;
                   9374:                        }
                   9375:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   9376:                        printer_out(c, data);
                   9377:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   9378:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   9379:                        }
                   9380:                        pio[c].esc_len++;
                   9381:                        
                   9382:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     9383:                        case 0x21: // 1Ch 21h XX
                   9384:                        case 0x2d: // 1Ch 2Dh XX
                   9385:                        case 0x57: // 1Ch 57h XX
                   9386:                        case 0x6b: // 1Ch 6Bh XX
                   9387:                        case 0x72: // 1Ch 72h XX
                   9388:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     9389:                                if(pio[c].esc_len == 3) {
                   9390:                                        pio[c].esc_buf[0] = 0x00;
                   9391:                                }
                   9392:                                break;
1.1.1.38  root     9393:                        case 0x26: // 1Ch 26h
1.1.1.37  root     9394:                                pio[c].jis_mode = true;
                   9395:                                pio[c].esc_buf[0] = 0x00;
                   9396:                                break;
1.1.1.38  root     9397:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     9398:                                pio[c].jis_mode = false;
                   9399:                                pio[c].esc_buf[0] = 0x00;
                   9400:                                break;
1.1.1.38  root     9401:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     9402:                                if(pio[c].esc_len == 76) {
                   9403:                                        pio[c].esc_buf[0] = 0x00;
                   9404:                                }
                   9405:                                break;
1.1.1.38  root     9406:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     9407:                                if(pio[c].esc_len == 6) {
                   9408:                                        pio[c].esc_buf[0] = 0x00;
                   9409:                                }
                   9410:                                break;
1.1.1.38  root     9411:                        case 0x53: // 1Ch 53h XX XX
                   9412:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     9413:                                if(pio[c].esc_len == 4) {
                   9414:                                        pio[c].esc_buf[0] = 0x00;
                   9415:                                }
                   9416:                                break;
1.1.1.38  root     9417:                        default: // 1Ch XX
1.1.1.37  root     9418:                                pio[c].esc_buf[0] = 0x00;
                   9419:                                break;
                   9420:                        }
                   9421:                } else if(data == 0x1b || data == 0x1c) {
                   9422:                        printer_out(c, data);
                   9423:                        pio[c].esc_buf[0] = data;
                   9424:                        pio[c].esc_len = 1;
                   9425:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   9426:                        pio[c].sjis_hi = data;
                   9427:                } else {
                   9428:                        if(pio[c].jis_mode) {
                   9429:                                printer_out(c, 0x1c);
                   9430:                                printer_out(c, 0x2e);
                   9431:                                pio[c].jis_mode = false;
                   9432:                        }
                   9433:                        printer_out(c, data);
                   9434:                }
                   9435:        } else {
                   9436:                if(pio[c].jis_mode) {
                   9437:                        printer_out(c, 0x1c);
                   9438:                        printer_out(c, 0x2e);
                   9439:                        pio[c].jis_mode = false;
                   9440:                }
                   9441:                printer_out(c, data);
                   9442:        }
                   9443: }
                   9444: 
                   9445: inline void pcbios_int_17h_00h()
                   9446: {
                   9447:        if(REG16(DX) < 3) {
                   9448:                pcbios_printer_out(REG16(DX), REG8(AL));
                   9449:                REG8(AH) = 0xd0;
                   9450:        }
                   9451: }
                   9452: 
                   9453: inline void pcbios_int_17h_01h()
                   9454: {
                   9455:        if(REG16(DX) < 3) {
                   9456:                REG8(AH) = 0xd0;
                   9457:        }
                   9458: }
                   9459: 
                   9460: inline void pcbios_int_17h_02h()
                   9461: {
                   9462:        if(REG16(DX) < 3) {
                   9463:                REG8(AH) = 0xd0;
                   9464:        }
                   9465: }
                   9466: 
                   9467: inline void pcbios_int_17h_03h()
                   9468: {
                   9469:        switch(REG8(AL)) {
                   9470:        case 0x00:
                   9471:                if(REG16(DX) < 3) {
                   9472:                        if(pio[REG16(DX)].jis_mode) {
                   9473:                                printer_out(REG16(DX), 0x1c);
                   9474:                                printer_out(REG16(DX), 0x2e);
                   9475:                                pio[REG16(DX)].jis_mode = false;
                   9476:                        }
                   9477:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   9478:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   9479:                        }
                   9480:                        REG16(CX) = 0x0000;
                   9481:                        REG8(AH) = 0xd0;
                   9482:                }
                   9483:                break;
                   9484:        default:
                   9485:                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));
                   9486:                break;
                   9487:        }
                   9488: }
                   9489: 
                   9490: inline void pcbios_int_17h_50h()
                   9491: {
                   9492:        switch(REG8(AL)) {
                   9493:        case 0x00:
                   9494:                if(REG16(DX) < 3) {
                   9495:                        if(REG16(BX) = 0x0001) {
                   9496:                                pio[REG16(DX)].conv_mode = false;
                   9497:                                REG8(AL) = 0x00;
                   9498:                        } else if(REG16(BX) = 0x0051) {
                   9499:                                pio[REG16(DX)].conv_mode = true;
                   9500:                                REG8(AL) = 0x00;
                   9501:                        } else {
                   9502:                                REG8(AL) = 0x01;
                   9503:                        }
                   9504:                } else {
                   9505:                        REG8(AL) = 0x02;
                   9506:                }
                   9507:                break;
                   9508:        case 0x01:
                   9509:                if(REG16(DX) < 3) {
                   9510:                        if(pio[REG16(DX)].conv_mode) {
                   9511:                                REG16(BX) = 0x0051;
                   9512:                        } else {
                   9513:                                REG16(BX) = 0x0001;
                   9514:                        }
                   9515:                        REG8(AL) = 0x00;
                   9516:                } else {
                   9517:                        REG8(AL) = 0x02;
                   9518:                }
                   9519:                break;
                   9520:        default:
                   9521:                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));
                   9522:                break;
                   9523:        }
                   9524: }
                   9525: 
                   9526: inline void pcbios_int_17h_51h()
                   9527: {
                   9528:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   9529:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   9530:        } else {
                   9531:                REG16(DX) = 0x0000;
                   9532:        }
                   9533: }
                   9534: 
                   9535: inline void pcbios_int_17h_52h()
                   9536: {
                   9537:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   9538:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   9539:        } else {
                   9540:                REG16(DX) = 0x0000;
                   9541:        }
                   9542: }
                   9543: 
                   9544: inline void pcbios_int_17h_84h()
                   9545: {
                   9546:        if(REG16(DX) < 3) {
                   9547:                if(pio[REG16(DX)].jis_mode) {
                   9548:                        printer_out(REG16(DX), 0x1c);
                   9549:                        printer_out(REG16(DX), 0x2e);
                   9550:                        pio[REG16(DX)].jis_mode = false;
                   9551:                }
                   9552:                printer_out(REG16(DX), REG8(AL));
                   9553:                REG8(AH) = 0xd0;
                   9554:        }
                   9555: }
                   9556: 
                   9557: inline void pcbios_int_17h_85h()
                   9558: {
                   9559:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   9560: }
                   9561: 
1.1       root     9562: inline void pcbios_int_1ah_00h()
                   9563: {
1.1.1.19  root     9564:        pcbios_update_daily_timer_counter(timeGetTime());
                   9565:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   9566:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   9567:        REG8(AL) = mem[0x470];
                   9568:        mem[0x470] = 0;
1.1       root     9569: }
                   9570: 
                   9571: inline int to_bcd(int t)
                   9572: {
                   9573:        int u = (t % 100) / 10;
                   9574:        return (u << 4) | (t % 10);
                   9575: }
                   9576: 
                   9577: inline void pcbios_int_1ah_02h()
                   9578: {
                   9579:        SYSTEMTIME time;
                   9580:        
                   9581:        GetLocalTime(&time);
                   9582:        REG8(CH) = to_bcd(time.wHour);
                   9583:        REG8(CL) = to_bcd(time.wMinute);
                   9584:        REG8(DH) = to_bcd(time.wSecond);
                   9585:        REG8(DL) = 0x00;
                   9586: }
                   9587: 
                   9588: inline void pcbios_int_1ah_04h()
                   9589: {
                   9590:        SYSTEMTIME time;
                   9591:        
                   9592:        GetLocalTime(&time);
                   9593:        REG8(CH) = to_bcd(time.wYear / 100);
                   9594:        REG8(CL) = to_bcd(time.wYear);
                   9595:        REG8(DH) = to_bcd(time.wMonth);
                   9596:        REG8(DL) = to_bcd(time.wDay);
                   9597: }
                   9598: 
                   9599: inline void pcbios_int_1ah_0ah()
                   9600: {
                   9601:        SYSTEMTIME time;
                   9602:        FILETIME file_time;
                   9603:        WORD dos_date, dos_time;
                   9604:        
                   9605:        GetLocalTime(&time);
                   9606:        SystemTimeToFileTime(&time, &file_time);
                   9607:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   9608:        REG16(CX) = dos_date;
                   9609: }
                   9610: 
                   9611: // msdos system call
                   9612: 
1.1.1.43  root     9613: inline void msdos_int_21h_56h(int lfn);
                   9614: 
1.1       root     9615: inline void msdos_int_21h_00h()
                   9616: {
1.1.1.3   root     9617:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     9618: }
                   9619: 
1.1.1.35  root     9620: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     9621: {
                   9622:        REG8(AL) = msdos_getche();
1.1.1.33  root     9623:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9624:        
1.1.1.35  root     9625: #ifdef USE_SERVICE_THREAD
                   9626:        service_exit = true;
                   9627: #endif
                   9628:        return(0);
                   9629: }
                   9630: 
                   9631: inline void msdos_int_21h_01h()
                   9632: {
                   9633: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9634:        if(!in_service && !in_service_29h &&
1.1.1.58  root     9635:           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9636:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9637:                // msdos_putch() will be used in this service
                   9638:                // if int 29h is hooked, run this service in main thread to call int 29h
                   9639:                start_service_loop(msdos_int_21h_01h_thread);
                   9640:        } else {
                   9641: #endif
                   9642:                msdos_int_21h_01h_thread(NULL);
                   9643:                REQUEST_HARDWRE_UPDATE();
                   9644: #ifdef USE_SERVICE_THREAD
                   9645:        }
1.1.1.35  root     9646: #endif
1.1       root     9647: }
                   9648: 
                   9649: inline void msdos_int_21h_02h()
                   9650: {
1.1.1.33  root     9651:        UINT8 data = REG8(DL);
                   9652:        msdos_putch(data);
                   9653:        REG8(AL) = data;
                   9654:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9655: }
                   9656: 
                   9657: inline void msdos_int_21h_03h()
                   9658: {
                   9659:        REG8(AL) = msdos_aux_in();
                   9660: }
                   9661: 
                   9662: inline void msdos_int_21h_04h()
                   9663: {
                   9664:        msdos_aux_out(REG8(DL));
                   9665: }
                   9666: 
                   9667: inline void msdos_int_21h_05h()
                   9668: {
                   9669:        msdos_prn_out(REG8(DL));
                   9670: }
                   9671: 
                   9672: inline void msdos_int_21h_06h()
                   9673: {
                   9674:        if(REG8(DL) == 0xff) {
                   9675:                if(msdos_kbhit()) {
                   9676:                        REG8(AL) = msdos_getch();
1.1.1.3   root     9677: #if defined(HAS_I386)
                   9678:                        m_ZF = 0;
                   9679: #else
                   9680:                        m_ZeroVal = 1;
                   9681: #endif
1.1       root     9682:                } else {
                   9683:                        REG8(AL) = 0;
1.1.1.3   root     9684: #if defined(HAS_I386)
                   9685:                        m_ZF = 1;
                   9686: #else
                   9687:                        m_ZeroVal = 0;
                   9688: #endif
1.1.1.14  root     9689:                        maybe_idle();
1.1       root     9690:                }
                   9691:        } else {
1.1.1.33  root     9692:                UINT8 data = REG8(DL);
                   9693:                msdos_putch(data);
                   9694:                REG8(AL) = data;
1.1       root     9695:        }
                   9696: }
                   9697: 
1.1.1.35  root     9698: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     9699: {
                   9700:        REG8(AL) = msdos_getch();
1.1.1.26  root     9701:        
1.1.1.35  root     9702: #ifdef USE_SERVICE_THREAD
                   9703:        service_exit = true;
                   9704: #endif
                   9705:        return(0);
1.1       root     9706: }
                   9707: 
1.1.1.35  root     9708: inline void msdos_int_21h_07h()
                   9709: {
                   9710: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9711:        if(!in_service && !in_service_29h) {
                   9712:                start_service_loop(msdos_int_21h_07h_thread);
                   9713:        } else {
                   9714: #endif
                   9715:                msdos_int_21h_07h_thread(NULL);
                   9716:                REQUEST_HARDWRE_UPDATE();
                   9717: #ifdef USE_SERVICE_THREAD
                   9718:        }
1.1.1.35  root     9719: #endif
                   9720: }
                   9721: 
                   9722: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9723: {
                   9724:        REG8(AL) = msdos_getch();
1.1.1.33  root     9725:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9726:        
1.1.1.35  root     9727: #ifdef USE_SERVICE_THREAD
                   9728:        service_exit = true;
                   9729: #endif
                   9730:        return(0);
                   9731: }
                   9732: 
                   9733: inline void msdos_int_21h_08h()
                   9734: {
                   9735: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9736:        if(!in_service && !in_service_29h) {
                   9737:                start_service_loop(msdos_int_21h_08h_thread);
                   9738:        } else {
                   9739: #endif
                   9740:                msdos_int_21h_08h_thread(NULL);
                   9741:                REQUEST_HARDWRE_UPDATE();
                   9742: #ifdef USE_SERVICE_THREAD
                   9743:        }
1.1.1.35  root     9744: #endif
1.1       root     9745: }
                   9746: 
                   9747: inline void msdos_int_21h_09h()
                   9748: {
1.1.1.21  root     9749:        msdos_stdio_reopen();
                   9750:        
1.1.1.20  root     9751:        process_t *process = msdos_process_info_get(current_psp);
                   9752:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9753:        
1.1.1.14  root     9754:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9755:        int len = 0;
1.1       root     9756:        
1.1.1.14  root     9757:        while(str[len] != '$' && len < 0x10000) {
                   9758:                len++;
                   9759:        }
1.1.1.20  root     9760:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9761:                // stdout is redirected to file
1.1.1.20  root     9762:                msdos_write(fd, str, len);
1.1       root     9763:        } else {
                   9764:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9765:                        msdos_putch(str[i]);
1.1       root     9766:                }
                   9767:        }
1.1.1.33  root     9768:        REG8(AL) = '$';
                   9769:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9770: }
                   9771: 
1.1.1.35  root     9772: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9773: {
1.1.1.3   root     9774:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9775:        int max = mem[ofs] - 1;
                   9776:        UINT8 *buf = mem + ofs + 2;
                   9777:        int chr, p = 0;
                   9778:        
                   9779:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9780:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9781:                        p = 0;
1.1.1.33  root     9782:                        msdos_putch(0x03);
                   9783:                        msdos_putch(0x0d);
                   9784:                        msdos_putch(0x0a);
1.1.1.26  root     9785:                        break;
1.1.1.33  root     9786:                } else if(ctrl_break_pressed) {
                   9787:                        // skip this byte
1.1.1.26  root     9788:                } else if(chr == 0x00) {
1.1       root     9789:                        // skip 2nd byte
                   9790:                        msdos_getch();
                   9791:                } else if(chr == 0x08) {
                   9792:                        // back space
                   9793:                        if(p > 0) {
                   9794:                                p--;
1.1.1.20  root     9795:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9796:                                        msdos_putch(0x08);
                   9797:                                        msdos_putch(0x08);
                   9798:                                        msdos_putch(0x20);
                   9799:                                        msdos_putch(0x20);
                   9800:                                        msdos_putch(0x08);
                   9801:                                        msdos_putch(0x08);
1.1.1.36  root     9802:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9803:                                        p--;
                   9804:                                        msdos_putch(0x08);
                   9805:                                        msdos_putch(0x08);
                   9806:                                        msdos_putch(0x20);
                   9807:                                        msdos_putch(0x20);
                   9808:                                        msdos_putch(0x08);
                   9809:                                        msdos_putch(0x08);
1.1.1.34  root     9810:                                } else {
                   9811:                                        msdos_putch(0x08);
                   9812:                                        msdos_putch(0x20);
                   9813:                                        msdos_putch(0x08);
                   9814:                                }
                   9815:                        }
                   9816:                } else if(chr == 0x1b) {
                   9817:                        // escape
                   9818:                        while(p > 0) {
                   9819:                                p--;
                   9820:                                if(msdos_ctrl_code_check(buf[p])) {
                   9821:                                        msdos_putch(0x08);
                   9822:                                        msdos_putch(0x08);
                   9823:                                        msdos_putch(0x20);
                   9824:                                        msdos_putch(0x20);
                   9825:                                        msdos_putch(0x08);
                   9826:                                        msdos_putch(0x08);
1.1.1.20  root     9827:                                } else {
1.1.1.34  root     9828:                                        msdos_putch(0x08);
                   9829:                                        msdos_putch(0x20);
                   9830:                                        msdos_putch(0x08);
1.1.1.20  root     9831:                                }
1.1       root     9832:                        }
                   9833:                } else if(p < max) {
                   9834:                        buf[p++] = chr;
                   9835:                        msdos_putch(chr);
                   9836:                }
                   9837:        }
                   9838:        buf[p] = 0x0d;
                   9839:        mem[ofs + 1] = p;
1.1.1.33  root     9840:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9841:        
1.1.1.35  root     9842: #ifdef USE_SERVICE_THREAD
                   9843:        service_exit = true;
                   9844: #endif
                   9845:        return(0);
                   9846: }
                   9847: 
                   9848: inline void msdos_int_21h_0ah()
                   9849: {
                   9850:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9851: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9852:                if(!in_service && !in_service_29h &&
1.1.1.58  root     9853:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     9854:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9855:                        // msdos_putch() will be used in this service
                   9856:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9857:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9858:                } else {
                   9859: #endif
                   9860:                        msdos_int_21h_0ah_thread(NULL);
                   9861:                        REQUEST_HARDWRE_UPDATE();
                   9862: #ifdef USE_SERVICE_THREAD
                   9863:                }
1.1.1.35  root     9864: #endif
                   9865:        }
1.1       root     9866: }
                   9867: 
                   9868: inline void msdos_int_21h_0bh()
                   9869: {
                   9870:        if(msdos_kbhit()) {
                   9871:                REG8(AL) = 0xff;
                   9872:        } else {
                   9873:                REG8(AL) = 0x00;
1.1.1.14  root     9874:                maybe_idle();
1.1       root     9875:        }
1.1.1.33  root     9876:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9877: }
                   9878: 
                   9879: inline void msdos_int_21h_0ch()
                   9880: {
                   9881:        // clear key buffer
1.1.1.21  root     9882:        msdos_stdio_reopen();
                   9883:        
1.1.1.20  root     9884:        process_t *process = msdos_process_info_get(current_psp);
                   9885:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9886:        
                   9887:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9888:                // stdin is redirected to file
                   9889:        } else {
                   9890:                while(msdos_kbhit()) {
                   9891:                        msdos_getch();
                   9892:                }
                   9893:        }
                   9894:        
                   9895:        switch(REG8(AL)) {
                   9896:        case 0x01:
                   9897:                msdos_int_21h_01h();
                   9898:                break;
                   9899:        case 0x06:
                   9900:                msdos_int_21h_06h();
                   9901:                break;
                   9902:        case 0x07:
                   9903:                msdos_int_21h_07h();
                   9904:                break;
                   9905:        case 0x08:
                   9906:                msdos_int_21h_08h();
                   9907:                break;
                   9908:        case 0x0a:
                   9909:                msdos_int_21h_0ah();
                   9910:                break;
                   9911:        default:
1.1.1.48  root     9912:                // the buffer is flushed but no input is attempted
1.1       root     9913:                break;
                   9914:        }
                   9915: }
                   9916: 
                   9917: inline void msdos_int_21h_0dh()
                   9918: {
                   9919: }
                   9920: 
                   9921: inline void msdos_int_21h_0eh()
                   9922: {
                   9923:        if(REG8(DL) < 26) {
                   9924:                _chdrive(REG8(DL) + 1);
                   9925:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9926:                msdos_sda_update(current_psp);
1.1       root     9927:        }
                   9928:        REG8(AL) = 26; // zdrive
                   9929: }
                   9930: 
1.1.1.14  root     9931: inline void msdos_int_21h_0fh()
                   9932: {
                   9933:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9934:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9935:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     9936:        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     9937:        
1.1.1.14  root     9938:        if(hFile == INVALID_HANDLE_VALUE) {
                   9939:                REG8(AL) = 0xff;
                   9940:        } else {
                   9941:                REG8(AL) = 0;
                   9942:                fcb->current_block = 0;
                   9943:                fcb->record_size = 128;
                   9944:                fcb->file_size = GetFileSize(hFile, NULL);
                   9945:                fcb->handle = hFile;
                   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);
1.1.1.65  root     10120:        SetFilePointer(fcb->handle, fcb->record_size * (fcb->current_block * 128 + fcb->cur_record), NULL, FILE_BEGIN);
                   10121:        
1.1.1.16  root     10122:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10123:                REG8(AL) = 1;
                   10124:        } else {
1.1.1.65  root     10125:                if(++fcb->cur_record >= 128) {
                   10126:                        fcb->current_block += fcb->cur_record / 128;
                   10127:                        fcb->cur_record %= 128;
                   10128:                }
1.1.1.16  root     10129:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   10130:        }
                   10131: }
                   10132: 
                   10133: inline void msdos_int_21h_15h()
1.1.1.14  root     10134: {
                   10135:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10136:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     10137:        process_t *process = msdos_process_info_get(current_psp);
                   10138:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10139:        DWORD num = 0;
1.1.1.14  root     10140:        
1.1.1.65  root     10141:        SetFilePointer(fcb->handle, fcb->record_size * (fcb->current_block * 128 + fcb->cur_record), NULL, FILE_BEGIN);
                   10142:        WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   10143:        fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10144:        
                   10145:        if(num != fcb->record_size) {
1.1.1.16  root     10146:                REG8(AL) = 1;
                   10147:        } else {
1.1.1.65  root     10148:                if(++fcb->cur_record >= 128) {
                   10149:                        fcb->current_block += fcb->cur_record / 128;
                   10150:                        fcb->cur_record %= 128;
                   10151:                }
                   10152:                REG8(AL) = 0;
1.1.1.16  root     10153:        }
                   10154: }
                   10155: 
                   10156: inline void msdos_int_21h_16h()
                   10157: {
                   10158:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10159:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10160:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10161:        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     10162:        
1.1.1.14  root     10163:        if(hFile == INVALID_HANDLE_VALUE) {
                   10164:                REG8(AL) = 0xff;
                   10165:        } else {
                   10166:                REG8(AL) = 0;
                   10167:                fcb->current_block = 0;
                   10168:                fcb->record_size = 128;
                   10169:                fcb->file_size = 0;
                   10170:                fcb->handle = hFile;
                   10171:        }
                   10172: }
                   10173: 
1.1.1.16  root     10174: inline void msdos_int_21h_17h()
                   10175: {
                   10176:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10177:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     10178: //     const char *path_src = msdos_fcb_path(fcb_src);
                   10179:        char path_src[MAX_PATH];
                   10180:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   10181:        
1.1.1.65  root     10182:        fcb_t *fcb_dst = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16 + (ext_fcb_src->flag == 0xff ? 7 : 0));
1.1.1.45  root     10183: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   10184:        char path_dst[MAX_PATH];
                   10185:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     10186:        
                   10187:        if(rename(path_src, path_dst)) {
                   10188:                REG8(AL) = 0xff;
                   10189:        } else {
                   10190:                REG8(AL) = 0;
                   10191:        }
                   10192: }
                   10193: 
1.1       root     10194: inline void msdos_int_21h_18h()
                   10195: {
                   10196:        REG8(AL) = 0x00;
                   10197: }
                   10198: 
                   10199: inline void msdos_int_21h_19h()
                   10200: {
                   10201:        REG8(AL) = _getdrive() - 1;
                   10202: }
                   10203: 
                   10204: inline void msdos_int_21h_1ah()
                   10205: {
                   10206:        process_t *process = msdos_process_info_get(current_psp);
                   10207:        
                   10208:        process->dta.w.l = REG16(DX);
1.1.1.3   root     10209:        process->dta.w.h = SREG(DS);
1.1.1.23  root     10210:        msdos_sda_update(current_psp);
1.1       root     10211: }
                   10212: 
                   10213: inline void msdos_int_21h_1bh()
                   10214: {
                   10215:        int drive_num = _getdrive() - 1;
                   10216:        UINT16 seg, ofs;
                   10217:        
                   10218:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10219:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10220:                REG8(AL) = dpb->highest_sector_num + 1;
                   10221:                REG16(CX) = dpb->bytes_per_sector;
                   10222:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10223:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10224:        } else {
                   10225:                REG8(AL) = 0xff;
1.1.1.3   root     10226:                m_CF = 1;
1.1       root     10227:        }
                   10228: 
                   10229: }
                   10230: 
                   10231: inline void msdos_int_21h_1ch()
                   10232: {
1.1.1.41  root     10233:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     10234:        UINT16 seg, ofs;
                   10235:        
                   10236:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10237:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10238:                REG8(AL) = dpb->highest_sector_num + 1;
                   10239:                REG16(CX) = dpb->bytes_per_sector;
                   10240:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     10241:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     10242:        } else {
                   10243:                REG8(AL) = 0xff;
1.1.1.3   root     10244:                m_CF = 1;
1.1       root     10245:        }
                   10246: 
                   10247: }
                   10248: 
                   10249: inline void msdos_int_21h_1dh()
                   10250: {
                   10251:        REG8(AL) = 0;
                   10252: }
                   10253: 
                   10254: inline void msdos_int_21h_1eh()
                   10255: {
                   10256:        REG8(AL) = 0;
                   10257: }
                   10258: 
                   10259: inline void msdos_int_21h_1fh()
                   10260: {
                   10261:        int drive_num = _getdrive() - 1;
                   10262:        UINT16 seg, ofs;
                   10263:        
                   10264:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10265:                REG8(AL) = 0;
1.1.1.3   root     10266:                SREG(DS) = seg;
                   10267:                i386_load_segment_descriptor(DS);
1.1       root     10268:                REG16(BX) = ofs;
                   10269:        } else {
                   10270:                REG8(AL) = 0xff;
1.1.1.3   root     10271:                m_CF = 1;
1.1       root     10272:        }
                   10273: }
                   10274: 
                   10275: inline void msdos_int_21h_20h()
                   10276: {
                   10277:        REG8(AL) = 0;
                   10278: }
                   10279: 
1.1.1.14  root     10280: inline void msdos_int_21h_21h()
                   10281: {
                   10282:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10283:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.65  root     10284:        UINT32 rec = (fcb->record_size >= 64) ? fcb->rand_record & 0xffffff : fcb->rand_record;
1.1.1.14  root     10285:        
1.1.1.65  root     10286:        fcb->current_block = rec / 128;
                   10287:        fcb->cur_record = rec % 128;
                   10288:        
                   10289:        if(SetFilePointer(fcb->handle, fcb->record_size * rec, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1.1.14  root     10290:                REG8(AL) = 1;
                   10291:        } else {
                   10292:                process_t *process = msdos_process_info_get(current_psp);
                   10293:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10294:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     10295:                DWORD num = 0;
1.1.1.14  root     10296:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   10297:                        REG8(AL) = 1;
                   10298:                } else {
1.1.1.16  root     10299:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     10300:                }
                   10301:        }
                   10302: }
                   10303: 
                   10304: inline void msdos_int_21h_22h()
                   10305: {
                   10306:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10307:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.65  root     10308:        UINT32 rec = (fcb->record_size >= 64) ? fcb->rand_record & 0xffffff : fcb->rand_record;
1.1.1.14  root     10309:        
1.1.1.65  root     10310:        fcb->current_block = rec / 128;
                   10311:        fcb->cur_record = rec % 128;
                   10312:        
                   10313:        if(SetFilePointer(fcb->handle, fcb->record_size * rec, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10314:                REG8(AL) = 1;
1.1.1.14  root     10315:        } else {
                   10316:                process_t *process = msdos_process_info_get(current_psp);
                   10317:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     10318:                DWORD num = 0;
1.1.1.14  root     10319:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   10320:                fcb->file_size = GetFileSize(fcb->handle, NULL);
1.1.1.16  root     10321:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     10322:        }
                   10323: }
                   10324: 
1.1.1.16  root     10325: inline void msdos_int_21h_23h()
                   10326: {
                   10327:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10328:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     10329:        const char *path = msdos_fcb_path(fcb);
1.1.1.60  root     10330:        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     10331:        
                   10332:        if(hFile == INVALID_HANDLE_VALUE) {
                   10333:                REG8(AL) = 0xff;
                   10334:        } else {
                   10335:                UINT32 size = GetFileSize(hFile, NULL);
1.1.1.65  root     10336:                UINT32 rec = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   10337:                fcb->rand_record = (fcb->record_size >= 64) ? (fcb->rand_record & 0xff000000) | (rec & 0xffffff) : rec;
                   10338:                CloseHandle(hFile);
1.1.1.16  root     10339:                REG8(AL) = 0;
                   10340:        }
                   10341: }
                   10342: 
                   10343: inline void msdos_int_21h_24h()
                   10344: {
                   10345:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10346:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.65  root     10347:        UINT32 rec = fcb->current_block * 128 + fcb->cur_record;
1.1.1.16  root     10348:        
1.1.1.65  root     10349:        fcb->rand_record = (fcb->record_size >= 64) ? (fcb->rand_record & 0xff000000) | (rec & 0xffffff) : rec;
1.1.1.16  root     10350: }
                   10351: 
1.1       root     10352: inline void msdos_int_21h_25h()
                   10353: {
                   10354:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     10355:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     10356: }
                   10357: 
                   10358: inline void msdos_int_21h_26h()
                   10359: {
                   10360:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   10361:        
                   10362:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     10363:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     10364:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10365:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10366:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10367:        psp->parent_psp = 0;
                   10368: }
                   10369: 
1.1.1.16  root     10370: inline void msdos_int_21h_27h()
                   10371: {
                   10372:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10373:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.65  root     10374:        UINT32 rec = (fcb->record_size >= 64) ? fcb->rand_record & 0xffffff : fcb->rand_record;
1.1.1.16  root     10375:        
1.1.1.65  root     10376:        if(SetFilePointer(fcb->handle, fcb->record_size * rec, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1.1.16  root     10377:                REG8(AL) = 1;
1.1.1.65  root     10378:        } else if(REG16(CX) == 0) {
                   10379:                REG8(AL) = 0;
1.1.1.16  root     10380:        } else {
                   10381:                process_t *process = msdos_process_info_get(current_psp);
                   10382:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.65  root     10383:                UINT32 len = fcb->record_size * REG16(CX);
                   10384:                memset(mem + dta_laddr, 0, len);
1.1.1.16  root     10385:                DWORD num = 0;
1.1.1.65  root     10386:                if(!ReadFile(fcb->handle, mem + dta_laddr, len, &num, NULL) || num == 0) {
1.1.1.16  root     10387:                        REG8(AL) = 1;
                   10388:                } else {
1.1.1.65  root     10389:                        UINT16 nrec = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10390:                        rec += nrec;
                   10391:                        fcb->rand_record = (fcb->record_size >= 64) ? (fcb->rand_record & 0xff000000) | (rec & 0xffffff) : rec;
                   10392:                        REG8(AL) = (num == len) ? 0 : 3;
                   10393:                        REG16(CX) = nrec;
1.1.1.16  root     10394:                }
                   10395:        }
1.1.1.65  root     10396:        fcb->current_block = rec / 128;
                   10397:        fcb->cur_record = rec % 128;
1.1.1.16  root     10398: }
                   10399: 
                   10400: inline void msdos_int_21h_28h()
                   10401: {
                   10402:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   10403:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.65  root     10404:        UINT32 rec = (fcb->record_size >= 64) ? fcb->rand_record & 0xffffff : fcb->rand_record;
1.1.1.16  root     10405:        
1.1.1.65  root     10406:        if(SetFilePointer(fcb->handle, fcb->record_size * rec, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   10407:                REG8(AL) = 1;
                   10408:        } else if(REG16(CX) == 0) {
                   10409:                if(!SetEndOfFile(fcb->handle)) {
                   10410:                        REG8(AL) = 1;
                   10411:                } else {
                   10412:                        fcb->file_size = GetFileSize(fcb->handle, NULL);
                   10413:                        REG8(AL) = 0;
                   10414:                }
1.1.1.16  root     10415:        } else {
                   10416:                process_t *process = msdos_process_info_get(current_psp);
                   10417:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.65  root     10418:                UINT32 len = fcb->record_size * REG16(CX);
1.1.1.16  root     10419:                DWORD num = 0;
1.1.1.65  root     10420:                WriteFile(fcb->handle, mem + dta_laddr, len, &num, NULL);
1.1.1.16  root     10421:                fcb->file_size = GetFileSize(fcb->handle, NULL);
1.1.1.65  root     10422:                UINT16 nrec = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   10423:                rec += nrec;
                   10424:                fcb->rand_record = (fcb->record_size >= 64) ? (fcb->rand_record & 0xff000000) | (rec & 0xffffff) : rec;
                   10425:                REG8(AL) = (num == len) ? 0 : 1;
                   10426:                REG16(CX) = nrec;
1.1.1.16  root     10427:        }
1.1.1.65  root     10428:        fcb->current_block = rec / 128;
                   10429:        fcb->cur_record = rec % 128;
1.1.1.16  root     10430: }
                   10431: 
1.1       root     10432: inline void msdos_int_21h_29h()
                   10433: {
1.1.1.20  root     10434:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   10435:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     10436:        UINT8 drv = 0;
                   10437:        char sep_chars[] = ":.;,=+";
                   10438:        char end_chars[] = "\\<>|/\"[]";
                   10439:        char spc_chars[] = " \t";
                   10440:        
1.1.1.20  root     10441:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   10442:        buffer[1023] = 0;
                   10443:        memset(name, 0x20, sizeof(name));
                   10444:        memset(ext, 0x20, sizeof(ext));
                   10445:        
1.1       root     10446:        if(REG8(AL) & 1) {
1.1.1.20  root     10447:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   10448:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     10449:                        ofs++;
                   10450:                }
                   10451:        }
1.1.1.20  root     10452:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     10453:        
1.1.1.24  root     10454:        if(buffer[ofs + 1] == ':') {
                   10455:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   10456:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     10457:                        ofs += 2;
1.1.1.24  root     10458:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10459:                                ofs++;
                   10460:                        }
                   10461:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   10462:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     10463:                        ofs += 2;
1.1.1.24  root     10464:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   10465:                                ofs++;
                   10466:                        }
1.1       root     10467:                }
                   10468:        }
1.1.1.20  root     10469:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10470:                UINT8 c = buffer[ofs];
                   10471:                if(is_kanji) {
                   10472:                        is_kanji = 0;
                   10473:                } else if(msdos_lead_byte_check(c)) {
                   10474:                        is_kanji = 1;
                   10475:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10476:                        break;
                   10477:                } else if(c >= 'a' && c <= 'z') {
                   10478:                        c -= 0x20;
                   10479:                }
                   10480:                ofs++;
                   10481:                name[i] = c;
                   10482:        }
1.1.1.20  root     10483:        if(buffer[ofs] == '.') {
1.1       root     10484:                ofs++;
1.1.1.20  root     10485:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   10486:                        UINT8 c = buffer[ofs];
                   10487:                        if(is_kanji) {
                   10488:                                is_kanji = 0;
                   10489:                        } else if(msdos_lead_byte_check(c)) {
                   10490:                                is_kanji = 1;
                   10491:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     10492:                                break;
                   10493:                        } else if(c >= 'a' && c <= 'z') {
                   10494:                                c -= 0x20;
                   10495:                        }
                   10496:                        ofs++;
                   10497:                        ext[i] = c;
                   10498:                }
                   10499:        }
1.1.1.20  root     10500:        int si = REG16(SI) + ofs;
1.1.1.3   root     10501:        int ds = SREG(DS);
1.1       root     10502:        while(si > 0xffff) {
                   10503:                si -= 0x10;
                   10504:                ds++;
                   10505:        }
                   10506:        REG16(SI) = si;
1.1.1.3   root     10507:        SREG(DS) = ds;
                   10508:        i386_load_segment_descriptor(DS);
1.1       root     10509:        
1.1.1.3   root     10510:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     10511:        if(!(REG8(AL) & 2) || drv != 0) {
                   10512:                fcb[0] = drv;
                   10513:        }
                   10514:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   10515:                memcpy(fcb + 1, name, 8);
                   10516:        }
                   10517:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   10518:                memcpy(fcb + 9, ext, 3);
                   10519:        }
                   10520:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     10521:                if(fcb[i] == '*') {
                   10522:                        found_star = 1;
                   10523:                }
                   10524:                if(found_star) {
                   10525:                        fcb[i] = '?';
                   10526:                }
                   10527:        }
1.1.1.20  root     10528:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     10529:                if(fcb[i] == '*') {
                   10530:                        found_star = 1;
                   10531:                }
                   10532:                if(found_star) {
                   10533:                        fcb[i] = '?';
                   10534:                }
                   10535:        }
                   10536:        
1.1.1.44  root     10537:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     10538:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   10539:                        REG8(AL) = 0x01;
1.1.1.20  root     10540:                } else {
                   10541:                        REG8(AL) = 0x00;
1.1       root     10542:                }
                   10543:        } else {
                   10544:                REG8(AL) = 0xff;
                   10545:        }
                   10546: }
                   10547: 
                   10548: inline void msdos_int_21h_2ah()
                   10549: {
                   10550:        SYSTEMTIME sTime;
                   10551:        
                   10552:        GetLocalTime(&sTime);
                   10553:        REG16(CX) = sTime.wYear;
                   10554:        REG8(DH) = (UINT8)sTime.wMonth;
                   10555:        REG8(DL) = (UINT8)sTime.wDay;
                   10556:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   10557: }
                   10558: 
                   10559: inline void msdos_int_21h_2bh()
                   10560: {
1.1.1.14  root     10561:        REG8(AL) = 0xff;
1.1       root     10562: }
                   10563: 
                   10564: inline void msdos_int_21h_2ch()
                   10565: {
                   10566:        SYSTEMTIME sTime;
                   10567:        
                   10568:        GetLocalTime(&sTime);
                   10569:        REG8(CH) = (UINT8)sTime.wHour;
                   10570:        REG8(CL) = (UINT8)sTime.wMinute;
                   10571:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     10572:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     10573: }
                   10574: 
                   10575: inline void msdos_int_21h_2dh()
                   10576: {
                   10577:        REG8(AL) = 0x00;
                   10578: }
                   10579: 
                   10580: inline void msdos_int_21h_2eh()
                   10581: {
                   10582:        process_t *process = msdos_process_info_get(current_psp);
                   10583:        
                   10584:        process->verify = REG8(AL);
                   10585: }
                   10586: 
                   10587: inline void msdos_int_21h_2fh()
                   10588: {
                   10589:        process_t *process = msdos_process_info_get(current_psp);
                   10590:        
                   10591:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     10592:        SREG(ES) = process->dta.w.h;
                   10593:        i386_load_segment_descriptor(ES);
1.1       root     10594: }
                   10595: 
                   10596: inline void msdos_int_21h_30h()
                   10597: {
                   10598:        // Version Flag / OEM
1.1.1.27  root     10599:        if(REG8(AL) == 0x01) {
1.1.1.29  root     10600: #ifdef SUPPORT_HMA
                   10601:                REG16(BX) = 0x0000;
                   10602: #else
                   10603:                REG16(BX) = 0x1000; // DOS is in HMA
                   10604: #endif
1.1       root     10605:        } else {
1.1.1.27  root     10606:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   10607:                // but this is not correct on Windows 98 SE
                   10608: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   10609:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     10610:        }
1.1.1.27  root     10611:        REG16(CX) = 0x0000;
1.1.1.30  root     10612:        REG8(AL) = dos_major_version;   // 7
                   10613:        REG8(AH) = dos_minor_version;   // 10
1.1       root     10614: }
                   10615: 
                   10616: inline void msdos_int_21h_31h()
                   10617: {
1.1.1.29  root     10618:        try {
                   10619:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10620:        } catch(...) {
                   10621:                // recover the broken mcb
                   10622:                int mcb_seg = current_psp - 1;
                   10623:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10624:                
1.1.1.29  root     10625:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10626:                        mcb->mz = 'M';
                   10627:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10628:                        
1.1.1.29  root     10629:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     10630:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     10631:                        } else {
1.1.1.39  root     10632:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     10633:                        }
                   10634:                } else {
                   10635:                        mcb->mz = 'Z';
1.1.1.30  root     10636:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10637:                }
                   10638:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   10639:        }
1.1       root     10640:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   10641: }
                   10642: 
                   10643: inline void msdos_int_21h_32h()
                   10644: {
                   10645:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10646:        UINT16 seg, ofs;
                   10647:        
                   10648:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10649:                REG8(AL) = 0;
1.1.1.3   root     10650:                SREG(DS) = seg;
                   10651:                i386_load_segment_descriptor(DS);
1.1       root     10652:                REG16(BX) = ofs;
                   10653:        } else {
                   10654:                REG8(AL) = 0xff;
1.1.1.3   root     10655:                m_CF = 1;
1.1       root     10656:        }
                   10657: }
                   10658: 
                   10659: inline void msdos_int_21h_33h()
                   10660: {
                   10661:        char path[MAX_PATH];
1.1.1.48  root     10662:        char drive = 3; // C:
1.1       root     10663:        
                   10664:        switch(REG8(AL)) {
                   10665:        case 0x00:
1.1.1.33  root     10666:                REG8(DL) = ctrl_break_checking;
1.1       root     10667:                break;
                   10668:        case 0x01:
1.1.1.33  root     10669:                ctrl_break_checking = REG8(DL);
                   10670:                break;
                   10671:        case 0x02:
                   10672:                {
                   10673:                        UINT8 old = ctrl_break_checking;
                   10674:                        ctrl_break_checking = REG8(DL);
                   10675:                        REG8(DL) = old;
                   10676:                }
                   10677:                break;
                   10678:        case 0x03:
                   10679:        case 0x04:
                   10680:                // DOS 4.0+ - Unused
1.1       root     10681:                break;
                   10682:        case 0x05:
1.1.1.60  root     10683:                if(GetSystemDirectoryA(path, MAX_PATH) != 0) {
1.1.1.48  root     10684:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   10685:                                drive = path[0] - 'a' + 1;
                   10686:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   10687:                                drive = path[0] - 'A' + 1;
                   10688:                        }
1.1       root     10689:                }
1.1.1.48  root     10690:                REG8(DL) = (UINT8)drive;
1.1       root     10691:                break;
                   10692:        case 0x06:
1.1.1.2   root     10693:                // MS-DOS version (7.10)
1.1       root     10694:                REG8(BL) = 7;
1.1.1.2   root     10695:                REG8(BH) = 10;
1.1       root     10696:                REG8(DL) = 0;
1.1.1.29  root     10697: #ifdef SUPPORT_HMA
                   10698:                REG8(DH) = 0x00;
                   10699: #else
                   10700:                REG8(DH) = 0x10; // DOS is in HMA
                   10701: #endif
1.1       root     10702:                break;
1.1.1.6   root     10703:        case 0x07:
                   10704:                if(REG8(DL) == 0) {
                   10705:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   10706:                } else if(REG8(DL) == 1) {
                   10707:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   10708:                }
                   10709:                break;
1.1       root     10710:        default:
1.1.1.22  root     10711:                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     10712: //             REG16(AX) = 0x01;
                   10713: //             m_CF = 1;
                   10714:                REG8(AL) = 0xff;
1.1       root     10715:                break;
                   10716:        }
                   10717: }
                   10718: 
1.1.1.23  root     10719: inline void msdos_int_21h_34h()
                   10720: {
                   10721:        SREG(ES) = SDA_TOP >> 4;
                   10722:        i386_load_segment_descriptor(ES);
1.1.1.65  root     10723:        REG16(BX) = offsetof(sda_t, indos_flag);
1.1.1.23  root     10724: }
                   10725: 
1.1       root     10726: inline void msdos_int_21h_35h()
                   10727: {
                   10728:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     10729:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   10730:        i386_load_segment_descriptor(ES);
1.1       root     10731: }
                   10732: 
                   10733: inline void msdos_int_21h_36h()
                   10734: {
                   10735:        struct _diskfree_t df = {0};
                   10736:        
                   10737:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   10738:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   10739:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     10740:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   10741:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     10742:        } else {
                   10743:                REG16(AX) = 0xffff;
                   10744:        }
                   10745: }
                   10746: 
                   10747: inline void msdos_int_21h_37h()
                   10748: {
1.1.1.22  root     10749:        static UINT8 dev_flag = 0xff;
1.1       root     10750:        
                   10751:        switch(REG8(AL)) {
                   10752:        case 0x00:
1.1.1.22  root     10753:                {
                   10754:                        process_t *process = msdos_process_info_get(current_psp);
                   10755:                        REG8(AL) = 0x00;
                   10756:                        REG8(DL) = process->switchar;
                   10757:                }
1.1       root     10758:                break;
                   10759:        case 0x01:
1.1.1.22  root     10760:                {
                   10761:                        process_t *process = msdos_process_info_get(current_psp);
                   10762:                        REG8(AL) = 0x00;
                   10763:                        process->switchar = REG8(DL);
1.1.1.23  root     10764:                        msdos_sda_update(current_psp);
1.1.1.22  root     10765:                }
                   10766:                break;
                   10767:        case 0x02:
                   10768:                REG8(DL) = dev_flag;
                   10769:                break;
                   10770:        case 0x03:
                   10771:                dev_flag = REG8(DL);
                   10772:                break;
                   10773:        case 0xd0:
                   10774:        case 0xd1:
                   10775:        case 0xd2:
                   10776:        case 0xd3:
                   10777:        case 0xd4:
                   10778:        case 0xd5:
                   10779:        case 0xd6:
                   10780:        case 0xd7:
                   10781:        case 0xdc:
                   10782:        case 0xdd:
                   10783:        case 0xde:
                   10784:        case 0xdf:
1.1.1.48  root     10785:                // DIET v1.43e
                   10786: //             REG16(AX) = 1;
                   10787:                REG8(AL) = 0xff;
1.1       root     10788:                break;
                   10789:        default:
1.1.1.22  root     10790:                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     10791: //             REG16(AX) = 1;
                   10792:                REG8(AL) = 0xff;
1.1       root     10793:                break;
                   10794:        }
                   10795: }
                   10796: 
1.1.1.52  root     10797: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10798: {
                   10799:        char LCdata[80];
                   10800:        
1.1.1.19  root     10801:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.60  root     10802:        GetLocaleInfoA(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10803:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.60  root     10804:        GetLocaleInfoA(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10805:        ci->currency_format = *LCdata - '0';
1.1.1.60  root     10806:        GetLocaleInfoA(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10807:        ci->date_format = *LCdata - '0';
1.1.1.60  root     10808:        GetLocaleInfoA(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10809:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.60  root     10810:        GetLocaleInfoA(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10811:        *ci->date_sep = *LCdata;
1.1.1.60  root     10812:        GetLocaleInfoA(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10813:        *ci->dec_sep = *LCdata;
1.1.1.60  root     10814:        GetLocaleInfoA(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10815:        *ci->list_sep = *LCdata;
1.1.1.60  root     10816:        GetLocaleInfoA(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10817:        *ci->thou_sep = *LCdata;
1.1.1.60  root     10818:        GetLocaleInfoA(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10819:        *ci->time_sep = *LCdata;
1.1.1.60  root     10820:        GetLocaleInfoA(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10821:        if(strchr(LCdata, 'H') != NULL) {
                   10822:                ci->time_format = 1;
                   10823:        }
1.1.1.49  root     10824:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10825:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.60  root     10826:        GetLocaleInfoA(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10827:        return atoi(LCdata);
                   10828: }
                   10829: 
1.1.1.42  root     10830: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10831: {
                   10832:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10833: }
                   10834: 
1.1.1.43  root     10835: void set_country_info(country_info_t *ci, int size)
                   10836: {
                   10837:        char LCdata[80];
                   10838:        
                   10839:        if(size >= 0x00 + 2) {
                   10840:                memset(LCdata, 0, sizeof(LCdata));
                   10841:                *LCdata = '0' + ci->date_format;
1.1.1.60  root     10842:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
1.1.1.43  root     10843:        }
                   10844:        if(size >= 0x02 + 5) {
                   10845:                memset(LCdata, 0, sizeof(LCdata));
                   10846:                memcpy(LCdata, &ci->currency_symbol, 4);
1.1.1.60  root     10847:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
1.1.1.43  root     10848:        }
                   10849:        if(size >= 0x07 + 2) {
                   10850:                memset(LCdata, 0, sizeof(LCdata));
                   10851:                *LCdata = *ci->thou_sep;
1.1.1.60  root     10852:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
1.1.1.43  root     10853:        }
                   10854:        if(size >= 0x09 + 2) {
                   10855:                memset(LCdata, 0, sizeof(LCdata));
                   10856:                *LCdata = *ci->dec_sep;
1.1.1.60  root     10857:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
1.1.1.43  root     10858:        }
                   10859:        if(size >= 0x0b + 2) {
                   10860:                memset(LCdata, 0, sizeof(LCdata));
                   10861:                *LCdata = *ci->date_sep;
1.1.1.60  root     10862:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
1.1.1.43  root     10863:        }
                   10864:        if(size >= 0x0d + 2) {
                   10865:                memset(LCdata, 0, sizeof(LCdata));
                   10866:                *LCdata = *ci->time_sep;
1.1.1.60  root     10867:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
1.1.1.43  root     10868:        }
                   10869:        if(size >= 0x0f + 1) {
                   10870:                memset(LCdata, 0, sizeof(LCdata));
                   10871:                *LCdata = '0' + ci->currency_format;
1.1.1.60  root     10872:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
1.1.1.43  root     10873:        }
                   10874:        if(size >= 0x10 + 1) {
                   10875:                sprintf(LCdata, "%d", ci->currency_dec_digits);
1.1.1.60  root     10876:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
1.1.1.43  root     10877:        }
                   10878:        if(size >= 0x11 + 1) {
                   10879:                // FIXME: is time format always H/h:mm:ss ???
                   10880:                if(ci->time_format & 1) {
                   10881:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10882:                } else {
                   10883:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10884:                }
1.1.1.60  root     10885:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
1.1.1.43  root     10886:        }
                   10887:        if(size >= 0x12 + 4) {
                   10888:                // 12h  DWORD   address of case map routine
                   10889:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10890:        }
                   10891:        if(size >= 0x16 + 2) {
                   10892:                memset(LCdata, 0, sizeof(LCdata));
                   10893:                *LCdata = *ci->list_sep;
1.1.1.60  root     10894:                SetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
1.1.1.43  root     10895:        }
                   10896: }
                   10897: 
1.1.1.42  root     10898: #ifndef SUBLANG_SWAHILI
                   10899:        #define SUBLANG_SWAHILI 0x01
                   10900: #endif
                   10901: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10902:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10903: #endif
                   10904: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10905:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10906: #endif
                   10907: #ifndef LANG_BANGLA
                   10908:        #define LANG_BANGLA 0x45
                   10909: #endif
                   10910: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10911:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10912: #endif
                   10913: 
                   10914: static const struct {
                   10915:        int code;
                   10916:        USHORT usPrimaryLanguage;
                   10917:        USHORT usSubLanguage;
                   10918: } country_table[] = {
                   10919:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10920:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10921:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10922:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10923:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10924:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10925: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10926: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10927: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10928: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10929: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10930:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10931:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10932: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10933:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10934: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10935:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10936:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10937:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10938:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10939:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10940:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10941:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10942: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10943: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10944:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10945:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10946:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10947:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10948:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10949: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10950: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10951: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10952:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10953: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10954: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10955: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10956: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10957:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10958:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10959:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10960: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10961:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10962:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10963:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10964:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10965:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10966:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10967:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10968:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10969:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10970:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10971:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10972:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10973:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10974: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10975:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10976:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10977:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10978:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10979:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10980:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10981:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10982:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10983:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10984:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10985: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10986:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10987: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10988:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10989:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10990:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10991:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10992:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10993:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10994:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10995:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10996: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10997:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10998: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10999: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   11000:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   11001: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   11002:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   11003:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   11004:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   11005:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   11006:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   11007:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   11008:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   11009: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   11010: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   11011:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   11012: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   11013:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   11014:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   11015:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   11016:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   11017: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   11018: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   11019: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   11020:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   11021:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   11022:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   11023:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   11024:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   11025:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   11026:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   11027:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   11028:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   11029:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   11030:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   11031:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   11032:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   11033:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   11034:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   11035:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   11036:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   11037:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   11038:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   11039:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   11040:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     11041: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     11042:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   11043: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   11044:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   11045:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   11046:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   11047:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   11048:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   11049:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   11050:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   11051:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   11052:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   11053:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   11054:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   11055:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   11056:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   11057:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   11058:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   11059:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   11060:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   11061:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   11062:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   11063:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   11064:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   11065:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   11066:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   11067:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   11068:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   11069: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   11070:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   11071:        {-1, 0, 0},
                   11072: };
                   11073: 
1.1.1.14  root     11074: inline void msdos_int_21h_38h()
                   11075: {
                   11076:        switch(REG8(AL)) {
                   11077:        case 0x00:
1.1.1.19  root     11078:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     11079:                break;
                   11080:        default:
1.1.1.42  root     11081:                for(int i = 0;; i++) {
                   11082:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   11083:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   11084:                                break;
                   11085:                        } else if(country_table[i].code == -1) {
                   11086: //                             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));
                   11087: //                             REG16(AX) = 2;
                   11088: //                             m_CF = 1;
1.1.1.48  root     11089:                                // get current coutry info
1.1.1.42  root     11090:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   11091:                                break;
                   11092:                        }
                   11093:                }
1.1.1.14  root     11094:                break;
                   11095:        }
                   11096: }
                   11097: 
1.1       root     11098: inline void msdos_int_21h_39h(int lfn)
                   11099: {
1.1.1.3   root     11100:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11101:                REG16(AX) = errno;
1.1.1.3   root     11102:                m_CF = 1;
1.1       root     11103:        }
                   11104: }
                   11105: 
                   11106: inline void msdos_int_21h_3ah(int lfn)
                   11107: {
1.1.1.3   root     11108:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11109:                REG16(AX) = errno;
1.1.1.3   root     11110:                m_CF = 1;
1.1       root     11111:        }
                   11112: }
                   11113: 
                   11114: inline void msdos_int_21h_3bh(int lfn)
                   11115: {
1.1.1.45  root     11116:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     11117:        
                   11118:        if(_chdir(path)) {
1.1.1.17  root     11119:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     11120:                m_CF = 1;
1.1.1.44  root     11121:        } else {
                   11122:                int drv = _getdrive() - 1;
                   11123:                if(path[1] == ':') {
                   11124:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   11125:                                drv = path[0] - 'A';
                   11126:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   11127:                                drv = path[0] - 'a';
                   11128:                        }
                   11129:                }
                   11130:                msdos_cds_update(drv, path);
1.1       root     11131:        }
                   11132: }
                   11133: 
                   11134: inline void msdos_int_21h_3ch()
                   11135: {
1.1.1.45  root     11136:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     11137:        int attr = GetFileAttributesA(path);
1.1.1.37  root     11138:        int fd = -1;
                   11139:        int sio_port = 0;
                   11140:        int lpt_port = 0;
1.1       root     11141:        
1.1.1.45  root     11142:        if(msdos_is_device_path(path)) {
                   11143:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     11144:        } else {
                   11145:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11146:        }
                   11147:        if(fd != -1) {
                   11148:                if(attr == -1) {
                   11149:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   11150:                }
1.1.1.60  root     11151:                SetFileAttributesA(path, attr);
1.1       root     11152:                REG16(AX) = fd;
1.1.1.45  root     11153:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11154:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11155:        } else {
                   11156:                REG16(AX) = errno;
1.1.1.3   root     11157:                m_CF = 1;
1.1       root     11158:        }
                   11159: }
                   11160: 
                   11161: inline void msdos_int_21h_3dh()
                   11162: {
1.1.1.45  root     11163:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11164:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     11165:        int fd = -1;
                   11166:        int sio_port = 0;
                   11167:        int lpt_port = 0;
1.1       root     11168:        
                   11169:        if(mode < 0x03) {
1.1.1.45  root     11170:                if(msdos_is_device_path(path)) {
                   11171:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     11172:                } else {
1.1.1.13  root     11173:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     11174:                }
1.1       root     11175:                if(fd != -1) {
                   11176:                        REG16(AX) = fd;
1.1.1.45  root     11177:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     11178:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11179:                } else {
                   11180:                        REG16(AX) = errno;
1.1.1.3   root     11181:                        m_CF = 1;
1.1       root     11182:                }
                   11183:        } else {
                   11184:                REG16(AX) = 0x0c;
1.1.1.3   root     11185:                m_CF = 1;
1.1       root     11186:        }
                   11187: }
                   11188: 
                   11189: inline void msdos_int_21h_3eh()
                   11190: {
                   11191:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11192:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11193:        
1.1.1.20  root     11194:        if(fd < process->max_files && file_handler[fd].valid) {
                   11195:                _close(fd);
                   11196:                msdos_file_handler_close(fd);
                   11197:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     11198:        } else {
                   11199:                REG16(AX) = 0x06;
1.1.1.3   root     11200:                m_CF = 1;
1.1       root     11201:        }
                   11202: }
                   11203: 
1.1.1.35  root     11204: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   11205: {
                   11206:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   11207:        int max = REG16(CX);
                   11208:        int p = 0;
                   11209:        
                   11210:        while(max > p) {
                   11211:                int chr = msdos_getch();
                   11212:                
                   11213:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   11214:                        p = 0;
                   11215:                        buf[p++] = 0x0d;
                   11216:                        if(max > p) {
                   11217:                                buf[p++] = 0x0a;
                   11218:                        }
                   11219:                        msdos_putch(0x03);
                   11220:                        msdos_putch(0x0d);
                   11221:                        msdos_putch(0x0a);
                   11222:                        break;
                   11223:                } else if(ctrl_break_pressed) {
                   11224:                        // skip this byte
                   11225:                } else if(chr == 0x00) {
                   11226:                        // skip 2nd byte
                   11227:                        msdos_getch();
                   11228:                } else if(chr == 0x0d) {
                   11229:                        // carriage return
                   11230:                        buf[p++] = 0x0d;
                   11231:                        if(max > p) {
                   11232:                                buf[p++] = 0x0a;
                   11233:                        }
                   11234:                        msdos_putch('\n');
                   11235:                        break;
                   11236:                } else if(chr == 0x08) {
                   11237:                        // back space
                   11238:                        if(p > 0) {
                   11239:                                p--;
                   11240:                                if(msdos_ctrl_code_check(buf[p])) {
                   11241:                                        msdos_putch(0x08);
                   11242:                                        msdos_putch(0x08);
                   11243:                                        msdos_putch(0x20);
                   11244:                                        msdos_putch(0x20);
                   11245:                                        msdos_putch(0x08);
                   11246:                                        msdos_putch(0x08);
1.1.1.36  root     11247:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   11248:                                        p--;
                   11249:                                        msdos_putch(0x08);
                   11250:                                        msdos_putch(0x08);
                   11251:                                        msdos_putch(0x20);
                   11252:                                        msdos_putch(0x20);
                   11253:                                        msdos_putch(0x08);
                   11254:                                        msdos_putch(0x08);
1.1.1.35  root     11255:                                } else {
                   11256:                                        msdos_putch(0x08);
                   11257:                                        msdos_putch(0x20);
                   11258:                                        msdos_putch(0x08);
                   11259:                                }
                   11260:                        }
                   11261:                } else if(chr == 0x1b) {
                   11262:                        // escape
                   11263:                        while(p > 0) {
                   11264:                                p--;
                   11265:                                if(msdos_ctrl_code_check(buf[p])) {
                   11266:                                        msdos_putch(0x08);
                   11267:                                        msdos_putch(0x08);
                   11268:                                        msdos_putch(0x20);
                   11269:                                        msdos_putch(0x20);
                   11270:                                        msdos_putch(0x08);
                   11271:                                        msdos_putch(0x08);
                   11272:                                } else {
                   11273:                                        msdos_putch(0x08);
                   11274:                                        msdos_putch(0x20);
                   11275:                                        msdos_putch(0x08);
                   11276:                                }
                   11277:                        }
                   11278:                } else {
                   11279:                        buf[p++] = chr;
                   11280:                        msdos_putch(chr);
                   11281:                }
                   11282:        }
                   11283:        REG16(AX) = p;
                   11284:        
                   11285: #ifdef USE_SERVICE_THREAD
                   11286:        service_exit = true;
                   11287: #endif
                   11288:        return(0);
                   11289: }
                   11290: 
1.1       root     11291: inline void msdos_int_21h_3fh()
                   11292: {
                   11293:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11294:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11295:        
1.1.1.20  root     11296:        if(fd < process->max_files && file_handler[fd].valid) {
                   11297:                if(file_mode[file_handler[fd].mode].in) {
                   11298:                        if(file_handler[fd].atty) {
1.1       root     11299:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     11300:                                if(REG16(CX) != 0) {
                   11301: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     11302:                                        if(!in_service && !in_service_29h &&
1.1.1.58  root     11303:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == (IRET_SIZE + 5 * 0x29) &&
1.1.1.50  root     11304:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   11305:                                                // msdos_putch() will be used in this service
                   11306:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   11307:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   11308:                                        } else {
                   11309: #endif
                   11310:                                                msdos_int_21h_3fh_thread(NULL);
                   11311:                                                REQUEST_HARDWRE_UPDATE();
                   11312: #ifdef USE_SERVICE_THREAD
                   11313:                                        }
1.1.1.35  root     11314: #endif
                   11315:                                } else {
                   11316:                                        REG16(AX) = 0;
1.1       root     11317:                                }
                   11318:                        } else {
1.1.1.37  root     11319:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11320:                        }
                   11321:                } else {
                   11322:                        REG16(AX) = 0x05;
1.1.1.3   root     11323:                        m_CF = 1;
1.1       root     11324:                }
                   11325:        } else {
                   11326:                REG16(AX) = 0x06;
1.1.1.3   root     11327:                m_CF = 1;
1.1       root     11328:        }
                   11329: }
                   11330: 
                   11331: inline void msdos_int_21h_40h()
                   11332: {
                   11333:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11334:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11335:        
1.1.1.20  root     11336:        if(fd < process->max_files && file_handler[fd].valid) {
                   11337:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     11338:                        if(REG16(CX)) {
1.1.1.20  root     11339:                                if(file_handler[fd].atty) {
1.1       root     11340:                                        // BX is stdout/stderr or is redirected to stdout
                   11341:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     11342:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     11343:                                        }
                   11344:                                        REG16(AX) = REG16(CX);
                   11345:                                } else {
1.1.1.20  root     11346:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11347:                                }
                   11348:                        } else {
1.1.1.20  root     11349:                                UINT32 pos = _tell(fd);
                   11350:                                _lseek(fd, 0, SEEK_END);
                   11351:                                UINT32 size = _tell(fd);
1.1.1.12  root     11352:                                if(pos < size) {
1.1.1.20  root     11353:                                        _lseek(fd, pos, SEEK_SET);
                   11354:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     11355:                                } else {
                   11356:                                        for(UINT32 i = size; i < pos; i++) {
                   11357:                                                UINT8 tmp = 0;
1.1.1.23  root     11358:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     11359:                                        }
1.1.1.20  root     11360:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     11361:                                }
1.1.1.23  root     11362:                                REG16(AX) = 0;
1.1       root     11363:                        }
                   11364:                } else {
                   11365:                        REG16(AX) = 0x05;
1.1.1.3   root     11366:                        m_CF = 1;
1.1       root     11367:                }
                   11368:        } else {
                   11369:                REG16(AX) = 0x06;
1.1.1.3   root     11370:                m_CF = 1;
1.1       root     11371:        }
                   11372: }
                   11373: 
                   11374: inline void msdos_int_21h_41h(int lfn)
                   11375: {
1.1.1.3   root     11376:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     11377:                REG16(AX) = errno;
1.1.1.3   root     11378:                m_CF = 1;
1.1       root     11379:        }
                   11380: }
                   11381: 
                   11382: inline void msdos_int_21h_42h()
                   11383: {
                   11384:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11385:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11386:        
1.1.1.20  root     11387:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11388:                if(REG8(AL) < 0x03) {
1.1.1.35  root     11389:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     11390:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   11391:                        UINT32 pos = _tell(fd);
1.1       root     11392:                        REG16(AX) = pos & 0xffff;
                   11393:                        REG16(DX) = (pos >> 16);
                   11394:                } else {
                   11395:                        REG16(AX) = 0x01;
1.1.1.3   root     11396:                        m_CF = 1;
1.1       root     11397:                }
                   11398:        } else {
                   11399:                REG16(AX) = 0x06;
1.1.1.3   root     11400:                m_CF = 1;
1.1       root     11401:        }
                   11402: }
                   11403: 
                   11404: inline void msdos_int_21h_43h(int lfn)
                   11405: {
1.1.1.45  root     11406:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     11407:        int attr;
                   11408:        
1.1.1.14  root     11409:        if(!lfn && REG8(AL) > 2) {
                   11410:                REG16(AX) = 0x01;
                   11411:                m_CF = 1;
                   11412:                return;
                   11413:        }
                   11414:        switch(REG8(lfn ? BL : AL)) {
1.1       root     11415:        case 0x00:
1.1.1.60  root     11416:                if((attr = GetFileAttributesA(path)) != -1) {
1.1.1.14  root     11417:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   11418:                } else {
                   11419:                        REG16(AX) = (UINT16)GetLastError();
                   11420:                        m_CF = 1;
                   11421:                }
                   11422:                break;
                   11423:        case 0x01:
1.1.1.60  root     11424:                if(!SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)))) {
1.1.1.14  root     11425:                        REG16(AX) = (UINT16)GetLastError();
                   11426:                        m_CF = 1;
                   11427:                }
                   11428:                break;
                   11429:        case 0x02:
                   11430:                {
1.1.1.60  root     11431:                        DWORD compressed_size = GetCompressedFileSizeA(path, NULL), file_size = 0;
1.1.1.45  root     11432:                        if(compressed_size != INVALID_FILE_SIZE) {
                   11433:                                if(compressed_size != 0) {
1.1.1.60  root     11434:                                        HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.45  root     11435:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   11436:                                                file_size = GetFileSize(hFile, NULL);
                   11437:                                                CloseHandle(hFile);
                   11438:                                        }
                   11439:                                        if(compressed_size == file_size) {
                   11440:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11441:                                                // this isn't correct if the file is in the NTFS MFT
1.1.1.60  root     11442:                                                if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1.1.45  root     11443:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   11444:                                                }
1.1.1.14  root     11445:                                        }
                   11446:                                }
1.1.1.45  root     11447:                                REG16(AX) = LOWORD(compressed_size);
                   11448:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     11449:                        } else {
                   11450:                                REG16(AX) = (UINT16)GetLastError();
                   11451:                                m_CF = 1;
1.1       root     11452:                        }
1.1.1.14  root     11453:                }
                   11454:                break;
                   11455:        case 0x03:
                   11456:        case 0x05:
                   11457:        case 0x07:
1.1.1.48  root     11458:                if(lfn) {
1.1.1.60  root     11459:                        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     11460:                        if(hFile != INVALID_HANDLE_VALUE) {
                   11461:                                FILETIME local, time;
                   11462:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   11463:                                if(REG8(BL) == 7) {
                   11464:                                        ULARGE_INTEGER hund;
                   11465:                                        hund.LowPart = local.dwLowDateTime;
                   11466:                                        hund.HighPart = local.dwHighDateTime;
                   11467:                                        hund.QuadPart += REG16(SI) * 100000;
                   11468:                                        local.dwLowDateTime = hund.LowPart;
                   11469:                                        local.dwHighDateTime = hund.HighPart;
                   11470:                                }
                   11471:                                LocalFileTimeToFileTime(&local, &time);
                   11472:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   11473:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   11474:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   11475:                                        REG16(AX) = (UINT16)GetLastError();
                   11476:                                        m_CF = 1;
                   11477:                                }
                   11478:                                CloseHandle(hFile);
                   11479:                        } else {
                   11480:                                REG16(AX) = (UINT16)GetLastError();
                   11481:                                m_CF = 1;
1.1       root     11482:                        }
1.1.1.48  root     11483:                } else {
                   11484:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   11485:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   11486:                        // 214307 DR DOS 6.0 - Set 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;
                   11492:        case 0x04:
                   11493:        case 0x06:
                   11494:        case 0x08:
1.1.1.48  root     11495:                if(lfn) {
1.1.1.14  root     11496:                        WIN32_FILE_ATTRIBUTE_DATA fad;
1.1.1.60  root     11497:                        if(GetFileAttributesExA(path, GetFileExInfoStandard, (LPVOID)&fad)) {
1.1.1.14  root     11498:                                FILETIME *time, local;
                   11499:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   11500:                                                   0x06 ? &fad.ftLastAccessTime :
                   11501:                                                          &fad.ftCreationTime;
                   11502:                                FileTimeToLocalFileTime(time, &local);
                   11503:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   11504:                                if(REG8(BL) == 0x08) {
                   11505:                                        ULARGE_INTEGER hund;
                   11506:                                        hund.LowPart = local.dwLowDateTime;
                   11507:                                        hund.HighPart = local.dwHighDateTime;
                   11508:                                        hund.QuadPart /= 100000;
                   11509:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   11510:                                }
                   11511:                        } else {
                   11512:                                REG16(AX) = (UINT16)GetLastError();
                   11513:                                m_CF = 1;
1.1       root     11514:                        }
1.1.1.48  root     11515:                } else {
                   11516:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   11517:                        // 214306 DR DOS 6.0 - Get File Owner
                   11518: //                     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));
                   11519:                        REG16(AX) = 0x01;
                   11520:                        m_CF = 1;
1.1.1.14  root     11521:                }
                   11522:                break;
1.1.1.43  root     11523:        case 0xff:
1.1.1.48  root     11524:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     11525:                        if(REG8(CL) == 0x39) {
                   11526:                                msdos_int_21h_39h(1);
                   11527:                                break;
                   11528:                        } else if(REG8(CL) == 0x56) {
                   11529:                                msdos_int_21h_56h(1);
                   11530:                                break;
                   11531:                        }
                   11532:                }
1.1.1.14  root     11533:        default:
1.1.1.22  root     11534:                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     11535:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     11536:                m_CF = 1;
                   11537:                break;
                   11538:        }
                   11539: }
                   11540: 
                   11541: inline void msdos_int_21h_44h()
                   11542: {
1.1.1.22  root     11543:        static UINT16 iteration_count = 0;
                   11544:        
1.1.1.44  root     11545:        process_t *process;
                   11546:        int fd, drv;
1.1.1.14  root     11547:        
                   11548:        switch(REG8(AL)) {
                   11549:        case 0x00:
                   11550:        case 0x01:
                   11551:        case 0x02:
                   11552:        case 0x03:
                   11553:        case 0x04:
                   11554:        case 0x05:
                   11555:        case 0x06:
                   11556:        case 0x07:
1.1.1.44  root     11557:                process = msdos_process_info_get(current_psp);
                   11558:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     11559:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   11560:                        REG16(AX) = 0x06;
                   11561:                        m_CF = 1;
                   11562:                        return;
1.1.1.14  root     11563:                }
                   11564:                break;
                   11565:        case 0x08:
                   11566:        case 0x09:
1.1.1.44  root     11567:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11568:                if(!msdos_is_valid_drive(drv)) {
                   11569:                        // invalid drive
1.1.1.14  root     11570:                        REG16(AX) = 0x0f;
                   11571:                        m_CF = 1;
                   11572:                        return;
1.1       root     11573:                }
                   11574:                break;
                   11575:        }
                   11576:        switch(REG8(AL)) {
1.1.1.48  root     11577:        case 0x00: // Get Device Information
1.1.1.20  root     11578:                REG16(DX) = file_handler[fd].info;
1.1       root     11579:                break;
1.1.1.48  root     11580:        case 0x01: // Set Device Information
1.1.1.45  root     11581:                if(REG8(DH) != 0) {
                   11582: //                     REG16(AX) = 0x0d; // data invalid
                   11583: //                     m_CF = 1;
                   11584:                        file_handler[fd].info = REG16(DX);
                   11585:                } else {
                   11586:                        file_handler[fd].info &= 0xff00;
                   11587:                        file_handler[fd].info |= REG8(DL);
                   11588:                }
1.1       root     11589:                break;
1.1.1.48  root     11590:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     11591:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   11592:                        // from DOSBox
                   11593:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   11594:                        case 0x00:
                   11595:                                if(REG16(CX) >= 6) {
                   11596:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   11597:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   11598:                                        REG16(AX) = 6; // number of bytes actually read
                   11599:                                } else {
                   11600:                                        REG16(AX) = 0x0d; // data invalid
                   11601:                                        m_CF = 1;
                   11602:                                }
                   11603:                                break;
                   11604:                        case 0x01:
                   11605:                                if(REG16(CX) >= 6) {
                   11606:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   11607:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   11608:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   11609:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   11610:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   11611:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   11612:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   11613:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   11614:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11615:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   11616:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   11617:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   11618:                                                } else {
                   11619:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   11620:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   11621:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   11622:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   11623:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   11624:                                                }
                   11625:                                        }
                   11626:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   11627:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   11628:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   11629:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   11630:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   11631:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   11632:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   11633:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   11634:                                        
                   11635:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
1.1.1.65  root     11636:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;
1.1.1.45  root     11637:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   11638:                                        REG16(AX) = 6; // number of bytes actually read
                   11639:                                } else {
                   11640:                                        REG16(AX) = 0x0d; // data invalid
                   11641:                                        m_CF = 1;
                   11642:                                }
                   11643:                                break;
                   11644:                        case 0x02:
                   11645:                                if(REG16(CX) >= 2) {
                   11646:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   11647:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   11648:                                        REG16(AX) = 2; // number of bytes actually read
                   11649:                                } else {
                   11650:                                        REG16(AX) = 0x0d; // data invalid
                   11651:                                        m_CF = 1;
                   11652:                                }
                   11653:                                break;
                   11654:                        case 0x03:
                   11655:                                if(REG16(CX) >= 4) {
                   11656:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   11657:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   11658:                                        REG16(AX) = 4; // number of bytes actually read
                   11659:                                } else {
                   11660:                                        REG16(AX) = 0x0d; // data invalid
                   11661:                                        m_CF = 1;
                   11662:                                }
                   11663:                                break;
                   11664:                        default:
                   11665:                                REG16(AX) = 0x01; // function number invalid
                   11666:                                m_CF = 1;
                   11667:                        }
                   11668:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   11669:                        if(REG16(CX) >= 5) {
                   11670:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   11671:                                REG16(AX) = 5; // number of bytes actually read
                   11672:                        } else {
                   11673:                                REG16(AX) = 0x0d; // data invalid
                   11674:                                m_CF = 1;
                   11675:                        }
                   11676:                } else {
                   11677: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   11678: //                     REG16(AX) = REG16(CX);
                   11679:                        REG16(AX) = 0x05; // access denied
                   11680:                        m_CF = 1;
                   11681:                }
                   11682:                break;
1.1.1.48  root     11683:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     11684: //             REG16(AX) = 0x05;
                   11685: //             m_CF = 1;
                   11686:                REG16(AX) = 0x00; // success
                   11687:                break;
1.1.1.48  root     11688:        case 0x04: // Read From Block Device Control Channel
                   11689:        case 0x05: // Write To Block Device Control Channel
1.1       root     11690:                REG16(AX) = 0x05;
1.1.1.3   root     11691:                m_CF = 1;
1.1       root     11692:                break;
1.1.1.48  root     11693:        case 0x06: // Get Input Status
1.1.1.20  root     11694:                if(file_mode[file_handler[fd].mode].in) {
                   11695:                        if(file_handler[fd].atty) {
1.1.1.14  root     11696:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     11697:                        } else {
1.1.1.20  root     11698:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     11699:                        }
1.1.1.14  root     11700:                } else {
                   11701:                        REG8(AL) = 0x00;
1.1       root     11702:                }
                   11703:                break;
1.1.1.48  root     11704:        case 0x07: // Get Output Status
1.1.1.20  root     11705:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     11706:                        REG8(AL) = 0xff;
                   11707:                } else {
                   11708:                        REG8(AL) = 0x00;
1.1       root     11709:                }
                   11710:                break;
1.1.1.48  root     11711:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     11712:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     11713:                        // removable drive
                   11714:                        REG16(AX) = 0x00;
1.1       root     11715:                } else {
1.1.1.14  root     11716:                        // fixed drive
                   11717:                        REG16(AX) = 0x01;
1.1       root     11718:                }
                   11719:                break;
1.1.1.48  root     11720:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     11721:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     11722:                        // remote drive
                   11723:                        REG16(DX) = 0x1000;
1.1.1.44  root     11724:                } else if(msdos_is_subst_drive(drv)) {
                   11725:                        // subst drive
                   11726:                        REG16(DX) = 0x8000;
1.1       root     11727:                } else {
1.1.1.14  root     11728:                        // local drive
1.1.1.44  root     11729:                        REG16(DX) = 0x0000;
1.1       root     11730:                }
                   11731:                break;
1.1.1.48  root     11732:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     11733:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   11734:                        REG16(DX) = 0x8000;
                   11735:                } else {
                   11736:                        REG16(DX) = 0x0000;
                   11737:                }
1.1.1.21  root     11738:                break;
1.1.1.48  root     11739:        case 0x0b: // Set Sharing Retry Count
1.1       root     11740:                break;
1.1.1.48  root     11741:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     11742:                if(REG8(CL) == 0x45) {
                   11743:                        // set iteration (retry) count
                   11744:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   11745:                } else if(REG8(CL) == 0x4a) {
                   11746:                        // select code page
                   11747:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   11748:                        msdos_nls_tables_update();
1.1.1.44  root     11749:                } else if(REG8(CL) == 0x4c) {
                   11750:                        // start code-page preparation
                   11751:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11752:                        int count = 1, offset = 0;
                   11753:                        if(active_code_page != 437) {
                   11754:                                ids[count++] = active_code_page;
                   11755:                        }
                   11756:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11757:                                ids[count++] = system_code_page;
                   11758:                        }
                   11759:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11760:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11761:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11762:                        for(int i = 0; i < count; i++) {
                   11763:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11764:                        }
                   11765:                } else if(REG8(CL) == 0x4d) {
                   11766:                        // end code-page preparation
                   11767:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11768:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11769:                } else if(REG8(CL) == 0x5f) {
                   11770:                        // set display information
                   11771:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11772:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11773:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11774:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11775:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11776:                                
                   11777:                                if(cur_width != new_width || cur_height != new_height) {
                   11778:                                        pcbios_set_console_size(new_width, new_height, true);
                   11779:                                }
                   11780:                        }
1.1.1.22  root     11781:                } else if(REG8(CL) == 0x65) {
                   11782:                        // get iteration (retry) count
                   11783:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11784:                } else if(REG8(CL) == 0x6a) {
                   11785:                        // query selected code page
                   11786:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11787:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11788:                        
                   11789:                        CPINFO info;
                   11790:                        GetCPInfo(active_code_page, &info);
                   11791:                        
                   11792:                        if(info.MaxCharSize != 1) {
                   11793:                                for(int i = 0;; i++) {
                   11794:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11795:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11796:                                        
                   11797:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11798:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11799:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11800:                                        
                   11801:                                        if(lo == 0 && hi == 0) {
                   11802:                                                break;
                   11803:                                        }
                   11804:                                }
                   11805:                        }
1.1.1.44  root     11806:                } else if(REG8(CL) == 0x6b) {
                   11807:                        // query prepare list
                   11808:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11809:                        int count = 1, offset = 0;
                   11810:                        if(active_code_page != 437) {
                   11811:                                ids[count++] = active_code_page;
                   11812:                        }
                   11813:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11814:                                ids[count++] = system_code_page;
                   11815:                        }
                   11816:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11817:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11818:                        for(int i = 0; i < count; i++) {
                   11819:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11820:                        }
                   11821:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11822:                        for(int i = 0; i < count; i++) {
                   11823:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11824:                        }
1.1.1.22  root     11825:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11826:                        // get display information
1.1.1.50  root     11827:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11828:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11829:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11830:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11831:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11832:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11833:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11834:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11835:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11836:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11837:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11838:                } else {
                   11839:                        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));
                   11840:                        REG16(AX) = 0x01; // invalid function
                   11841:                        m_CF = 1;
                   11842:                }
                   11843:                break;
1.1.1.48  root     11844:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11845:                if(REG8(CL) == 0x40) {
                   11846:                        // set device parameters
1.1.1.48  root     11847: //             } else if(REG8(CL) == 0x41) {
                   11848: //                     // write logical device track
                   11849: //             } else if(REG8(CL) == 0x42) {
                   11850: //                     // format and verify logical device track
1.1.1.22  root     11851:                } else if(REG8(CL) == 0x46) {
                   11852:                        // set volume serial number
1.1.1.48  root     11853:                } else if(REG8(CL) == 0x47) {
                   11854:                        // set access flag
                   11855: //             } else if(REG8(CL) == 0x48) {
                   11856: //                     // set media lock state
                   11857: //             } else if(REG8(CL) == 0x49) {
                   11858: //                     // eject media in drive
1.1.1.22  root     11859:                } else if(REG8(CL) == 0x4a) {
                   11860:                        // lock logical volume
                   11861:                } else if(REG8(CL) == 0x4b) {
                   11862:                        // lock physical volume
                   11863:                } else if(REG8(CL) == 0x60) {
                   11864:                        // get device parameters
1.1.1.42  root     11865:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11866:                        
1.1.1.42  root     11867:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11868:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11869:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11870:                                
                   11871:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11872:                                switch(geo->MediaType) {
                   11873:                                case F5_360_512:
                   11874:                                case F5_320_512:
                   11875:                                case F5_320_1024:
                   11876:                                case F5_180_512:
                   11877:                                case F5_160_512:
                   11878:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11879:                                        break;
                   11880:                                case F5_1Pt2_512:
                   11881:                                case F3_1Pt2_512:
                   11882:                                case F3_1Pt23_1024:
                   11883:                                case F5_1Pt23_1024:
                   11884:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11885:                                        break;
                   11886:                                case F3_720_512:
                   11887:                                case F3_640_512:
                   11888:                                case F5_640_512:
                   11889:                                case F5_720_512:
                   11890:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11891:                                        break;
                   11892:                                case F8_256_128:
                   11893:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11894:                                        break;
                   11895:                                case FixedMedia:
                   11896:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11897:                                        break;
                   11898:                                case F3_1Pt44_512:
                   11899:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11900:                                        break;
                   11901:                                case F3_2Pt88_512:
                   11902:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11903:                                        break;
                   11904:                                default:
                   11905:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11906: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11907:                                        break;
1.1.1.22  root     11908:                                }
1.1.1.42  root     11909:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11910:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11911:                                switch(geo->MediaType) {
                   11912:                                case F5_360_512:
                   11913:                                case F5_320_512:
                   11914:                                case F5_320_1024:
                   11915:                                case F5_180_512:
                   11916:                                case F5_160_512:
                   11917:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11918:                                        break;
                   11919:                                default:
                   11920:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11921:                                        break;
                   11922:                                }
                   11923:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11924:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11925:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11926:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11927:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11928:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11929:                                switch(geo->MediaType) {
                   11930:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11931:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11932:                                        break;
                   11933:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11934:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11935:                                        break;
                   11936:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11937:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11938:                                        break;
                   11939:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11940:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11941:                                        break;
                   11942:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11943:                                case F3_1Pt2_512:
                   11944:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11945:                                case F5_720_512:
                   11946:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11947:                                        break;
                   11948:                                case FixedMedia:        // hard disk
                   11949:                                case RemovableMedia:
                   11950:                                case Unknown:
                   11951:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11952:                                        break;
                   11953:                                default:
                   11954:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11955:                                        break;
                   11956:                                }
                   11957:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11958:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11959:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11960:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11961:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11962:                                // 21h  BYTE    device type
                   11963:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11964:                        } else {
                   11965:                                REG16(AX) = 0x0f; // invalid drive
                   11966:                                m_CF = 1;
                   11967:                        }
1.1.1.48  root     11968: //             } else if(REG8(CL) == 0x61) {
                   11969: //                     // read logical device track
                   11970: //             } else if(REG8(CL) == 0x62) {
                   11971: //                     // verify logical device track
1.1.1.22  root     11972:                } else if(REG8(CL) == 0x66) {
                   11973:                        // get volume serial number
                   11974:                        char path[] = "A:\\";
                   11975:                        char volume_label[MAX_PATH];
                   11976:                        DWORD serial_number = 0;
                   11977:                        char file_system[MAX_PATH];
                   11978:                        
                   11979:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11980:                        
1.1.1.60  root     11981:                        if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1.1.22  root     11982:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11983:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11984:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11985:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11986:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11987:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11988:                        } else {
                   11989:                                REG16(AX) = 0x0f; // invalid drive
                   11990:                                m_CF = 1;
                   11991:                        }
                   11992:                } else if(REG8(CL) == 0x67) {
                   11993:                        // get access flag
                   11994:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11995:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11996:                } else if(REG8(CL) == 0x68) {
                   11997:                        // sense media type
1.1.1.42  root     11998:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11999:                        
1.1.1.42  root     12000:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   12001:                                drive_param_t *drive_param = &drive_params[drive_num];
                   12002:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   12003:                                
                   12004:                                switch(geo->MediaType) {
                   12005:                                case F3_720_512:
                   12006:                                case F5_720_512:
                   12007:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   12008:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   12009:                                        break;
                   12010:                                case F3_1Pt44_512:
                   12011:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   12012:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   12013:                                        break;
                   12014:                                case F3_2Pt88_512:
                   12015:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   12016:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   12017:                                        break;
                   12018:                                default:
                   12019:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   12020:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   12021:                                        break;
1.1.1.22  root     12022:                                }
                   12023:                        } else {
                   12024:                                REG16(AX) = 0x0f; // invalid drive
                   12025:                                m_CF = 1;
                   12026:                        }
                   12027:                } else if(REG8(CL) == 0x6a) {
                   12028:                        // unlock logical volume
                   12029:                } else if(REG8(CL) == 0x6b) {
                   12030:                        // unlock physical volume
1.1.1.48  root     12031: //             } else if(REG8(CL) == 0x6c) {
                   12032: //                     // get lock flag
                   12033: //             } else if(REG8(CL) == 0x6d) {
                   12034: //                     // enumerate open files
                   12035: //             } else if(REG8(CL) == 0x6e) {
                   12036: //                     // find swap file
                   12037: //             } else if(REG8(CL) == 0x6f) {
                   12038: //                     // get drive map information
                   12039: //             } else if(REG8(CL) == 0x70) {
                   12040: //                     // get current lock state
                   12041: //             } else if(REG8(CL) == 0x71) {
                   12042: //                     // get first cluster
1.1.1.22  root     12043:                } else {
                   12044:                        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));
                   12045:                        REG16(AX) = 0x01; // invalid function
                   12046:                        m_CF = 1;
                   12047:                }
                   12048:                break;
1.1.1.48  root     12049:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     12050:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12051:                        REG16(AX) = 0x0f; // invalid drive
                   12052:                        m_CF = 1;
                   12053:                } else {
                   12054:                        REG8(AL) = 0;
1.1.1.22  root     12055:                }
                   12056:                break;
1.1.1.48  root     12057:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     12058:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12059:                        REG16(AX) = 0x0f; // invalid drive
                   12060:                        m_CF = 1;
1.1.1.22  root     12061:                }
                   12062:                break;
1.1.1.48  root     12063:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     12064:                switch(REG8(CL)) {
                   12065:                case 0x45:
                   12066:                case 0x4a:
1.1.1.48  root     12067:                case 0x4c:
                   12068:                case 0x4d:
1.1.1.22  root     12069:                case 0x65:
                   12070:                case 0x6a:
1.1.1.48  root     12071:                case 0x6b:
1.1.1.22  root     12072:                case 0x7f:
                   12073:                        REG16(AX) = 0x0000; // supported
                   12074:                        break;
                   12075:                default:
                   12076:                        REG8(AL) = 0x01; // ioctl capability not available
                   12077:                        m_CF = 1;
                   12078:                        break;
                   12079:                }
                   12080:                break;
1.1.1.48  root     12081:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     12082:                switch(REG8(CL)) {
                   12083:                case 0x40:
                   12084:                case 0x46:
                   12085:                case 0x4a:
                   12086:                case 0x4b:
                   12087:                case 0x60:
                   12088:                case 0x66:
                   12089:                case 0x67:
                   12090:                case 0x68:
                   12091:                case 0x6a:
                   12092:                case 0x6b:
1.1.1.48  root     12093:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   12094:                                // CH = 00h     Unknown
                   12095:                                // CH = 01h     COMn:
                   12096:                                // CH = 03h     CON
                   12097:                                // CH = 05h     LPTn:
                   12098:                                REG16(AX) = 0x0000; // supported
                   12099:                                break;
                   12100:                        }
1.1.1.22  root     12101:                default:
                   12102:                        REG8(AL) = 0x01; // ioctl capability not available
                   12103:                        m_CF = 1;
                   12104:                        break;
                   12105:                }
                   12106:                break;
1.1.1.48  root     12107:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   12108:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   12109:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   12110:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   12111:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   12112:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   12113:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   12114:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   12115:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   12116:        case 0x59: // DR Multiuser DOS 5.0 - API
                   12117:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     12118:                m_CF = 1;
                   12119:                break;
1.1       root     12120:        default:
1.1.1.22  root     12121:                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     12122:                REG16(AX) = 0x01;
1.1.1.3   root     12123:                m_CF = 1;
1.1       root     12124:                break;
                   12125:        }
                   12126: }
                   12127: 
                   12128: inline void msdos_int_21h_45h()
                   12129: {
                   12130:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12131:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12132:        
1.1.1.20  root     12133:        if(fd < process->max_files && file_handler[fd].valid) {
                   12134:                int dup_fd = _dup(fd);
                   12135:                if(dup_fd != -1) {
                   12136:                        REG16(AX) = dup_fd;
                   12137:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12138: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12139:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12140:                } else {
                   12141:                        REG16(AX) = errno;
1.1.1.3   root     12142:                        m_CF = 1;
1.1       root     12143:                }
                   12144:        } else {
                   12145:                REG16(AX) = 0x06;
1.1.1.3   root     12146:                m_CF = 1;
1.1       root     12147:        }
                   12148: }
                   12149: 
                   12150: inline void msdos_int_21h_46h()
                   12151: {
                   12152:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12153:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12154:        int dup_fd = REG16(CX);
                   12155:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     12156:        
1.1.1.20  root     12157:        if(REG16(BX) == REG16(CX)) {
                   12158:                REG16(AX) = 0x06;
                   12159:                m_CF = 1;
                   12160:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   12161:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   12162:                        _close(tmp_fd);
                   12163:                        msdos_file_handler_close(tmp_fd);
                   12164:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   12165:                }
                   12166:                if(_dup2(fd, dup_fd) != -1) {
                   12167:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   12168: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   12169:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     12170:                } else {
                   12171:                        REG16(AX) = errno;
1.1.1.3   root     12172:                        m_CF = 1;
1.1       root     12173:                }
                   12174:        } else {
                   12175:                REG16(AX) = 0x06;
1.1.1.3   root     12176:                m_CF = 1;
1.1       root     12177:        }
                   12178: }
                   12179: 
                   12180: inline void msdos_int_21h_47h(int lfn)
                   12181: {
                   12182:        char path[MAX_PATH];
                   12183:        
                   12184:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     12185:                if(!lfn) {
                   12186:                        strcpy(path, msdos_short_path(path));
                   12187:                }
1.1       root     12188:                if(path[1] == ':') {
                   12189:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     12190:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     12191:                } else {
1.1.1.45  root     12192:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     12193:                }
                   12194:        } else {
                   12195:                REG16(AX) = errno;
1.1.1.3   root     12196:                m_CF = 1;
1.1       root     12197:        }
                   12198: }
                   12199: 
                   12200: inline void msdos_int_21h_48h()
                   12201: {
1.1.1.19  root     12202:        int seg, umb_linked;
1.1       root     12203:        
1.1.1.8   root     12204:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     12205:                // unlink umb not to allocate memory in umb
                   12206:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   12207:                        msdos_mem_unlink_umb();
                   12208:                }
1.1.1.8   root     12209:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12210:                        REG16(AX) = seg;
                   12211:                } else {
                   12212:                        REG16(AX) = 0x08;
                   12213:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   12214:                        m_CF = 1;
                   12215:                }
1.1.1.19  root     12216:                if(umb_linked != 0) {
                   12217:                        msdos_mem_link_umb();
                   12218:                }
1.1.1.8   root     12219:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   12220:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12221:                        REG16(AX) = seg;
                   12222:                } else {
                   12223:                        REG16(AX) = 0x08;
                   12224:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   12225:                        m_CF = 1;
                   12226:                }
                   12227:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   12228:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   12229:                        REG16(AX) = seg;
                   12230:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   12231:                        REG16(AX) = seg;
                   12232:                } else {
                   12233:                        REG16(AX) = 0x08;
                   12234:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   12235:                        m_CF = 1;
                   12236:                }
1.1       root     12237:        }
                   12238: }
                   12239: 
                   12240: inline void msdos_int_21h_49h()
                   12241: {
1.1.1.14  root     12242:        int mcb_seg = SREG(ES) - 1;
                   12243:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   12244:        
                   12245:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12246:                msdos_mem_free(SREG(ES));
                   12247:        } else {
1.1.1.33  root     12248:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     12249:                m_CF = 1;
                   12250:        }
1.1       root     12251: }
                   12252: 
                   12253: inline void msdos_int_21h_4ah()
                   12254: {
1.1.1.14  root     12255:        int mcb_seg = SREG(ES) - 1;
                   12256:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     12257:        int max_paragraphs;
                   12258:        
1.1.1.14  root     12259:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   12260:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   12261:                        REG16(AX) = 0x08;
                   12262:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   12263:                        m_CF = 1;
                   12264:                }
                   12265:        } else {
1.1.1.33  root     12266:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     12267:                m_CF = 1;
1.1       root     12268:        }
                   12269: }
                   12270: 
                   12271: inline void msdos_int_21h_4bh()
                   12272: {
1.1.1.3   root     12273:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12274:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     12275:        
                   12276:        switch(REG8(AL)) {
                   12277:        case 0x00:
                   12278:        case 0x01:
                   12279:                if(msdos_process_exec(command, param, REG8(AL))) {
                   12280:                        REG16(AX) = 0x02;
1.1.1.3   root     12281:                        m_CF = 1;
1.1       root     12282:                }
                   12283:                break;
1.1.1.14  root     12284:        case 0x03:
                   12285:                {
                   12286:                        int fd;
                   12287:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   12288:                                REG16(AX) = 0x02;
                   12289:                                m_CF = 1;
                   12290:                                break;
                   12291:                        }
                   12292:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   12293:                        _close(fd);
                   12294:                        
                   12295:                        UINT16 *overlay = (UINT16 *)param;
                   12296:                        
                   12297:                        // check exe header
                   12298:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   12299:                        int header_size = 0;
                   12300:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   12301:                                header_size = header->header_size * 16;
                   12302:                                // relocation
                   12303:                                int start_seg = overlay[1];
                   12304:                                for(int i = 0; i < header->relocations; i++) {
                   12305:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   12306:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   12307:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   12308:                                }
                   12309:                        }
                   12310:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   12311:                }
                   12312:                break;
1.1.1.48  root     12313:        case 0x04:
                   12314:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   12315: //     case 0x05:
                   12316: //             // DOS 5+ - Set Execution State
                   12317:        case 0x80:
                   12318:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   12319:        case 0xf0:
                   12320:        case 0xf1:
                   12321:                // DIET v1.10+
1.1.1.43  root     12322:        case 0xfd:
                   12323:        case 0xfe:
                   12324:                // unknown function called in FreeCOM
                   12325:                REG16(AX) = 0x01;
                   12326:                m_CF = 1;
                   12327:                break;
1.1       root     12328:        default:
1.1.1.22  root     12329:                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     12330:                REG16(AX) = 0x01;
1.1.1.3   root     12331:                m_CF = 1;
1.1       root     12332:                break;
                   12333:        }
                   12334: }
                   12335: 
                   12336: inline void msdos_int_21h_4ch()
                   12337: {
                   12338:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   12339: }
                   12340: 
                   12341: inline void msdos_int_21h_4dh()
                   12342: {
                   12343:        REG16(AX) = retval;
                   12344: }
                   12345: 
                   12346: inline void msdos_int_21h_4eh()
                   12347: {
                   12348:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12349:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12350:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     12351:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1.1.60  root     12352:        WIN32_FIND_DATAA fd;
1.1       root     12353:        
1.1.1.14  root     12354:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12355:        find->find_magic = FIND_MAGIC;
                   12356:        find->dta_index = dtainfo - dtalist;
1.1       root     12357:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12358:        dtainfo->allowable_mask = REG8(CL);
1.1.1.58  root     12359:        // note: SO1 dir command sets 0x3f, but only directories and volue label are found if bit3 is set :-(
                   12360:        if((dtainfo->allowable_mask & 0x3f) == 0x3f) {
                   12361:                dtainfo->allowable_mask &= ~0x08;
                   12362:        }
1.1.1.14  root     12363:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12364:        
1.1.1.14  root     12365:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12366:                dtainfo->allowable_mask &= ~8;
1.1       root     12367:        }
1.1.1.60  root     12368:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     12369:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12370:                      !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12371:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12372:                                FindClose(dtainfo->find_handle);
                   12373:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12374:                                break;
                   12375:                        }
                   12376:                }
                   12377:        }
1.1.1.13  root     12378:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12379:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12380:                msdos_find_file_conv_local_time(&fd);
                   12381:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12382:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12383:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12384:                REG16(AX) = 0;
1.1.1.14  root     12385:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12386:                find->attrib = 8;
                   12387:                find->size = 0;
                   12388:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12389:                dtainfo->allowable_mask &= ~8;
1.1       root     12390:                REG16(AX) = 0;
                   12391:        } else {
                   12392:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12393:                m_CF = 1;
1.1       root     12394:        }
                   12395: }
                   12396: 
                   12397: inline void msdos_int_21h_4fh()
                   12398: {
                   12399:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     12400:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   12401:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.60  root     12402:        WIN32_FIND_DATAA fd;
1.1       root     12403:        
1.1.1.14  root     12404:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   12405:                REG16(AX) = 0x12;
                   12406:                m_CF = 1;
                   12407:                return;
                   12408:        }
                   12409:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     12410:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     12411:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12412:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     12413:                              !msdos_find_file_has_8dot3name(&fd)) {
1.1.1.60  root     12414:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     12415:                                        FindClose(dtainfo->find_handle);
                   12416:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12417:                                        break;
                   12418:                                }
                   12419:                        }
                   12420:                } else {
1.1.1.13  root     12421:                        FindClose(dtainfo->find_handle);
                   12422:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12423:                }
                   12424:        }
1.1.1.13  root     12425:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12426:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   12427:                msdos_find_file_conv_local_time(&fd);
                   12428:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   12429:                find->size = fd.nFileSizeLow;
1.1.1.13  root     12430:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     12431:                REG16(AX) = 0;
1.1.1.14  root     12432:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12433:                find->attrib = 8;
                   12434:                find->size = 0;
                   12435:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12436:                dtainfo->allowable_mask &= ~8;
1.1       root     12437:                REG16(AX) = 0;
                   12438:        } else {
                   12439:                REG16(AX) = 0x12;
1.1.1.3   root     12440:                m_CF = 1;
1.1       root     12441:        }
                   12442: }
                   12443: 
                   12444: inline void msdos_int_21h_50h()
                   12445: {
1.1.1.8   root     12446:        if(current_psp != REG16(BX)) {
                   12447:                process_t *process = msdos_process_info_get(current_psp);
                   12448:                if(process != NULL) {
                   12449:                        process->psp = REG16(BX);
                   12450:                }
                   12451:                current_psp = REG16(BX);
1.1.1.23  root     12452:                msdos_sda_update(current_psp);
1.1.1.8   root     12453:        }
1.1       root     12454: }
                   12455: 
                   12456: inline void msdos_int_21h_51h()
                   12457: {
                   12458:        REG16(BX) = current_psp;
                   12459: }
                   12460: 
                   12461: inline void msdos_int_21h_52h()
                   12462: {
1.1.1.25  root     12463:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     12464:        i386_load_segment_descriptor(ES);
1.1.1.25  root     12465:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     12466: }
                   12467: 
1.1.1.43  root     12468: inline void msdos_int_21h_53h()
                   12469: {
                   12470:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   12471:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   12472:        
                   12473:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   12474:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   12475:        dpb->shift_count = 0;
                   12476:        dpb->reserved_sectors = 0;
                   12477:        dpb->fat_num = bpb->fat_num;
                   12478:        dpb->root_entries = bpb->root_entries;
                   12479:        dpb->first_data_sector = 0;
                   12480:        if(bpb->sectors_per_cluster != 0) {
                   12481:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   12482:        } else {
                   12483:                dpb->highest_cluster_num = 0;
                   12484:        }
                   12485:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   12486:        dpb->first_dir_sector = 0;
                   12487:        dpb->device_driver_header = 0;
                   12488:        dpb->media_type = bpb->media_type;
                   12489:        dpb->drive_accessed = 0;
                   12490:        dpb->next_dpb_ofs = 0xffff;
                   12491:        dpb->next_dpb_seg = 0xffff;
                   12492:        dpb->first_free_cluster = 0;
                   12493:        dpb->free_clusters = 0xffff;
                   12494: }
                   12495: 
1.1       root     12496: inline void msdos_int_21h_54h()
                   12497: {
                   12498:        process_t *process = msdos_process_info_get(current_psp);
                   12499:        
                   12500:        REG8(AL) = process->verify;
                   12501: }
                   12502: 
                   12503: inline void msdos_int_21h_55h()
                   12504: {
                   12505:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   12506:        
                   12507:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   12508:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   12509:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   12510:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   12511:        psp->parent_psp = current_psp;
                   12512: }
                   12513: 
                   12514: inline void msdos_int_21h_56h(int lfn)
                   12515: {
                   12516:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     12517:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   12518:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     12519:        
1.1.1.63  root     12520:        if(msdos_is_existing_file(dst) || msdos_is_existing_dir(dst)) {
                   12521:                REG16(AX) = 0x05; // access denied
                   12522:                m_CF = 1;
                   12523:        } else if(rename(src, dst)) {
1.1       root     12524:                REG16(AX) = errno;
1.1.1.3   root     12525:                m_CF = 1;
1.1       root     12526:        }
                   12527: }
                   12528: 
                   12529: inline void msdos_int_21h_57h()
                   12530: {
                   12531:        FILETIME time, local;
1.1.1.14  root     12532:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     12533:        HANDLE hHandle;
1.1       root     12534:        
1.1.1.21  root     12535:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     12536:                REG16(AX) = (UINT16)GetLastError();
                   12537:                m_CF = 1;
                   12538:                return;
                   12539:        }
                   12540:        ctime = atime = mtime = NULL;
                   12541:        
1.1       root     12542:        switch(REG8(AL)) {
                   12543:        case 0x00:
1.1.1.6   root     12544:        case 0x01:
1.1.1.14  root     12545:                mtime = &time;
1.1.1.6   root     12546:                break;
1.1.1.65  root     12547: //     case 0x02: // DOS 4.x only - Get Extended Attributes For File
                   12548: //     case 0x03: // DOS 4.x only - Get Extended Attribute Properties
                   12549: //             break;
1.1.1.6   root     12550:        case 0x04:
                   12551:        case 0x05:
1.1.1.14  root     12552:                atime = &time;
1.1       root     12553:                break;
1.1.1.6   root     12554:        case 0x06:
                   12555:        case 0x07:
1.1.1.14  root     12556:                ctime = &time;
                   12557:                break;
                   12558:        default:
1.1.1.22  root     12559:                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     12560:                REG16(AX) = 0x01;
                   12561:                m_CF = 1;
                   12562:                return;
                   12563:        }
                   12564:        if(REG8(AL) & 1) {
1.1       root     12565:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   12566:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     12567:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     12568:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12569:                        m_CF = 1;
1.1       root     12570:                }
1.1.1.14  root     12571:        } else {
1.1.1.21  root     12572:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     12573:                        // assume a device and use the current time
                   12574:                        GetSystemTimeAsFileTime(&time);
                   12575:                }
                   12576:                FileTimeToLocalFileTime(&time, &local);
                   12577:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     12578:        }
                   12579: }
                   12580: 
                   12581: inline void msdos_int_21h_58h()
                   12582: {
                   12583:        switch(REG8(AL)) {
                   12584:        case 0x00:
1.1.1.7   root     12585:                REG16(AX) = malloc_strategy;
                   12586:                break;
                   12587:        case 0x01:
1.1.1.24  root     12588: //             switch(REG16(BX)) {
                   12589:                switch(REG8(BL)) {
1.1.1.7   root     12590:                case 0x0000:
                   12591:                case 0x0001:
                   12592:                case 0x0002:
                   12593:                case 0x0040:
                   12594:                case 0x0041:
                   12595:                case 0x0042:
                   12596:                case 0x0080:
                   12597:                case 0x0081:
                   12598:                case 0x0082:
                   12599:                        malloc_strategy = REG16(BX);
1.1.1.23  root     12600:                        msdos_sda_update(current_psp);
1.1.1.7   root     12601:                        break;
                   12602:                default:
1.1.1.22  root     12603:                        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     12604:                        REG16(AX) = 0x01;
                   12605:                        m_CF = 1;
                   12606:                        break;
                   12607:                }
                   12608:                break;
                   12609:        case 0x02:
1.1.1.19  root     12610:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     12611:                break;
                   12612:        case 0x03:
1.1.1.24  root     12613: //             switch(REG16(BX)) {
                   12614:                switch(REG8(BL)) {
1.1.1.7   root     12615:                case 0x0000:
1.1.1.19  root     12616:                        msdos_mem_unlink_umb();
                   12617:                        break;
1.1.1.7   root     12618:                case 0x0001:
1.1.1.19  root     12619:                        msdos_mem_link_umb();
1.1.1.7   root     12620:                        break;
                   12621:                default:
1.1.1.22  root     12622:                        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     12623:                        REG16(AX) = 0x01;
                   12624:                        m_CF = 1;
                   12625:                        break;
                   12626:                }
1.1       root     12627:                break;
                   12628:        default:
1.1.1.22  root     12629:                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     12630:                REG16(AX) = 0x01;
1.1.1.3   root     12631:                m_CF = 1;
1.1       root     12632:                break;
                   12633:        }
                   12634: }
                   12635: 
                   12636: inline void msdos_int_21h_59h()
                   12637: {
1.1.1.47  root     12638:        if(REG16(BX) == 0x0000) {
                   12639:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12640:                
                   12641:                REG16(AX) = sda->extended_error_code;
                   12642:                REG8(BH)  = sda->error_class;
                   12643:                REG8(BL)  = sda->suggested_action;
                   12644:                REG8(CH)  = sda->locus_of_last_error;
                   12645:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   12646:                if(sda->int21h_5d0ah_called != 0) {
                   12647:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   12648:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   12649: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   12650:                        REG16(DI) = sda->last_error_pointer.w.l;
                   12651: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   12652: //                     i386_load_segment_descriptor(DS);
                   12653:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   12654:                        i386_load_segment_descriptor(ES);
                   12655:                }
                   12656:                sda->int21h_5d0ah_called = 0;
                   12657: //     } else if(REG16(BX) == 0x0001) {
                   12658: //             // European MS-DOS 4.0 - Get Hard Error Information
                   12659:        } else {
                   12660:                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));
                   12661:                REG16(AX) = 0x01;
                   12662:                m_CF = 1;
                   12663:        }
1.1       root     12664: }
                   12665: 
                   12666: inline void msdos_int_21h_5ah()
                   12667: {
1.1.1.3   root     12668:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12669:        int len = strlen(path);
                   12670:        char tmp[MAX_PATH];
                   12671:        
1.1.1.60  root     12672:        if(GetTempFileNameA(path, "TMP", 0, tmp)) {
1.1       root     12673:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12674:                
1.1.1.60  root     12675:                SetFileAttributesA(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12676:                REG16(AX) = fd;
                   12677:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12678:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12679:                
                   12680:                strcpy(path, tmp);
                   12681:                int dx = REG16(DX) + len;
1.1.1.3   root     12682:                int ds = SREG(DS);
1.1       root     12683:                while(dx > 0xffff) {
                   12684:                        dx -= 0x10;
                   12685:                        ds++;
                   12686:                }
                   12687:                REG16(DX) = dx;
1.1.1.3   root     12688:                SREG(DS) = ds;
                   12689:                i386_load_segment_descriptor(DS);
1.1       root     12690:        } else {
                   12691:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12692:                m_CF = 1;
1.1       root     12693:        }
                   12694: }
                   12695: 
                   12696: inline void msdos_int_21h_5bh()
                   12697: {
1.1.1.45  root     12698:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     12699:        
1.1.1.45  root     12700:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12701:                // already exists
                   12702:                REG16(AX) = 0x50;
1.1.1.3   root     12703:                m_CF = 1;
1.1       root     12704:        } else {
                   12705:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12706:                
                   12707:                if(fd != -1) {
1.1.1.60  root     12708:                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     12709:                        REG16(AX) = fd;
                   12710:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12711:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12712:                } else {
                   12713:                        REG16(AX) = errno;
1.1.1.3   root     12714:                        m_CF = 1;
1.1       root     12715:                }
                   12716:        }
                   12717: }
                   12718: 
                   12719: inline void msdos_int_21h_5ch()
                   12720: {
                   12721:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12722:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12723:        
1.1.1.20  root     12724:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     12725:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     12726:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     12727:                        UINT32 pos = _tell(fd);
                   12728:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   12729:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     12730:                                REG16(AX) = errno;
1.1.1.3   root     12731:                                m_CF = 1;
1.1       root     12732:                        }
1.1.1.20  root     12733:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     12734:                        
1.1       root     12735:                        // some seconds may be passed in _locking()
1.1.1.35  root     12736:                        REQUEST_HARDWRE_UPDATE();
1.1       root     12737:                } else {
                   12738:                        REG16(AX) = 0x01;
1.1.1.3   root     12739:                        m_CF = 1;
1.1       root     12740:                }
                   12741:        } else {
                   12742:                REG16(AX) = 0x06;
1.1.1.3   root     12743:                m_CF = 1;
1.1       root     12744:        }
                   12745: }
                   12746: 
1.1.1.22  root     12747: inline void msdos_int_21h_5dh()
                   12748: {
                   12749:        switch(REG8(AL)) {
1.1.1.65  root     12750:        case 0x00: // DOS 3.1+ internal - Server Function Call
1.1.1.45  root     12751:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   12752:                        // current system
                   12753:                        static bool reenter = false;
                   12754:                        if(!reenter) {
                   12755:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   12756:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   12757:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   12758:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12759:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12760:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12761:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12762:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12763:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12764:                                i386_load_segment_descriptor(DS);
                   12765:                                i386_load_segment_descriptor(ES);
                   12766:                                reenter = true;
                   12767:                                try {
                   12768:                                        msdos_syscall(0x21);
                   12769:                                } catch(...) {
                   12770:                                }
                   12771:                                reenter = false;
                   12772:                        }
                   12773:                } else {
                   12774:                        REG16(AX) = 0x49; //  network software not installed
                   12775:                        m_CF = 1;
                   12776:                }
                   12777:                break;
1.1.1.65  root     12778: //     case 0x01: // DOS 3.1+ internal - Commit All Files For Specified Computer/Process
                   12779: //     case 0x02: // DOS 3.1+ internal - SHARE.EXE - Close File By Name
                   12780: //     case 0x03: // DOS 3.1+ internal - SHARE.EXE - Close ALL Files For Given Computer
                   12781: //     case 0x04: // DOS 3.1+ internal - SHARE.EXE - Close ALL Files For Given Process
                   12782: //     case 0x05: // DOS 3.1+ internal - SHARE.EXE - Get Open File List Entry
                   12783:        case 0x06: // DOS 3.0+ internal - Get Address Of DOS Swappable Data Area
1.1.1.23  root     12784:                SREG(DS) = (SDA_TOP >> 4);
                   12785:                i386_load_segment_descriptor(DS);
                   12786:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12787:                REG16(CX) = 0x80;
                   12788:                REG16(DX) = 0x1a;
                   12789:                break;
1.1.1.65  root     12790:        case 0x07: // DOS 3.1+ network - Get Redirected Printer Mode
                   12791:        case 0x08: // DOS 3.1+ network - Set Redirected Printer Mode
                   12792:        case 0x09: // DOS 3.1+ network - Flush Redirected Printer Output
1.1.1.45  root     12793:                REG16(AX) = 0x49; //  network software not installed
                   12794:                m_CF = 1;
                   12795:                break;
1.1.1.65  root     12796:        case 0x0a: // DOS 3.1+ - Set Extended Error Information
1.1.1.43  root     12797:                {
                   12798:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12799:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12800:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12801:                        // XXX: which one is correct ???
                   12802: #if 1
                   12803:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12804:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12805:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12806:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12807:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12808: #else
                   12809:                        // PC DOS 7 Technical Update
                   12810:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12811:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12812:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12813:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12814: #endif
                   12815:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12816: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12817:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12818: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12819:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12820:                }
                   12821:                break;
1.1.1.65  root     12822:        case 0x0b: // DOS 4.x only - internal - Get DOS Swappable Data Areas
1.1.1.22  root     12823:                REG16(AX) = 0x01;
                   12824:                m_CF = 1;
                   12825:                break;
                   12826:        default:
                   12827:                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));
                   12828:                REG16(AX) = 0x01;
                   12829:                m_CF = 1;
                   12830:                break;
                   12831:        }
                   12832: }
                   12833: 
1.1.1.42  root     12834: inline void msdos_int_21h_5eh()
                   12835: {
                   12836:        switch(REG8(AL)) {
1.1.1.65  root     12837:        case 0x00: // DOS 3.1+ network - Get Machine Name
1.1.1.42  root     12838:                {
                   12839:                        char name[256] = {0};
                   12840:                        DWORD dwSize = 256;
                   12841:                        
1.1.1.60  root     12842:                        if(GetComputerNameA(name, &dwSize)) {
1.1.1.42  root     12843:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12844:                                for(int i = 0; i < 15; i++) {
                   12845:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12846:                                }
                   12847:                                dest[15] = '\0';
                   12848:                                REG8(CH) = 0x01; // nonzero valid
                   12849:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12850:                        } else {
                   12851:                                REG16(AX) = 0x01;
                   12852:                                m_CF = 1;
                   12853:                        }
                   12854:                }
                   12855:                break;
1.1.1.65  root     12856: //     case 0x01: // DOS 3.1+ network - Set Machine Name
                   12857: //     case 0x02: // DOS 3.1+ network - Set Network Printer Setup String
                   12858: //     case 0x03: // DOS 3.1+ network - Get Network Printer Setup String
                   12859: //     case 0x04: // DOS 3.1+ network - Set Printer Mode
                   12860: //     case 0x05: // DOS 3.1+ network - Get Printer Mode
1.1.1.42  root     12861:        default:
1.1.1.45  root     12862: //             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));
                   12863: //             REG16(AX) = 0x01;
                   12864:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12865:                m_CF = 1;
                   12866:                break;
                   12867:        }
                   12868: }
                   12869: 
1.1.1.30  root     12870: inline void msdos_int_21h_5fh()
                   12871: {
                   12872:        switch(REG8(AL)) {
1.1.1.65  root     12873: //     case 0x00: // DOS 3.1+ network - Get Redirection Mode
                   12874: //     case 0x01: // DOS 3.1+ network - Set Redirection Mode
                   12875:        case 0x05: // DOS 4.0+ network - Get Extended Redirection List Entry
1.1.1.44  root     12876:                REG16(BP) = 0;
                   12877:                for(int i = 0; i < 26; i++) {
                   12878:                        if(msdos_is_remote_drive(i)) {
                   12879:                                REG16(BP)++;
1.1.1.42  root     12880:                        }
                   12881:                }
1.1.1.65  root     12882:        case 0x02: // DOS 3.1+ network - Get Redirection List Entry
1.1.1.44  root     12883:                for(int i = 0, index = 0; i < 26; i++) {
                   12884:                        if(msdos_is_remote_drive(i)) {
                   12885:                                if(index == REG16(BX)) {
                   12886:                                        char volume[] = "A:";
1.1.1.30  root     12887:                                        volume[0] = 'A' + i;
1.1.1.44  root     12888:                                        DWORD dwSize = 128;
                   12889:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
1.1.1.60  root     12890:                                        WNetGetConnectionA(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
1.1.1.44  root     12891:                                        REG8(BH) = 0x00; // valid
                   12892:                                        REG8(BL) = 0x04; // disk drive
                   12893:                                        REG16(CX) = 0x00; // LANtastic
                   12894:                                        return;
1.1.1.30  root     12895:                                }
1.1.1.44  root     12896:                                index++;
1.1.1.30  root     12897:                        }
                   12898:                }
                   12899:                REG16(AX) = 0x12; // no more files
                   12900:                m_CF = 1;
                   12901:                break;
1.1.1.65  root     12902: //     case 0x03: // DOS 3.1+ network - Redirect Device
                   12903: //     case 0x04: // DOS 3.1+ network - Cancel Redirection
                   12904: //     case 0x06: // Network - Get Full Redirection List
                   12905:        case 0x07: // DOS 5+ - Enable Drive
1.1.1.44  root     12906:                if(msdos_is_valid_drive(REG8(DL))) {
                   12907:                        msdos_cds_update(REG8(DL));
                   12908:                } else {
                   12909:                        REG16(AX) = 0x0f; // invalid drive
                   12910:                        m_CF = 1;
                   12911:                }
                   12912:                break;
1.1.1.65  root     12913:        case 0x08: // DOS 5+ - Disable Drive
1.1.1.44  root     12914:                if(msdos_is_valid_drive(REG8(DL))) {
                   12915:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12916:                        cds->drive_attrib = 0x0000;
                   12917:                } else {
                   12918:                        REG16(AX) = 0x0f; // invalid drive
                   12919:                        m_CF = 1;
                   12920:                }
                   12921:                break;
1.1.1.30  root     12922:        default:
1.1.1.45  root     12923: //             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));
                   12924: //             REG16(AX) = 0x01;
                   12925:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12926:                m_CF = 1;
                   12927:                break;
                   12928:        }
                   12929: }
                   12930: 
1.1       root     12931: inline void msdos_int_21h_60h(int lfn)
                   12932: {
1.1.1.45  root     12933:        char full[MAX_PATH];
                   12934:        const char *path = NULL;
1.1.1.14  root     12935:        
1.1       root     12936:        if(lfn) {
1.1.1.14  root     12937:                char *name;
                   12938:                *full = '\0';
1.1.1.60  root     12939:                GetFullPathNameA((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12940:                switch(REG8(CL)) {
                   12941:                case 1:
1.1.1.60  root     12942:                        GetShortPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12943:                        my_strupr(full);
                   12944:                        break;
                   12945:                case 2:
1.1.1.60  root     12946:                        GetLongPathNameA(full, full, MAX_PATH);
1.1.1.14  root     12947:                        break;
                   12948:                }
                   12949:                path = full;
                   12950:        } else {
                   12951:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12952:        }
                   12953:        if(*path != '\0') {
                   12954:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12955:        } else {
1.1.1.14  root     12956:                REG16(AX) = (UINT16)GetLastError();
                   12957:                m_CF = 1;
1.1       root     12958:        }
                   12959: }
                   12960: 
                   12961: inline void msdos_int_21h_61h()
                   12962: {
                   12963:        REG8(AL) = 0;
                   12964: }
                   12965: 
                   12966: inline void msdos_int_21h_62h()
                   12967: {
                   12968:        REG16(BX) = current_psp;
                   12969: }
                   12970: 
                   12971: inline void msdos_int_21h_63h()
                   12972: {
                   12973:        switch(REG8(AL)) {
                   12974:        case 0x00:
1.1.1.3   root     12975:                SREG(DS) = (DBCS_TABLE >> 4);
                   12976:                i386_load_segment_descriptor(DS);
1.1       root     12977:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12978:                REG8(AL) = 0x00;
                   12979:                break;
1.1.1.22  root     12980:        case 0x01: // set korean input mode
                   12981:        case 0x02: // get korean input mode
                   12982:                REG8(AL) = 0xff; // not supported
                   12983:                break;
1.1       root     12984:        default:
1.1.1.22  root     12985:                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     12986:                REG16(AX) = 0x01;
1.1.1.3   root     12987:                m_CF = 1;
1.1       root     12988:                break;
                   12989:        }
                   12990: }
                   12991: 
1.1.1.25  root     12992: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12993: {
1.1.1.25  root     12994:        switch(func) {
1.1.1.17  root     12995:        case 0x01:
                   12996:                if(REG16(CX) >= 5) {
1.1.1.19  root     12997:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12998:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12999:                                REG16(CX) = sizeof(data);
                   13000:                        ZeroMemory(data, sizeof(data));
                   13001:                        data[0] = 0x01;
                   13002:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     13003:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     13004:                        *(UINT16 *)(data + 5) = active_code_page;
                   13005:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     13006: //                     REG16(AX) = active_code_page;
1.1.1.17  root     13007:                } else {
1.1.1.25  root     13008:                        return(0x08); // insufficient memory
1.1.1.17  root     13009:                }
                   13010:                break;
                   13011:        case 0x02:
                   13012:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   13013:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   13014:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     13015: //             REG16(AX) = active_code_page;
1.1.1.17  root     13016:                REG16(CX) = 0x05;
                   13017:                break;
1.1.1.23  root     13018:        case 0x03:
                   13019:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   13020:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   13021:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     13022: //             REG16(AX) = active_code_page;
1.1.1.23  root     13023:                REG16(CX) = 0x05;
                   13024:                break;
1.1.1.17  root     13025:        case 0x04:
                   13026:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   13027:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   13028:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     13029: //             REG16(AX) = active_code_page;
1.1.1.17  root     13030:                REG16(CX) = 0x05;
                   13031:                break;
                   13032:        case 0x05:
                   13033:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   13034:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   13035:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     13036: //             REG16(AX) = active_code_page;
1.1.1.17  root     13037:                REG16(CX) = 0x05;
                   13038:                break;
                   13039:        case 0x06:
                   13040:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   13041:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   13042:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     13043: //             REG16(AX) = active_code_page;
1.1.1.17  root     13044:                REG16(CX) = 0x05;
                   13045:                break;
1.1       root     13046:        case 0x07:
1.1.1.3   root     13047:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   13048:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   13049:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     13050: //             REG16(AX) = active_code_page;
1.1       root     13051:                REG16(CX) = 0x05;
                   13052:                break;
1.1.1.25  root     13053:        default:
                   13054:                return(0x01); // function number invalid
                   13055:        }
                   13056:        return(0x00);
                   13057: }
                   13058: 
                   13059: inline void msdos_int_21h_65h()
                   13060: {
                   13061:        char tmp[0x10000];
                   13062:        
                   13063:        switch(REG8(AL)) {
1.1.1.43  root     13064:        case 0x00:
                   13065:                if(REG16(CX) >= 7) {
                   13066:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   13067:                        REG16(AX) = system_code_page;
                   13068:                } else {
                   13069:                        REG16(AX) = 0x0c;
                   13070:                        m_CF = 1;
                   13071:                }
                   13072:                break;
1.1.1.25  root     13073:        case 0x01:
                   13074:        case 0x02:
                   13075:        case 0x03:
                   13076:        case 0x04:
                   13077:        case 0x05:
                   13078:        case 0x06:
                   13079:        case 0x07:
                   13080:                {
                   13081:                        UINT16 result = get_extended_country_info(REG8(AL));
                   13082:                        if(result) {
                   13083:                                REG16(AX) = result;
                   13084:                                m_CF = 1;
                   13085:                        } else {
                   13086:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   13087:                        }
                   13088:                }
                   13089:                break;
1.1       root     13090:        case 0x20:
1.1.1.25  root     13091:        case 0xa0:
1.1.1.19  root     13092:                memset(tmp, 0, sizeof(tmp));
                   13093:                tmp[0] = REG8(DL);
1.1       root     13094:                my_strupr(tmp);
                   13095:                REG8(DL) = tmp[0];
                   13096:                break;
                   13097:        case 0x21:
1.1.1.25  root     13098:        case 0xa1:
1.1       root     13099:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13100:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     13101:                my_strupr(tmp);
1.1.1.3   root     13102:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     13103:                break;
                   13104:        case 0x22:
1.1.1.25  root     13105:        case 0xa2:
1.1.1.3   root     13106:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     13107:                break;
1.1.1.25  root     13108:        case 0x23:
                   13109:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     13110:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     13111:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     13112:                        REG16(AX) = 0x00;
                   13113:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   13114:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     13115:                        REG16(AX) = 0x01;
                   13116:                } else {
                   13117:                        REG16(AX) = 0x02;
                   13118:                }
                   13119:                break;
1.1       root     13120:        default:
1.1.1.22  root     13121:                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     13122:                REG16(AX) = 0x01;
1.1.1.3   root     13123:                m_CF = 1;
1.1       root     13124:                break;
                   13125:        }
                   13126: }
                   13127: 
                   13128: inline void msdos_int_21h_66h()
                   13129: {
                   13130:        switch(REG8(AL)) {
                   13131:        case 0x01:
                   13132:                REG16(BX) = active_code_page;
                   13133:                REG16(DX) = system_code_page;
                   13134:                break;
                   13135:        case 0x02:
                   13136:                if(active_code_page == REG16(BX)) {
                   13137:                        REG16(AX) = 0xeb41;
                   13138:                } else if(_setmbcp(REG16(BX)) == 0) {
                   13139:                        active_code_page = REG16(BX);
1.1.1.17  root     13140:                        msdos_nls_tables_update();
1.1       root     13141:                        REG16(AX) = 0xeb41;
1.1.1.32  root     13142:                        SetConsoleCP(active_code_page);
                   13143:                        SetConsoleOutputCP(active_code_page);
1.1       root     13144:                } else {
                   13145:                        REG16(AX) = 0x25;
1.1.1.3   root     13146:                        m_CF = 1;
1.1       root     13147:                }
                   13148:                break;
                   13149:        default:
1.1.1.22  root     13150:                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     13151:                REG16(AX) = 0x01;
1.1.1.3   root     13152:                m_CF = 1;
1.1       root     13153:                break;
                   13154:        }
                   13155: }
                   13156: 
                   13157: inline void msdos_int_21h_67h()
                   13158: {
                   13159:        process_t *process = msdos_process_info_get(current_psp);
                   13160:        
                   13161:        if(REG16(BX) <= MAX_FILES) {
                   13162:                process->max_files = max(REG16(BX), 20);
                   13163:        } else {
                   13164:                REG16(AX) = 0x08;
1.1.1.3   root     13165:                m_CF = 1;
1.1       root     13166:        }
                   13167: }
                   13168: 
                   13169: inline void msdos_int_21h_68h()
                   13170: {
                   13171:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13172:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     13173:        
1.1.1.20  root     13174:        if(fd < process->max_files && file_handler[fd].valid) {
                   13175:                // fflush(_fdopen(fd, ""));
1.1       root     13176:        } else {
                   13177:                REG16(AX) = 0x06;
1.1.1.3   root     13178:                m_CF = 1;
1.1       root     13179:        }
                   13180: }
                   13181: 
                   13182: inline void msdos_int_21h_69h()
                   13183: {
1.1.1.3   root     13184:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13185:        char path[] = "A:\\";
                   13186:        char volume_label[MAX_PATH];
                   13187:        DWORD serial_number = 0;
                   13188:        char file_system[MAX_PATH];
                   13189:        
                   13190:        if(REG8(BL) == 0) {
                   13191:                path[0] = 'A' + _getdrive() - 1;
                   13192:        } else {
                   13193:                path[0] = 'A' + REG8(BL) - 1;
                   13194:        }
                   13195:        
                   13196:        switch(REG8(AL)) {
                   13197:        case 0x00:
1.1.1.60  root     13198:                if(GetVolumeInformationA(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
1.1       root     13199:                        info->info_level = 0;
                   13200:                        info->serial_number = serial_number;
                   13201:                        memset(info->volume_label, 0x20, 11);
                   13202:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   13203:                        memset(info->file_system, 0x20, 8);
                   13204:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   13205:                } else {
                   13206:                        REG16(AX) = errno;
1.1.1.3   root     13207:                        m_CF = 1;
1.1       root     13208:                }
                   13209:                break;
                   13210:        case 0x01:
                   13211:                REG16(AX) = 0x03;
1.1.1.3   root     13212:                m_CF = 1;
1.1.1.45  root     13213:                break;
                   13214:        default:
                   13215:                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));
                   13216:                REG16(AX) = 0x01;
                   13217:                m_CF = 1;
                   13218:                break;
1.1       root     13219:        }
                   13220: }
                   13221: 
                   13222: inline void msdos_int_21h_6ah()
                   13223: {
                   13224:        REG8(AH) = 0x68;
                   13225:        msdos_int_21h_68h();
                   13226: }
                   13227: 
                   13228: inline void msdos_int_21h_6bh()
                   13229: {
1.1.1.45  root     13230:        REG8(AL) = 0x00;
1.1       root     13231: }
                   13232: 
                   13233: inline void msdos_int_21h_6ch(int lfn)
                   13234: {
1.1.1.45  root     13235:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     13236:        int mode = REG8(BL) & 0x03;
                   13237:        
                   13238:        if(mode < 0x03) {
1.1.1.29  root     13239:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     13240:                        // file exists
                   13241:                        if(REG8(DL) & 1) {
1.1.1.37  root     13242:                                int fd = -1;
                   13243:                                int sio_port = 0;
                   13244:                                int lpt_port = 0;
1.1       root     13245:                                
1.1.1.45  root     13246:                                if(msdos_is_device_path(path)) {
                   13247:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     13248:                                } else {
1.1.1.13  root     13249:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     13250:                                }
1.1       root     13251:                                if(fd != -1) {
                   13252:                                        REG16(AX) = fd;
                   13253:                                        REG16(CX) = 1;
1.1.1.45  root     13254:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13255:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13256:                                } else {
                   13257:                                        REG16(AX) = errno;
1.1.1.3   root     13258:                                        m_CF = 1;
1.1       root     13259:                                }
                   13260:                        } else if(REG8(DL) & 2) {
1.1.1.60  root     13261:                                int attr = GetFileAttributesA(path);
1.1.1.37  root     13262:                                int fd = -1;
                   13263:                                int sio_port = 0;
                   13264:                                int lpt_port = 0;
1.1       root     13265:                                
1.1.1.45  root     13266:                                if(msdos_is_device_path(path)) {
                   13267:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     13268:                                } else {
                   13269:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13270:                                }
                   13271:                                if(fd != -1) {
                   13272:                                        if(attr == -1) {
                   13273:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   13274:                                        }
1.1.1.60  root     13275:                                        SetFileAttributesA(path, attr);
1.1       root     13276:                                        REG16(AX) = fd;
                   13277:                                        REG16(CX) = 3;
1.1.1.45  root     13278:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     13279:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13280:                                } else {
                   13281:                                        REG16(AX) = errno;
1.1.1.3   root     13282:                                        m_CF = 1;
1.1       root     13283:                                }
                   13284:                        } else {
                   13285:                                REG16(AX) = 0x50;
1.1.1.3   root     13286:                                m_CF = 1;
1.1       root     13287:                        }
                   13288:                } else {
                   13289:                        // file not exists
                   13290:                        if(REG8(DL) & 0x10) {
                   13291:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   13292:                                
                   13293:                                if(fd != -1) {
1.1.1.60  root     13294:                                        SetFileAttributesA(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
1.1       root     13295:                                        REG16(AX) = fd;
                   13296:                                        REG16(CX) = 2;
                   13297:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     13298:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     13299:                                } else {
                   13300:                                        REG16(AX) = errno;
1.1.1.3   root     13301:                                        m_CF = 1;
1.1       root     13302:                                }
                   13303:                        } else {
                   13304:                                REG16(AX) = 0x02;
1.1.1.3   root     13305:                                m_CF = 1;
1.1       root     13306:                        }
                   13307:                }
                   13308:        } else {
                   13309:                REG16(AX) = 0x0c;
1.1.1.3   root     13310:                m_CF = 1;
1.1       root     13311:        }
                   13312: }
                   13313: 
1.1.1.43  root     13314: inline void msdos_int_21h_70h()
                   13315: {
                   13316:        switch(REG8(AL)) {
1.1.1.48  root     13317:        case 0x00: // get ??? info
                   13318:        case 0x01: // set above info
                   13319: //             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));
                   13320:                REG16(AX) = 0x7000;
                   13321:                m_CF = 1;
                   13322:                break;
                   13323:        case 0x02: // set general internationalization info
1.1.1.43  root     13324:                if(REG16(CX) >= 7) {
                   13325:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   13326:                        msdos_nls_tables_update();
                   13327:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   13328:                        REG16(AX) = system_code_page;
                   13329:                } else {
                   13330:                        REG16(AX) = 0x0c;
                   13331:                        m_CF = 1;
                   13332:                }
                   13333:                break;
                   13334:        default:
                   13335:                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     13336:                REG16(AX) = 0x7000;
1.1.1.43  root     13337:                m_CF = 1;
                   13338:                break;
                   13339:        }
                   13340: }
                   13341: 
1.1       root     13342: inline void msdos_int_21h_710dh()
                   13343: {
                   13344:        // reset drive
                   13345: }
                   13346: 
1.1.1.48  root     13347: inline void msdos_int_21h_7141h()
1.1.1.17  root     13348: {
                   13349:        if(REG16(SI) == 0) {
1.1.1.48  root     13350:                msdos_int_21h_41h(1);
1.1.1.17  root     13351:                return;
                   13352:        }
                   13353:        if(REG16(SI) != 1) {
                   13354:                REG16(AX) = 5;
                   13355:                m_CF = 1;
                   13356:        }
                   13357:        /* wild card and matching attributes... */
                   13358:        char tmp[MAX_PATH * 2];
                   13359:        // copy search pathname (and quick check overrun)
                   13360:        ZeroMemory(tmp, sizeof(tmp));
                   13361:        tmp[MAX_PATH - 1] = '\0';
                   13362:        tmp[MAX_PATH] = 1;
                   13363:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   13364:        
                   13365:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   13366:                REG16(AX) = 1;
                   13367:                m_CF = 1;
                   13368:                return;
                   13369:        }
                   13370:        for(char *s = tmp; *s; ++s) {
                   13371:                if(*s == '/') {
                   13372:                        *s = '\\';
                   13373:                }
                   13374:        }
1.1.1.60  root     13375:        char *tmp_name = my_strrchr(tmp, '\\');
1.1.1.17  root     13376:        if(tmp_name) {
                   13377:                ++tmp_name;
                   13378:        } else {
                   13379:                tmp_name = strchr(tmp, ':');
                   13380:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   13381:        }
                   13382:        
                   13383:        WIN32_FIND_DATAA fd;
                   13384:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   13385:        if(fh == INVALID_HANDLE_VALUE) {
                   13386:                REG16(AX) = 2;
                   13387:                m_CF = 1;
                   13388:                return;
                   13389:        }
                   13390:        do {
                   13391:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   13392:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     13393:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     13394:                                REG16(AX) = 5;
                   13395:                                m_CF = 1;
                   13396:                                break;
                   13397:                        }
                   13398:                }
                   13399:        } while(FindNextFileA(fh, &fd));
                   13400:        if(!m_CF) {
                   13401:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   13402:                        m_CF = 1;
                   13403:                        REG16(AX) = 2;
                   13404:                }
                   13405:        }
                   13406:        FindClose(fh);
                   13407: }
                   13408: 
1.1       root     13409: inline void msdos_int_21h_714eh()
                   13410: {
                   13411:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13412:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   13413:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.60  root     13414:        WIN32_FIND_DATAA fd;
1.1       root     13415:        
1.1.1.13  root     13416:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   13417:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13418:                FindClose(dtainfo->find_handle);
                   13419:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13420:        }
                   13421:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     13422:        dtainfo->allowable_mask = REG8(CL);
                   13423:        dtainfo->required_mask = REG8(CH);
                   13424:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     13425:        
1.1.1.14  root     13426:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   13427:                dtainfo->allowable_mask &= ~8;
1.1       root     13428:        }
1.1.1.60  root     13429:        if(!label_only && (dtainfo->find_handle = FindFirstFileA(path, &fd)) != INVALID_HANDLE_VALUE) {
1.1.1.14  root     13430:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13431:                        if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13432:                                FindClose(dtainfo->find_handle);
                   13433:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13434:                                break;
                   13435:                        }
                   13436:                }
                   13437:        }
1.1.1.13  root     13438:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13439:                find->attrib = fd.dwFileAttributes;
                   13440:                msdos_find_file_conv_local_time(&fd);
                   13441:                if(REG16(SI) == 0) {
                   13442:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13443:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13444:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13445:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13446:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13447:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13448:                } else {
                   13449:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13450:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13451:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13452:                }
                   13453:                find->size_hi = fd.nFileSizeHigh;
                   13454:                find->size_lo = fd.nFileSizeLow;
                   13455:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13456:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13457:                REG16(AX) = dtainfo - dtalist + 1;
                   13458:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13459:                // volume label
                   13460:                find->attrib = 8;
                   13461:                find->size_hi = find->size_lo = 0;
                   13462:                strcpy(find->full_name, process->volume_label);
                   13463:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13464:                dtainfo->allowable_mask &= ~8;
                   13465:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     13466:        } else {
                   13467:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     13468:                m_CF = 1;
1.1       root     13469:        }
                   13470: }
                   13471: 
                   13472: inline void msdos_int_21h_714fh()
                   13473: {
                   13474:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     13475:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1.1.60  root     13476:        WIN32_FIND_DATAA fd;
1.1       root     13477:        
1.1.1.14  root     13478:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13479:                REG16(AX) = 6;
1.1.1.13  root     13480:                m_CF = 1;
                   13481:                return;
                   13482:        }
1.1.1.14  root     13483:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13484:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1.1.60  root     13485:                if(FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.14  root     13486:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.60  root     13487:                                if(!FindNextFileA(dtainfo->find_handle, &fd)) {
1.1.1.13  root     13488:                                        FindClose(dtainfo->find_handle);
                   13489:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13490:                                        break;
                   13491:                                }
                   13492:                        }
                   13493:                } else {
1.1.1.13  root     13494:                        FindClose(dtainfo->find_handle);
                   13495:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13496:                }
                   13497:        }
1.1.1.13  root     13498:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     13499:                find->attrib = fd.dwFileAttributes;
                   13500:                msdos_find_file_conv_local_time(&fd);
                   13501:                if(REG16(SI) == 0) {
                   13502:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   13503:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   13504:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   13505:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   13506:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   13507:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   13508:                } else {
                   13509:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   13510:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   13511:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   13512:                }
                   13513:                find->size_hi = fd.nFileSizeHigh;
                   13514:                find->size_lo = fd.nFileSizeLow;
                   13515:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     13516:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     13517:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     13518:                // volume label
                   13519:                find->attrib = 8;
                   13520:                find->size_hi = find->size_lo = 0;
                   13521:                strcpy(find->full_name, process->volume_label);
                   13522:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     13523:                dtainfo->allowable_mask &= ~8;
1.1       root     13524:        } else {
                   13525:                REG16(AX) = 0x12;
1.1.1.3   root     13526:                m_CF = 1;
1.1       root     13527:        }
                   13528: }
                   13529: 
                   13530: inline void msdos_int_21h_71a0h()
                   13531: {
                   13532:        DWORD max_component_len, file_sys_flag;
                   13533:        
1.1.1.60  root     13534:        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     13535:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   13536:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     13537:                REG16(CX) = (UINT16)max_component_len;          // 255
                   13538:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   13539:        } else {
                   13540:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13541:                m_CF = 1;
1.1       root     13542:        }
                   13543: }
                   13544: 
                   13545: inline void msdos_int_21h_71a1h()
                   13546: {
1.1.1.14  root     13547:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   13548:                REG16(AX) = 6;
1.1.1.13  root     13549:                m_CF = 1;
                   13550:                return;
                   13551:        }
1.1.1.14  root     13552:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     13553:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   13554:                FindClose(dtainfo->find_handle);
                   13555:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     13556:        }
                   13557: }
                   13558: 
                   13559: inline void msdos_int_21h_71a6h()
                   13560: {
                   13561:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     13562:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13563:        
1.1.1.3   root     13564:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     13565:        struct _stat64 status;
                   13566:        DWORD serial_number = 0;
                   13567:        
1.1.1.20  root     13568:        if(fd < process->max_files && file_handler[fd].valid) {
                   13569:                if(_fstat64(fd, &status) == 0) {
                   13570:                        if(file_handler[fd].path[1] == ':') {
1.1       root     13571:                                // NOTE: we need to consider the network file path "\\host\share\"
                   13572:                                char volume[] = "A:\\";
1.1.1.20  root     13573:                                volume[0] = file_handler[fd].path[1];
1.1.1.60  root     13574:                                GetVolumeInformationA(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
1.1       root     13575:                        }
1.1.1.60  root     13576:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributesA(file_handler[fd].path);
1.1       root     13577:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   13578:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   13579:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   13580:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   13581:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   13582:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   13583:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   13584:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   13585:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   13586:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     13587:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     13588:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     13589:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     13590:                } else {
                   13591:                        REG16(AX) = errno;
1.1.1.3   root     13592:                        m_CF = 1;
1.1       root     13593:                }
                   13594:        } else {
                   13595:                REG16(AX) = 0x06;
1.1.1.3   root     13596:                m_CF = 1;
1.1       root     13597:        }
                   13598: }
                   13599: 
                   13600: inline void msdos_int_21h_71a7h()
                   13601: {
                   13602:        switch(REG8(BL)) {
                   13603:        case 0x00:
1.1.1.3   root     13604:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     13605:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13606:                        m_CF = 1;
1.1       root     13607:                }
                   13608:                break;
                   13609:        case 0x01:
                   13610:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     13611:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     13612:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     13613:                        m_CF = 1;
1.1       root     13614:                }
                   13615:                break;
                   13616:        default:
1.1.1.22  root     13617:                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     13618:                REG16(AX) = 0x7100;
1.1.1.3   root     13619:                m_CF = 1;
1.1       root     13620:                break;
                   13621:        }
                   13622: }
                   13623: 
                   13624: inline void msdos_int_21h_71a8h()
                   13625: {
                   13626:        if(REG8(DH) == 0) {
                   13627:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     13628:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13629:                memset(fcb, 0x20, sizeof(fcb));
                   13630:                int len = strlen(tmp);
1.1.1.21  root     13631:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     13632:                        if(tmp[i] == '.') {
                   13633:                                pos = 8;
                   13634:                        } else {
                   13635:                                if(msdos_lead_byte_check(tmp[i])) {
                   13636:                                        fcb[pos++] = tmp[i++];
                   13637:                                }
                   13638:                                fcb[pos++] = tmp[i];
                   13639:                        }
                   13640:                }
1.1.1.3   root     13641:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     13642:        } else {
1.1.1.3   root     13643:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     13644:        }
                   13645: }
                   13646: 
1.1.1.22  root     13647: inline void msdos_int_21h_71aah()
                   13648: {
                   13649:        char drv[] = "A:", path[MAX_PATH];
                   13650:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13651:        
                   13652:        if(REG8(BL) == 0) {
                   13653:                drv[0] = 'A' + _getdrive() - 1;
                   13654:        } else {
                   13655:                drv[0] = 'A' + REG8(BL) - 1;
                   13656:        }
                   13657:        switch(REG8(BH)) {
                   13658:        case 0x00:
1.1.1.44  root     13659:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13660:                        REG16(AX) = 0x0f; // invalid drive
                   13661:                        m_CF = 1;
1.1.1.60  root     13662:                } else if(DefineDosDeviceA(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
1.1.1.44  root     13663:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     13664:                        m_CF = 1;
                   13665:                }
                   13666:                break;
                   13667:        case 0x01:
1.1.1.44  root     13668:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13669:                        REG16(AX) = 0x0f; // invalid drive
                   13670:                        m_CF = 1;
1.1.1.60  root     13671:                } else if(DefineDosDeviceA(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     13672:                        REG16(AX) = 0x0f; // invalid drive
                   13673:                        m_CF = 1;
                   13674:                }
                   13675:                break;
                   13676:        case 0x02:
1.1.1.44  root     13677:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   13678:                        REG16(AX) = 0x0f; // invalid drive
                   13679:                        m_CF = 1;
1.1.1.60  root     13680:                } else if(QueryDosDeviceA(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     13681:                        REG16(AX) = 0x0f; // invalid drive
                   13682:                        m_CF = 1;
                   13683:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   13684:                        REG16(AX) = 0x0f; // invalid drive
                   13685:                        m_CF = 1;
                   13686:                } else {
                   13687:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   13688:                }
                   13689:                break;
                   13690:        default:
                   13691:                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     13692:                REG16(AX) = 0x7100;
1.1.1.22  root     13693:                m_CF = 1;
                   13694:                break;
                   13695:        }
                   13696: }
                   13697: 
1.1.1.14  root     13698: inline void msdos_int_21h_7300h()
                   13699: {
1.1.1.44  root     13700:        REG8(AL) = REG8(CL);
                   13701:        REG8(AH) = 0;
1.1.1.14  root     13702: }
                   13703: 
                   13704: inline void msdos_int_21h_7302h()
                   13705: {
                   13706:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   13707:        UINT16 seg, ofs;
                   13708:        
                   13709:        if(REG16(CX) < 0x3f) {
                   13710:                REG8(AL) = 0x18;
                   13711:                m_CF = 1;
                   13712:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   13713:                REG8(AL) = 0xff;
                   13714:                m_CF = 1;
                   13715:        } else {
                   13716:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   13717:        }
                   13718: }
                   13719: 
1.1       root     13720: inline void msdos_int_21h_7303h()
                   13721: {
1.1.1.3   root     13722:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   13723:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     13724:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   13725:        
1.1.1.60  root     13726:        if(GetDiskFreeSpaceA(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
1.1       root     13727:                info->size_of_structure = sizeof(ext_space_info_t);
                   13728:                info->structure_version = 0;
                   13729:                info->sectors_per_cluster = sectors_per_cluster;
                   13730:                info->bytes_per_sector = bytes_per_sector;
                   13731:                info->available_clusters_on_drive = free_clusters;
                   13732:                info->total_clusters_on_drive = total_clusters;
                   13733:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   13734:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   13735:                info->available_allocation_units = free_clusters;       // ???
                   13736:                info->total_allocation_units = total_clusters;          // ???
                   13737:        } else {
                   13738:                REG16(AX) = errno;
1.1.1.3   root     13739:                m_CF = 1;
1.1       root     13740:        }
                   13741: }
                   13742: 
1.1.1.30  root     13743: inline void msdos_int_21h_dbh()
                   13744: {
                   13745:        // Novell NetWare - Workstation - Get Number of Local Drives
                   13746:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   13747:        REG8(AL) = dos_info->last_drive;
                   13748: }
                   13749: 
                   13750: inline void msdos_int_21h_dch()
                   13751: {
                   13752:        // Novell NetWare - Connection Services - Get Connection Number
                   13753:        REG8(AL) = 0x00;
                   13754: }
                   13755: 
1.1.1.32  root     13756: inline void msdos_int_24h()
                   13757: {
                   13758:        const char *message = NULL;
                   13759:        int key = 0;
                   13760:        
                   13761:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   13762:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   13763:                        if(active_code_page == 932) {
                   13764:                                message = critical_error_table[i].message_japanese;
                   13765:                        }
                   13766:                        if(message == NULL) {
                   13767:                                message = critical_error_table[i].message_english;
                   13768:                        }
                   13769:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   13770:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   13771:                        
                   13772:                        SREG(ES) = WORK_TOP >> 4;
                   13773:                        i386_load_segment_descriptor(ES);
                   13774:                        REG16(DI) = 0x0000;
                   13775:                        break;
                   13776:                }
                   13777:        }
                   13778:        fprintf(stderr, "\n%s", message);
                   13779:        if(!(REG8(AH) & 0x80)) {
                   13780:                if(REG8(AH) & 0x01) {
                   13781:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13782:                } else {
                   13783:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13784:                }
                   13785:        }
                   13786:        fprintf(stderr, "\n");
                   13787:        
1.1.1.33  root     13788:        {
1.1.1.32  root     13789:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13790:        }
1.1.1.32  root     13791:        if(REG8(AH) & 0x10) {
                   13792:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13793:        }
                   13794:        if(REG8(AH) & 0x20) {
                   13795:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13796:        }
                   13797:        if(REG8(AH) & 0x08) {
                   13798:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13799:        }
                   13800:        fprintf(stderr, "? ");
                   13801:        
                   13802:        while(1) {
                   13803:                while(!_kbhit()) {
                   13804:                        Sleep(10);
                   13805:                }
                   13806:                key = _getch();
                   13807:                
                   13808:                if(key == 'I' || key == 'i') {
                   13809:                        if(REG8(AH) & 0x20) {
                   13810:                                REG8(AL) = 0;
                   13811:                                break;
                   13812:                        }
                   13813:                } else if(key == 'R' || key == 'r') {
                   13814:                        if(REG8(AH) & 0x10) {
                   13815:                                REG8(AL) = 1;
                   13816:                                break;
                   13817:                        }
                   13818:                } else if(key == 'A' || key == 'a') {
                   13819:                        REG8(AL) = 2;
                   13820:                        break;
                   13821:                } else if(key == 'F' || key == 'f') {
                   13822:                        if(REG8(AH) & 0x08) {
                   13823:                                REG8(AL) = 3;
                   13824:                                break;
                   13825:                        }
                   13826:                }
                   13827:        }
                   13828:        fprintf(stderr, "%c\n", key);
                   13829: }
                   13830: 
1.1       root     13831: inline void msdos_int_25h()
                   13832: {
                   13833:        UINT16 seg, ofs;
                   13834:        DWORD dwSize;
                   13835:        
1.1.1.3   root     13836: #if defined(HAS_I386)
                   13837:        I386OP(pushf)();
                   13838: #else
                   13839:        PREFIX86(_pushf());
                   13840: #endif
1.1       root     13841:        
                   13842:        if(!(REG8(AL) < 26)) {
                   13843:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13844:                m_CF = 1;
1.1       root     13845:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13846:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13847:                m_CF = 1;
1.1       root     13848:        } else {
                   13849:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13850:                char dev[64];
                   13851:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13852:                
1.1.1.60  root     13853:                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     13854:                if(hFile == INVALID_HANDLE_VALUE) {
                   13855:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13856:                        m_CF = 1;
1.1       root     13857:                } else {
1.1.1.19  root     13858:                        UINT32 top_sector  = REG16(DX);
                   13859:                        UINT16 sector_num  = REG16(CX);
                   13860:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13861:                        
                   13862:                        if(sector_num == 0xffff) {
                   13863:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13864:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13865:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13866:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13867:                                buffer_addr = (seg << 4) + ofs;
                   13868:                        }
                   13869: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13870: //                             REG8(AL) = 0x02; // drive not ready
                   13871: //                             m_CF = 1;
                   13872: //                     } else 
                   13873:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13874:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13875:                                m_CF = 1;
1.1.1.19  root     13876:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13877:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13878:                                m_CF = 1;
1.1       root     13879:                        }
                   13880:                        CloseHandle(hFile);
                   13881:                }
                   13882:        }
                   13883: }
                   13884: 
                   13885: inline void msdos_int_26h()
                   13886: {
1.1.1.42  root     13887:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13888:        UINT16 seg, ofs;
                   13889:        DWORD dwSize;
                   13890:        
1.1.1.3   root     13891: #if defined(HAS_I386)
                   13892:        I386OP(pushf)();
                   13893: #else
                   13894:        PREFIX86(_pushf());
                   13895: #endif
1.1       root     13896:        
                   13897:        if(!(REG8(AL) < 26)) {
                   13898:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13899:                m_CF = 1;
1.1       root     13900:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13901:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13902:                m_CF = 1;
1.1       root     13903:        } else {
                   13904:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13905:                char dev[64];
                   13906:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13907:                
                   13908:                if(dpb->media_type == 0xf8) {
                   13909:                        // this drive is not a floppy
1.1.1.6   root     13910: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13911: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13912: //                     }
1.1       root     13913:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13914:                        m_CF = 1;
1.1       root     13915:                } else {
1.1.1.60  root     13916:                        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     13917:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13918:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13919:                                m_CF = 1;
1.1       root     13920:                        } else {
1.1.1.19  root     13921:                                UINT32 top_sector  = REG16(DX);
                   13922:                                UINT16 sector_num  = REG16(CX);
                   13923:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13924:                                
                   13925:                                if(sector_num == 0xffff) {
                   13926:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13927:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13928:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13929:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13930:                                        buffer_addr = (seg << 4) + ofs;
                   13931:                                }
1.1       root     13932:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13933:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13934:                                        m_CF = 1;
1.1.1.19  root     13935:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13936:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13937:                                        m_CF = 1;
1.1.1.19  root     13938:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13939:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13940:                                        m_CF = 1;
1.1       root     13941:                                }
                   13942:                                CloseHandle(hFile);
                   13943:                        }
                   13944:                }
                   13945:        }
                   13946: }
                   13947: 
                   13948: inline void msdos_int_27h()
                   13949: {
1.1.1.29  root     13950:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13951:        try {
                   13952:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13953:        } catch(...) {
                   13954:                // recover the broken mcb
                   13955:                int mcb_seg = SREG(CS) - 1;
                   13956:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13957:                
1.1.1.29  root     13958:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13959:                        mcb->mz = 'M';
                   13960:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13961:                        
1.1.1.29  root     13962:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13963:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13964:                        } else {
1.1.1.39  root     13965:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13966:                        }
                   13967:                } else {
                   13968:                        mcb->mz = 'Z';
1.1.1.30  root     13969:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13970:                }
                   13971:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13972:        }
1.1.1.3   root     13973:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13974: }
                   13975: 
                   13976: inline void msdos_int_29h()
                   13977: {
1.1.1.50  root     13978:        msdos_putch_fast(REG8(AL));
1.1       root     13979: }
                   13980: 
                   13981: inline void msdos_int_2eh()
                   13982: {
                   13983:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13984:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13985:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13986:        char *token = my_strtok(tmp, " ");
                   13987:        strcpy(command, token);
                   13988:        strcpy(opt, token + strlen(token) + 1);
                   13989:        
                   13990:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13991:        param->env_seg = 0;
                   13992:        param->cmd_line.w.l = 44;
                   13993:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13994:        param->fcb1.w.l = 24;
                   13995:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13996:        param->fcb2.w.l = 24;
                   13997:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13998:        
                   13999:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14000:        
                   14001:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   14002:        cmd_line->len = strlen(opt);
                   14003:        strcpy(cmd_line->cmd, opt);
                   14004:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14005:        
1.1.1.28  root     14006:        try {
                   14007:                if(msdos_process_exec(command, param, 0)) {
                   14008:                        REG16(AX) = 0xffff; // error before processing command
                   14009:                } else {
                   14010:                        // set flag to set retval to ax when the started process is terminated
                   14011:                        process_t *process = msdos_process_info_get(current_psp);
                   14012:                        process->called_by_int2eh = true;
                   14013:                }
                   14014:        } catch(...) {
                   14015:                REG16(AX) = 0xffff; // error before processing command
                   14016:        }
1.1       root     14017: }
                   14018: 
1.1.1.29  root     14019: inline void msdos_int_2fh_05h()
                   14020: {
                   14021:        switch(REG8(AL)) {
                   14022:        case 0x00:
1.1.1.49  root     14023:                // critical error handler is installed
1.1.1.32  root     14024:                REG8(AL) = 0xff;
                   14025:                break;
                   14026:        case 0x01:
                   14027:        case 0x02:
                   14028:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   14029:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   14030:                                const char *message = NULL;
                   14031:                                if(active_code_page == 932) {
                   14032:                                        message = standard_error_table[i].message_japanese;
                   14033:                                }
                   14034:                                if(message == NULL) {
                   14035:                                        message = standard_error_table[i].message_english;
                   14036:                                }
                   14037:                                strcpy((char *)(mem + WORK_TOP), message);
                   14038:                                
                   14039:                                SREG(ES) = WORK_TOP >> 4;
                   14040:                                i386_load_segment_descriptor(ES);
                   14041:                                REG16(DI) = 0x0000;
                   14042:                                REG8(AL) = 0x01;
                   14043:                                break;
                   14044:                        }
                   14045:                }
1.1.1.29  root     14046:                break;
                   14047:        default:
                   14048:                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     14049:                REG16(AX) = 0x01;
1.1.1.29  root     14050:                m_CF = 1;
                   14051:        }
                   14052: }
                   14053: 
1.1.1.44  root     14054: inline void msdos_int_2fh_06h()
                   14055: {
                   14056:        switch(REG8(AL)) {
                   14057:        case 0x00:
                   14058:                // ASSIGN is not installed
1.1.1.49  root     14059: //             REG8(AL) = 0x00;
1.1.1.44  root     14060:                break;
                   14061:        case 0x01:
                   14062:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   14063:                REG16(AX) = 0x01;
                   14064:                m_CF = 1;
                   14065:                break;
                   14066:        default:
                   14067:                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));
                   14068:                REG16(AX) = 0x01;
                   14069:                m_CF = 1;
                   14070:                break;
                   14071:        }
                   14072: }
                   14073: 
1.1.1.22  root     14074: inline void msdos_int_2fh_11h()
                   14075: {
                   14076:        switch(REG8(AL)) {
                   14077:        case 0x00:
1.1.1.29  root     14078:                if(i386_read_stack() == 0xdada) {
1.1.1.53  root     14079: #ifdef SUPPORT_MSCDEX
                   14080:                        // MSCDEX is installed
                   14081:                        REG8(AL) = 0xff;
                   14082:                        i386_write_stack(0xadad);
                   14083: #else
1.1.1.29  root     14084:                        // MSCDEX is not installed
                   14085: //                     REG8(AL) = 0x00;
1.1.1.53  root     14086: #endif
1.1.1.29  root     14087:                } else {
                   14088:                        // Network Redirector is not installed
                   14089: //                     REG8(AL) = 0x00;
                   14090:                }
1.1.1.22  root     14091:                break;
                   14092:        default:
1.1.1.43  root     14093: //             unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.29  root     14094:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     14095:                m_CF = 1;
                   14096:                break;
                   14097:        }
                   14098: }
                   14099: 
1.1.1.21  root     14100: inline void msdos_int_2fh_12h()
                   14101: {
                   14102:        switch(REG8(AL)) {
1.1.1.22  root     14103:        case 0x00:
1.1.1.29  root     14104:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     14105:                REG8(AL) = 0xff;
                   14106:                break;
1.1.1.29  root     14107: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   14108:        case 0x02:
                   14109:                {
                   14110:                        UINT16 stack = i386_read_stack();
                   14111:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   14112:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   14113:                        i386_load_segment_descriptor(ES);
                   14114:                }
                   14115:                break;
1.1.1.30  root     14116:        case 0x03:
                   14117:                SREG(DS) = (DEVICE_TOP >> 4);
                   14118:                i386_load_segment_descriptor(DS);
                   14119:                break;
1.1.1.29  root     14120:        case 0x04:
                   14121:                {
                   14122:                        UINT16 stack = i386_read_stack();
                   14123:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   14124: #if defined(HAS_I386)
                   14125:                        m_ZF = (REG8(AL) == '\\');
                   14126: #else
                   14127:                        m_ZeroVal = (REG8(AL) != '\\');
                   14128: #endif
                   14129:                }
                   14130:                break;
                   14131:        case 0x05:
1.1.1.49  root     14132:                {
                   14133:                        UINT16 c = i386_read_stack();
                   14134:                        if((c >> 0) & 0xff) {
                   14135:                                msdos_putch((c >> 0) & 0xff);
                   14136:                        }
                   14137:                        if((c >> 8) & 0xff) {
                   14138:                                msdos_putch((c >> 8) & 0xff);
                   14139:                        }
                   14140:                }
1.1.1.29  root     14141:                break;
1.1.1.49  root     14142: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     14143: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   14144: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     14145: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     14146: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   14147: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   14148: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   14149:        case 0x0d:
                   14150:                {
                   14151:                        SYSTEMTIME time;
                   14152:                        FILETIME file_time;
                   14153:                        WORD dos_date, dos_time;
                   14154:                        GetLocalTime(&time);
                   14155:                        SystemTimeToFileTime(&time, &file_time);
                   14156:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   14157:                        REG16(AX) = dos_date;
                   14158:                        REG16(DX) = dos_time;
                   14159:                }
                   14160:                break;
                   14161: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   14162: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   14163: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   14164:        case 0x11:
                   14165:                {
                   14166:                        char path[MAX_PATH], *p;
                   14167:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14168:                        my_strupr(path);
                   14169:                        while((p = my_strchr(path, '/')) != NULL) {
                   14170:                                *p = '\\';
                   14171:                        }
                   14172:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   14173:                }
                   14174:                break;
                   14175:        case 0x12:
                   14176:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14177:                break;
                   14178:        case 0x13:
                   14179:                {
                   14180:                        char tmp[2] = {0};
                   14181:                        tmp[0] = i386_read_stack();
                   14182:                        my_strupr(tmp);
                   14183:                        REG8(AL) = tmp[0];
                   14184:                }
                   14185:                break;
                   14186:        case 0x14:
                   14187: #if defined(HAS_I386)
                   14188:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   14189: #else
                   14190:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14191: #endif
                   14192:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   14193:                break;
                   14194: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     14195:        case 0x16:
                   14196:                if(REG16(BX) < 20) {
                   14197:                        SREG(ES) = SFT_TOP >> 4;
                   14198:                        i386_load_segment_descriptor(ES);
                   14199:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   14200:                        
                   14201:                        // update system file table
                   14202:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   14203:                        if(file_handler[REG16(BX)].valid) {
                   14204:                                int count = 0;
                   14205:                                for(int i = 0; i < 20; i++) {
                   14206:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   14207:                                                count++;
                   14208:                                        }
                   14209:                                }
                   14210:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   14211:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   14212:                                _lseek(REG16(BX), 0, SEEK_END);
                   14213:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   14214:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   14215:                        } else {
                   14216:                                memset(sft, 0, 0x3b);
                   14217:                        }
                   14218:                } else {
                   14219:                        REG16(AX) = 0x06;
                   14220:                        m_CF = 1;
                   14221:                }
                   14222:                break;
1.1.1.49  root     14223:        case 0x17:
                   14224:                {
                   14225:                        UINT16 drive = i386_read_stack();
                   14226:                        if(msdos_is_valid_drive(drive)) {
                   14227:                                msdos_cds_update(drive);
                   14228:                        }
                   14229:                        REG16(SI) = 88 * drive;
                   14230:                        SREG(DS) = (CDS_TOP >> 4);
                   14231:                        i386_load_segment_descriptor(DS);
                   14232:                }
                   14233:                break;
1.1.1.29  root     14234: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   14235: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   14236:        case 0x1a:
                   14237:                {
                   14238:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   14239:                        if(path[1] == ':') {
                   14240:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   14241:                                        REG8(AL) = path[0] - 'a' + 1;
                   14242:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   14243:                                        REG8(AL) = path[0] - 'A' + 1;
                   14244:                                } else {
                   14245:                                        REG8(AL) = 0xff; // invalid
                   14246:                                }
                   14247:                                strcpy(full, path);
                   14248:                                strcpy(path, full + 2);
1.1.1.60  root     14249:                        } else if(GetFullPathNameA(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
1.1.1.29  root     14250:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   14251:                                        REG8(AL) = full[0] - 'a' + 1;
                   14252:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   14253:                                        REG8(AL) = full[0] - 'A' + 1;
                   14254:                                } else {
                   14255:                                        REG8(AL) = 0xff; // invalid
                   14256:                                }
                   14257:                        } else {
                   14258:                                REG8(AL) = 0x00; // default
                   14259:                        }
                   14260:                }
                   14261:                break;
                   14262:        case 0x1b:
                   14263:                {
                   14264:                        int year = REG16(CX) + 1980;
                   14265:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   14266:                }
                   14267:                break;
                   14268: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   14269: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   14270:        case 0x1e:
                   14271:                {
                   14272:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   14273:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
1.1.1.60  root     14274:                        if(GetFullPathNameA(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathNameA(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
1.1.1.29  root     14275: #if defined(HAS_I386)
                   14276:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   14277: #else
                   14278:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   14279: #endif
                   14280:                        } else {
                   14281: #if defined(HAS_I386)
                   14282:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   14283: #else
                   14284:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   14285: #endif
                   14286:                        }
                   14287:                }
                   14288:                break;
1.1.1.49  root     14289:        case 0x1f:
                   14290:                {
                   14291:                        UINT16 drive = i386_read_stack();
                   14292:                        if(msdos_is_valid_drive(drive)) {
                   14293:                                msdos_cds_update(drive);
                   14294:                        }
                   14295:                        REG16(SI) = 88 * drive;
                   14296:                        SREG(ES) = (CDS_TOP >> 4);
                   14297:                        i386_load_segment_descriptor(ES);
                   14298:                }
                   14299:                break;
1.1.1.21  root     14300:        case 0x20:
                   14301:                {
                   14302:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   14303:                        
                   14304:                        if(fd < 20) {
                   14305:                                SREG(ES) = current_psp;
                   14306:                                i386_load_segment_descriptor(ES);
                   14307:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   14308:                        } else {
                   14309:                                REG16(AX) = 0x06;
                   14310:                                m_CF = 1;
                   14311:                        }
                   14312:                }
                   14313:                break;
1.1.1.29  root     14314:        case 0x21:
                   14315:                msdos_int_21h_60h(0);
                   14316:                break;
1.1.1.49  root     14317:        case 0x22:
                   14318:                {
                   14319:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14320:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   14321:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   14322:                        }
                   14323:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   14324:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   14325:                        }
                   14326:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   14327:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   14328:                        }
                   14329:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   14330:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   14331:                        }
                   14332:                }
                   14333:                break;
1.1.1.29  root     14334: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   14335: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   14336:        case 0x25:
                   14337:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   14338:                break;
                   14339:        case 0x26:
                   14340:                REG8(AL) = REG8(CL);
                   14341:                msdos_int_21h_3dh();
                   14342:                break;
                   14343:        case 0x27:
                   14344:                msdos_int_21h_3eh();
                   14345:                break;
                   14346:        case 0x28:
                   14347:                REG16(AX) = REG16(BP);
                   14348:                msdos_int_21h_42h();
                   14349:                break;
                   14350:        case 0x29:
                   14351:                msdos_int_21h_3fh();
                   14352:                break;
                   14353: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   14354:        case 0x2b:
                   14355:                REG16(AX) = REG16(BP);
                   14356:                msdos_int_21h_44h();
                   14357:                break;
                   14358:        case 0x2c:
                   14359:                REG16(BX) = DEVICE_TOP >> 4;
                   14360:                REG16(AX) = 22;
                   14361:                break;
                   14362:        case 0x2d:
                   14363:                {
                   14364:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14365:                        REG16(AX) = sda->extended_error_code;
                   14366:                }
                   14367:                break;
                   14368:        case 0x2e:
                   14369:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     14370:                        SREG(ES) = 0x0001;
                   14371:                        i386_load_segment_descriptor(ES);
                   14372:                        REG16(DI) = 0x00;
                   14373:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     14374:                        // dummy parameter error message read routine is at fffc:0010
                   14375:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     14376:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     14377:                        REG16(DI) = 0x0010;
1.1.1.22  root     14378:                }
                   14379:                break;
1.1.1.29  root     14380:        case 0x2f:
                   14381:                if(REG16(DX) != 0) {
1.1.1.30  root     14382:                        dos_major_version = REG8(DL);
                   14383:                        dos_minor_version = REG8(DH);
1.1.1.29  root     14384:                } else {
                   14385:                        REG8(DL) = 7;
                   14386:                        REG8(DH) = 10;
                   14387:                }
                   14388:                break;
                   14389: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   14390: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     14391:        default:
                   14392:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14393:                REG16(AX) = 0x01;
                   14394:                m_CF = 1;
                   14395:                break;
                   14396:        }
                   14397: }
                   14398: 
1.1.1.30  root     14399: inline void msdos_int_2fh_13h()
                   14400: {
                   14401:        static UINT16 prevDS = 0, prevDX = 0;
                   14402:        static UINT16 prevES = 0, prevBX = 0;
                   14403:        UINT16 tmp;
                   14404:        
                   14405:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   14406:        i386_load_segment_descriptor(DS);
                   14407:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   14408:        
                   14409:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   14410:        i386_load_segment_descriptor(ES);
                   14411:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   14412: }
                   14413: 
1.1.1.22  root     14414: inline void msdos_int_2fh_14h()
                   14415: {
                   14416:        switch(REG8(AL)) {
                   14417:        case 0x00:
1.1.1.29  root     14418:                // NLSFUNC.COM is installed
                   14419:                REG8(AL) = 0xff;
1.1.1.25  root     14420:                break;
                   14421:        case 0x01:
                   14422:        case 0x03:
                   14423:                REG8(AL) = 0x00;
                   14424:                active_code_page = REG16(BX);
                   14425:                msdos_nls_tables_update();
                   14426:                break;
                   14427:        case 0x02:
                   14428:                REG8(AL) = get_extended_country_info(REG16(BP));
                   14429:                break;
                   14430:        case 0x04:
1.1.1.42  root     14431:                for(int i = 0;; i++) {
                   14432:                        if(country_table[i].code == REG16(DX)) {
                   14433:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   14434:                                break;
                   14435:                        } else if(country_table[i].code == -1) {
                   14436:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   14437:                                break;
                   14438:                        }
                   14439:                }
1.1.1.25  root     14440:                REG8(AL) = 0x00;
1.1.1.22  root     14441:                break;
                   14442:        default:
                   14443:                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));
                   14444:                REG16(AX) = 0x01;
                   14445:                m_CF = 1;
                   14446:                break;
                   14447:        }
                   14448: }
                   14449: 
                   14450: inline void msdos_int_2fh_15h()
                   14451: {
                   14452:        switch(REG8(AL)) {
1.1.1.29  root     14453:        case 0x00: // CD-ROM - Installation Check
                   14454:                if(REG16(BX) == 0x0000) {
1.1.1.53  root     14455: #ifdef SUPPORT_MSCDEX
1.1.1.43  root     14456:                        // MSCDEX is installed
                   14457:                        REG16(BX) = 0;
                   14458:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     14459:                                if(msdos_is_cdrom_drive(i)) {
                   14460:                                        if(REG16(BX) == 0) {
                   14461:                                                REG16(CX) = i;
1.1.1.43  root     14462:                                        }
1.1.1.44  root     14463:                                        REG16(BX)++;
1.1.1.43  root     14464:                                }
                   14465:                        }
                   14466: #else
1.1.1.29  root     14467:                        // MSCDEX is not installed
                   14468: //                     REG8(AL) = 0x00;
1.1.1.43  root     14469: #endif
1.1.1.29  root     14470:                } else {
                   14471:                        // GRAPHICS.COM is not installed
                   14472: //                     REG8(AL) = 0x00;
                   14473:                }
1.1.1.22  root     14474:                break;
1.1.1.43  root     14475:        case 0x0b:
1.1.1.44  root     14476:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   14477:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   14478:                REG16(BX) = 0xadad;
1.1.1.43  root     14479:                break;
                   14480:        case 0x0d:
1.1.1.44  root     14481:                for(int i = 0, n = 0; i < 26; i++) {
                   14482:                        if(msdos_is_cdrom_drive(i)) {
                   14483:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     14484:                        }
                   14485:                }
                   14486:                break;
1.1.1.22  root     14487:        case 0xff:
1.1.1.29  root     14488:                if(REG16(BX) == 0x0000) {
                   14489:                        // CORELCDX is not installed
                   14490:                } else {
                   14491:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14492:                        REG16(AX) = 0x01;
                   14493:                        m_CF = 1;
                   14494:                }
1.1.1.22  root     14495:                break;
1.1.1.21  root     14496:        default:
1.1.1.22  root     14497:                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     14498:                REG16(AX) = 0x01;
                   14499:                m_CF = 1;
                   14500:                break;
                   14501:        }
                   14502: }
                   14503: 
1.1       root     14504: inline void msdos_int_2fh_16h()
                   14505: {
                   14506:        switch(REG8(AL)) {
                   14507:        case 0x00:
1.1.1.14  root     14508:                if(no_windows) {
1.1.1.29  root     14509:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   14510: //                     REG8(AL) = 0x00;
1.1.1.14  root     14511:                } else {
1.1.1.30  root     14512:                        REG8(AL) = win_major_version;
                   14513:                        REG8(AH) = win_minor_version;
1.1       root     14514:                }
                   14515:                break;
1.1.1.43  root     14516:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     14517:                // from DOSBox
                   14518:                i386_set_a20_line(1);
                   14519:                break;
1.1.1.49  root     14520:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     14521:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   14522:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   14523:                break;
                   14524:        case 0x07:
                   14525:                // Virtual Device Call API
                   14526:                break;
1.1.1.22  root     14527:        case 0x0a:
                   14528:                if(!no_windows) {
                   14529:                        REG16(AX) = 0x0000;
1.1.1.30  root     14530:                        REG8(BH) = win_major_version;
                   14531:                        REG8(BL) = win_minor_version;
1.1.1.49  root     14532: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     14533:                        REG16(CX) = 0x0003; // enhanced
                   14534:                }
                   14535:                break;
1.1.1.30  root     14536:        case 0x0b:
                   14537:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     14538:        case 0x0e:
                   14539:        case 0x0f:
1.1.1.30  root     14540:        case 0x10:
1.1.1.22  root     14541:        case 0x11:
                   14542:        case 0x12:
                   14543:        case 0x13:
                   14544:        case 0x14:
1.1.1.30  root     14545:        case 0x15:
1.1.1.43  root     14546:        case 0x81:
                   14547:        case 0x82:
1.1.1.44  root     14548:        case 0x84:
1.1.1.49  root     14549:        case 0x85:
1.1.1.33  root     14550:        case 0x86:
1.1.1.22  root     14551:        case 0x87:
1.1.1.30  root     14552:        case 0x89:
1.1.1.33  root     14553:        case 0x8a:
1.1.1.22  root     14554:                // function not supported, do not clear AX
                   14555:                break;
1.1.1.14  root     14556:        case 0x80:
                   14557:                Sleep(10);
1.1.1.35  root     14558:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     14559:                REG8(AL) = 0x00;
1.1.1.14  root     14560:                break;
1.1.1.33  root     14561:        case 0x83:
                   14562:                REG16(BX) = 0x01; // system vm id
                   14563:                break;
1.1.1.22  root     14564:        case 0x8e:
                   14565:                REG16(AX) = 0x00; // failed
                   14566:                break;
1.1.1.20  root     14567:        case 0x8f:
                   14568:                switch(REG8(DH)) {
                   14569:                case 0x01:
1.1.1.49  root     14570: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   14571: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   14572:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   14573:                        break;
                   14574:                default:
                   14575:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     14576:                        break;
                   14577:                }
                   14578:                break;
1.1       root     14579:        default:
1.1.1.22  root     14580:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14581:                REG16(AX) = 0x01;
                   14582:                m_CF = 1;
                   14583:                break;
                   14584:        }
                   14585: }
                   14586: 
                   14587: inline void msdos_int_2fh_19h()
                   14588: {
                   14589:        switch(REG8(AL)) {
                   14590:        case 0x00:
1.1.1.29  root     14591:                // SHELLB.COM is not installed
                   14592: //             REG8(AL) = 0x00;
1.1.1.22  root     14593:                break;
                   14594:        case 0x01:
                   14595:        case 0x02:
                   14596:        case 0x03:
                   14597:        case 0x04:
                   14598:                REG16(AX) = 0x01;
                   14599:                m_CF = 1;
                   14600:                break;
1.1.1.29  root     14601:        case 0x80:
                   14602:                // IBM ROM-DOS v4.0 is not installed
                   14603: //             REG8(AL) = 0x00;
                   14604:                break;
1.1.1.22  root     14605:        default:
                   14606:                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     14607:                REG16(AX) = 0x01;
1.1.1.3   root     14608:                m_CF = 1;
1.1       root     14609:                break;
                   14610:        }
                   14611: }
                   14612: 
                   14613: inline void msdos_int_2fh_1ah()
                   14614: {
                   14615:        switch(REG8(AL)) {
                   14616:        case 0x00:
1.1.1.29  root     14617:                // ANSI.SYS is installed
1.1       root     14618:                REG8(AL) = 0xff;
                   14619:                break;
1.1.1.49  root     14620:        case 0x01:
1.1.1.50  root     14621:                if(REG8(CL) == 0x5f) {
                   14622:                        // set display information
                   14623:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   14624:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   14625:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   14626:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   14627:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   14628:                                
                   14629:                                if(cur_width != new_width || cur_height != new_height) {
                   14630:                                        pcbios_set_console_size(new_width, new_height, true);
                   14631:                                }
                   14632:                        }
                   14633:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     14634:                        // get display information
1.1.1.50  root     14635:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   14636:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   14637:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   14638:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   14639:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   14640:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   14641:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   14642:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   14643:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   14644:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   14645:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     14646:                } else {
                   14647:                        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));
                   14648:                        REG16(AX) = 0x01;
                   14649:                        m_CF = 1;
                   14650:                }
                   14651:                break;
1.1       root     14652:        default:
1.1.1.22  root     14653:                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));
                   14654:                REG16(AX) = 0x01;
                   14655:                m_CF = 1;
                   14656:                break;
                   14657:        }
                   14658: }
                   14659: 
1.1.1.30  root     14660: inline void msdos_int_2fh_40h()
1.1.1.22  root     14661: {
                   14662:        switch(REG8(AL)) {
                   14663:        case 0x00:
1.1.1.30  root     14664:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   14665:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     14666:                break;
1.1.1.43  root     14667:        case 0x10:
                   14668:                // OS/2 v2.0+ - Installation Check
                   14669:                REG16(AX) = 0x01;
                   14670:                m_CF = 1;
                   14671:                break;
1.1.1.22  root     14672:        default:
                   14673:                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     14674:                REG16(AX) = 0x01;
1.1.1.3   root     14675:                m_CF = 1;
1.1       root     14676:                break;
                   14677:        }
                   14678: }
                   14679: 
                   14680: inline void msdos_int_2fh_43h()
                   14681: {
                   14682:        switch(REG8(AL)) {
                   14683:        case 0x00:
1.1.1.29  root     14684:                // XMS is installed ?
1.1.1.19  root     14685: #ifdef SUPPORT_XMS
                   14686:                if(support_xms) {
                   14687:                        REG8(AL) = 0x80;
1.1.1.44  root     14688:                }
                   14689: #endif
                   14690:                break;
                   14691:        case 0x08:
                   14692: #ifdef SUPPORT_XMS
                   14693:                if(support_xms) {
                   14694:                        REG8(AL) = 0x43;
                   14695:                        REG8(BL) = 0x01; // IBM PC/AT
                   14696:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   14697:                }
1.1.1.19  root     14698: #endif
                   14699:                break;
                   14700:        case 0x10:
                   14701:                SREG(ES) = XMS_TOP >> 4;
                   14702:                i386_load_segment_descriptor(ES);
1.1.1.26  root     14703:                REG16(BX) = 0x15;
1.1       root     14704:                break;
1.1.1.44  root     14705:        case 0xe0:
                   14706:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   14707:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   14708:                        break;
                   14709:                }
1.1       root     14710:        default:
1.1.1.22  root     14711:                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));
                   14712:                REG16(AX) = 0x01;
                   14713:                m_CF = 1;
                   14714:                break;
                   14715:        }
                   14716: }
                   14717: 
                   14718: inline void msdos_int_2fh_46h()
                   14719: {
                   14720:        switch(REG8(AL)) {
                   14721:        case 0x80:
1.1.1.29  root     14722:                // Windows v3.0 is not installed
                   14723: //             REG8(AL) = 0x00;
1.1.1.22  root     14724:                break;
                   14725:        default:
                   14726:                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));
                   14727:                REG16(AX) = 0x01;
                   14728:                m_CF = 1;
                   14729:                break;
                   14730:        }
                   14731: }
                   14732: 
                   14733: inline void msdos_int_2fh_48h()
                   14734: {
                   14735:        switch(REG8(AL)) {
                   14736:        case 0x00:
1.1.1.29  root     14737:                // DOSKEY is not installed
                   14738: //             REG8(AL) = 0x00;
1.1.1.22  root     14739:                break;
                   14740:        case 0x10:
                   14741:                msdos_int_21h_0ah();
                   14742:                REG16(AX) = 0x00;
                   14743:                break;
                   14744:        default:
                   14745:                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     14746:                REG16(AX) = 0x01;
1.1.1.3   root     14747:                m_CF = 1;
1.1       root     14748:                break;
                   14749:        }
                   14750: }
                   14751: 
                   14752: inline void msdos_int_2fh_4ah()
                   14753: {
                   14754:        switch(REG8(AL)) {
1.1.1.29  root     14755: #ifdef SUPPORT_HMA
                   14756:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   14757:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14758:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14759:                                // restore first free mcb in high memory area
                   14760:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14761:                        }
                   14762:                        int offset = 0xffff;
                   14763:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   14764:                                REG16(DI) = offset + 0x10;
                   14765:                        } else {
                   14766:                                REG16(DI) = 0xffff;
                   14767:                        }
                   14768:                } else {
                   14769:                        // HMA is already used
                   14770:                        REG16(BX) = 0;
                   14771:                        REG16(DI) = 0xffff;
                   14772:                }
                   14773:                SREG(ES) = 0xffff;
                   14774:                i386_load_segment_descriptor(ES);
                   14775:                break;
                   14776:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   14777:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   14778:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14779:                                // restore first free mcb in high memory area
                   14780:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14781:                        }
                   14782:                        int size = REG16(BX), offset;
                   14783:                        if((size % 16) != 0) {
                   14784:                                size &= ~15;
                   14785:                                size += 16;
                   14786:                        }
                   14787:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14788:                                REG16(BX) = size;
                   14789:                                REG16(DI) = offset + 0x10;
                   14790:                                is_hma_used_by_int_2fh = true;
                   14791:                        } else {
                   14792:                                REG16(BX) = 0;
                   14793:                                REG16(DI) = 0xffff;
                   14794:                        }
                   14795:                } else {
                   14796:                        // HMA is already used
                   14797:                        REG16(BX) = 0;
                   14798:                        REG16(DI) = 0xffff;
                   14799:                }
                   14800:                SREG(ES) = 0xffff;
                   14801:                i386_load_segment_descriptor(ES);
                   14802:                break;
                   14803:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14804:                if(REG8(DL) == 0x00) {
                   14805:                        if(!is_hma_used_by_xms) {
                   14806:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14807:                                        // restore first free mcb in high memory area
                   14808:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14809:                                        is_hma_used_by_int_2fh = false;
                   14810:                                }
                   14811:                                int size = REG16(BX), offset;
                   14812:                                if((size % 16) != 0) {
                   14813:                                        size &= ~15;
                   14814:                                        size += 16;
                   14815:                                }
                   14816:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14817: //                                     REG16(BX) = size;
                   14818:                                        SREG(ES) = 0xffff;
                   14819:                                        i386_load_segment_descriptor(ES);
                   14820:                                        REG16(DI) = offset + 0x10;
                   14821:                                        is_hma_used_by_int_2fh = true;
                   14822:                                } else {
                   14823:                                        REG16(DI) = 0xffff;
                   14824:                                }
                   14825:                        } else {
                   14826:                                REG16(DI) = 0xffff;
                   14827:                        }
                   14828:                } else if(REG8(DL) == 0x01) {
                   14829:                        if(!is_hma_used_by_xms) {
                   14830:                                int size = REG16(BX);
                   14831:                                if((size % 16) != 0) {
                   14832:                                        size &= ~15;
                   14833:                                        size += 16;
                   14834:                                }
                   14835:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14836:                                        // memory block address is not changed
                   14837:                                } else {
                   14838:                                        REG16(DI) = 0xffff;
                   14839:                                }
                   14840:                        } else {
                   14841:                                REG16(DI) = 0xffff;
                   14842:                        }
                   14843:                } else if(REG8(DL) == 0x02) {
                   14844:                        if(!is_hma_used_by_xms) {
                   14845:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14846:                                        // restore first free mcb in high memory area
                   14847:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14848:                                        is_hma_used_by_int_2fh = false;
                   14849:                                } else {
                   14850:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14851:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14852:                                                is_hma_used_by_int_2fh = false;
                   14853:                                        }
                   14854:                                }
                   14855:                        }
                   14856:                } else {
                   14857:                        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));
                   14858:                        REG16(AX) = 0x01;
                   14859:                        m_CF = 1;
                   14860:                }
                   14861:                break;
                   14862:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14863:                if(!is_hma_used_by_xms) {
                   14864:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14865:                                // restore first free mcb in high memory area
                   14866:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14867:                                is_hma_used_by_int_2fh = false;
                   14868:                        }
                   14869:                        REG16(AX) = 0x0000;
                   14870:                        SREG(ES) = 0xffff;
                   14871:                        i386_load_segment_descriptor(ES);
                   14872:                        REG16(DI) = 0x10;
                   14873:                }
                   14874:                break;
                   14875: #else
1.1       root     14876:        case 0x01:
                   14877:        case 0x02:
1.1.1.29  root     14878:                // HMA is already used
1.1.1.27  root     14879:                REG16(BX) = 0x0000;
1.1.1.3   root     14880:                SREG(ES) = 0xffff;
                   14881:                i386_load_segment_descriptor(ES);
1.1       root     14882:                REG16(DI) = 0xffff;
                   14883:                break;
1.1.1.19  root     14884:        case 0x03:
                   14885:                // unable to allocate
                   14886:                REG16(DI) = 0xffff;
                   14887:                break;
                   14888:        case 0x04:
                   14889:                // function not supported, do not clear AX
                   14890:                break;
1.1.1.29  root     14891: #endif
                   14892:        case 0x10:
1.1.1.42  root     14893:                switch(REG16(BX)) {
                   14894:                case 0x0000:
                   14895:                case 0x0001:
                   14896:                case 0x0002:
                   14897:                case 0x0003:
                   14898:                case 0x0004:
                   14899:                case 0x0005:
                   14900:                case 0x0006:
                   14901:                case 0x0007:
                   14902:                case 0x0008:
                   14903:                case 0x000a:
                   14904:                case 0x1234:
                   14905:                        // SMARTDRV v4.00+ is not installed
                   14906:                        break;
                   14907:                default:
1.1.1.29  root     14908:                        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));
                   14909:                        REG16(AX) = 0x01;
                   14910:                        m_CF = 1;
1.1.1.42  root     14911:                        break;
1.1.1.29  root     14912:                }
                   14913:                break;
                   14914:        case 0x11:
1.1.1.42  root     14915:                switch(REG16(BX)) {
                   14916:                case 0x0000:
                   14917:                case 0x0001:
                   14918:                case 0x0002:
                   14919:                case 0x0003:
                   14920:                case 0x0004:
                   14921:                case 0x0005:
                   14922:                case 0x0006:
                   14923:                case 0x0007:
                   14924:                case 0x0008:
                   14925:                case 0x0009:
                   14926:                case 0x000a:
                   14927:                case 0x000b:
                   14928:                case 0xfffe:
                   14929:                case 0xffff:
1.1.1.29  root     14930:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14931:                        break;
                   14932:                default:
                   14933:                        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));
                   14934:                        REG16(AX) = 0x01;
                   14935:                        m_CF = 1;
                   14936:                        break;
                   14937:                }
                   14938:                break;
                   14939:        case 0x12:
                   14940:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14941:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14942:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14943:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14944:                } else {
                   14945:                        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));
                   14946:                        REG16(AX) = 0x01;
                   14947:                        m_CF = 1;
                   14948:                }
1.1.1.22  root     14949:                break;
1.1.1.42  root     14950:        case 0x13:
                   14951:                // DBLSPACE.BIN is not installed
                   14952:                break;
1.1.1.22  root     14953:        default:
                   14954:                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));
                   14955:                REG16(AX) = 0x01;
                   14956:                m_CF = 1;
                   14957:                break;
                   14958:        }
                   14959: }
                   14960: 
                   14961: inline void msdos_int_2fh_4bh()
                   14962: {
                   14963:        switch(REG8(AL)) {
1.1.1.24  root     14964:        case 0x01:
1.1.1.22  root     14965:        case 0x02:
1.1.1.29  root     14966:                // Task Switcher is not installed
1.1.1.24  root     14967:                break;
                   14968:        case 0x03:
                   14969:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14970:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14971:                break;
1.1.1.30  root     14972:        case 0x04:
                   14973:                REG16(BX) = 0x0000; // free switcher id successfully
                   14974:                break;
1.1.1.43  root     14975:        case 0x05:
                   14976:                REG16(BX) = 0x0000; // no instance data chain
                   14977:                SREG(ES) = 0x0000;
                   14978:                i386_load_segment_descriptor(ES);
                   14979:                break;
1.1       root     14980:        default:
1.1.1.22  root     14981:                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     14982:                REG16(AX) = 0x01;
1.1.1.3   root     14983:                m_CF = 1;
1.1       root     14984:                break;
                   14985:        }
                   14986: }
                   14987: 
1.1.1.44  root     14988: inline void msdos_int_2fh_4dh()
                   14989: {
                   14990:        switch(REG8(AL)) {
                   14991:        case 0x00:
                   14992:                // KKCFUNC is not installed ???
                   14993:                break;
                   14994:        default:
                   14995: //             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));
                   14996:                REG16(AX) = 0x01; // invalid function
                   14997:                m_CF = 1;
                   14998:                break;
                   14999:        }
                   15000: }
                   15001: 
1.1       root     15002: inline void msdos_int_2fh_4fh()
                   15003: {
                   15004:        switch(REG8(AL)) {
                   15005:        case 0x00:
1.1.1.29  root     15006:                // BILING is installed
1.1.1.27  root     15007:                REG16(AX) = 0x0000;
                   15008:                REG8(DL) = 0x01;        // major version
                   15009:                REG8(DH) = 0x00;        // minor version
1.1       root     15010:                break;
                   15011:        case 0x01:
1.1.1.27  root     15012:                REG16(AX) = 0x0000;
1.1       root     15013:                REG16(BX) = active_code_page;
                   15014:                break;
                   15015:        default:
1.1.1.22  root     15016:                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));
                   15017:                REG16(AX) = 0x01;
                   15018:                m_CF = 1;
                   15019:                break;
                   15020:        }
                   15021: }
                   15022: 
                   15023: inline void msdos_int_2fh_55h()
                   15024: {
                   15025:        switch(REG8(AL)) {
                   15026:        case 0x00:
                   15027:        case 0x01:
                   15028: //             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));
                   15029:                break;
                   15030:        default:
                   15031:                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     15032:                REG16(AX) = 0x01;
1.1.1.3   root     15033:                m_CF = 1;
1.1       root     15034:                break;
                   15035:        }
                   15036: }
                   15037: 
1.1.1.44  root     15038: inline void msdos_int_2fh_56h()
                   15039: {
                   15040:        switch(REG8(AL)) {
                   15041:        case 0x00:
                   15042:                // INTERLNK is not installed
                   15043:                break;
                   15044:        case 0x01:
                   15045:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   15046: //             if(msdos_is_remote_drive(REG8(BH))) {
                   15047: //                     REG8(AL) = 0x00;
                   15048: //             }
                   15049:                break;
                   15050:        default:
                   15051:                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));
                   15052:                REG16(AX) = 0x01;
                   15053:                m_CF = 1;
                   15054:                break;
                   15055:        }
                   15056: }
                   15057: 
1.1.1.24  root     15058: inline void msdos_int_2fh_adh()
                   15059: {
                   15060:        switch(REG8(AL)) {
                   15061:        case 0x00:
1.1.1.29  root     15062:                // DISPLAY.SYS is installed
1.1.1.24  root     15063:                REG8(AL) = 0xff;
                   15064:                REG16(BX) = 0x100; // ???
                   15065:                break;
                   15066:        case 0x01:
                   15067:                active_code_page = REG16(BX);
                   15068:                msdos_nls_tables_update();
                   15069:                REG16(AX) = 0x01;
                   15070:                break;
                   15071:        case 0x02:
                   15072:                REG16(BX) = active_code_page;
                   15073:                break;
                   15074:        case 0x03:
                   15075:                // FIXME
                   15076:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   15077:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   15078:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   15079:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   15080:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   15081:                break;
                   15082:        case 0x80:
1.1.1.49  root     15083:                // KEYB.COM is not installed
                   15084:                break;
1.1.1.24  root     15085:        default:
                   15086:                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));
                   15087:                REG16(AX) = 0x01;
                   15088:                m_CF = 1;
                   15089:                break;
                   15090:        }
                   15091: }
                   15092: 
1.1       root     15093: inline void msdos_int_2fh_aeh()
                   15094: {
                   15095:        switch(REG8(AL)) {
                   15096:        case 0x00:
1.1.1.28  root     15097:                // FIXME: we need to check the given command line
                   15098:                REG8(AL) = 0x00; // the command should be executed as usual
                   15099: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     15100:                break;
                   15101:        case 0x01:
                   15102:                {
                   15103:                        char command[MAX_PATH];
                   15104:                        memset(command, 0, sizeof(command));
1.1.1.3   root     15105:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     15106:                        
                   15107:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   15108:                        param->env_seg = 0;
                   15109:                        param->cmd_line.w.l = 44;
                   15110:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   15111:                        param->fcb1.w.l = 24;
                   15112:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   15113:                        param->fcb2.w.l = 24;
                   15114:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   15115:                        
                   15116:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   15117:                        
                   15118:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     15119:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   15120:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     15121:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   15122:                        
1.1.1.28  root     15123:                        try {
                   15124:                                msdos_process_exec(command, param, 0);
                   15125:                        } catch(...) {
                   15126:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     15127:                        }
                   15128:                }
                   15129:                break;
                   15130:        default:
1.1.1.22  root     15131:                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     15132:                REG16(AX) = 0x01;
1.1.1.3   root     15133:                m_CF = 1;
1.1       root     15134:                break;
                   15135:        }
                   15136: }
                   15137: 
1.1.1.34  root     15138: inline void msdos_int_2fh_b7h()
                   15139: {
                   15140:        switch(REG8(AL)) {
                   15141:        case 0x00:
                   15142:                // APPEND is not installed
                   15143: //             REG8(AL) = 0x00;
                   15144:                break;
1.1.1.44  root     15145:        case 0x06:
                   15146:                REG16(BX) = 0x0000;
                   15147:                break;
1.1.1.34  root     15148:        case 0x07:
1.1.1.43  root     15149:        case 0x11:
1.1.1.34  root     15150:                // COMMAND.COM calls this service without checking APPEND is installed
                   15151:                break;
                   15152:        default:
                   15153:                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));
                   15154:                REG16(AX) = 0x01;
                   15155:                m_CF = 1;
                   15156:                break;
                   15157:        }
                   15158: }
                   15159: 
1.1.1.24  root     15160: inline void msdos_int_33h_0000h()
                   15161: {
                   15162:        REG16(AX) = 0xffff; // hardware/driver installed
                   15163:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15164: }
                   15165: 
                   15166: inline void msdos_int_33h_0001h()
                   15167: {
1.1.1.34  root     15168:        if(mouse.hidden > 0) {
                   15169:                mouse.hidden--;
                   15170:        }
                   15171:        if(mouse.hidden == 0) {
1.1.1.64  root     15172:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), (dwConsoleMode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~ENABLE_QUICK_EDIT_MODE);
1.1.1.59  root     15173:                pic[1].imr &= ~0x10; // enable irq12
1.1.1.24  root     15174:        }
                   15175: }
                   15176: 
                   15177: inline void msdos_int_33h_0002h()
                   15178: {
1.1.1.34  root     15179:        mouse.hidden++;
1.1.1.64  root     15180:        if(dwConsoleMode & (ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE)) {
                   15181:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_EXTENDED_FLAGS);
                   15182:        } else {
                   15183:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   15184:        }
1.1.1.59  root     15185:        pic[1].imr |= 0x10; // disable irq12
1.1.1.24  root     15186: }
                   15187: 
                   15188: inline void msdos_int_33h_0003h()
                   15189: {
1.1.1.34  root     15190: //     if(mouse.hidden > 0) {
                   15191:                update_console_input();
                   15192: //     }
1.1.1.24  root     15193:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     15194:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   15195:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   15196: }
                   15197: 
                   15198: inline void msdos_int_33h_0004h()
                   15199: {
                   15200:        mouse.position.x = REG16(CX);
                   15201:        mouse.position.x = REG16(DX);
1.1.1.24  root     15202: }
                   15203: 
                   15204: inline void msdos_int_33h_0005h()
                   15205: {
1.1.1.34  root     15206: //     if(mouse.hidden > 0) {
                   15207:                update_console_input();
                   15208: //     }
1.1.1.24  root     15209:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   15210:                int idx = REG16(BX);
1.1.1.34  root     15211:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   15212:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   15213:                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     15214:                mouse.buttons[idx].pressed_times = 0;
                   15215:        } else {
                   15216:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   15217:        }
                   15218:        REG16(AX) = mouse.get_buttons();
                   15219: }
                   15220: 
                   15221: inline void msdos_int_33h_0006h()
                   15222: {
1.1.1.34  root     15223: //     if(mouse.hidden > 0) {
                   15224:                update_console_input();
                   15225: //     }
1.1.1.24  root     15226:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   15227:                int idx = REG16(BX);
1.1.1.34  root     15228:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   15229:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   15230:                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     15231:                mouse.buttons[idx].released_times = 0;
                   15232:        } else {
                   15233:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   15234:        }
                   15235:        REG16(AX) = mouse.get_buttons();
                   15236: }
                   15237: 
                   15238: inline void msdos_int_33h_0007h()
                   15239: {
                   15240:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   15241:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   15242: }
                   15243: 
                   15244: inline void msdos_int_33h_0008h()
                   15245: {
                   15246:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   15247:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   15248: }
                   15249: 
                   15250: inline void msdos_int_33h_0009h()
                   15251: {
                   15252:        mouse.hot_spot[0] = REG16(BX);
                   15253:        mouse.hot_spot[1] = REG16(CX);
                   15254: }
                   15255: 
1.1.1.49  root     15256: inline void msdos_int_33h_000ah()
                   15257: {
                   15258:        mouse.screen_mask = REG16(CX);
                   15259:        mouse.cursor_mask = REG16(DX);
                   15260: }
                   15261: 
1.1.1.24  root     15262: inline void msdos_int_33h_000bh()
                   15263: {
1.1.1.34  root     15264: //     if(mouse.hidden > 0) {
                   15265:                update_console_input();
                   15266: //     }
1.1.1.24  root     15267:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15268:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15269:        mouse.prev_position.x = mouse.position.x;
                   15270:        mouse.prev_position.y = mouse.position.y;
                   15271:        REG16(CX) = dx;
                   15272:        REG16(DX) = dy;
                   15273: }
                   15274: 
                   15275: inline void msdos_int_33h_000ch()
                   15276: {
                   15277:        mouse.call_mask = REG16(CX);
                   15278:        mouse.call_addr.w.l = REG16(DX);
                   15279:        mouse.call_addr.w.h = SREG(ES);
                   15280: }
                   15281: 
                   15282: inline void msdos_int_33h_000fh()
                   15283: {
                   15284:        mouse.mickey.x = REG16(CX);
                   15285:        mouse.mickey.y = REG16(DX);
                   15286: }
                   15287: 
                   15288: inline void msdos_int_33h_0011h()
                   15289: {
                   15290:        REG16(AX) = 0xffff;
                   15291:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15292: }
                   15293: 
                   15294: inline void msdos_int_33h_0014h()
                   15295: {
                   15296:        UINT16 old_mask = mouse.call_mask;
                   15297:        UINT16 old_ofs = mouse.call_addr.w.l;
                   15298:        UINT16 old_seg = mouse.call_addr.w.h;
                   15299:        
                   15300:        mouse.call_mask = REG16(CX);
                   15301:        mouse.call_addr.w.l = REG16(DX);
                   15302:        mouse.call_addr.w.h = SREG(ES);
                   15303:        
                   15304:        REG16(CX) = old_mask;
                   15305:        REG16(DX) = old_ofs;
                   15306:        SREG(ES) = old_seg;
                   15307:        i386_load_segment_descriptor(ES);
                   15308: }
                   15309: 
                   15310: inline void msdos_int_33h_0015h()
                   15311: {
                   15312:        REG16(BX) = sizeof(mouse);
                   15313: }
                   15314: 
                   15315: inline void msdos_int_33h_0016h()
                   15316: {
                   15317:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   15318: }
                   15319: 
                   15320: inline void msdos_int_33h_0017h()
                   15321: {
                   15322:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   15323: }
                   15324: 
1.1.1.43  root     15325: inline void msdos_int_33h_0018h()
                   15326: {
                   15327:        for(int i = 0; i < 8; i++) {
                   15328:                if(REG16(CX) & (1 << i)) {
                   15329:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   15330:                                // event handler already exists
                   15331:                                REG16(AX) = 0xffff;
                   15332:                                break;
                   15333:                        }
                   15334:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   15335:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   15336:                }
                   15337:        }
                   15338: }
                   15339: 
                   15340: inline void msdos_int_33h_0019h()
                   15341: {
                   15342:        UINT16 call_mask = REG16(CX);
                   15343:        
                   15344:        REG16(CX) = 0;
                   15345:        
                   15346:        for(int i = 0; i < 8; i++) {
                   15347:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   15348:                        for(int j = 0; j < 8; j++) {
                   15349:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   15350:                                        REG16(CX) |= (1 << j);
                   15351:                                }
                   15352:                        }
                   15353:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   15354:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   15355:                        break;
                   15356:                }
                   15357:        }
                   15358: }
                   15359: 
1.1.1.24  root     15360: inline void msdos_int_33h_001ah()
                   15361: {
                   15362:        mouse.sensitivity[0] = REG16(BX);
                   15363:        mouse.sensitivity[1] = REG16(CX);
                   15364:        mouse.sensitivity[2] = REG16(DX);
                   15365: }
                   15366: 
                   15367: inline void msdos_int_33h_001bh()
                   15368: {
                   15369:        REG16(BX) = mouse.sensitivity[0];
                   15370:        REG16(CX) = mouse.sensitivity[1];
                   15371:        REG16(DX) = mouse.sensitivity[2];
                   15372: }
                   15373: 
                   15374: inline void msdos_int_33h_001dh()
                   15375: {
                   15376:        mouse.display_page = REG16(BX);
                   15377: }
                   15378: 
                   15379: inline void msdos_int_33h_001eh()
                   15380: {
                   15381:        REG16(BX) = mouse.display_page;
                   15382: }
                   15383: 
1.1.1.34  root     15384: inline void msdos_int_33h_001fh()
                   15385: {
                   15386:        // from DOSBox
                   15387:        REG16(BX) = 0x0000;
                   15388:        SREG(ES) = 0x0000;
                   15389:        i386_load_segment_descriptor(ES);
                   15390:        mouse.enabled = false;
                   15391:        mouse.old_hidden = mouse.hidden;
                   15392:        mouse.hidden = 1;
                   15393: }
                   15394: 
                   15395: inline void msdos_int_33h_0020h()
                   15396: {
                   15397:        // from DOSBox
                   15398:        mouse.enabled = true;
                   15399:        mouse.hidden = mouse.old_hidden;
                   15400: }
                   15401: 
1.1.1.24  root     15402: inline void msdos_int_33h_0021h()
                   15403: {
                   15404:        REG16(AX) = 0xffff;
                   15405:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   15406: }
                   15407: 
                   15408: inline void msdos_int_33h_0022h()
                   15409: {
                   15410:        mouse.language = REG16(BX);
                   15411: }
                   15412: 
                   15413: inline void msdos_int_33h_0023h()
                   15414: {
                   15415:        REG16(BX) = mouse.language;
                   15416: }
                   15417: 
                   15418: inline void msdos_int_33h_0024h()
                   15419: {
                   15420:        REG16(BX) = 0x0805; // V8.05
                   15421:        REG16(CX) = 0x0400; // PS/2
                   15422: }
                   15423: 
1.1.1.49  root     15424: inline void msdos_int_33h_0025h()
                   15425: {
                   15426:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   15427: }
                   15428: 
1.1.1.24  root     15429: inline void msdos_int_33h_0026h()
                   15430: {
                   15431:        REG16(BX) = 0x0000;
                   15432:        REG16(CX) = mouse.max_position.x;
                   15433:        REG16(DX) = mouse.max_position.y;
                   15434: }
                   15435: 
1.1.1.49  root     15436: inline void msdos_int_33h_0027h()
                   15437: {
                   15438: //     if(mouse.hidden > 0) {
                   15439:                update_console_input();
                   15440: //     }
                   15441:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   15442:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   15443:        mouse.prev_position.x = mouse.position.x;
                   15444:        mouse.prev_position.y = mouse.position.y;
                   15445:        REG16(AX) = mouse.screen_mask;
                   15446:        REG16(BX) = mouse.cursor_mask;
                   15447:        REG16(CX) = dx;
                   15448:        REG16(DX) = dy;
                   15449: }
                   15450: 
                   15451: inline void msdos_int_33h_0028h()
                   15452: {
                   15453:        if(REG16(CX) != 0) {
                   15454:                UINT8 tmp = REG8(AL);
                   15455:                REG8(AL) = REG8(CL);
                   15456:                pcbios_int_10h_00h();
                   15457:                REG8(AL) = tmp;
                   15458:        }
                   15459:        REG8(CL) = 0x00; // successful
                   15460: }
                   15461: 
                   15462: inline void msdos_int_33h_0029h()
                   15463: {
                   15464:        switch(REG16(CX)) {
                   15465:        case 0x0000:
                   15466:                REG16(CX) = 0x0003;
                   15467:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   15468:                break;
                   15469:        case 0x0003:
                   15470:                REG16(CX) = 0x0070;
                   15471:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15472:                break;
                   15473:        case 0x0070:
                   15474:                REG16(CX) = 0x0071;
                   15475:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   15476:                break;
                   15477:        case 0x0071:
                   15478:                REG16(CX) = 0x0073;
                   15479:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   15480:                break;
                   15481:        default:
                   15482:                REG16(CX) = 0x0000;
                   15483:                break;
                   15484:        }
                   15485:        if(REG16(CX) != 0) {
                   15486:                SREG(DS) = (WORK_TOP >> 4);
                   15487:        } else {
                   15488:                SREG(DS) = 0x0000;
                   15489:        }
                   15490:        i386_load_segment_descriptor(DS);
                   15491:        REG16(DX) = 0x0000;
                   15492: }
                   15493: 
1.1.1.24  root     15494: inline void msdos_int_33h_002ah()
                   15495: {
1.1.1.34  root     15496:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     15497:        REG16(BX) = mouse.hot_spot[0];
                   15498:        REG16(CX) = mouse.hot_spot[1];
                   15499:        REG16(DX) = 4; // PS/2
                   15500: }
                   15501: 
                   15502: inline void msdos_int_33h_0031h()
                   15503: {
                   15504:        REG16(AX) = mouse.min_position.x;
                   15505:        REG16(BX) = mouse.min_position.y;
                   15506:        REG16(CX) = mouse.max_position.x;
                   15507:        REG16(DX) = mouse.max_position.y;
                   15508: }
                   15509: 
                   15510: inline void msdos_int_33h_0032h()
                   15511: {
                   15512:        REG16(AX) = 0;
1.1.1.49  root     15513:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     15514:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     15515:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     15516: //     REG16(AX) |= 0x1000; // 0028h
                   15517: //     REG16(AX) |= 0x0800; // 0029h
                   15518:        REG16(AX) |= 0x0400; // 002ah
                   15519: //     REG16(AX) |= 0x0200; // 002bh
                   15520: //     REG16(AX) |= 0x0100; // 002ch
                   15521: //     REG16(AX) |= 0x0080; // 002dh
                   15522: //     REG16(AX) |= 0x0040; // 002eh
                   15523:        REG16(AX) |= 0x0020; // 002fh
                   15524: //     REG16(AX) |= 0x0010; // 0030h
                   15525:        REG16(AX) |= 0x0008; // 0031h
                   15526:        REG16(AX) |= 0x0004; // 0032h
                   15527: //     REG16(AX) |= 0x0002; // 0033h
                   15528: //     REG16(AX) |= 0x0001; // 0034h
                   15529: }
                   15530: 
1.1.1.49  root     15531: inline void msdos_int_33h_004dh()
                   15532: {
                   15533:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   15534: }
                   15535: 
                   15536: inline void msdos_int_33h_006dh()
                   15537: {
                   15538:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   15539:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   15540: }
                   15541: 
1.1.1.19  root     15542: inline void msdos_int_67h_40h()
                   15543: {
                   15544:        if(!support_ems) {
                   15545:                REG8(AH) = 0x84;
                   15546:        } else {
                   15547:                REG8(AH) = 0x00;
                   15548:        }
                   15549: }
                   15550: 
                   15551: inline void msdos_int_67h_41h()
                   15552: {
                   15553:        if(!support_ems) {
                   15554:                REG8(AH) = 0x84;
                   15555:        } else {
                   15556:                REG8(AH) = 0x00;
                   15557:                REG16(BX) = EMS_TOP >> 4;
                   15558:        }
                   15559: }
                   15560: 
                   15561: inline void msdos_int_67h_42h()
                   15562: {
                   15563:        if(!support_ems) {
                   15564:                REG8(AH) = 0x84;
                   15565:        } else {
                   15566:                REG8(AH) = 0x00;
                   15567:                REG16(BX) = free_ems_pages;
                   15568:                REG16(DX) = MAX_EMS_PAGES;
                   15569:        }
                   15570: }
                   15571: 
                   15572: inline void msdos_int_67h_43h()
                   15573: {
                   15574:        if(!support_ems) {
                   15575:                REG8(AH) = 0x84;
                   15576:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15577:                REG8(AH) = 0x87;
                   15578:        } else if(REG16(BX) > free_ems_pages) {
                   15579:                REG8(AH) = 0x88;
                   15580:        } else if(REG16(BX) == 0) {
                   15581:                REG8(AH) = 0x89;
                   15582:        } else {
1.1.1.31  root     15583:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15584:                        if(!ems_handles[i].allocated) {
                   15585:                                ems_allocate_pages(i, REG16(BX));
                   15586:                                REG8(AH) = 0x00;
                   15587:                                REG16(DX) = i;
                   15588:                                return;
                   15589:                        }
                   15590:                }
                   15591:                REG8(AH) = 0x85;
                   15592:        }
                   15593: }
                   15594: 
                   15595: inline void msdos_int_67h_44h()
                   15596: {
                   15597:        if(!support_ems) {
                   15598:                REG8(AH) = 0x84;
1.1.1.31  root     15599:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15600:                REG8(AH) = 0x83;
                   15601:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   15602:                REG8(AH) = 0x8a;
                   15603: //     } else if(!(REG8(AL) < 4)) {
                   15604: //             REG8(AH) = 0x8b;
                   15605:        } else if(REG16(BX) == 0xffff) {
                   15606:                ems_unmap_page(REG8(AL) & 3);
                   15607:                REG8(AH) = 0x00;
                   15608:        } else {
                   15609:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   15610:                REG8(AH) = 0x00;
                   15611:        }
                   15612: }
                   15613: 
                   15614: inline void msdos_int_67h_45h()
                   15615: {
                   15616:        if(!support_ems) {
                   15617:                REG8(AH) = 0x84;
1.1.1.31  root     15618:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15619:                REG8(AH) = 0x83;
                   15620:        } else {
                   15621:                ems_release_pages(REG16(DX));
                   15622:                REG8(AH) = 0x00;
                   15623:        }
                   15624: }
                   15625: 
                   15626: inline void msdos_int_67h_46h()
                   15627: {
                   15628:        if(!support_ems) {
                   15629:                REG8(AH) = 0x84;
                   15630:        } else {
1.1.1.29  root     15631: //             REG16(AX) = 0x0032; // EMS 3.2
                   15632:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     15633:        }
                   15634: }
                   15635: 
                   15636: inline void msdos_int_67h_47h()
                   15637: {
                   15638:        // NOTE: the map data should be stored in the specified ems page, not process data
                   15639:        process_t *process = msdos_process_info_get(current_psp);
                   15640:        
                   15641:        if(!support_ems) {
                   15642:                REG8(AH) = 0x84;
1.1.1.31  root     15643: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15644: //             REG8(AH) = 0x83;
                   15645:        } else if(process->ems_pages_stored) {
                   15646:                REG8(AH) = 0x8d;
                   15647:        } else {
                   15648:                for(int i = 0; i < 4; i++) {
                   15649:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   15650:                        process->ems_pages[i].page   = ems_pages[i].page;
                   15651:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   15652:                }
                   15653:                process->ems_pages_stored = true;
                   15654:                REG8(AH) = 0x00;
                   15655:        }
                   15656: }
                   15657: 
                   15658: inline void msdos_int_67h_48h()
                   15659: {
                   15660:        // NOTE: the map data should be restored from the specified ems page, not process data
                   15661:        process_t *process = msdos_process_info_get(current_psp);
                   15662:        
                   15663:        if(!support_ems) {
                   15664:                REG8(AH) = 0x84;
1.1.1.31  root     15665: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15666: //             REG8(AH) = 0x83;
                   15667:        } else if(!process->ems_pages_stored) {
                   15668:                REG8(AH) = 0x8e;
                   15669:        } else {
                   15670:                for(int i = 0; i < 4; i++) {
                   15671:                        if(process->ems_pages[i].mapped) {
                   15672:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   15673:                        } else {
                   15674:                                ems_unmap_page(i);
                   15675:                        }
                   15676:                }
                   15677:                process->ems_pages_stored = false;
                   15678:                REG8(AH) = 0x00;
                   15679:        }
                   15680: }
                   15681: 
                   15682: inline void msdos_int_67h_4bh()
                   15683: {
                   15684:        if(!support_ems) {
                   15685:                REG8(AH) = 0x84;
                   15686:        } else {
                   15687:                REG8(AH) = 0x00;
                   15688:                REG16(BX) = 0;
1.1.1.31  root     15689:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15690:                        if(ems_handles[i].allocated) {
                   15691:                                REG16(BX)++;
                   15692:                        }
                   15693:                }
                   15694:        }
                   15695: }
                   15696: 
                   15697: inline void msdos_int_67h_4ch()
                   15698: {
                   15699:        if(!support_ems) {
                   15700:                REG8(AH) = 0x84;
1.1.1.31  root     15701:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15702:                REG8(AH) = 0x83;
                   15703:        } else {
                   15704:                REG8(AH) = 0x00;
                   15705:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   15706:        }
                   15707: }
                   15708: 
                   15709: inline void msdos_int_67h_4dh()
                   15710: {
                   15711:        if(!support_ems) {
                   15712:                REG8(AH) = 0x84;
                   15713:        } else {
                   15714:                REG8(AH) = 0x00;
                   15715:                REG16(BX) = 0;
1.1.1.31  root     15716:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15717:                        if(ems_handles[i].allocated) {
                   15718:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   15719:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   15720:                                REG16(BX)++;
                   15721:                        }
                   15722:                }
                   15723:        }
                   15724: }
                   15725: 
1.1.1.20  root     15726: inline void msdos_int_67h_4eh()
                   15727: {
                   15728:        if(!support_ems) {
                   15729:                REG8(AH) = 0x84;
                   15730:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15731:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   15732:                        // save page map
                   15733:                        for(int i = 0; i < 4; i++) {
                   15734:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15735:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15736:                        }
                   15737:                }
                   15738:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15739:                        // restore page map
                   15740:                        for(int i = 0; i < 4; i++) {
                   15741:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15742:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15743:                                
1.1.1.31  root     15744:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     15745:                                        ems_map_page(i, handle, page);
                   15746:                                } else {
                   15747:                                        ems_unmap_page(i);
                   15748:                                }
                   15749:                        }
                   15750:                }
                   15751:                REG8(AH) = 0x00;
                   15752:        } else if(REG8(AL) == 0x03) {
                   15753:                REG8(AH) = 0x00;
1.1.1.21  root     15754:                REG8(AL) = 4 * 4;
                   15755:        } else {
1.1.1.22  root     15756:                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     15757:                REG8(AH) = 0x8f;
                   15758:        }
                   15759: }
                   15760: 
                   15761: inline void msdos_int_67h_4fh()
                   15762: {
                   15763:        if(!support_ems) {
                   15764:                REG8(AH) = 0x84;
                   15765:        } else if(REG8(AL) == 0x00) {
                   15766:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15767:                
                   15768:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   15769:                for(int i = 0; i < count; i++) {
                   15770:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   15771:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15772:                        
                   15773: //                     if(!(physical < 4)) {
                   15774: //                             REG8(AH) = 0x8b;
                   15775: //                             return;
                   15776: //                     }
                   15777:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     15778:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   15779:                        *(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     15780:                }
                   15781:                REG8(AH) = 0x00;
                   15782:        } else if(REG8(AL) == 0x01) {
                   15783:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15784:                
                   15785:                for(int i = 0; i < count; i++) {
                   15786:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15787:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15788:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15789:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15790:                        
                   15791: //                     if(!(physical < 4)) {
                   15792: //                             REG8(AH) = 0x8b;
                   15793: //                             return;
                   15794: //                     } else
1.1.1.41  root     15795:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15796:                                ems_map_page(physical & 3, handle, logical);
                   15797:                        } else {
1.1.1.41  root     15798:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15799:                        }
                   15800:                }
                   15801:                REG8(AH) = 0x00;
                   15802:        } else if(REG8(AL) == 0x02) {
                   15803:                REG8(AH) = 0x00;
                   15804:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15805:        } else {
1.1.1.22  root     15806:                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     15807:                REG8(AH) = 0x8f;
                   15808:        }
                   15809: }
                   15810: 
                   15811: inline void msdos_int_67h_50h()
                   15812: {
                   15813:        if(!support_ems) {
                   15814:                REG8(AH) = 0x84;
1.1.1.31  root     15815:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15816:                REG8(AH) = 0x83;
                   15817:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15818:                for(int i = 0; i < REG16(CX); i++) {
                   15819:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15820:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15821:                        
                   15822:                        if(REG8(AL) == 0x01) {
                   15823:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15824:                        }
                   15825: //                     if(!(physical < 4)) {
                   15826: //                             REG8(AH) = 0x8b;
                   15827: //                             return;
                   15828: //                     } else
                   15829:                        if(logical == 0xffff) {
                   15830:                                ems_unmap_page(physical & 3);
                   15831:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15832:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15833:                        } else {
                   15834:                                REG8(AH) = 0x8a;
                   15835:                                return;
                   15836:                        }
                   15837:                }
                   15838:                REG8(AH) = 0x00;
                   15839:        } else {
1.1.1.22  root     15840:                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     15841:                REG8(AH) = 0x8f;
                   15842:        }
                   15843: }
                   15844: 
1.1.1.19  root     15845: inline void msdos_int_67h_51h()
                   15846: {
                   15847:        if(!support_ems) {
                   15848:                REG8(AH) = 0x84;
1.1.1.31  root     15849:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15850:                REG8(AH) = 0x83;
                   15851:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15852:                REG8(AH) = 0x87;
                   15853:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15854:                REG8(AH) = 0x88;
                   15855:        } else {
                   15856:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15857:                REG8(AH) = 0x00;
                   15858:        }
                   15859: }
                   15860: 
1.1.1.20  root     15861: inline void msdos_int_67h_52h()
                   15862: {
                   15863:        if(!support_ems) {
                   15864:                REG8(AH) = 0x84;
1.1.1.31  root     15865: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15866: //             REG8(AH) = 0x83;
1.1.1.20  root     15867:        } else if(REG8(AL) == 0x00) {
                   15868:                REG8(AL) = 0x00; // handle is volatile
                   15869:                REG8(AH) = 0x00;
                   15870:        } else if(REG8(AL) == 0x01) {
                   15871:                if(REG8(BL) == 0x00) {
                   15872:                        REG8(AH) = 0x00;
                   15873:                } else {
                   15874:                        REG8(AH) = 0x90; // undefined attribute type
                   15875:                }
                   15876:        } else if(REG8(AL) == 0x02) {
                   15877:                REG8(AL) = 0x00; // only volatile handles supported
                   15878:                REG8(AH) = 0x00;
                   15879:        } else {
1.1.1.22  root     15880:                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     15881:                REG8(AH) = 0x8f;
                   15882:        }
                   15883: }
                   15884: 
1.1.1.19  root     15885: inline void msdos_int_67h_53h()
                   15886: {
                   15887:        if(!support_ems) {
                   15888:                REG8(AH) = 0x84;
1.1.1.31  root     15889:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15890:                REG8(AH) = 0x83;
                   15891:        } else if(REG8(AL) == 0x00) {
                   15892:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15893:                REG8(AH) = 0x00;
                   15894:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15895:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15896:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15897:                                REG8(AH) = 0xa1;
                   15898:                                return;
                   15899:                        }
                   15900:                }
                   15901:                REG8(AH) = 0x00;
                   15902:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15903:        } else {
1.1.1.22  root     15904:                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     15905:                REG8(AH) = 0x8f;
1.1.1.19  root     15906:        }
                   15907: }
                   15908: 
                   15909: inline void msdos_int_67h_54h()
                   15910: {
                   15911:        if(!support_ems) {
                   15912:                REG8(AH) = 0x84;
                   15913:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15914:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15915:                        if(ems_handles[i].allocated) {
                   15916:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15917:                        } else {
                   15918:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15919:                        }
                   15920:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15921:                }
                   15922:                REG8(AH) = 0x00;
                   15923:                REG8(AL) = MAX_EMS_HANDLES;
                   15924:        } else if(REG8(AL) == 0x01) {
                   15925:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15926:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15927:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15928:                                REG8(AH) = 0x00;
                   15929:                                REG16(DX) = i;
                   15930:                                break;
                   15931:                        }
                   15932:                }
                   15933:        } else if(REG8(AL) == 0x02) {
                   15934:                REG8(AH) = 0x00;
                   15935:                REG16(BX) = MAX_EMS_HANDLES;
                   15936:        } else {
1.1.1.22  root     15937:                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     15938:                REG8(AH) = 0x8f;
                   15939:        }
                   15940: }
                   15941: 
1.1.1.49  root     15942: inline void msdos_int_67h_55h()
                   15943: {
                   15944:        if(!support_ems) {
                   15945:                REG8(AH) = 0x84;
                   15946:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15947:                REG8(AH) = 0x83;
                   15948:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15949:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15950:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15951:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15952:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15953:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15954:                
                   15955:                for(int i = 0; i < (int)entries; i++) {
                   15956:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15957:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15958:                        
                   15959:                        if(REG8(AL) == 0x01) {
                   15960:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15961:                        }
                   15962: //                     if(!(physical < 4)) {
                   15963: //                             REG8(AH) = 0x8b;
                   15964: //                             return;
                   15965: //                     } else
                   15966:                        if(logical == 0xffff) {
                   15967:                                ems_unmap_page(physical & 3);
                   15968:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15969:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15970:                        } else {
                   15971:                                REG8(AH) = 0x8a;
                   15972:                                return;
                   15973:                        }
                   15974:                }
                   15975:                i386_jmp_far(jump_seg, jump_ofs);
                   15976:                REG8(AH) = 0x00;
                   15977:        } else {
                   15978:                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));
                   15979:                REG8(AH) = 0x8f;
                   15980:        }
                   15981: }
                   15982: 
                   15983: inline void msdos_int_67h_56h()
                   15984: {
                   15985:        if(!support_ems) {
                   15986:                REG8(AH) = 0x84;
                   15987:        } else if(REG8(AL) == 0x02) {
                   15988:                REG16(BX) = (2 + 2) * 4;
                   15989:                REG8(AH) = 0x00;
                   15990:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15991:                REG8(AH) = 0x83;
                   15992:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15993:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15994:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15995:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15996:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15997:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15998: #if 0
                   15999:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   16000:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   16001:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   16002: #endif
                   16003:                UINT16 handles[4], pages[4];
                   16004:                
                   16005:                // alter page map and call routine is at fffc:001f
                   16006:                if(!(call_seg == 0 && call_ofs == 0)) {
                   16007:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   16008:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   16009:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   16010:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   16011:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   16012:                } else {
                   16013:                        // invalid call addr :-(
                   16014:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   16015:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   16016:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   16017:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   16018:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   16019:                }
                   16020:                // do call far (push cs/ip) in old mapping
                   16021:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   16022:                
                   16023:                // get old mapping data
                   16024: #if 0
                   16025:                for(int i = 0; i < (int)old_entries; i++) {
                   16026:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   16027:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   16028:                        
                   16029:                        if(REG8(AL) == 0x01) {
                   16030:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   16031:                        }
                   16032: //                     if(!(physical < 4)) {
                   16033: //                             REG8(AH) = 0x8b;
                   16034: //                             return;
                   16035: //                     } else
                   16036:                        if(logical == 0xffff) {
                   16037:                                ems_unmap_page(physical & 3);
                   16038:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   16039:                                ems_map_page(physical & 3, REG16(DX), logical);
                   16040:                        } else {
                   16041:                                REG8(AH) = 0x8a;
                   16042:                                return;
                   16043:                        }
                   16044:                }
                   16045: #endif
                   16046:                for(int i = 0; i < 4; i++) {
                   16047:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   16048:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   16049:                }
                   16050:                
                   16051:                // set new mapping
                   16052:                for(int i = 0; i < (int)new_entries; i++) {
                   16053:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   16054:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   16055:                        
                   16056:                        if(REG8(AL) == 0x01) {
                   16057:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   16058:                        }
                   16059: //                     if(!(physical < 4)) {
                   16060: //                             REG8(AH) = 0x8b;
                   16061: //                             return;
                   16062: //                     } else
                   16063:                        if(logical == 0xffff) {
                   16064:                                ems_unmap_page(physical & 3);
                   16065:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   16066:                                ems_map_page(physical & 3, REG16(DX), logical);
                   16067:                        } else {
                   16068:                                REG8(AH) = 0x8a;
                   16069:                                return;
                   16070:                        }
                   16071:                }
                   16072:                
                   16073:                // push old mapping data in new mapping
                   16074:                for(int i = 0; i < 4; i++) {
                   16075:                        i386_push16(handles[i]);
                   16076:                        i386_push16(pages  [i]);
                   16077:                }
                   16078:                REG8(AH) = 0x00;
                   16079:        } else {
                   16080:                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));
                   16081:                REG8(AH) = 0x8f;
                   16082:        }
                   16083: }
                   16084: 
1.1.1.20  root     16085: inline void msdos_int_67h_57h_tmp()
                   16086: {
                   16087:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16088:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16089:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   16090:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   16091:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   16092:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   16093:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16094:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   16095:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   16096:        
1.1.1.32  root     16097:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     16098:        UINT32 src_addr, dest_addr;
                   16099:        UINT32 src_addr_max, dest_addr_max;
                   16100:        
                   16101:        if(src_type == 0) {
                   16102:                src_buffer = mem;
                   16103:                src_addr = (src_seg << 4) + src_ofs;
                   16104:                src_addr_max = MAX_MEM;
                   16105:        } else {
1.1.1.31  root     16106:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     16107:                        REG8(AH) = 0x83;
                   16108:                        return;
                   16109:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   16110:                        REG8(AH) = 0x8a;
                   16111:                        return;
                   16112:                }
1.1.1.32  root     16113:                if(ems_handles[src_handle].buffer != NULL) {
                   16114:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   16115:                }
1.1.1.20  root     16116:                src_addr = src_ofs;
1.1.1.32  root     16117:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     16118:        }
                   16119:        if(dest_type == 0) {
                   16120:                dest_buffer = mem;
                   16121:                dest_addr = (dest_seg << 4) + dest_ofs;
                   16122:                dest_addr_max = MAX_MEM;
                   16123:        } else {
1.1.1.31  root     16124:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     16125:                        REG8(AH) = 0x83;
                   16126:                        return;
                   16127:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   16128:                        REG8(AH) = 0x8a;
                   16129:                        return;
                   16130:                }
1.1.1.32  root     16131:                if(ems_handles[dest_handle].buffer != NULL) {
                   16132:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   16133:                }
1.1.1.20  root     16134:                dest_addr = dest_ofs;
1.1.1.32  root     16135:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     16136:        }
1.1.1.32  root     16137:        if(src_buffer != NULL && dest_buffer != NULL) {
                   16138:                for(int i = 0; i < copy_length; i++) {
                   16139:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16140:                                if(REG8(AL) == 0x00) {
                   16141:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16142:                                } else if(REG8(AL) == 0x01) {
                   16143:                                        UINT8 tmp = dest_buffer[dest_addr];
                   16144:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   16145:                                        src_buffer[src_addr++] = tmp;
                   16146:                                }
                   16147:                        } else {
                   16148:                                REG8(AH) = 0x93;
                   16149:                                return;
1.1.1.20  root     16150:                        }
                   16151:                }
1.1.1.32  root     16152:                REG8(AH) = 0x00;
                   16153:        } else {
                   16154:                REG8(AH) = 0x80;
1.1.1.20  root     16155:        }
                   16156: }
                   16157: 
                   16158: inline void msdos_int_67h_57h()
                   16159: {
                   16160:        if(!support_ems) {
                   16161:                REG8(AH) = 0x84;
                   16162:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   16163:                struct {
                   16164:                        UINT16 handle;
                   16165:                        UINT16 page;
                   16166:                        bool mapped;
                   16167:                } tmp_pages[4];
                   16168:                
                   16169:                // unmap pages to copy memory data to ems buffer
                   16170:                for(int i = 0; i < 4; i++) {
                   16171:                        tmp_pages[i].handle = ems_pages[i].handle;
                   16172:                        tmp_pages[i].page   = ems_pages[i].page;
                   16173:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   16174:                        ems_unmap_page(i);
                   16175:                }
                   16176:                
                   16177:                // run move/exchange operation
                   16178:                msdos_int_67h_57h_tmp();
                   16179:                
                   16180:                // restore unmapped pages
                   16181:                for(int i = 0; i < 4; i++) {
                   16182:                        if(tmp_pages[i].mapped) {
                   16183:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   16184:                        }
                   16185:                }
                   16186:        } else {
1.1.1.22  root     16187:                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     16188:                REG8(AH) = 0x8f;
                   16189:        }
                   16190: }
                   16191: 
                   16192: inline void msdos_int_67h_58h()
                   16193: {
                   16194:        if(!support_ems) {
                   16195:                REG8(AH) = 0x84;
                   16196:        } else if(REG8(AL) == 0x00) {
                   16197:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     16198:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   16199:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     16200:                }
                   16201:                REG8(AH) = 0x00;
                   16202:                REG16(CX) = 4;
                   16203:        } else if(REG8(AL) == 0x01) {
                   16204:                REG8(AH) = 0x00;
                   16205:                REG16(CX) = 4;
                   16206:        } else {
1.1.1.22  root     16207:                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     16208:                REG8(AH) = 0x8f;
                   16209:        }
                   16210: }
                   16211: 
1.1.1.42  root     16212: inline void msdos_int_67h_59h()
                   16213: {
                   16214:        if(!support_ems) {
                   16215:                REG8(AH) = 0x84;
                   16216:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     16217:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   16218:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   16219:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   16220:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   16221:                REG8(AH) = 0x00;
                   16222: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     16223:        } else if(REG8(AL) == 0x01) {
                   16224:                REG8(AH) = 0x00;
                   16225:                REG16(BX) = free_ems_pages;
                   16226:                REG16(DX) = MAX_EMS_PAGES;
                   16227:        } else {
                   16228:                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));
                   16229:                REG8(AH) = 0x8f;
                   16230:        }
                   16231: }
                   16232: 
1.1.1.20  root     16233: inline void msdos_int_67h_5ah()
                   16234: {
                   16235:        if(!support_ems) {
1.1.1.19  root     16236:                REG8(AH) = 0x84;
1.1.1.20  root     16237:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   16238:                REG8(AH) = 0x87;
                   16239:        } else if(REG16(BX) > free_ems_pages) {
                   16240:                REG8(AH) = 0x88;
                   16241: //     } else if(REG16(BX) == 0) {
                   16242: //             REG8(AH) = 0x89;
                   16243:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     16244:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     16245:                        if(!ems_handles[i].allocated) {
                   16246:                                ems_allocate_pages(i, REG16(BX));
                   16247:                                REG8(AH) = 0x00;
                   16248:                                REG16(DX) = i;
                   16249:                                return;
                   16250:                        }
                   16251:                }
                   16252:                REG8(AH) = 0x85;
                   16253:        } else {
1.1.1.22  root     16254:                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     16255:                REG8(AH) = 0x8f;
1.1.1.19  root     16256:        }
                   16257: }
                   16258: 
1.1.1.49  root     16259: inline void msdos_int_67h_5bh()
                   16260: {
                   16261:        static UINT8  stored_bl = 0x00;
                   16262:        static UINT16 stored_es = 0x0000;
                   16263:        static UINT16 stored_di = 0x0000;
                   16264:        
                   16265:        if(!support_ems) {
                   16266:                REG8(AH) = 0x84;
                   16267:        } else if(REG8(AL) == 0x00) {
                   16268:                if(stored_bl == 0x00) {
                   16269:                        if(!(stored_es == 0 && stored_di == 0)) {
                   16270:                                for(int i = 0; i < 4; i++) {
                   16271:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   16272:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   16273:                                }
                   16274:                        }
                   16275:                        SREG(ES) = stored_es;
                   16276:                        i386_load_segment_descriptor(ES);
                   16277:                        REG16(DI) = stored_di;
                   16278:                } else {
                   16279:                        REG8(BL) = stored_bl;
                   16280:                }
                   16281:                REG8(AH) = 0x00;
                   16282:        } else if(REG8(AL) == 0x01) {
                   16283:                if(REG8(BL) == 0x00) {
                   16284:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   16285:                                for(int i = 0; i < 4; i++) {
                   16286:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   16287:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   16288:                                        
                   16289:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   16290:                                                ems_map_page(i, handle, page);
                   16291:                                        } else {
                   16292:                                                ems_unmap_page(i);
                   16293:                                        }
                   16294:                                }
                   16295:                        }
                   16296:                }
                   16297:                stored_bl = REG8(BL);
                   16298:                stored_es = SREG(ES);
                   16299:                stored_di = REG16(DI);
                   16300:                REG8(AH) = 0x00;
                   16301:        } else if(REG8(AL) == 0x02) {
                   16302:                REG16(DX) = 4 * 4;
                   16303:                REG8(AH) = 0x00;
                   16304:        } else if(REG8(AL) == 0x03) {
                   16305:                REG8(BL) = 0x00; // not supported
                   16306:                REG8(AH) = 0x00;
                   16307:        } else if(REG8(AL) == 0x04) {
                   16308:                REG8(AH) = 0x00;
                   16309:        } else if(REG8(AL) == 0x05) {
                   16310:                REG8(BL) = 0x00; // not supported
                   16311:                REG8(AH) = 0x00;
                   16312:        } else {
                   16313:                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));
                   16314:                REG8(AH) = 0x8f;
                   16315:        }
                   16316: }
                   16317: 
1.1.1.43  root     16318: inline void msdos_int_67h_5dh()
                   16319: {
                   16320:        if(!support_ems) {
                   16321:                REG8(AH) = 0x84;
                   16322:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   16323:                REG8(AH) = 0xa4; // operating system denied access
                   16324:        } else {
                   16325:                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));
                   16326:                REG8(AH) = 0x8f;
                   16327:        }
                   16328: }
                   16329: 
1.1.1.49  root     16330: inline void msdos_int_67h_70h()
                   16331: {
                   16332:        if(!support_ems) {
                   16333:                REG8(AH) = 0x84;
                   16334:        } else if(REG8(AL) == 0x00) {
                   16335:                REG8(AL) = 0x00;
                   16336:                REG8(AH) = 0x00;
                   16337:        } else if(REG8(AL) == 0x01) {
                   16338:                REG8(AL) = 0x00;
                   16339: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   16340:                REG8(AH) = 0x00;
                   16341:        } else {
                   16342:                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));
                   16343:                REG8(AH) = 0x8f;
                   16344:        }
                   16345: }
                   16346: 
1.1.1.30  root     16347: inline void msdos_int_67h_deh()
                   16348: {
1.1.1.63  root     16349: #if defined(SUPPORT_VCPI)
                   16350:        if(!support_ems) {
                   16351:                REG8(AH) = 0x84;
                   16352:        } else if(REG8(AL) == 0x00) {
                   16353:                REG8(AH) = 0x00;
                   16354:                REG16(BX) = 0x0100;
                   16355:        } else if(REG8(AL) == 0x01) {
                   16356:                REG8(AH) = 0x00;
                   16357:                // from DOSBox
                   16358:                for(int ct = 0; ct < 0xff; ct++) {
                   16359:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x00) = 0x67;            // access bits
                   16360:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x01) = ct * 0x10;       // mapping
                   16361:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x03) = 0x00;
                   16362:                }
                   16363:                for(int ct = 0xff; ct < 0x100; ct++) {
                   16364:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x00) = 0x67;            // access bits
                   16365:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x01) = (ct - 0xff) * 0x10 + 0x1100;     // mapping
                   16366:                        *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + ct * 4 + 0x03) = 0x00;
                   16367:                }
                   16368:                REG16(DI) += 0x400;             // advance pointer by 0x100*4
                   16369:                
                   16370:                // Set up three descriptor table entries
                   16371:                UINT32 cbseg_low  = (DUMMY_TOP & 0x00ffff) << 16;
                   16372:                UINT32 cbseg_high = (DUMMY_TOP & 0x1f0000) >> 16;
                   16373:                // Descriptor 1 (code segment, callback segment)
                   16374:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x0000ffff | cbseg_low ;
                   16375:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = 0x00009a00 | cbseg_high;
                   16376:                // Descriptor 2 (data segment, full access)
                   16377:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x08) = 0x0000ffff;
                   16378:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c) = 0x00009200;
                   16379:                // Descriptor 3 (full access)
                   16380:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10) = 0x0000ffff;
                   16381:                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x14) = 0x00009200;
                   16382:                // Offset in code segment of protected mode entry point
                   16383:                REG32(EBX) = 0x2a; // fffc:002a
                   16384:        } else if(REG8(AL) == 0x02) {
                   16385:                REG8(AH) = 0x00;
                   16386:                REG32(EDX) = (MAX_MEM - 1) & 0xfffff000;
                   16387:        } else if(REG8(AL) == 0x03) {
                   16388:                REG8(AH) = 0x00;
                   16389:                REG32(EDX) = 0;
                   16390:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16391:                        if(emb_handle->handle == 0) {
                   16392:                                REG32(EDX) += emb_handle->size_kb;
                   16393:                        }
                   16394:                }
                   16395:                REG32(EDX) /= 4;
                   16396:        } else if(REG8(AL) == 0x04) {
                   16397:                emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(4);
                   16398:                if(emb_handle != NULL) {
                   16399:                        REG8(AH) = 0x00;
                   16400:                        REG32(EDX) = emb_handle->address;
                   16401:                }
                   16402:        } else if(REG8(AL) == 0x05) {
                   16403:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16404:                        if(emb_handle->handle != 0 && emb_handle->address == REG32(EDX)) {
                   16405:                                REG8(AH) = 0x00;
                   16406:                                msdos_xms_free_emb_handle(emb_handle);
                   16407:                                break;
                   16408:                        }
                   16409:                }
                   16410:        } else if(REG8(AL) == 0x06) {
                   16411:                REG8(AH) = 0x00;
                   16412:                REG32(EDX) = REG16(CX) << 12;
                   16413:        } else if(REG8(AL) == 0x07) {
                   16414:                REG8(AH) = 0x00;
                   16415:                REG32(EBX) = m_cr[0];
                   16416:        } else if(REG8(AL) == 0x08) {
                   16417:                REG8(AH) = 0x00;
                   16418:                for(int i = 0; i < 8; i++) {
                   16419:                        *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i) = m_dr[i];
                   16420:                }
                   16421:        } else if(REG8(AL) == 0x09) {
                   16422:                REG8(AH) = 0x00;
                   16423:                for(int i = 0; i < 8; i++) {
                   16424:                        m_dr[i] = *(UINT32 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i);
                   16425:                }
                   16426:        } else if(REG8(AL) == 0x0a) {
                   16427:                REG8(AH) = 0x00;
                   16428:                REG16(BX) = pic[0].icw2;
                   16429:                REG16(CX) = pic[1].icw2;
                   16430:        } else if(REG8(AL) == 0x0b) {
                   16431:                REG8(AH) = 0x00;
                   16432:                pic[0].icw2 = REG8(BL);
                   16433:                pic[1].icw2 = REG8(CL);
                   16434:        } else if(REG8(AL) == 0x0c) {
                   16435:                // from DOSBox
                   16436:                m_IF = 0;
                   16437:                m_CPL = 0;
                   16438:                
                   16439:                // Read data from ESI (linear address)
                   16440:                UINT32 new_cr3      = *(UINT32 *)(mem + REG32(ESI) + 0x00);
                   16441:                UINT32 new_gdt_addr = *(UINT32 *)(mem + REG32(ESI) + 0x04);
                   16442:                UINT32 new_idt_addr = *(UINT32 *)(mem + REG32(ESI) + 0x08);
                   16443:                UINT16 new_ldt      = *(UINT16 *)(mem + REG32(ESI) + 0x0c);
                   16444:                UINT16 new_tr       = *(UINT16 *)(mem + REG32(ESI) + 0x0e);
                   16445:                UINT32 new_eip      = *(UINT32 *)(mem + REG32(ESI) + 0x10);
                   16446:                UINT16 new_cs       = *(UINT16 *)(mem + REG32(ESI) + 0x14);
                   16447:                
                   16448:                // Get GDT and IDT entries
                   16449:                UINT16 new_gdt_limit = *(UINT16 *)(mem + new_gdt_addr + 0);
                   16450:                UINT32 new_gdt_base  = *(UINT32 *)(mem + new_gdt_addr + 2);
                   16451:                UINT16 new_idt_limit = *(UINT16 *)(mem + new_idt_addr + 0);
                   16452:                UINT32 new_idt_base  = *(UINT32 *)(mem + new_idt_addr + 2);
                   16453:                
                   16454:                // Switch to protected mode, paging enabled if necessary
                   16455:                if(new_cr3 != 0) {
                   16456:                        m_cr[0] |= 0x80000000;
                   16457:                }
                   16458:                m_cr[3] = new_cr3;
                   16459:                
                   16460:                *(UINT8 *)(mem + new_gdt_base + (new_tr & 0xfff8) + 5) &= 0xfd;
                   16461:                
                   16462:                // Load tables and initialize segment registers
                   16463:                m_gdtr.limit = new_gdt_limit;
                   16464:                m_gdtr.base = new_gdt_base;
                   16465:                m_idtr.limit = new_idt_limit;
                   16466:                m_idtr.base = new_idt_base;
                   16467:                
1.1.1.64  root     16468:                i386_sreg_load(0x00, DS, NULL);
                   16469:                i386_sreg_load(0x00, ES, NULL);
                   16470:                i386_sreg_load(0x00, FS, NULL);
                   16471:                i386_sreg_load(0x00, GS, NULL);
1.1.1.63  root     16472:                
                   16473: //             i386_set_a20_line(1);
                   16474:                
                   16475:                /* Switch to protected mode */
                   16476:                m_VM = m_NT = 0;
                   16477:                m_IOP1 = m_IOP2 = 1;
                   16478:                
                   16479:                i386_jmp_far(new_cs, new_eip);
                   16480:        } else {
                   16481:                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));
                   16482:                REG8(AH) = 0x8f;
                   16483:        }
                   16484: #else
1.1.1.30  root     16485:        REG8(AH) = 0x84;
1.1.1.63  root     16486: #endif
1.1.1.30  root     16487: }
                   16488: 
1.1.1.19  root     16489: #ifdef SUPPORT_XMS
                   16490: 
1.1.1.32  root     16491: void msdos_xms_init()
1.1.1.26  root     16492: {
1.1.1.30  root     16493:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16494:        emb_handle_top->address = EMB_TOP;
                   16495:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     16496:        xms_a20_local_enb_count = 0;
                   16497: }
                   16498: 
1.1.1.32  root     16499: void msdos_xms_finish()
                   16500: {
                   16501:        msdos_xms_release();
                   16502: }
                   16503: 
                   16504: void msdos_xms_release()
1.1.1.30  root     16505: {
                   16506:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   16507:                emb_handle_t *next_handle = emb_handle->next;
                   16508:                free(emb_handle);
                   16509:                emb_handle = next_handle;
                   16510:        }
                   16511: }
                   16512: 
                   16513: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   16514: {
                   16515:        if(handle != 0) {
                   16516:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16517:                        if(emb_handle->handle == handle) {
                   16518:                                return(emb_handle);
                   16519:                        }
                   16520:                }
                   16521:        }
                   16522:        return(NULL);
                   16523: }
                   16524: 
                   16525: int msdos_xms_get_unused_emb_handle_id()
                   16526: {
                   16527:        for(int handle = 1;; handle++) {
                   16528:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   16529:                        return(handle);
                   16530:                }
                   16531:        }
                   16532:        return(0);
                   16533: }
                   16534: 
                   16535: int msdos_xms_get_unused_emb_handle_count()
                   16536: {
                   16537:        int count = 64; //255;
                   16538:        
                   16539:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16540:                if(emb_handle->handle != 0) {
                   16541:                        if(--count == 1) {
                   16542:                                break;
                   16543:                        }
                   16544:                }
                   16545:        }
                   16546:        return(count);
                   16547: }
                   16548: 
                   16549: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   16550: {
                   16551:        if(emb_handle->size_kb > size_kb) {
                   16552:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   16553:                
                   16554:                new_handle->address = emb_handle->address + size_kb * 1024;
                   16555:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   16556:                emb_handle->size_kb = size_kb;
                   16557:                
                   16558:                new_handle->prev = emb_handle;
                   16559:                new_handle->next = emb_handle->next;
                   16560:                if(emb_handle->next != NULL) {
                   16561:                        emb_handle->next->prev = new_handle;
                   16562:                }
                   16563:                emb_handle->next = new_handle;
                   16564:        }
                   16565: }
                   16566: 
                   16567: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   16568: {
                   16569:        emb_handle_t *next_handle = emb_handle->next;
                   16570:        
                   16571:        if(next_handle != NULL) {
                   16572:                emb_handle->size_kb += next_handle->size_kb;
                   16573:                
                   16574:                if(next_handle->next != NULL) {
                   16575:                        next_handle->next->prev = emb_handle;
                   16576:                }
                   16577:                emb_handle->next = next_handle->next;
                   16578:                free(next_handle);
                   16579:        }
                   16580: }
                   16581: 
                   16582: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   16583: {
                   16584:        emb_handle_t *target_handle = NULL;
                   16585:        
                   16586:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16587:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   16588:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   16589:                                target_handle = emb_handle;
                   16590:                        }
                   16591:                }
                   16592:        }
                   16593:        if(target_handle != NULL) {
                   16594:                if(target_handle->size_kb > size_kb) {
                   16595:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   16596:                }
                   16597: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16598:                return(target_handle);
                   16599:        }
                   16600:        return(NULL);
                   16601: }
                   16602: 
                   16603: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   16604: {
                   16605:        emb_handle_t *prev_handle = emb_handle->prev;
                   16606:        emb_handle_t *next_handle = emb_handle->next;
                   16607:        
                   16608:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   16609:                msdos_xms_combine_emb_handles(prev_handle);
                   16610:                emb_handle = prev_handle;
                   16611:        }
                   16612:        if(next_handle != NULL && next_handle->handle == 0) {
                   16613:                msdos_xms_combine_emb_handles(emb_handle);
                   16614:        }
                   16615:        emb_handle->handle = 0;
                   16616: }
                   16617: 
1.1.1.19  root     16618: inline void msdos_call_xms_00h()
                   16619: {
1.1.1.29  root     16620: #if defined(HAS_I386)
                   16621:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.63  root     16622:        REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
                   16623: //     REG16(BX) = 0x035f; // V3.95 (Driver Revision)
1.1.1.29  root     16624: #else
                   16625:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   16626:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   16627: #endif
                   16628: //     REG16(DX) = 0x0000; // HMA does not exist
                   16629:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     16630: }
                   16631: 
                   16632: inline void msdos_call_xms_01h()
                   16633: {
1.1.1.29  root     16634:        if(REG8(AL) == 0x40) {
                   16635:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   16636:                // DX=KB free extended memory returned by last call of function 08h
                   16637:                REG16(AX) = 0x0000;
                   16638:                REG8(BL) = 0x91;
                   16639:                REG16(DX) = xms_dx_after_call_08h;
                   16640:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16641:                REG16(AX) = 0x0000;
                   16642:                REG8(BL) = 0x81; // Vdisk was detected
                   16643: #ifdef SUPPORT_HMA
                   16644:        } else if(is_hma_used_by_int_2fh) {
                   16645:                REG16(AX) = 0x0000;
                   16646:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16647:        } else if(is_hma_used_by_xms) {
                   16648:                REG16(AX) = 0x0000;
                   16649:                REG8(BL) = 0x91; // HMA is already in use
                   16650:        } else {
                   16651:                REG16(AX) = 0x0001;
                   16652:                is_hma_used_by_xms = true;
                   16653: #else
                   16654:        } else {
                   16655:                REG16(AX) = 0x0000;
                   16656:                REG8(BL) = 0x91; // HMA is already in use
                   16657: #endif
                   16658:        }
1.1.1.19  root     16659: }
                   16660: 
                   16661: inline void msdos_call_xms_02h()
                   16662: {
1.1.1.29  root     16663:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   16664:                REG16(AX) = 0x0000;
                   16665:                REG8(BL) = 0x81; // Vdisk was detected
                   16666: #ifdef SUPPORT_HMA
                   16667:        } else if(is_hma_used_by_int_2fh) {
                   16668:                REG16(AX) = 0x0000;
                   16669:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   16670:        } else if(!is_hma_used_by_xms) {
                   16671:                REG16(AX) = 0x0000;
                   16672:                REG8(BL) = 0x93; // HMA is not allocated
                   16673:        } else {
                   16674:                REG16(AX) = 0x0001;
                   16675:                is_hma_used_by_xms = false;
                   16676:                // restore first free mcb in high memory area
                   16677:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16678: #else
                   16679:        } else {
                   16680:                REG16(AX) = 0x0000;
                   16681:                REG8(BL) = 0x91; // HMA is already in use
                   16682: #endif
                   16683:        }
1.1.1.19  root     16684: }
                   16685: 
                   16686: inline void msdos_call_xms_03h()
                   16687: {
                   16688:        i386_set_a20_line(1);
                   16689:        REG16(AX) = 0x0001;
                   16690:        REG8(BL) = 0x00;
                   16691: }
                   16692: 
                   16693: inline void msdos_call_xms_04h()
                   16694: {
1.1.1.21  root     16695:        i386_set_a20_line(0);
                   16696:        REG16(AX) = 0x0001;
                   16697:        REG8(BL) = 0x00;
1.1.1.19  root     16698: }
                   16699: 
                   16700: inline void msdos_call_xms_05h()
                   16701: {
                   16702:        i386_set_a20_line(1);
                   16703:        REG16(AX) = 0x0001;
                   16704:        REG8(BL) = 0x00;
1.1.1.21  root     16705:        xms_a20_local_enb_count++;
1.1.1.19  root     16706: }
                   16707: 
                   16708: void msdos_call_xms_06h()
                   16709: {
1.1.1.21  root     16710:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     16711:                if(--xms_a20_local_enb_count == 0) {
                   16712:                        i386_set_a20_line(0);
                   16713:                        REG16(AX) = 0x0001;
                   16714:                        REG8(BL) = 0x00;
                   16715:                } else {
                   16716:                        REG16(AX) = 0x0000;
                   16717:                        REG8(BL) = 0x94;
                   16718:                }
1.1.1.21  root     16719:        } else {
1.1.1.45  root     16720:                i386_set_a20_line(0);
1.1.1.21  root     16721:                REG16(AX) = 0x0001;
                   16722:                REG8(BL) = 0x00;
1.1.1.19  root     16723:        }
                   16724: }
                   16725: 
                   16726: inline void msdos_call_xms_07h()
                   16727: {
                   16728:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   16729:        REG8(BL) = 0x00;
                   16730: }
                   16731: 
                   16732: inline void msdos_call_xms_08h()
                   16733: {
1.1.1.45  root     16734:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     16735:        
1.1.1.30  root     16736:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16737:                if(emb_handle->handle == 0) {
1.1.1.45  root     16738:                        if(eax < emb_handle->size_kb) {
                   16739:                                eax = emb_handle->size_kb;
1.1.1.19  root     16740:                        }
1.1.1.45  root     16741:                        edx += emb_handle->size_kb;
1.1.1.19  root     16742:                }
                   16743:        }
1.1.1.45  root     16744:        if(eax > 65535) {
                   16745:                eax = 65535;
                   16746:        }
                   16747:        if(edx > 65535) {
                   16748:                edx = 65535;
                   16749:        }
                   16750:        if(eax == 0 && edx == 0) {
1.1.1.19  root     16751:                REG8(BL) = 0xa0;
                   16752:        } else {
                   16753:                REG8(BL) = 0x00;
                   16754:        }
1.1.1.45  root     16755: #if defined(HAS_I386)
                   16756:        REG32(EAX) = eax;
                   16757:        REG32(EDX) = edx;
                   16758: #else
                   16759:        REG16(AX) = (UINT16)eax;
                   16760:        REG16(DX) = (UINT16)edx;
                   16761: #endif
1.1.1.29  root     16762:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     16763: }
                   16764: 
1.1.1.30  root     16765: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     16766: {
1.1.1.30  root     16767:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   16768:        
                   16769:        if(emb_handle != NULL) {
                   16770:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   16771:                
                   16772:                REG16(AX) = 0x0001;
                   16773:                REG16(DX) = emb_handle->handle;
                   16774:                REG8(BL) = 0x00;
                   16775:        } else {
                   16776:                REG16(AX) = REG16(DX) = 0x0000;
                   16777:                REG8(BL) = 0xa0;
1.1.1.19  root     16778:        }
1.1.1.30  root     16779: }
                   16780: 
                   16781: inline void msdos_call_xms_09h()
                   16782: {
                   16783:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     16784: }
                   16785: 
                   16786: inline void msdos_call_xms_0ah()
                   16787: {
1.1.1.30  root     16788:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16789:        
                   16790:        if(emb_handle == NULL) {
1.1.1.19  root     16791:                REG16(AX) = 0x0000;
                   16792:                REG8(BL) = 0xa2;
1.1.1.45  root     16793: //     } else if(emb_handle->lock > 0) {
                   16794: //             REG16(AX) = 0x0000;
                   16795: //             REG8(BL) = 0xab;
1.1.1.19  root     16796:        } else {
1.1.1.30  root     16797:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     16798:                
                   16799:                REG16(AX) = 0x0001;
                   16800:                REG8(BL) = 0x00;
                   16801:        }
                   16802: }
                   16803: 
                   16804: inline void msdos_call_xms_0bh()
                   16805: {
                   16806:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   16807:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   16808:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   16809:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   16810:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   16811:        
                   16812:        UINT8 *src_buffer, *dest_buffer;
                   16813:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     16814:        emb_handle_t *emb_handle;
1.1.1.19  root     16815:        
                   16816:        if(src_handle == 0) {
                   16817:                src_buffer = mem;
                   16818:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   16819:                src_addr_max = MAX_MEM;
                   16820:        } else {
1.1.1.30  root     16821:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     16822:                        REG16(AX) = 0x0000;
                   16823:                        REG8(BL) = 0xa3;
                   16824:                        return;
                   16825:                }
1.1.1.30  root     16826:                src_buffer = mem + emb_handle->address;
                   16827:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16828:        }
                   16829:        if(dest_handle == 0) {
                   16830:                dest_buffer = mem;
                   16831:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   16832:                dest_addr_max = MAX_MEM;
                   16833:        } else {
1.1.1.30  root     16834:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     16835:                        REG16(AX) = 0x0000;
                   16836:                        REG8(BL) = 0xa5;
                   16837:                        return;
                   16838:                }
1.1.1.30  root     16839:                dest_buffer = mem + emb_handle->address;
                   16840:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     16841:        }
                   16842:        for(int i = 0; i < copy_length; i++) {
                   16843:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   16844:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   16845:                } else {
                   16846:                        break;
                   16847:                }
                   16848:        }
                   16849:        REG16(AX) = 0x0001;
                   16850:        REG8(BL) = 0x00;
                   16851: }
                   16852: 
                   16853: inline void msdos_call_xms_0ch()
                   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;
                   16860:        } else {
1.1.1.45  root     16861:                if(emb_handle->lock < 255) {
                   16862:                        emb_handle->lock++;
                   16863:                }
1.1.1.19  root     16864:                REG16(AX) = 0x0001;
1.1.1.30  root     16865:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   16866:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     16867:        }
                   16868: }
                   16869: 
                   16870: inline void msdos_call_xms_0dh()
                   16871: {
1.1.1.30  root     16872:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16873:        
                   16874:        if(emb_handle == NULL) {
1.1.1.19  root     16875:                REG16(AX) = 0x0000;
                   16876:                REG8(BL) = 0xa2;
1.1.1.30  root     16877:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     16878:                REG16(AX) = 0x0000;
                   16879:                REG8(BL) = 0xaa;
                   16880:        } else {
1.1.1.30  root     16881:                emb_handle->lock--;
1.1.1.19  root     16882:                REG16(AX) = 0x0001;
                   16883:                REG8(BL) = 0x00;
                   16884:        }
                   16885: }
                   16886: 
                   16887: inline void msdos_call_xms_0eh()
                   16888: {
1.1.1.30  root     16889:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16890:        
                   16891:        if(emb_handle == NULL) {
1.1.1.19  root     16892:                REG16(AX) = 0x0000;
                   16893:                REG8(BL) = 0xa2;
                   16894:        } else {
                   16895:                REG16(AX) = 0x0001;
1.1.1.30  root     16896:                REG8(BH) = emb_handle->lock;
                   16897:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   16898:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     16899:        }
                   16900: }
                   16901: 
1.1.1.30  root     16902: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     16903: {
1.1.1.30  root     16904:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16905:        
                   16906:        if(emb_handle == NULL) {
1.1.1.19  root     16907:                REG16(AX) = 0x0000;
                   16908:                REG8(BL) = 0xa2;
1.1.1.30  root     16909:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     16910:                REG16(AX) = 0x0000;
                   16911:                REG8(BL) = 0xab;
                   16912:        } else {
1.1.1.30  root     16913:                if(emb_handle->size_kb < size_kb) {
                   16914:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   16915:                                msdos_xms_combine_emb_handles(emb_handle);
                   16916:                                if(emb_handle->size_kb > size_kb) {
                   16917:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16918:                                }
                   16919:                        } else {
                   16920:                                int old_handle = emb_handle->handle;
                   16921:                                int old_size_kb = emb_handle->size_kb;
                   16922:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16923:                                
                   16924:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16925:                                msdos_xms_free_emb_handle(emb_handle);
                   16926:                                
                   16927:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16928:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16929:                                }
                   16930:                                emb_handle->handle = old_handle;
                   16931:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16932:                                free(buffer);
                   16933:                        }
                   16934:                } else if(emb_handle->size_kb > size_kb) {
                   16935:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16936:                }
                   16937:                if(emb_handle->size_kb != size_kb) {
                   16938:                        REG16(AX) = 0x0000;
                   16939:                        REG8(BL) = 0xa0;
                   16940:                } else {
                   16941:                        REG16(AX) = 0x0001;
                   16942:                        REG8(BL) = 0x00;
                   16943:                }
1.1.1.19  root     16944:        }
                   16945: }
                   16946: 
1.1.1.30  root     16947: inline void msdos_call_xms_0fh()
                   16948: {
                   16949:        msdos_call_xms_0fh(REG16(BX));
                   16950: }
                   16951: 
1.1.1.19  root     16952: inline void msdos_call_xms_10h()
                   16953: {
                   16954:        int seg;
                   16955:        
                   16956:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16957:                REG16(AX) = 0x0001;
                   16958:                REG16(BX) = seg;
                   16959:        } else {
                   16960:                REG16(AX) = 0x0000;
                   16961:                REG8(BL) = 0xb0;
                   16962:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16963:        }
                   16964: }
                   16965: 
                   16966: inline void msdos_call_xms_11h()
                   16967: {
                   16968:        int mcb_seg = REG16(DX) - 1;
                   16969:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16970:        
                   16971:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16972:                msdos_mem_free(REG16(DX));
                   16973:                REG16(AX) = 0x0001;
                   16974:                REG8(BL) = 0x00;
                   16975:        } else {
                   16976:                REG16(AX) = 0x0000;
                   16977:                REG8(BL) = 0xb2;
                   16978:        }
                   16979: }
                   16980: 
                   16981: inline void msdos_call_xms_12h()
                   16982: {
                   16983:        int mcb_seg = REG16(DX) - 1;
                   16984:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16985:        int max_paragraphs;
                   16986:        
                   16987:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16988:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16989:                        REG16(AX) = 0x0001;
                   16990:                        REG8(BL) = 0x00;
                   16991:                } else {
                   16992:                        REG16(AX) = 0x0000;
                   16993:                        REG8(BL) = 0xb0;
                   16994:                        REG16(DX) = max_paragraphs;
                   16995:                }
                   16996:        } else {
                   16997:                REG16(AX) = 0x0000;
                   16998:                REG8(BL) = 0xb2;
                   16999:        }
                   17000: }
                   17001: 
1.1.1.29  root     17002: #if defined(HAS_I386)
                   17003: 
                   17004: inline void msdos_call_xms_88h()
                   17005: {
                   17006:        REG32(EAX) = REG32(EDX) = 0x0000;
                   17007:        
1.1.1.30  root     17008:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   17009:                if(emb_handle->handle == 0) {
                   17010:                        if(REG32(EAX) < emb_handle->size_kb) {
                   17011:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     17012:                        }
1.1.1.30  root     17013:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     17014:                }
                   17015:        }
                   17016:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   17017:                REG8(BL) = 0xa0;
                   17018:        } else {
                   17019:                REG8(BL) = 0x00;
                   17020:        }
                   17021:        REG32(ECX) = EMB_END - 1;
                   17022: }
                   17023: 
                   17024: inline void msdos_call_xms_89h()
                   17025: {
1.1.1.30  root     17026:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     17027: }
                   17028: 
                   17029: inline void msdos_call_xms_8eh()
                   17030: {
1.1.1.30  root     17031:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   17032:        
                   17033:        if(emb_handle == NULL) {
1.1.1.29  root     17034:                REG16(AX) = 0x0000;
                   17035:                REG8(BL) = 0xa2;
                   17036:        } else {
                   17037:                REG16(AX) = 0x0001;
1.1.1.30  root     17038:                REG8(BH) = emb_handle->lock;
                   17039:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   17040:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     17041:        }
                   17042: }
                   17043: 
                   17044: inline void msdos_call_xms_8fh()
                   17045: {
1.1.1.30  root     17046:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     17047: }
                   17048: 
                   17049: #endif
1.1.1.19  root     17050: #endif
                   17051: 
1.1.1.26  root     17052: UINT16 msdos_get_equipment()
                   17053: {
                   17054:        static UINT16 equip = 0;
                   17055:        
                   17056:        if(equip == 0) {
                   17057: #ifdef SUPPORT_FPU
                   17058:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   17059: #endif
                   17060:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   17061:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   17062: //             equip |= (1 << 8);      // 0 if DMA installed
                   17063:                equip |= (2 << 9);      // number of serial ports
                   17064:                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     17065:                
                   17066:                // check only A: and B: if it is floppy drive
                   17067:                int n = 0;
                   17068:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     17069:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   17070:                                n++;
1.1.1.28  root     17071:                        }
                   17072:                }
                   17073:                if(n != 0) {
                   17074:                        equip |= (1 << 0);      // floppy disk(s) installed
                   17075:                        n--;
                   17076:                        equip |= (n << 6);      // number of floppies installed less 1
                   17077:                }
                   17078: //             if(joyGetNumDevs() != 0) {
                   17079: //                     equip |= (1 << 12);     // game port installed
                   17080: //             }
1.1.1.26  root     17081:        }
                   17082:        return(equip);
                   17083: }
                   17084: 
1.1       root     17085: void msdos_syscall(unsigned num)
                   17086: {
1.1.1.22  root     17087: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     17088:        if(num == 0x08 || num == 0x1c) {
                   17089:                // don't log the timer interrupts
1.1.1.45  root     17090: //             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     17091:        } else if(num == 0x30) {
                   17092:                // dummy interrupt for call 0005h (call near)
                   17093:                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     17094:        } else if(num == 0x65) {
1.1.1.22  root     17095:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     17096:                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     17097:        } else if(num == 0x66) {
1.1.1.22  root     17098:                // dummy interrupt for XMS (call far)
1.1.1.33  root     17099:                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     17100:        } else if(num >= 0x68 && num <= 0x6f) {
1.1.1.45  root     17101:                // dummy interrupt
1.1.1.22  root     17102:        } else {
1.1.1.33  root     17103:                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     17104:        }
                   17105: #endif
1.1.1.36  root     17106:        // update cursor position
                   17107:        if(cursor_moved) {
                   17108:                pcbios_update_cursor_position();
                   17109:                cursor_moved = false;
                   17110:        }
1.1.1.50  root     17111: #ifdef USE_SERVICE_THREAD
                   17112:        // this is called from dummy loop to wait until a serive that waits input is done
                   17113:        if(!in_service)
                   17114: #endif
1.1.1.33  root     17115:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     17116:        
1.1       root     17117:        switch(num) {
                   17118:        case 0x00:
1.1.1.28  root     17119:                try {
                   17120:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17121:                        error("division by zero\n");
                   17122:                } catch(...) {
                   17123:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   17124:                }
1.1       root     17125:                break;
                   17126:        case 0x04:
1.1.1.28  root     17127:                try {
                   17128:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17129:                        error("overflow\n");
                   17130:                } catch(...) {
                   17131:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   17132:                }
1.1       root     17133:                break;
                   17134:        case 0x06:
                   17135:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     17136:                if(!ignore_illegal_insn) {
1.1.1.28  root     17137:                        try {
                   17138:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17139:                                error("illegal instruction\n");
                   17140:                        } catch(...) {
                   17141:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   17142:                        }
1.1.1.14  root     17143:                } else {
                   17144: #if defined(HAS_I386)
1.1.1.39  root     17145:                        m_eip = m_int6h_skip_eip;
                   17146: #elif defined(HAS_I286)
                   17147:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     17148: #else
1.1.1.39  root     17149:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     17150: #endif
                   17151:                }
1.1       root     17152:                break;
1.1.1.33  root     17153:        case 0x09:
                   17154:                // ctrl-break is pressed
                   17155:                if(raise_int_1bh) {
                   17156: #if defined(HAS_I386)
                   17157:                        m_ext = 0; // not an external interrupt
                   17158:                        i386_trap(0x1b, 1, 0);
                   17159:                        m_ext = 1;
                   17160: #else
                   17161:                        PREFIX86(_interrupt)(0x1b);
                   17162: #endif
                   17163:                        raise_int_1bh = false;
                   17164:                }
1.1.1.8   root     17165:        case 0x08:
1.1.1.14  root     17166: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     17167:        case 0x0b:
                   17168:        case 0x0c:
                   17169:        case 0x0d:
                   17170:        case 0x0e:
                   17171:        case 0x0f:
                   17172:                // EOI
                   17173:                pic[0].isr &= ~(1 << (num - 0x08));
                   17174:                pic_update();
                   17175:                break;
1.1       root     17176:        case 0x10:
                   17177:                // PC BIOS - Video
1.1.1.14  root     17178:                if(!restore_console_on_exit) {
1.1.1.15  root     17179:                        change_console_size(scr_width, scr_height);
1.1       root     17180:                }
1.1.1.3   root     17181:                m_CF = 0;
1.1       root     17182:                switch(REG8(AH)) {
1.1.1.16  root     17183:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     17184:                case 0x01: pcbios_int_10h_01h(); break;
                   17185:                case 0x02: pcbios_int_10h_02h(); break;
                   17186:                case 0x03: pcbios_int_10h_03h(); break;
                   17187:                case 0x05: pcbios_int_10h_05h(); break;
                   17188:                case 0x06: pcbios_int_10h_06h(); break;
                   17189:                case 0x07: pcbios_int_10h_07h(); break;
                   17190:                case 0x08: pcbios_int_10h_08h(); break;
                   17191:                case 0x09: pcbios_int_10h_09h(); break;
                   17192:                case 0x0a: pcbios_int_10h_0ah(); break;
                   17193:                case 0x0b: break;
1.1.1.40  root     17194:                case 0x0c: pcbios_int_10h_0ch(); break;
                   17195:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     17196:                case 0x0e: pcbios_int_10h_0eh(); break;
                   17197:                case 0x0f: pcbios_int_10h_0fh(); break;
                   17198:                case 0x10: break;
1.1.1.14  root     17199:                case 0x11: pcbios_int_10h_11h(); break;
                   17200:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     17201:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     17202:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     17203:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     17204:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   17205:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     17206:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     17207:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   17208:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     17209:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     17210:                case 0x6f: break;
1.1.1.22  root     17211:                case 0x80: m_CF = 1; break; // unknown
                   17212:                case 0x81: m_CF = 1; break; // unknown
1.1       root     17213:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     17214:                case 0x83: pcbios_int_10h_83h(); break;
                   17215:                case 0x8b: break;
                   17216:                case 0x8c: m_CF = 1; break; // unknown
                   17217:                case 0x8d: m_CF = 1; break; // unknown
                   17218:                case 0x8e: m_CF = 1; break; // unknown
                   17219:                case 0x90: pcbios_int_10h_90h(); break;
                   17220:                case 0x91: pcbios_int_10h_91h(); break;
                   17221:                case 0x92: break;
                   17222:                case 0x93: break;
                   17223:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     17224:                case 0xfa: break; // ega register interface library is not installed
1.1       root     17225:                case 0xfe: pcbios_int_10h_feh(); break;
                   17226:                case 0xff: pcbios_int_10h_ffh(); break;
                   17227:                default:
1.1.1.22  root     17228:                        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));
                   17229:                        m_CF = 1;
1.1       root     17230:                        break;
                   17231:                }
                   17232:                break;
                   17233:        case 0x11:
                   17234:                // PC BIOS - Get Equipment List
1.1.1.26  root     17235:                REG16(AX) = msdos_get_equipment();
1.1       root     17236:                break;
                   17237:        case 0x12:
                   17238:                // PC BIOS - Get Memory Size
1.1.1.33  root     17239:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     17240:                break;
                   17241:        case 0x13:
1.1.1.42  root     17242:                // PC BIOS - Disk I/O
                   17243:                {
                   17244:                        static UINT8 last = 0x00;
                   17245:                        switch(REG8(AH)) {
                   17246:                        case 0x00: pcbios_int_13h_00h(); break;
                   17247:                        case 0x01: // get last status
                   17248:                                REG8(AH) = last;
                   17249:                                break;
                   17250:                        case 0x02: pcbios_int_13h_02h(); break;
                   17251:                        case 0x03: pcbios_int_13h_03h(); break;
                   17252:                        case 0x04: pcbios_int_13h_04h(); break;
                   17253:                        case 0x08: pcbios_int_13h_08h(); break;
                   17254:                        case 0x0a: pcbios_int_13h_02h(); break;
                   17255:                        case 0x0b: pcbios_int_13h_03h(); break;
                   17256:                        case 0x0d: pcbios_int_13h_00h(); break;
                   17257:                        case 0x10: pcbios_int_13h_10h(); break;
                   17258:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     17259:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     17260:                        case 0x05: // format
                   17261:                        case 0x06:
                   17262:                        case 0x07:
                   17263:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   17264:                                m_CF = 1;
                   17265:                                break;
                   17266:                        case 0x09:
                   17267:                        case 0x0c: // seek
                   17268:                        case 0x11: // recalib
                   17269:                        case 0x14:
                   17270:                        case 0x17:
                   17271:                                REG8(AH) = 0x00; // successful completion
                   17272:                                break;
1.1.1.43  root     17273:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   17274:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   17275:                                REG8(AH) = 0x01; // invalid function
                   17276:                                m_CF = 1;
                   17277:                                break;
1.1.1.42  root     17278:                        default:
                   17279:                                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));
                   17280:                                REG8(AH) = 0x01; // invalid function
                   17281:                                m_CF = 1;
                   17282:                                break;
                   17283:                        }
                   17284:                        last = REG8(AH);
                   17285:                }
1.1       root     17286:                break;
                   17287:        case 0x14:
                   17288:                // PC BIOS - Serial I/O
1.1.1.25  root     17289:                switch(REG8(AH)) {
                   17290:                case 0x00: pcbios_int_14h_00h(); break;
                   17291:                case 0x01: pcbios_int_14h_01h(); break;
                   17292:                case 0x02: pcbios_int_14h_02h(); break;
                   17293:                case 0x03: pcbios_int_14h_03h(); break;
                   17294:                case 0x04: pcbios_int_14h_04h(); break;
                   17295:                case 0x05: pcbios_int_14h_05h(); break;
                   17296:                default:
                   17297:                        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));
                   17298:                        break;
                   17299:                }
1.1       root     17300:                break;
                   17301:        case 0x15:
                   17302:                // PC BIOS
1.1.1.3   root     17303:                m_CF = 0;
1.1       root     17304:                switch(REG8(AH)) {
                   17305:                case 0x23: pcbios_int_15h_23h(); break;
                   17306:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     17307:                case 0x41: break;
1.1       root     17308:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.64  root     17309:                case 0x4f: m_CF = 1; break; // from DOSBox
1.1.1.22  root     17310:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     17311:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     17312:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     17313:                case 0x86: pcbios_int_15h_86h(); break;
                   17314:                case 0x87: pcbios_int_15h_87h(); break;
                   17315:                case 0x88: pcbios_int_15h_88h(); break;
                   17316:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     17317:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.64  root     17318:                case 0x90: REG8(AH) = 0x00; break; // from DOSBox
                   17319:                case 0x91: REG8(AH) = 0x00; break; // from DOSBox
1.1.1.22  root     17320:                case 0xc0: // PS/2 ???
1.1.1.54  root     17321: #ifndef EXT_BIOS_TOP
1.1.1.22  root     17322:                case 0xc1:
1.1.1.54  root     17323: #endif
1.1.1.30  root     17324:                case 0xc3: // PS50+ ???
                   17325:                case 0xc4:
1.1.1.22  root     17326:                        REG8(AH) = 0x86;
                   17327:                        m_CF = 1;
                   17328:                        break;
1.1.1.54  root     17329: #ifdef EXT_BIOS_TOP
                   17330:                case 0xc1: pcbios_int_15h_c1h(); break;
                   17331: #endif
                   17332:                case 0xc2: pcbios_int_15h_c2h(); break;
1.1.1.3   root     17333: #if defined(HAS_I386)
1.1       root     17334:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     17335: #endif
1.1       root     17336:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     17337:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     17338:                default:
1.1.1.22  root     17339:                        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));
                   17340:                        REG8(AH) = 0x86;
1.1.1.3   root     17341:                        m_CF = 1;
1.1       root     17342:                        break;
                   17343:                }
                   17344:                break;
                   17345:        case 0x16:
                   17346:                // PC BIOS - Keyboard
1.1.1.3   root     17347:                m_CF = 0;
1.1       root     17348:                switch(REG8(AH)) {
                   17349:                case 0x00: pcbios_int_16h_00h(); break;
                   17350:                case 0x01: pcbios_int_16h_01h(); break;
                   17351:                case 0x02: pcbios_int_16h_02h(); break;
                   17352:                case 0x03: pcbios_int_16h_03h(); break;
                   17353:                case 0x05: pcbios_int_16h_05h(); break;
1.1.1.60  root     17354:                case 0x09: pcbios_int_16h_09h(); break;
                   17355:                case 0x0a: pcbios_int_16h_0ah(); break;
1.1       root     17356:                case 0x10: pcbios_int_16h_00h(); break;
1.1.1.60  root     17357:                case 0x11: pcbios_int_16h_11h(); break;
1.1       root     17358:                case 0x12: pcbios_int_16h_12h(); break;
                   17359:                case 0x13: pcbios_int_16h_13h(); break;
                   17360:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     17361:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     17362:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     17363:                case 0xda: break; // unknown
1.1.1.43  root     17364:                case 0xdb: break; // unknown
1.1.1.22  root     17365:                case 0xff: break; // unknown
1.1       root     17366:                default:
1.1.1.22  root     17367:                        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     17368:                        break;
                   17369:                }
                   17370:                break;
                   17371:        case 0x17:
                   17372:                // PC BIOS - Printer
1.1.1.37  root     17373:                m_CF = 0;
                   17374:                switch(REG8(AH)) {
                   17375:                case 0x00: pcbios_int_17h_00h(); break;
                   17376:                case 0x01: pcbios_int_17h_01h(); break;
                   17377:                case 0x02: pcbios_int_17h_02h(); break;
                   17378:                case 0x03: pcbios_int_17h_03h(); break;
                   17379:                case 0x50: pcbios_int_17h_50h(); break;
                   17380:                case 0x51: pcbios_int_17h_51h(); break;
                   17381:                case 0x52: pcbios_int_17h_52h(); break;
                   17382:                case 0x84: pcbios_int_17h_84h(); break;
                   17383:                case 0x85: pcbios_int_17h_85h(); break;
                   17384:                default:
                   17385:                        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));
                   17386:                        break;
                   17387:                }
1.1       root     17388:                break;
                   17389:        case 0x1a:
                   17390:                // PC BIOS - Timer
1.1.1.3   root     17391:                m_CF = 0;
1.1       root     17392:                switch(REG8(AH)) {
                   17393:                case 0x00: pcbios_int_1ah_00h(); break;
                   17394:                case 0x01: break;
                   17395:                case 0x02: pcbios_int_1ah_02h(); break;
                   17396:                case 0x03: break;
                   17397:                case 0x04: pcbios_int_1ah_04h(); break;
                   17398:                case 0x05: break;
                   17399:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   17400:                case 0x0b: break;
1.1.1.14  root     17401:                case 0x35: break; // Word Perfect Third Party Interface?
                   17402:                case 0x36: break; // Word Perfect Third Party Interface
                   17403:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     17404:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     17405:                case 0xb1: break; // PCI BIOS v2.0c+
                   17406:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     17407:                default:
1.1.1.22  root     17408:                        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     17409:                        break;
                   17410:                }
                   17411:                break;
1.1.1.33  root     17412:        case 0x1b:
                   17413:                mem[0x471] = 0x00;
                   17414:                break;
1.1       root     17415:        case 0x20:
1.1.1.28  root     17416:                try {
                   17417:                        msdos_process_terminate(SREG(CS), retval, 1);
                   17418:                } catch(...) {
                   17419:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   17420:                }
1.1       root     17421:                break;
1.1.1.49  root     17422:        case 0x30:
1.1.1.46  root     17423:                // dummy interrupt for case map routine pointed in the country info
                   17424: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   17425: //                     REG8(AL) = 0x00;
                   17426: //                     break;
                   17427: //             }
1.1       root     17428:        case 0x21:
                   17429:                // MS-DOS System Call
1.1.1.3   root     17430:                m_CF = 0;
1.1.1.28  root     17431:                try {
1.1.1.46  root     17432:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     17433:                        case 0x00: msdos_int_21h_00h(); break;
                   17434:                        case 0x01: msdos_int_21h_01h(); break;
                   17435:                        case 0x02: msdos_int_21h_02h(); break;
                   17436:                        case 0x03: msdos_int_21h_03h(); break;
                   17437:                        case 0x04: msdos_int_21h_04h(); break;
                   17438:                        case 0x05: msdos_int_21h_05h(); break;
                   17439:                        case 0x06: msdos_int_21h_06h(); break;
                   17440:                        case 0x07: msdos_int_21h_07h(); break;
                   17441:                        case 0x08: msdos_int_21h_08h(); break;
                   17442:                        case 0x09: msdos_int_21h_09h(); break;
                   17443:                        case 0x0a: msdos_int_21h_0ah(); break;
                   17444:                        case 0x0b: msdos_int_21h_0bh(); break;
                   17445:                        case 0x0c: msdos_int_21h_0ch(); break;
                   17446:                        case 0x0d: msdos_int_21h_0dh(); break;
                   17447:                        case 0x0e: msdos_int_21h_0eh(); break;
                   17448:                        case 0x0f: msdos_int_21h_0fh(); break;
                   17449:                        case 0x10: msdos_int_21h_10h(); break;
                   17450:                        case 0x11: msdos_int_21h_11h(); break;
                   17451:                        case 0x12: msdos_int_21h_12h(); break;
                   17452:                        case 0x13: msdos_int_21h_13h(); break;
                   17453:                        case 0x14: msdos_int_21h_14h(); break;
                   17454:                        case 0x15: msdos_int_21h_15h(); break;
                   17455:                        case 0x16: msdos_int_21h_16h(); break;
                   17456:                        case 0x17: msdos_int_21h_17h(); break;
                   17457:                        case 0x18: msdos_int_21h_18h(); break;
                   17458:                        case 0x19: msdos_int_21h_19h(); break;
                   17459:                        case 0x1a: msdos_int_21h_1ah(); break;
                   17460:                        case 0x1b: msdos_int_21h_1bh(); break;
                   17461:                        case 0x1c: msdos_int_21h_1ch(); break;
                   17462:                        case 0x1d: msdos_int_21h_1dh(); break;
                   17463:                        case 0x1e: msdos_int_21h_1eh(); break;
                   17464:                        case 0x1f: msdos_int_21h_1fh(); break;
                   17465:                        case 0x20: msdos_int_21h_20h(); break;
                   17466:                        case 0x21: msdos_int_21h_21h(); break;
                   17467:                        case 0x22: msdos_int_21h_22h(); break;
                   17468:                        case 0x23: msdos_int_21h_23h(); break;
                   17469:                        case 0x24: msdos_int_21h_24h(); break;
                   17470:                        case 0x25: msdos_int_21h_25h(); break;
                   17471:                        case 0x26: msdos_int_21h_26h(); break;
                   17472:                        case 0x27: msdos_int_21h_27h(); break;
                   17473:                        case 0x28: msdos_int_21h_28h(); break;
                   17474:                        case 0x29: msdos_int_21h_29h(); break;
                   17475:                        case 0x2a: msdos_int_21h_2ah(); break;
                   17476:                        case 0x2b: msdos_int_21h_2bh(); break;
                   17477:                        case 0x2c: msdos_int_21h_2ch(); break;
                   17478:                        case 0x2d: msdos_int_21h_2dh(); break;
                   17479:                        case 0x2e: msdos_int_21h_2eh(); break;
                   17480:                        case 0x2f: msdos_int_21h_2fh(); break;
                   17481:                        case 0x30: msdos_int_21h_30h(); break;
                   17482:                        case 0x31: msdos_int_21h_31h(); break;
                   17483:                        case 0x32: msdos_int_21h_32h(); break;
                   17484:                        case 0x33: msdos_int_21h_33h(); break;
                   17485:                        case 0x34: msdos_int_21h_34h(); break;
                   17486:                        case 0x35: msdos_int_21h_35h(); break;
                   17487:                        case 0x36: msdos_int_21h_36h(); break;
                   17488:                        case 0x37: msdos_int_21h_37h(); break;
                   17489:                        case 0x38: msdos_int_21h_38h(); break;
                   17490:                        case 0x39: msdos_int_21h_39h(0); break;
                   17491:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   17492:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   17493:                        case 0x3c: msdos_int_21h_3ch(); break;
                   17494:                        case 0x3d: msdos_int_21h_3dh(); break;
                   17495:                        case 0x3e: msdos_int_21h_3eh(); break;
                   17496:                        case 0x3f: msdos_int_21h_3fh(); break;
                   17497:                        case 0x40: msdos_int_21h_40h(); break;
                   17498:                        case 0x41: msdos_int_21h_41h(0); break;
                   17499:                        case 0x42: msdos_int_21h_42h(); break;
                   17500:                        case 0x43: msdos_int_21h_43h(0); break;
                   17501:                        case 0x44: msdos_int_21h_44h(); break;
                   17502:                        case 0x45: msdos_int_21h_45h(); break;
                   17503:                        case 0x46: msdos_int_21h_46h(); break;
                   17504:                        case 0x47: msdos_int_21h_47h(0); break;
                   17505:                        case 0x48: msdos_int_21h_48h(); break;
                   17506:                        case 0x49: msdos_int_21h_49h(); break;
                   17507:                        case 0x4a: msdos_int_21h_4ah(); break;
                   17508:                        case 0x4b: msdos_int_21h_4bh(); break;
                   17509:                        case 0x4c: msdos_int_21h_4ch(); break;
                   17510:                        case 0x4d: msdos_int_21h_4dh(); break;
                   17511:                        case 0x4e: msdos_int_21h_4eh(); break;
                   17512:                        case 0x4f: msdos_int_21h_4fh(); break;
                   17513:                        case 0x50: msdos_int_21h_50h(); break;
                   17514:                        case 0x51: msdos_int_21h_51h(); break;
                   17515:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     17516:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     17517:                        case 0x54: msdos_int_21h_54h(); break;
                   17518:                        case 0x55: msdos_int_21h_55h(); break;
                   17519:                        case 0x56: msdos_int_21h_56h(0); break;
                   17520:                        case 0x57: msdos_int_21h_57h(); break;
                   17521:                        case 0x58: msdos_int_21h_58h(); break;
                   17522:                        case 0x59: msdos_int_21h_59h(); break;
                   17523:                        case 0x5a: msdos_int_21h_5ah(); break;
                   17524:                        case 0x5b: msdos_int_21h_5bh(); break;
                   17525:                        case 0x5c: msdos_int_21h_5ch(); break;
                   17526:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     17527:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     17528:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     17529:                        case 0x60: msdos_int_21h_60h(0); break;
                   17530:                        case 0x61: msdos_int_21h_61h(); break;
                   17531:                        case 0x62: msdos_int_21h_62h(); break;
                   17532:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     17533:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     17534:                        case 0x65: msdos_int_21h_65h(); break;
                   17535:                        case 0x66: msdos_int_21h_66h(); break;
                   17536:                        case 0x67: msdos_int_21h_67h(); break;
                   17537:                        case 0x68: msdos_int_21h_68h(); break;
                   17538:                        case 0x69: msdos_int_21h_69h(); break;
                   17539:                        case 0x6a: msdos_int_21h_6ah(); break;
                   17540:                        case 0x6b: msdos_int_21h_6bh(); break;
                   17541:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     17542:                        case 0x6d: // Find First ROM Program
                   17543:                        case 0x6e: // Find Next ROM Program
                   17544:                        case 0x6f: // Get/Set ROM Scan Start Address
                   17545:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   17546:                                break;
1.1.1.43  root     17547:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     17548:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     17549:                                switch(REG8(AL)) {
                   17550:                                case 0x0d: msdos_int_21h_710dh(); break;
                   17551:                                case 0x39: msdos_int_21h_39h(1); break;
                   17552:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   17553:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     17554:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     17555:                                case 0x43: msdos_int_21h_43h(1); break;
                   17556:                                case 0x47: msdos_int_21h_47h(1); break;
                   17557:                                case 0x4e: msdos_int_21h_714eh(); break;
                   17558:                                case 0x4f: msdos_int_21h_714fh(); break;
                   17559:                                case 0x56: msdos_int_21h_56h(1); break;
                   17560:                                case 0x60: msdos_int_21h_60h(1); break;
                   17561:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   17562:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   17563:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   17564:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   17565:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   17566:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     17567:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     17568:                                case 0xaa: msdos_int_21h_71aah(); break;
                   17569:                                default:
                   17570:                                        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));
                   17571:                                        REG16(AX) = 0x7100;
                   17572:                                        m_CF = 1;
                   17573:                                        break;
                   17574:                                }
                   17575:                                break;
1.1.1.48  root     17576:                        case 0x72: // Windows95 beta - LFN FindClose
                   17577: //                             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));
                   17578:                                REG16(AX) = 0x7200;
                   17579:                                m_CF = 1;
                   17580:                                break;
                   17581:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     17582:                                switch(REG8(AL)) {
                   17583:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     17584:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     17585:                                case 0x02: msdos_int_21h_7302h(); break;
                   17586:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     17587:                                // 0x04: Set DPB to Use for Formatting
                   17588:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     17589:                                default:
                   17590:                                        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));
                   17591:                                        REG16(AX) = 0x7300;
                   17592:                                        m_CF = 1;
                   17593:                                        break;
                   17594:                                }
1.1       root     17595:                                break;
1.1.1.30  root     17596:                        case 0xdb: msdos_int_21h_dbh(); break;
                   17597:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     17598:                        default:
1.1.1.22  root     17599:                                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     17600:                                REG16(AX) = 0x01;
1.1.1.3   root     17601:                                m_CF = 1;
1.1       root     17602:                                break;
                   17603:                        }
1.1.1.28  root     17604:                } catch(int error) {
                   17605:                        REG16(AX) = error;
                   17606:                        m_CF = 1;
                   17607:                } catch(...) {
                   17608:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     17609:                        m_CF = 1;
1.1       root     17610:                }
1.1.1.3   root     17611:                if(m_CF) {
1.1.1.23  root     17612:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     17613:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     17614:                        sda->extended_error_code = REG16(AX);
                   17615:                        switch(sda->extended_error_code) {
                   17616:                        case  4: // Too many open files
                   17617:                        case  8: // Insufficient memory
                   17618:                                sda->error_class = 1; // Out of resource
                   17619:                                break;
                   17620:                        case  5: // Access denied
                   17621:                                sda->error_class = 3; // Authorization
                   17622:                                break;
                   17623:                        case  7: // Memory control block destroyed
                   17624:                                sda->error_class = 4; // Internal
                   17625:                                break;
                   17626:                        case  2: // File not found
                   17627:                        case  3: // Path not found
                   17628:                        case 15: // Invaid drive specified
                   17629:                        case 18: // No more files
                   17630:                                sda->error_class = 8; // Not found
                   17631:                                break;
                   17632:                        case 32: // Sharing violation
                   17633:                        case 33: // Lock violation
                   17634:                                sda->error_class = 10; // Locked
                   17635:                                break;
                   17636: //                     case 16: // Removal of current directory attempted
                   17637:                        case 19: // Attempted write on protected disk
                   17638:                        case 21: // Drive not ready
                   17639: //                     case 29: // Write failure
                   17640: //                     case 30: // Read failure
                   17641: //                     case 82: // Cannot create subdirectory
                   17642:                                sda->error_class = 11; // Media
                   17643:                                break;
                   17644:                        case 80: // File already exists
                   17645:                                sda->error_class = 12; // Already exist
                   17646:                                break;
                   17647:                        default:
                   17648:                                sda->error_class = 13; // Unknown
                   17649:                                break;
                   17650:                        }
                   17651:                        sda->suggested_action = 1; // Retry
                   17652:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     17653:                }
1.1.1.33  root     17654:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     17655:                        // raise int 23h
                   17656: #if defined(HAS_I386)
                   17657:                        m_ext = 0; // not an external interrupt
                   17658:                        i386_trap(0x23, 1, 0);
                   17659:                        m_ext = 1;
                   17660: #else
                   17661:                        PREFIX86(_interrupt)(0x23);
                   17662: #endif
                   17663:                }
1.1       root     17664:                break;
                   17665:        case 0x22:
                   17666:                fatalerror("int 22h (terminate address)\n");
                   17667:        case 0x23:
1.1.1.28  root     17668:                try {
                   17669:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   17670:                } catch(...) {
                   17671:                        fatalerror("failed to terminate the current process by int 23h\n");
                   17672:                }
1.1       root     17673:                break;
                   17674:        case 0x24:
1.1.1.32  root     17675: /*
1.1.1.28  root     17676:                try {
                   17677:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   17678:                } catch(...) {
                   17679:                        fatalerror("failed to terminate the current process by int 24h\n");
                   17680:                }
1.1.1.32  root     17681: */
                   17682:                msdos_int_24h();
1.1       root     17683:                break;
                   17684:        case 0x25:
                   17685:                msdos_int_25h();
                   17686:                break;
                   17687:        case 0x26:
                   17688:                msdos_int_26h();
                   17689:                break;
                   17690:        case 0x27:
1.1.1.28  root     17691:                try {
                   17692:                        msdos_int_27h();
                   17693:                } catch(...) {
                   17694:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   17695:                }
1.1       root     17696:                break;
                   17697:        case 0x28:
                   17698:                Sleep(10);
1.1.1.35  root     17699:                REQUEST_HARDWRE_UPDATE();
1.1       root     17700:                break;
                   17701:        case 0x29:
                   17702:                msdos_int_29h();
                   17703:                break;
                   17704:        case 0x2e:
                   17705:                msdos_int_2eh();
                   17706:                break;
                   17707:        case 0x2f:
                   17708:                // multiplex interrupt
                   17709:                switch(REG8(AH)) {
1.1.1.22  root     17710:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     17711:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     17712:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     17713:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     17714:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     17715:                case 0x14: msdos_int_2fh_14h(); break;
                   17716:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     17717:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     17718:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     17719:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     17720:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     17721:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     17722:                case 0x46: msdos_int_2fh_46h(); break;
                   17723:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     17724:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     17725:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     17726:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     17727:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     17728:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     17729:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     17730:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     17731:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     17732:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     17733:                default:
1.1.1.30  root     17734:                        switch(REG8(AL)) {
                   17735:                        case 0x00:
                   17736:                                // This is not installed
                   17737: //                             REG8(AL) = 0x00;
                   17738:                                break;
1.1.1.33  root     17739:                        case 0x01:
1.1.1.42  root     17740:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   17741:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   17742:                                        break;
                   17743:                                }
1.1.1.33  root     17744:                                // Banyan VINES v4+ is not installed
                   17745:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   17746:                                        break;
                   17747:                                }
1.1.1.42  root     17748:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   17749:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   17750:                                        break;
                   17751:                                }
1.1.1.30  root     17752:                        default:
1.1.1.42  root     17753:                                // NORTON UTILITIES 5.0+
                   17754:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   17755:                                        break;
                   17756:                                }
1.1.1.30  root     17757:                                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     17758:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     17759:                                m_CF = 1;
                   17760:                                break;
                   17761:                        }
                   17762:                        break;
1.1       root     17763:                }
                   17764:                break;
1.1.1.24  root     17765:        case 0x33:
                   17766:                switch(REG8(AH)) {
                   17767:                case 0x00:
                   17768:                        // Mouse
1.1.1.49  root     17769:                        switch(REG16(AX)) {
                   17770:                        case 0x0000: msdos_int_33h_0000h(); break;
                   17771:                        case 0x0001: msdos_int_33h_0001h(); break;
                   17772:                        case 0x0002: msdos_int_33h_0002h(); break;
                   17773:                        case 0x0003: msdos_int_33h_0003h(); break;
                   17774:                        case 0x0004: msdos_int_33h_0004h(); break;
                   17775:                        case 0x0005: msdos_int_33h_0005h(); break;
                   17776:                        case 0x0006: msdos_int_33h_0006h(); break;
                   17777:                        case 0x0007: msdos_int_33h_0007h(); break;
                   17778:                        case 0x0008: msdos_int_33h_0008h(); break;
                   17779:                        case 0x0009: msdos_int_33h_0009h(); break;
                   17780:                        case 0x000a: msdos_int_33h_000ah(); break;
                   17781:                        case 0x000b: msdos_int_33h_000bh(); break;
                   17782:                        case 0x000c: msdos_int_33h_000ch(); break;
                   17783:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   17784:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   17785:                        case 0x000f: msdos_int_33h_000fh(); break;
                   17786:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   17787:                        case 0x0011: msdos_int_33h_0011h(); break;
                   17788:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   17789:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   17790:                        case 0x0014: msdos_int_33h_0014h(); break;
                   17791:                        case 0x0015: msdos_int_33h_0015h(); break;
                   17792:                        case 0x0016: msdos_int_33h_0016h(); break;
                   17793:                        case 0x0017: msdos_int_33h_0017h(); break;
                   17794:                        case 0x0018: msdos_int_33h_0018h(); break;
                   17795:                        case 0x0019: msdos_int_33h_0019h(); break;
                   17796:                        case 0x001a: msdos_int_33h_001ah(); break;
                   17797:                        case 0x001b: msdos_int_33h_001bh(); break;
                   17798:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   17799:                        case 0x001d: msdos_int_33h_001dh(); break;
                   17800:                        case 0x001e: msdos_int_33h_001eh(); break;
                   17801:                        case 0x001f: msdos_int_33h_001fh(); break;
                   17802:                        case 0x0020: msdos_int_33h_0020h(); break;
                   17803:                        case 0x0021: msdos_int_33h_0021h(); break;
                   17804:                        case 0x0022: msdos_int_33h_0022h(); break;
                   17805:                        case 0x0023: msdos_int_33h_0023h(); break;
                   17806:                        case 0x0024: msdos_int_33h_0024h(); break;
                   17807:                        case 0x0025: msdos_int_33h_0025h(); break;
                   17808:                        case 0x0026: msdos_int_33h_0026h(); break;
                   17809:                        case 0x0027: msdos_int_33h_0027h(); break;
                   17810:                        case 0x0028: msdos_int_33h_0028h(); break;
                   17811:                        case 0x0029: msdos_int_33h_0029h(); break;
                   17812:                        case 0x002a: msdos_int_33h_002ah(); break;
                   17813:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   17814:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   17815:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   17816:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   17817:                        case 0x002f: break; // Mouse Hardware Reset
                   17818:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   17819:                        case 0x0031: msdos_int_33h_0031h(); break;
                   17820:                        case 0x0032: msdos_int_33h_0032h(); break;
                   17821:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   17822:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   17823:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   17824:                        case 0x004d: msdos_int_33h_004dh(); break;
                   17825:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     17826:                        default:
                   17827:                                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));
                   17828:                                break;
                   17829:                        }
                   17830:                        break;
                   17831:                default:
                   17832:                        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));
                   17833:                        break;
                   17834:                }
                   17835:                break;
1.1.1.59  root     17836:        case 0x65:
1.1.1.19  root     17837:                // dummy interrupt for EMS (int 67h)
                   17838:                switch(REG8(AH)) {
                   17839:                case 0x40: msdos_int_67h_40h(); break;
                   17840:                case 0x41: msdos_int_67h_41h(); break;
                   17841:                case 0x42: msdos_int_67h_42h(); break;
                   17842:                case 0x43: msdos_int_67h_43h(); break;
                   17843:                case 0x44: msdos_int_67h_44h(); break;
                   17844:                case 0x45: msdos_int_67h_45h(); break;
                   17845:                case 0x46: msdos_int_67h_46h(); break;
                   17846:                case 0x47: msdos_int_67h_47h(); break;
                   17847:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     17848:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   17849:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     17850:                case 0x4b: msdos_int_67h_4bh(); break;
                   17851:                case 0x4c: msdos_int_67h_4ch(); break;
                   17852:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     17853:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     17854:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     17855:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     17856:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     17857:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     17858:                case 0x53: msdos_int_67h_53h(); break;
                   17859:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     17860:                case 0x55: msdos_int_67h_55h(); break;
                   17861:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     17862:                case 0x57: msdos_int_67h_57h(); break;
                   17863:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     17864:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     17865:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     17866:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     17867:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   17868:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     17869:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     17870:                // 0xde: VCPI
1.1.1.30  root     17871:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     17872:                default:
1.1.1.22  root     17873:                        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     17874:                        REG8(AH) = 0x84;
                   17875:                        break;
                   17876:                }
                   17877:                break;
                   17878: #ifdef SUPPORT_XMS
1.1.1.59  root     17879:        case 0x66:
1.1.1.19  root     17880:                // dummy interrupt for XMS (call far)
1.1.1.28  root     17881:                try {
                   17882:                        switch(REG8(AH)) {
                   17883:                        case 0x00: msdos_call_xms_00h(); break;
                   17884:                        case 0x01: msdos_call_xms_01h(); break;
                   17885:                        case 0x02: msdos_call_xms_02h(); break;
                   17886:                        case 0x03: msdos_call_xms_03h(); break;
                   17887:                        case 0x04: msdos_call_xms_04h(); break;
                   17888:                        case 0x05: msdos_call_xms_05h(); break;
                   17889:                        case 0x06: msdos_call_xms_06h(); break;
                   17890:                        case 0x07: msdos_call_xms_07h(); break;
                   17891:                        case 0x08: msdos_call_xms_08h(); break;
                   17892:                        case 0x09: msdos_call_xms_09h(); break;
                   17893:                        case 0x0a: msdos_call_xms_0ah(); break;
                   17894:                        case 0x0b: msdos_call_xms_0bh(); break;
                   17895:                        case 0x0c: msdos_call_xms_0ch(); break;
                   17896:                        case 0x0d: msdos_call_xms_0dh(); break;
                   17897:                        case 0x0e: msdos_call_xms_0eh(); break;
                   17898:                        case 0x0f: msdos_call_xms_0fh(); break;
                   17899:                        case 0x10: msdos_call_xms_10h(); break;
                   17900:                        case 0x11: msdos_call_xms_11h(); break;
                   17901:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     17902: #if defined(HAS_I386)
                   17903:                        case 0x88: msdos_call_xms_88h(); break;
                   17904:                        case 0x89: msdos_call_xms_89h(); break;
                   17905:                        case 0x8e: msdos_call_xms_8eh(); break;
                   17906:                        case 0x8f: msdos_call_xms_8fh(); break;
                   17907: #endif
1.1.1.28  root     17908:                        default:
                   17909:                                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));
                   17910:                                REG16(AX) = 0x0000;
                   17911:                                REG8(BL) = 0x80; // function not implemented
                   17912:                                break;
                   17913:                        }
                   17914:                } catch(...) {
1.1.1.19  root     17915:                        REG16(AX) = 0x0000;
1.1.1.28  root     17916:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     17917:                }
                   17918:                break;
                   17919: #endif
1.1.1.59  root     17920: /*
                   17921:        case 0x67:
                   17922:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 65h
                   17923:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   17924:                break;
                   17925: */
                   17926:        case 0x69:
1.1.1.24  root     17927:                // irq12 (mouse)
                   17928:                mouse_push_ax = REG16(AX);
                   17929:                mouse_push_bx = REG16(BX);
                   17930:                mouse_push_cx = REG16(CX);
                   17931:                mouse_push_dx = REG16(DX);
                   17932:                mouse_push_si = REG16(SI);
                   17933:                mouse_push_di = REG16(DI);
                   17934:                
1.1.1.43  root     17935:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17936:                        REG16(AX) = mouse.status_irq;
                   17937:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17938:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17939:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17940:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17941:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17942:                        
1.1.1.49  root     17943:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17944:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17945:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17946:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17947:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.59  root     17948:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17949:                        mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17950:                        break;
1.1.1.24  root     17951:                }
1.1.1.43  root     17952:                for(int i = 0; i < 8; i++) {
                   17953:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17954:                                REG16(AX) = mouse.status_irq_alt;
                   17955:                                REG16(BX) = mouse.get_buttons();
                   17956:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17957:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17958:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17959:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17960:                                
1.1.1.49  root     17961:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17962:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17963:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17964:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17965:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.59  root     17966:                                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17967:                                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.43  root     17968:                                break;
                   17969:                        }
                   17970:                }
1.1.1.59  root     17971:                if(mouse.status_irq_ps2 && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     17972:                        UINT16 data_1st, data_2nd, data_3rd;
                   17973:                        pcbios_read_from_ps2_mouse(&data_1st, &data_2nd, &data_3rd);
                   17974:                        i386_push16(data_1st);
                   17975:                        i386_push16(data_2nd);
                   17976:                        i386_push16(data_3rd);
                   17977:                        i386_push16(0x0000);
                   17978:                        
                   17979:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17980:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr_ps2.w.l & 0xff;
                   17981:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr_ps2.w.l >> 8;
                   17982:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr_ps2.w.h & 0xff;
                   17983:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr_ps2.w.h >> 8;
1.1.1.59  root     17984:                        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   17985:                        mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.54  root     17986:                        break;
                   17987:                }
1.1.1.43  root     17988:                // invalid call addr :-(
1.1.1.49  root     17989:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17990:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17991:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17992:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17993:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.59  root     17994:                mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17995:                mem[DUMMY_TOP + 0x08] = 0x6b;
1.1.1.24  root     17996:                break;
1.1.1.59  root     17997:        case 0x6a:
                   17998:                // end of ps/2 mouse bios
                   17999:                i386_pop16();
                   18000:                i386_pop16();
                   18001:                i386_pop16();
                   18002:                i386_pop16();
1.1.1.24  root     18003:        case 0x6b:
                   18004:                // end of irq12 (mouse)
                   18005:                REG16(AX) = mouse_push_ax;
                   18006:                REG16(BX) = mouse_push_bx;
                   18007:                REG16(CX) = mouse_push_cx;
                   18008:                REG16(DX) = mouse_push_dx;
                   18009:                REG16(SI) = mouse_push_si;
                   18010:                REG16(DI) = mouse_push_di;
                   18011:                
                   18012:                // EOI
                   18013:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   18014:                        pic[0].isr &= ~(1 << 2); // master
                   18015:                }
                   18016:                pic_update();
                   18017:                break;
                   18018:        case 0x6c:
1.1.1.19  root     18019:                // dummy interrupt for case map routine pointed in the country info
                   18020:                if(REG8(AL) >= 0x80) {
                   18021:                        char tmp[2] = {0};
                   18022:                        tmp[0] = REG8(AL);
                   18023:                        my_strupr(tmp);
                   18024:                        REG8(AL) = tmp[0];
                   18025:                }
                   18026:                break;
1.1.1.27  root     18027:        case 0x6d:
                   18028:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   18029:                REG8(AL) = 0x86; // not supported
                   18030:                m_CF = 1;
                   18031:                break;
1.1.1.32  root     18032:        case 0x6e:
                   18033:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   18034:                {
                   18035:                        UINT16 code = REG16(AX);
                   18036:                        if(code & 0xf0) {
                   18037:                                code = (code & 7) | ((code & 0x10) >> 1);
                   18038:                        }
                   18039:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   18040:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   18041:                                        const char *message = NULL;
                   18042:                                        if(active_code_page == 932) {
                   18043:                                                message = param_error_table[i].message_japanese;
                   18044:                                        }
                   18045:                                        if(message == NULL) {
                   18046:                                                message = param_error_table[i].message_english;
                   18047:                                        }
                   18048:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   18049:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   18050:                                        
                   18051:                                        SREG(ES) = WORK_TOP >> 4;
                   18052:                                        i386_load_segment_descriptor(ES);
                   18053:                                        REG16(DI) = 0x0000;
                   18054:                                        break;
                   18055:                                }
                   18056:                        }
                   18057:                }
                   18058:                break;
1.1.1.49  root     18059:        case 0x6f:
                   18060:                // dummy interrupt for end of alter page map and call
                   18061:                {
                   18062:                        UINT16 handles[4], pages[4];
                   18063:                        
                   18064:                        // pop old mapping data in new mapping
                   18065:                        for(int i = 0; i < 4; i++) {
                   18066:                                pages  [3 - i] = i386_pop16();
                   18067:                                handles[3 - i] = i386_pop16();
                   18068:                        }
                   18069:                        
                   18070:                        // restore old mapping
                   18071:                        for(int i = 0; i < 4; i++) {
                   18072:                                UINT16 handle = handles[i];
                   18073:                                UINT16 page   = pages  [i];
                   18074:                                
                   18075:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   18076:                                        ems_map_page(i, handle, page);
                   18077:                                } else {
                   18078:                                        ems_unmap_page(i);
                   18079:                                }
                   18080:                        }
                   18081:                        // do ret_far (pop cs/ip) in old mapping
                   18082:                }
                   18083:                break;
1.1.1.8   root     18084:        case 0x70:
                   18085:        case 0x71:
                   18086:        case 0x72:
                   18087:        case 0x73:
                   18088:        case 0x74:
                   18089:        case 0x75:
                   18090:        case 0x76:
                   18091:        case 0x77:
                   18092:                // EOI
                   18093:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   18094:                        pic[0].isr &= ~(1 << 2); // master
                   18095:                }
                   18096:                pic_update();
                   18097:                break;
1.1       root     18098:        default:
1.1.1.22  root     18099: //             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     18100:                break;
                   18101:        }
                   18102:        
                   18103:        // update cursor position
                   18104:        if(cursor_moved) {
1.1.1.36  root     18105:                pcbios_update_cursor_position();
1.1       root     18106:                cursor_moved = false;
                   18107:        }
                   18108: }
                   18109: 
                   18110: // init
                   18111: 
                   18112: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   18113: {
                   18114:        // init file handler
                   18115:        memset(file_handler, 0, sizeof(file_handler));
                   18116:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   18117:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   18118:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     18119: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18120:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     18121: #else
                   18122:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   18123: #endif
                   18124:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     18125:        }
1.1.1.21  root     18126:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     18127: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   18128:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     18129:        }
1.1       root     18130:        _dup2(0, DUP_STDIN);
                   18131:        _dup2(1, DUP_STDOUT);
                   18132:        _dup2(2, DUP_STDERR);
1.1.1.21  root     18133:        _dup2(3, DUP_STDAUX);
                   18134:        _dup2(4, DUP_STDPRN);
1.1       root     18135:        
1.1.1.24  root     18136:        // init mouse
                   18137:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     18138:        mouse.enabled = true;   // from DOSBox
                   18139:        mouse.hidden = 1;       // hidden in default ???
                   18140:        mouse.old_hidden = 1;   // from DOSBox
                   18141:        mouse.max_position.x = 8 * (scr_width  - 1);
                   18142:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     18143:        mouse.mickey.x = 8;
                   18144:        mouse.mickey.y = 16;
                   18145:        
1.1.1.26  root     18146: #ifdef SUPPORT_XMS
                   18147:        // init xms
                   18148:        msdos_xms_init();
                   18149: #endif
                   18150:        
1.1       root     18151:        // init process
                   18152:        memset(process, 0, sizeof(process));
                   18153:        
1.1.1.13  root     18154:        // init dtainfo
                   18155:        msdos_dta_info_init();
                   18156:        
1.1       root     18157:        // init memory
                   18158:        memset(mem, 0, sizeof(mem));
                   18159:        
                   18160:        // bios data area
1.1.1.23  root     18161:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     18162:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   18163:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.58  root     18164: //     CONSOLE_FONT_INFO cfi;
1.1.1.57  root     18165: //     GetCurrentConsoleFont(hStdout, FALSE, &cfi);
1.1.1.14  root     18166:        
                   18167:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   18168:        text_vram_top_address = TEXT_VRAM_TOP;
                   18169:        text_vram_end_address = text_vram_top_address + regen;
                   18170:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   18171:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     18172:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     18173:        
                   18174:        if(regen > 0x4000) {
                   18175:                regen = 0x8000;
                   18176:                vram_pages = 1;
                   18177:        } else if(regen > 0x2000) {
                   18178:                regen = 0x4000;
                   18179:                vram_pages = 2;
                   18180:        } else if(regen > 0x1000) {
                   18181:                regen = 0x2000;
                   18182:                vram_pages = 4;
                   18183:        } else {
                   18184:                regen = 0x1000;
                   18185:                vram_pages = 8;
                   18186:        }
1.1       root     18187:        
1.1.1.25  root     18188:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   18189:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     18190:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   18191:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     18192:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     18193:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   18194:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     18195: #ifdef EXT_BIOS_TOP
1.1.1.25  root     18196:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     18197: #endif
1.1.1.26  root     18198:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     18199:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     18200:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   18201:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     18202:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     18203:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   18204:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     18205:        *(UINT16 *)(mem + 0x44e) = 0;
                   18206:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     18207:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     18208:        *(UINT8  *)(mem + 0x460) = 7;
                   18209:        *(UINT8  *)(mem + 0x461) = 7;
                   18210:        *(UINT8  *)(mem + 0x462) = 0;
                   18211:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     18212:        *(UINT8  *)(mem + 0x465) = 0x09;
                   18213:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     18214:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     18215:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   18216:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     18217:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
1.1.1.58  root     18218:        *(UINT16 *)(mem + 0x485) = font_height;
1.1.1.14  root     18219:        *(UINT8  *)(mem + 0x487) = 0x60;
                   18220:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     18221: #ifdef EXT_BIOS_TOP
1.1.1.25  root     18222:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     18223: #endif
1.1.1.14  root     18224:        
                   18225:        // initial screen
                   18226:        SMALL_RECT rect;
                   18227:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
1.1.1.60  root     18228:        ReadConsoleOutputA(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.14  root     18229:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   18230:                for(int x = 0; x < scr_width; x++) {
                   18231:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   18232:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   18233:                }
                   18234:        }
1.1       root     18235:        
1.1.1.19  root     18236:        // init mcb
1.1       root     18237:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     18238:        
                   18239:        // iret table
                   18240:        // note: int 2eh vector should address the routine in command.com,
                   18241:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   18242:        // so move iret table into allocated memory block
                   18243:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.58  root     18244:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, (IRET_SIZE + 5 * 128) >> 4);
1.1.1.19  root     18245:        IRET_TOP = seg << 4;
1.1.1.58  root     18246:        seg += (IRET_SIZE + 5 * 128) >> 4;
1.1.1.25  root     18247:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     18248:        
1.1.1.58  root     18249:        // note: SO1 checks int 21h vector and if it aims iret (cfh)
                   18250:        // it is recognized SO1 is not running on MS-DOS environment
                   18251:        for(int i = 0; i < 128; i++) {
                   18252:                // jmp far (IRET_TOP >> 4):(interrupt number)
                   18253:                *(UINT8  *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 0) = 0xea;
                   18254:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 1) = i;
                   18255:                *(UINT16 *)(mem + IRET_TOP + IRET_SIZE + 5 * i + 3) = IRET_TOP >> 4;
                   18256:        }
                   18257:        
1.1.1.19  root     18258:        // dummy xms/ems device
1.1.1.33  root     18259:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     18260:        XMS_TOP = seg << 4;
                   18261:        seg += XMS_SIZE >> 4;
                   18262:        
                   18263:        // environment
1.1.1.33  root     18264:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     18265:        int env_seg = seg;
                   18266:        int ofs = 0;
1.1.1.32  root     18267:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   18268:        char comspec_added = 0;
1.1.1.33  root     18269:        char lastdrive_added = 0;
1.1.1.32  root     18270:        char env_msdos_path[ENV_SIZE] = {0};
                   18271:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     18272:        char prompt_added = 0;
1.1.1.32  root     18273:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     18274:        char tz_added = 0;
1.1.1.45  root     18275:        const char *path, *short_path;
1.1.1.32  root     18276:        
                   18277:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   18278:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18279:                        strcpy(env_append, short_path);
                   18280:                }
                   18281:        }
                   18282:        if((path = getenv("APPEND")) != NULL) {
                   18283:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18284:                        if(env_append[0] != '\0') {
                   18285:                                strcat(env_append, ";");
                   18286:                        }
                   18287:                        strcat(env_append, short_path);
                   18288:                }
                   18289:        }
                   18290:        
                   18291:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   18292:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18293:                        strcpy(comspec_path, short_path);
                   18294:                }
                   18295:        }
                   18296:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   18297:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18298:                        strcpy(comspec_path, short_path);
                   18299:                }
                   18300:        }
1.1       root     18301:        
1.1.1.28  root     18302:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     18303:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18304:                        strcpy(env_msdos_path, short_path);
                   18305:                        strcpy(env_path, short_path);
1.1.1.14  root     18306:                }
                   18307:        }
1.1.1.28  root     18308:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     18309:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18310:                        if(env_path[0] != '\0') {
                   18311:                                strcat(env_path, ";");
                   18312:                        }
                   18313:                        strcat(env_path, short_path);
1.1.1.9   root     18314:                }
                   18315:        }
1.1.1.32  root     18316:        
1.1.1.60  root     18317:        if(GetTempPathA(ENV_SIZE, env_temp) != 0) {
1.1.1.32  root     18318:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     18319:        }
1.1.1.32  root     18320:        for(int i = 0; i < 4; i++) {
                   18321:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   18322:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   18323:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   18324:                                strcpy(env_temp, short_path);
                   18325:                                break;
                   18326:                        }
                   18327:                }
1.1.1.24  root     18328:        }
1.1.1.32  root     18329:        
1.1.1.9   root     18330:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     18331:                // lower to upper
1.1.1.28  root     18332:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     18333:                strcpy(tmp, *p);
                   18334:                for(int i = 0;; i++) {
                   18335:                        if(tmp[i] == '=') {
                   18336:                                tmp[i] = '\0';
                   18337:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     18338:                                my_strupr(name);
1.1       root     18339:                                tmp[i] = '=';
                   18340:                                break;
                   18341:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     18342:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     18343:                        }
                   18344:                }
1.1.1.33  root     18345:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   18346:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   18347:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     18348:                        // ignore non standard environments
                   18349:                } else {
1.1.1.33  root     18350:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     18351:                                if(env_append[0] != '\0') {
                   18352:                                        sprintf(tmp, "APPEND=%s", env_append);
                   18353:                                } else {
                   18354:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   18355:                                }
                   18356:                                append_added = 1;
                   18357:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     18358:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     18359:                                comspec_added = 1;
1.1.1.33  root     18360:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   18361:                                char *env = getenv("MSDOS_LASTDRIVE");
                   18362:                                if(env != NULL) {
                   18363:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   18364:                                }
                   18365:                                lastdrive_added = 1;
                   18366:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     18367:                                if(env_msdos_path[0] != '\0') {
                   18368:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   18369:                                } else {
                   18370:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   18371:                                }
1.1.1.33  root     18372:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     18373:                                if(env_path[0] != '\0') {
                   18374:                                        sprintf(tmp, "PATH=%s", env_path);
                   18375:                                } else {
                   18376:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   18377:                                }
1.1.1.32  root     18378:                                path_added = 1;
1.1.1.33  root     18379:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   18380:                                prompt_added = 1;
1.1.1.28  root     18381:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   18382:                                if(env_temp[0] != '\0') {
                   18383:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   18384:                                } else {
                   18385:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   18386:                                }
1.1.1.32  root     18387:                                temp_added = 1;
1.1.1.33  root     18388:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     18389:                                if(env_temp[0] != '\0') {
                   18390:                                        sprintf(tmp, "TMP=%s", env_temp);
                   18391:                                } else {
                   18392:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     18393:                                }
1.1.1.32  root     18394:                                tmp_added = 1;
1.1.1.33  root     18395:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   18396:                                char *env = getenv("MSDOS_TZ");
                   18397:                                if(env != NULL) {
                   18398:                                        sprintf(tmp, "TZ=%s", env);
                   18399:                                }
                   18400:                                tz_added = 1;
1.1       root     18401:                        }
                   18402:                        int len = strlen(tmp);
1.1.1.14  root     18403:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     18404:                                fatalerror("too many environments\n");
                   18405:                        }
                   18406:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   18407:                        ofs += len + 1;
                   18408:                }
                   18409:        }
1.1.1.32  root     18410:        if(!append_added && env_append[0] != '\0') {
                   18411:                #define SET_ENV(name, value) { \
                   18412:                        char tmp[ENV_SIZE]; \
                   18413:                        sprintf(tmp, "%s=%s", name, value); \
                   18414:                        int len = strlen(tmp); \
                   18415:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   18416:                                fatalerror("too many environments\n"); \
                   18417:                        } \
                   18418:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   18419:                        ofs += len + 1; \
                   18420:                }
                   18421:                SET_ENV("APPEND", env_append);
                   18422:        }
                   18423:        if(!comspec_added) {
                   18424:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   18425:        }
1.1.1.33  root     18426:        if(!lastdrive_added) {
                   18427:                SET_ENV("LASTDRIVE", "Z");
                   18428:        }
1.1.1.32  root     18429:        if(!path_added) {
                   18430:                SET_ENV("PATH", env_path);
                   18431:        }
1.1.1.33  root     18432:        if(!prompt_added) {
                   18433:                SET_ENV("PROMPT", "$P$G");
                   18434:        }
1.1.1.32  root     18435:        if(!temp_added) {
                   18436:                SET_ENV("TEMP", env_temp);
                   18437:        }
                   18438:        if(!tmp_added) {
                   18439:                SET_ENV("TMP", env_temp);
                   18440:        }
1.1.1.33  root     18441:        if(!tz_added) {
                   18442:                TIME_ZONE_INFORMATION tzi;
                   18443:                HKEY hKey, hSubKey;
                   18444:                char tzi_std_name[64];
                   18445:                char tz_std[8] = "GMT";
                   18446:                char tz_dlt[8] = "GST";
                   18447:                char tz_value[32];
                   18448:                
                   18449:                // timezone name from GetTimeZoneInformation may not be english
                   18450:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   18451:                setlocale(LC_CTYPE, "");
                   18452:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   18453:                
                   18454:                // get english timezone name from registry
1.1.1.60  root     18455:                if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
1.1.1.33  root     18456:                        for(DWORD i = 0; !tz_added; i++) {
                   18457:                                char reg_name[256], sub_key[1024], std_name[256];
                   18458:                                DWORD size;
                   18459:                                FILETIME ftTime;
1.1.1.60  root     18460:                                LONG result = RegEnumKeyExA(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
1.1.1.33  root     18461:                                
                   18462:                                if(result == ERROR_SUCCESS) {
                   18463:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
1.1.1.60  root     18464:                                        if(RegOpenKeyExA(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   18465:                                                if(RegQueryValueExA(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
1.1.1.33  root     18466:                                                        // search english timezone name from table
1.1.1.37  root     18467:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     18468:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   18469:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   18470:                                                                                if(tz_table[j].std != NULL) {
                   18471:                                                                                        strcpy(tz_std, tz_table[j].std);
                   18472:                                                                                }
                   18473:                                                                                if(tz_table[j].dlt != NULL) {
                   18474:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   18475:                                                                                }
                   18476:                                                                                tz_added = 1;
                   18477:                                                                                break;
                   18478:                                                                        }
                   18479:                                                                }
                   18480:                                                        }
                   18481:                                                }
                   18482:                                                RegCloseKey(hSubKey);
                   18483:                                        }
                   18484:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   18485:                                        break;
                   18486:                                }
                   18487:                        }
                   18488:                        RegCloseKey(hKey);
                   18489:                }
                   18490:                if((tzi.Bias % 60) != 0) {
                   18491:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   18492:                } else {
                   18493:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   18494:                }
                   18495:                if(daylight) {
                   18496:                        strcat(tz_value, tz_dlt);
                   18497:                }
                   18498:                SET_ENV("TZ", tz_value);
                   18499:        }
1.1       root     18500:        seg += (ENV_SIZE >> 4);
                   18501:        
                   18502:        // psp
1.1.1.33  root     18503:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     18504:        current_psp = seg;
1.1.1.35  root     18505:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   18506:        psp->parent_psp = current_psp;
1.1       root     18507:        seg += (PSP_SIZE >> 4);
                   18508:        
1.1.1.19  root     18509:        // first free mcb in conventional memory
1.1.1.33  root     18510:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     18511:        first_mcb = seg;
                   18512:        
1.1.1.19  root     18513:        // dummy mcb to link to umb
1.1.1.33  root     18514: #if 0
1.1.1.39  root     18515:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     18516: #else
1.1.1.39  root     18517:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     18518: #endif
1.1.1.19  root     18519:        
                   18520:        // first free mcb in upper memory block
1.1.1.8   root     18521:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     18522:        
1.1.1.29  root     18523: #ifdef SUPPORT_HMA
                   18524:        // first free mcb in high memory area
                   18525:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   18526: #endif
                   18527:        
1.1.1.26  root     18528:        // interrupt vector
1.1.1.58  root     18529:        for(int i = 0; i < 256; i++) {
                   18530:                // 00-07: CPU exception handler
                   18531:                // 08-0F: IRQ 0-7
                   18532:                // 10-1F: PC BIOS
                   18533:                // 20-3F: MS-DOS system call
                   18534:                // 70-77: IRQ 8-15
                   18535:                *(UINT16 *)(mem + 4 * i + 0) = (i <= 0x3f || (i >= 0x70 && i <= 0x77)) ? (IRET_SIZE + 5 * i) : i;
1.1.1.26  root     18536:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   18537:        }
1.1.1.49  root     18538:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   18539:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18540:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   18541:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   18542:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   18543:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     18544:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   18545:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     18546:        
1.1.1.29  root     18547:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     18548:        static const struct {
                   18549:                UINT16 attributes;
                   18550:                char *dev_name;
                   18551:        } dummy_devices[] = {
                   18552:                {0x8013, "CON     "},
                   18553:                {0x8000, "AUX     "},
                   18554:                {0xa0c0, "PRN     "},
                   18555:                {0x8008, "CLOCK$  "},
                   18556:                {0x8000, "COM1    "},
                   18557:                {0xa0c0, "LPT1    "},
                   18558:                {0xa0c0, "LPT2    "},
                   18559:                {0xa0c0, "LPT3    "},
                   18560:                {0x8000, "COM2    "},
                   18561:                {0x8000, "COM3    "},
                   18562:                {0x8000, "COM4    "},
1.1.1.30  root     18563: //             {0xc000, "CONFIG$ "},
                   18564:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     18565:        };
                   18566:        static const UINT8 dummy_device_routine[] = {
                   18567:                // from NUL device of Windows 98 SE
                   18568:                // or word ptr ES:[BX+03],0100
                   18569:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   18570:                // retf
                   18571:                0xcb,
                   18572:        };
1.1.1.29  root     18573:        device_t *last = NULL;
1.1.1.32  root     18574:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     18575:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     18576:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   18577:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18578:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     18579:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   18580:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     18581:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     18582:                last = device;
                   18583:        }
                   18584:        if(last != NULL) {
                   18585:                last->next_driver.w.l = 0;
                   18586:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     18587:        }
1.1.1.29  root     18588:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     18589:        
1.1.1.25  root     18590:        // dos info
                   18591:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   18592:        dos_info->magic_word = 1;
                   18593:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   18594:        dos_info->first_dpb.w.l = 0;
                   18595:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   18596:        dos_info->first_sft.w.l = 0;
                   18597:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     18598:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     18599:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     18600:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     18601:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   18602:        dos_info->max_sector_len = 512;
                   18603:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   18604:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   18605:        dos_info->cds.w.l = 0;
                   18606:        dos_info->cds.w.h = CDS_TOP >> 4;
                   18607:        dos_info->fcb_table.w.l = 0;
                   18608:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   18609:        dos_info->last_drive = 'Z' - 'A' + 1;
                   18610:        dos_info->buffers_x = 20;
                   18611:        dos_info->buffers_y = 0;
                   18612:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     18613:        dos_info->nul_device.next_driver.w.l = 22;
                   18614:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     18615:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     18616:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   18617:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18618:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   18619:        dos_info->disk_buf_heads.w.l = 0;
                   18620:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     18621:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     18622:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   18623:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     18624:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     18625:        
                   18626:        char *env;
                   18627:        if((env = getenv("LASTDRIVE")) != NULL) {
                   18628:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18629:                        dos_info->last_drive = env[0] - 'A' + 1;
                   18630:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18631:                        dos_info->last_drive = env[0] - 'a' + 1;
                   18632:                }
                   18633:        }
                   18634:        if((env = getenv("windir")) != NULL) {
                   18635:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   18636:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   18637:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   18638:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   18639:                }
                   18640:        }
                   18641: #if defined(HAS_I386)
                   18642:        dos_info->i386_or_later = 1;
                   18643: #else
                   18644:        dos_info->i386_or_later = 0;
                   18645: #endif
                   18646:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   18647:        
1.1.1.27  root     18648:        // ems (int 67h) and xms
1.1.1.25  root     18649:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   18650:        xms_device->next_driver.w.l = 0xffff;
                   18651:        xms_device->next_driver.w.h = 0xffff;
                   18652:        xms_device->attributes = 0xc000;
1.1.1.29  root     18653:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   18654:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     18655:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   18656:        
1.1.1.59  root     18657:        mem[XMS_TOP + 0x12] = 0xcd;     // int 65h (dummy)
                   18658:        mem[XMS_TOP + 0x13] = 0x65;
1.1.1.26  root     18659:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     18660: #ifdef SUPPORT_XMS
                   18661:        if(support_xms) {
1.1.1.59  root     18662:                mem[XMS_TOP + 0x15] = 0xcd;     // int 66h (dummy)
                   18663:                mem[XMS_TOP + 0x16] = 0x66;
1.1.1.26  root     18664:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     18665:        } else
                   18666: #endif
1.1.1.26  root     18667:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     18668:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     18669:        
1.1.1.26  root     18670:        // irq12 routine (mouse)
1.1.1.59  root     18671:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 69h (dummy)
                   18672:        mem[DUMMY_TOP + 0x01] = 0x69;
1.1.1.49  root     18673:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   18674:        mem[DUMMY_TOP + 0x03] = 0xff;
                   18675:        mem[DUMMY_TOP + 0x04] = 0xff;
                   18676:        mem[DUMMY_TOP + 0x05] = 0xff;
                   18677:        mem[DUMMY_TOP + 0x06] = 0xff;
1.1.1.59  root     18678: //     mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6ah (dummy)
                   18679: //     mem[DUMMY_TOP + 0x08] = 0x6a;
1.1.1.49  root     18680:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   18681:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   18682:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     18683:        
1.1.1.27  root     18684:        // case map routine
1.1.1.49  root     18685:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   18686:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   18687:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     18688:        
                   18689:        // font read routine
1.1.1.49  root     18690:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   18691:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   18692:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     18693:        
1.1.1.32  root     18694:        // error message read routine
1.1.1.49  root     18695:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   18696:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   18697:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     18698:        
1.1.1.35  root     18699:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     18700:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   18701:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   18702:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   18703:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   18704:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     18705:        
1.1.1.50  root     18706:        // irq0 routine (system timer)
1.1.1.49  root     18707:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   18708:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   18709:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   18710:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   18711:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   18712:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   18713:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   18714:        
                   18715:        // alter page map and call routine
                   18716:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   18717:        mem[DUMMY_TOP + 0x20] = 0xff;
                   18718:        mem[DUMMY_TOP + 0x21] = 0xff;
                   18719:        mem[DUMMY_TOP + 0x22] = 0xff;
                   18720:        mem[DUMMY_TOP + 0x23] = 0xff;
                   18721:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   18722:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   18723:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     18724:        
1.1.1.50  root     18725:        // call int 29h routine
                   18726:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   18727:        mem[DUMMY_TOP + 0x28] = 0x29;
                   18728:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   18729:        
1.1.1.63  root     18730:        // VCPI entry point
                   18731:        mem[DUMMY_TOP + 0x2a] = 0xcd;   // int 65h
                   18732:        mem[DUMMY_TOP + 0x2b] = 0x65;
                   18733:        mem[DUMMY_TOP + 0x2c] = 0xcb;   // retf
                   18734:        
1.1.1.26  root     18735:        // boot routine
1.1.1.59  root     18736:        mem[0xffff0 + 0x00] = 0xf4;     // halt to exit MS-DOS Player
                   18737: #if 1
                   18738:        mem[0xffff0 + 0x05] = '0';      // rom date (same as DOSBox)
                   18739:        mem[0xffff0 + 0x06] = '1';
                   18740:        mem[0xffff0 + 0x07] = '/';
                   18741:        mem[0xffff0 + 0x08] = '0';
                   18742:        mem[0xffff0 + 0x09] = '1';
                   18743:        mem[0xffff0 + 0x0a] = '/';
                   18744:        mem[0xffff0 + 0x0b] = '9';
                   18745:        mem[0xffff0 + 0x0c] = '2';
                   18746:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
                   18747:        mem[0xffff0 + 0x0f] = 0x55;     // signature
                   18748: #else
                   18749:        mem[0xffff0 + 0x05] = '0';      // rom date (same as Windows 98 SE)
1.1.1.49  root     18750:        mem[0xffff0 + 0x06] = '2';
                   18751:        mem[0xffff0 + 0x07] = '/';
                   18752:        mem[0xffff0 + 0x08] = '2';
                   18753:        mem[0xffff0 + 0x09] = '2';
                   18754:        mem[0xffff0 + 0x0a] = '/';
                   18755:        mem[0xffff0 + 0x0b] = '0';
                   18756:        mem[0xffff0 + 0x0c] = '6';
1.1.1.59  root     18757:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id (pc/at)
1.1.1.49  root     18758:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.59  root     18759: #endif
1.1.1.24  root     18760:        
1.1       root     18761:        // param block
                   18762:        // + 0: param block (22bytes)
                   18763:        // +24: fcb1/2 (20bytes)
                   18764:        // +44: command tail (128bytes)
                   18765:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   18766:        param->env_seg = 0;
                   18767:        param->cmd_line.w.l = 44;
                   18768:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   18769:        param->fcb1.w.l = 24;
                   18770:        param->fcb1.w.h = (WORK_TOP >> 4);
                   18771:        param->fcb2.w.l = 24;
                   18772:        param->fcb2.w.h = (WORK_TOP >> 4);
                   18773:        
                   18774:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   18775:        
                   18776:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   18777:        if(argc > 1) {
                   18778:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   18779:                for(int i = 2; i < argc; i++) {
                   18780:                        char tmp[128];
                   18781:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   18782:                        strcpy(cmd_line->cmd, tmp);
                   18783:                }
                   18784:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   18785:        } else {
                   18786:                cmd_line->len = 0;
                   18787:        }
                   18788:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   18789:        
                   18790:        // system file table
1.1.1.21  root     18791:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   18792:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     18793:        
1.1.1.19  root     18794:        // disk buffer header (from DOSBox)
                   18795:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   18796:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   18797:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   18798:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   18799:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   18800:        
1.1       root     18801:        // fcb table
                   18802:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     18803:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     18804:        
1.1.1.41  root     18805:        // drive parameter block
1.1.1.42  root     18806:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     18807:                // may be a floppy drive
1.1.1.44  root     18808:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   18809:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   18810:                cds->drive_attrib = 0x4000;     // physical drive
                   18811:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   18812:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   18813:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   18814:                cds->bs_offset = 2;
                   18815:                
1.1.1.41  root     18816:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   18817:                dpb->drive_num = i;
                   18818:                dpb->unit_num = i;
1.1.1.43  root     18819:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   18820:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     18821:        }
                   18822:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     18823:                msdos_cds_update(i);
1.1.1.42  root     18824:                UINT16 seg, ofs;
                   18825:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     18826:        }
                   18827:        
1.1.1.17  root     18828:        // nls stuff
                   18829:        msdos_nls_tables_init();
1.1       root     18830:        
                   18831:        // execute command
1.1.1.28  root     18832:        try {
1.1.1.52  root     18833:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     18834:                        fatalerror("'%s' not found\n", argv[0]);
                   18835:                }
                   18836:        } catch(...) {
                   18837:                // we should not reach here :-(
                   18838:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     18839:        }
                   18840:        retval = 0;
                   18841:        return(0);
                   18842: }
                   18843: 
                   18844: #define remove_std_file(path) { \
                   18845:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   18846:        if(fd != -1) { \
                   18847:                _lseek(fd, 0, SEEK_END); \
                   18848:                int size = _tell(fd); \
                   18849:                _close(fd); \
                   18850:                if(size == 0) { \
                   18851:                        remove(path); \
                   18852:                } \
                   18853:        } \
                   18854: }
                   18855: 
                   18856: void msdos_finish()
                   18857: {
                   18858:        for(int i = 0; i < MAX_FILES; i++) {
                   18859:                if(file_handler[i].valid) {
                   18860:                        _close(i);
                   18861:                }
                   18862:        }
1.1.1.21  root     18863: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     18864:        remove_std_file("stdaux.txt");
1.1.1.21  root     18865: #endif
1.1.1.30  root     18866: #ifdef SUPPORT_XMS
                   18867:        msdos_xms_finish();
                   18868: #endif
1.1       root     18869:        msdos_dbcs_table_finish();
                   18870: }
                   18871: 
                   18872: /* ----------------------------------------------------------------------------
                   18873:        PC/AT hardware emulation
                   18874: ---------------------------------------------------------------------------- */
                   18875: 
                   18876: void hardware_init()
                   18877: {
1.1.1.3   root     18878:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     18879:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     18880:        m_IF = 1;
1.1.1.3   root     18881: #if defined(HAS_I386)
1.1       root     18882:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   18883:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     18884: #endif
                   18885:        i386_set_a20_line(0);
1.1.1.14  root     18886:        
1.1.1.19  root     18887:        ems_init();
1.1.1.25  root     18888:        dma_init();
1.1       root     18889:        pic_init();
1.1.1.25  root     18890:        pio_init();
1.1.1.8   root     18891: #ifdef PIT_ALWAYS_RUNNING
                   18892:        pit_init();
                   18893: #else
1.1       root     18894:        pit_active = 0;
                   18895: #endif
1.1.1.25  root     18896:        sio_init();
1.1.1.8   root     18897:        cmos_init();
                   18898:        kbd_init();
1.1       root     18899: }
                   18900: 
1.1.1.10  root     18901: void hardware_finish()
                   18902: {
                   18903: #if defined(HAS_I386)
                   18904:        vtlb_free(m_vtlb);
                   18905: #endif
1.1.1.19  root     18906:        ems_finish();
1.1.1.37  root     18907:        pio_finish();
1.1.1.25  root     18908:        sio_finish();
1.1.1.10  root     18909: }
                   18910: 
1.1.1.28  root     18911: void hardware_release()
                   18912: {
                   18913:        // release hardware resources when this program will be terminated abnormally
                   18914: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18915:        if(fp_debug_log != NULL) {
                   18916:                fclose(fp_debug_log);
                   18917:                fp_debug_log = NULL;
1.1.1.28  root     18918:        }
                   18919: #endif
                   18920: #if defined(HAS_I386)
                   18921:        vtlb_free(m_vtlb);
                   18922: #endif
                   18923:        ems_release();
1.1.1.37  root     18924:        pio_release();
1.1.1.28  root     18925:        sio_release();
                   18926: }
                   18927: 
1.1       root     18928: void hardware_run()
                   18929: {
1.1.1.22  root     18930: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     18931:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     18932:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     18933: #endif
1.1.1.51  root     18934: #ifdef USE_DEBUGGER
                   18935:        m_int_num = -1;
                   18936: #endif
1.1.1.54  root     18937:        while(!m_exit) {
1.1.1.50  root     18938:                hardware_run_cpu();
1.1       root     18939:        }
1.1.1.22  root     18940: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     18941:        if(fp_debug_log != NULL) {
                   18942:                fclose(fp_debug_log);
                   18943:                fp_debug_log = NULL;
1.1.1.28  root     18944:        }
1.1.1.22  root     18945: #endif
1.1       root     18946: }
                   18947: 
1.1.1.50  root     18948: inline void hardware_run_cpu()
                   18949: {
                   18950: #if defined(HAS_I386)
                   18951:        CPU_EXECUTE_CALL(i386);
                   18952:        if(m_eip != m_prev_eip) {
                   18953:                idle_ops++;
                   18954:        }
                   18955: #else
                   18956:        CPU_EXECUTE_CALL(CPU_MODEL);
                   18957:        if(m_pc != m_prevpc) {
                   18958:                idle_ops++;
                   18959:        }
                   18960: #endif
                   18961: #ifdef USE_DEBUGGER
                   18962:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   18963:        if(m_int_num >= 0) {
                   18964:                unsigned num = (unsigned)m_int_num;
                   18965:                m_int_num = -1;
                   18966:                msdos_syscall(num);
                   18967:        }
                   18968: #endif
                   18969:        if(++update_ops == UPDATE_OPS) {
                   18970:                update_ops = 0;
                   18971:                hardware_update();
                   18972:        }
                   18973: }
                   18974: 
1.1       root     18975: void hardware_update()
                   18976: {
1.1.1.8   root     18977:        static UINT32 prev_time = 0;
                   18978:        UINT32 cur_time = timeGetTime();
                   18979:        
                   18980:        if(prev_time != cur_time) {
                   18981:                // update pit and raise irq0
                   18982: #ifndef PIT_ALWAYS_RUNNING
                   18983:                if(pit_active)
                   18984: #endif
                   18985:                {
                   18986:                        if(pit_run(0, cur_time)) {
                   18987:                                pic_req(0, 0, 1);
                   18988:                        }
                   18989:                        pit_run(1, cur_time);
                   18990:                        pit_run(2, cur_time);
                   18991:                }
1.1.1.24  root     18992:                
1.1.1.25  root     18993:                // update sio and raise irq4/3
1.1.1.29  root     18994:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18995:                        sio_update(c);
                   18996:                }
                   18997:                
1.1.1.24  root     18998:                // update keyboard and mouse
1.1.1.14  root     18999:                static UINT32 prev_tick = 0;
                   19000:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     19001:                
1.1.1.14  root     19002:                if(prev_tick != cur_tick) {
                   19003:                        // update keyboard flags
                   19004:                        UINT8 state;
1.1.1.24  root     19005:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   19006:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   19007:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   19008:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   19009:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   19010:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   19011:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   19012:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     19013:                        mem[0x417] = state;
                   19014:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   19015:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   19016:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   19017:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     19018: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   19019: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     19020:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   19021:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   19022:                        mem[0x418] = state;
                   19023:                        
1.1.1.24  root     19024:                        // update console input if needed
1.1.1.34  root     19025:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     19026:                                update_console_input();
                   19027:                        }
1.1.1.57  root     19028:                        if(!(kbd_status & 1)) {
                   19029:                                if(key_buf_data != NULL) {
                   19030: #ifdef USE_SERVICE_THREAD
                   19031:                                        EnterCriticalSection(&key_buf_crit_sect);
                   19032: #endif
                   19033:                                        if(!key_buf_data->empty()) {
                   19034:                                                kbd_data = key_buf_data->read();
                   19035:                                                kbd_status |= 1;
                   19036:                                                key_changed = true;
                   19037:                                        }
                   19038: #ifdef USE_SERVICE_THREAD
                   19039:                                        LeaveCriticalSection(&key_buf_crit_sect);
                   19040: #endif
                   19041:                                }
                   19042:                        }
1.1.1.24  root     19043:                        
1.1.1.57  root     19044:                        // raise irq1 if key is pressed/released or key buffer is not empty
1.1.1.56  root     19045:                        if(!key_changed) {
1.1.1.55  root     19046: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     19047:                                EnterCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     19048: #endif
1.1.1.57  root     19049:                                if(!pcbios_is_key_buffer_empty()) {
                   19050: /*
                   19051:                                        if(!(kbd_status & 1)) {
                   19052:                                                UINT16 head = *(UINT16 *)(mem + 0x41a);
                   19053:                                                UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   19054:                                                if(head != tail) {
                   19055:                                                        int key_char = mem[0x400 + (head++)];
                   19056:                                                        int key_scan = mem[0x400 + (head++)];
                   19057:                                                        kbd_data = key_char ? key_char : key_scan;
                   19058:                                                        kbd_status |= 1;
                   19059:                                                }
                   19060:                                        }
                   19061: */
                   19062:                                        key_changed = true;
                   19063:                                }
1.1.1.55  root     19064: #ifdef USE_SERVICE_THREAD
1.1.1.56  root     19065:                                LeaveCriticalSection(&key_buf_crit_sect);
1.1.1.55  root     19066: #endif
1.1.1.56  root     19067:                        }
                   19068:                        if(key_changed) {
1.1.1.8   root     19069:                                pic_req(0, 1, 1);
1.1.1.56  root     19070:                                key_changed = false;
1.1.1.24  root     19071:                        }
                   19072:                        
                   19073:                        // raise irq12 if mouse status is changed
1.1.1.59  root     19074:                        if((mouse.status & 0x1f) && mouse.call_addr_ps2.dw && mouse.enabled_ps2) {
1.1.1.54  root     19075:                                mouse.status_irq = 0; // ???
                   19076:                                mouse.status_irq_alt = 0; // ???
                   19077:                                mouse.status_irq_ps2 = mouse.status & 0x1f;
                   19078:                                mouse.status = 0;
                   19079:                                pic_req(1, 4, 1);
1.1.1.59  root     19080:                        } else if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
1.1.1.43  root     19081:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   19082:                                mouse.status_irq_alt = 0; // ???
1.1.1.54  root     19083:                                mouse.status_irq_ps2 = 0; // ???
1.1.1.24  root     19084:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     19085:                                pic_req(1, 4, 1);
                   19086:                        } else {
                   19087:                                for(int i = 0; i < 8; i++) {
                   19088:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   19089:                                                mouse.status_irq = 0; // ???
                   19090:                                                mouse.status_irq_alt = 0;
1.1.1.54  root     19091:                                                mouse.status_irq_ps2 = 0; // ???
1.1.1.43  root     19092:                                                for(int j = 0; j < 8; j++) {
                   19093:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   19094:                                                                mouse.status_irq_alt |=  (1 << j);
                   19095:                                                                mouse.status_alt     &= ~(1 << j);
                   19096:                                                        }
                   19097:                                                }
                   19098:                                                pic_req(1, 4, 1);
                   19099:                                                break;
                   19100:                                        }
                   19101:                                }
1.1.1.8   root     19102:                        }
1.1.1.24  root     19103:                        
1.1.1.60  root     19104:                        prev_tick = cur_tick;
                   19105:                }
                   19106:                
                   19107:                // update cursor size/position by crtc
                   19108:                if(crtc_changed[10] != 0 || crtc_changed[11] != 0) {
                   19109:                        int size = (int)(crtc_regs[11] & 7) - (int)(crtc_regs[10] & 7) + 1;
                   19110:                        if(!((crtc_regs[10] & 0x20) != 0 || size < 0)) {
                   19111:                                ci_new.bVisible = TRUE;
                   19112:                                ci_new.dwSize = (size + 2) * 100 / (8 + 2);
1.1.1.59  root     19113:                        } else {
1.1.1.60  root     19114:                                ci_new.bVisible = FALSE;
                   19115:                        }
                   19116:                        crtc_changed[10] = crtc_changed[11] = 0;
                   19117:                }
                   19118:                if(crtc_changed[14] != 0 || crtc_changed[15] != 0) {
                   19119:                        if(cursor_moved) {
                   19120:                                pcbios_update_cursor_position();
                   19121:                                cursor_moved = false;
1.1.1.59  root     19122:                        }
1.1.1.60  root     19123:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   19124:                        int position = crtc_regs[14] * 256 + crtc_regs[15];
                   19125:                        int width = *(UINT16 *)(mem + 0x44a);
                   19126:                        COORD co;
                   19127:                        co.X = position % width;
                   19128:                        co.Y = position / width + scr_top;
                   19129:                        SetConsoleCursorPosition(hStdout, co);
1.1.1.59  root     19130:                        
1.1.1.60  root     19131:                        crtc_changed[14] = crtc_changed[15] = 0;
                   19132:                        cursor_moved_by_crtc = true;
                   19133:                }
                   19134:                
                   19135:                // update cursor info
                   19136:                if(!is_cursor_blink_off()) {
                   19137:                        ci_new.bVisible = TRUE;
                   19138:                }
                   19139:                if(!(ci_old.dwSize == ci_new.dwSize && ci_old.bVisible == ci_new.bVisible)) {
                   19140:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
                   19141:                        SetConsoleCursorInfo(hStdout, &ci_new);
1.1.1.8   root     19142:                }
1.1.1.60  root     19143:                ci_old = ci_new;
1.1.1.24  root     19144:                
1.1.1.19  root     19145:                // update daily timer counter
                   19146:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     19147:                
1.1.1.8   root     19148:                prev_time = cur_time;
1.1       root     19149:        }
                   19150: }
                   19151: 
1.1.1.19  root     19152: // ems
                   19153: 
                   19154: void ems_init()
                   19155: {
                   19156:        memset(ems_handles, 0, sizeof(ems_handles));
                   19157:        memset(ems_pages, 0, sizeof(ems_pages));
                   19158:        free_ems_pages = MAX_EMS_PAGES;
                   19159: }
                   19160: 
                   19161: void ems_finish()
                   19162: {
1.1.1.28  root     19163:        ems_release();
                   19164: }
                   19165: 
                   19166: void ems_release()
                   19167: {
1.1.1.31  root     19168:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     19169:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     19170:                        free(ems_handles[i].buffer);
                   19171:                        ems_handles[i].buffer = NULL;
                   19172:                }
                   19173:        }
                   19174: }
                   19175: 
                   19176: void ems_allocate_pages(int handle, int pages)
                   19177: {
                   19178:        if(pages > 0) {
                   19179:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   19180:        } else {
                   19181:                ems_handles[handle].buffer = NULL;
                   19182:        }
                   19183:        ems_handles[handle].pages = pages;
                   19184:        ems_handles[handle].allocated = true;
                   19185:        free_ems_pages -= pages;
                   19186: }
                   19187: 
                   19188: void ems_reallocate_pages(int handle, int pages)
                   19189: {
                   19190:        if(ems_handles[handle].allocated) {
                   19191:                if(ems_handles[handle].pages != pages) {
                   19192:                        UINT8 *new_buffer = NULL;
                   19193:                        
                   19194:                        if(pages > 0) {
                   19195:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   19196:                        }
1.1.1.32  root     19197:                        if(ems_handles[handle].buffer != NULL) {
                   19198:                                if(new_buffer != NULL) {
1.1.1.19  root     19199:                                        if(pages > ems_handles[handle].pages) {
                   19200:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   19201:                                        } else {
                   19202:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   19203:                                        }
                   19204:                                }
                   19205:                                free(ems_handles[handle].buffer);
                   19206:                                ems_handles[handle].buffer = NULL;
                   19207:                        }
                   19208:                        free_ems_pages += ems_handles[handle].pages;
                   19209:                        
                   19210:                        ems_handles[handle].buffer = new_buffer;
                   19211:                        ems_handles[handle].pages = pages;
                   19212:                        free_ems_pages -= pages;
                   19213:                }
                   19214:        } else {
                   19215:                ems_allocate_pages(handle, pages);
                   19216:        }
                   19217: }
                   19218: 
                   19219: void ems_release_pages(int handle)
                   19220: {
                   19221:        if(ems_handles[handle].allocated) {
1.1.1.32  root     19222:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     19223:                        free(ems_handles[handle].buffer);
                   19224:                        ems_handles[handle].buffer = NULL;
                   19225:                }
                   19226:                free_ems_pages += ems_handles[handle].pages;
                   19227:                ems_handles[handle].allocated = false;
                   19228:        }
                   19229: }
                   19230: 
                   19231: void ems_map_page(int physical, int handle, int logical)
                   19232: {
                   19233:        if(ems_pages[physical].mapped) {
                   19234:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   19235:                        return;
                   19236:                }
                   19237:                ems_unmap_page(physical);
                   19238:        }
1.1.1.32  root     19239:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     19240:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   19241:        }
                   19242:        ems_pages[physical].handle = handle;
                   19243:        ems_pages[physical].page = logical;
                   19244:        ems_pages[physical].mapped = true;
                   19245: }
                   19246: 
                   19247: void ems_unmap_page(int physical)
                   19248: {
                   19249:        if(ems_pages[physical].mapped) {
                   19250:                int handle = ems_pages[physical].handle;
                   19251:                int logical = ems_pages[physical].page;
                   19252:                
1.1.1.32  root     19253:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     19254:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   19255:                }
                   19256:                ems_pages[physical].mapped = false;
                   19257:        }
                   19258: }
                   19259: 
1.1.1.25  root     19260: // dma
1.1       root     19261: 
1.1.1.25  root     19262: void dma_init()
1.1       root     19263: {
1.1.1.26  root     19264:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     19265:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     19266: //             for(int ch = 0; ch < 4; ch++) {
                   19267: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   19268: //             }
1.1.1.25  root     19269:                dma_reset(c);
                   19270:        }
1.1       root     19271: }
                   19272: 
1.1.1.25  root     19273: void dma_reset(int c)
1.1       root     19274: {
1.1.1.25  root     19275:        dma[c].low_high = false;
                   19276:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   19277:        dma[c].mask = 0xff;
                   19278: }
                   19279: 
                   19280: void dma_write(int c, UINT32 addr, UINT8 data)
                   19281: {
                   19282:        int ch = (addr >> 1) & 3;
                   19283:        UINT8 bit = 1 << (data & 3);
                   19284:        
                   19285:        switch(addr & 0x0f) {
                   19286:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19287:                if(dma[c].low_high) {
                   19288:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     19289:                } else {
1.1.1.25  root     19290:                        dma[c].ch[ch].bareg.b.l = data;
                   19291:                }
                   19292:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19293:                dma[c].low_high = !dma[c].low_high;
                   19294:                break;
                   19295:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19296:                if(dma[c].low_high) {
                   19297:                        dma[c].ch[ch].bcreg.b.h = data;
                   19298:                } else {
                   19299:                        dma[c].ch[ch].bcreg.b.l = data;
                   19300:                }
                   19301:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19302:                dma[c].low_high = !dma[c].low_high;
                   19303:                break;
                   19304:        case 0x08:
                   19305:                // command register
                   19306:                dma[c].cmd = data;
                   19307:                break;
                   19308:        case 0x09:
                   19309:                // dma[c].request register
                   19310:                if(data & 4) {
                   19311:                        if(!(dma[c].req & bit)) {
                   19312:                                dma[c].req |= bit;
                   19313: //                             dma_run(c, ch);
                   19314:                        }
                   19315:                } else {
                   19316:                        dma[c].req &= ~bit;
                   19317:                }
                   19318:                break;
                   19319:        case 0x0a:
                   19320:                // single mask register
                   19321:                if(data & 4) {
                   19322:                        dma[c].mask |= bit;
                   19323:                } else {
                   19324:                        dma[c].mask &= ~bit;
                   19325:                }
                   19326:                break;
                   19327:        case 0x0b:
                   19328:                // mode register
                   19329:                dma[c].ch[data & 3].mode = data;
                   19330:                break;
                   19331:        case 0x0c:
                   19332:                dma[c].low_high = false;
                   19333:                break;
                   19334:        case 0x0d:
                   19335:                // clear master
                   19336:                dma_reset(c);
                   19337:                break;
                   19338:        case 0x0e:
                   19339:                // clear mask register
                   19340:                dma[c].mask = 0;
                   19341:                break;
                   19342:        case 0x0f:
                   19343:                // all mask register
                   19344:                dma[c].mask = data & 0x0f;
                   19345:                break;
                   19346:        }
                   19347: }
                   19348: 
                   19349: UINT8 dma_read(int c, UINT32 addr)
                   19350: {
                   19351:        int ch = (addr >> 1) & 3;
                   19352:        UINT8 val = 0xff;
                   19353:        
                   19354:        switch(addr & 0x0f) {
                   19355:        case 0x00: case 0x02: case 0x04: case 0x06:
                   19356:                if(dma[c].low_high) {
                   19357:                        val = dma[c].ch[ch].areg.b.h;
                   19358:                } else {
                   19359:                        val = dma[c].ch[ch].areg.b.l;
                   19360:                }
                   19361:                dma[c].low_high = !dma[c].low_high;
                   19362:                return(val);
                   19363:        case 0x01: case 0x03: case 0x05: case 0x07:
                   19364:                if(dma[c].low_high) {
                   19365:                        val = dma[c].ch[ch].creg.b.h;
                   19366:                } else {
                   19367:                        val = dma[c].ch[ch].creg.b.l;
                   19368:                }
                   19369:                dma[c].low_high = !dma[c].low_high;
                   19370:                return(val);
                   19371:        case 0x08:
                   19372:                // status register
                   19373:                val = (dma[c].req << 4) | dma[c].tc;
                   19374:                dma[c].tc = 0;
                   19375:                return(val);
                   19376:        case 0x0d:
1.1.1.26  root     19377:                // temporary register (intel 82374 does not support)
1.1.1.25  root     19378:                return(dma[c].tmp & 0xff);
1.1.1.26  root     19379:        case 0x0f:
                   19380:                // mask register (intel 82374 does support)
                   19381:                return(dma[c].mask);
1.1.1.25  root     19382:        }
                   19383:        return(0xff);
                   19384: }
                   19385: 
                   19386: void dma_page_write(int c, int ch, UINT8 data)
                   19387: {
                   19388:        dma[c].ch[ch].pagereg = data;
                   19389: }
                   19390: 
                   19391: UINT8 dma_page_read(int c, int ch)
                   19392: {
                   19393:        return(dma[c].ch[ch].pagereg);
                   19394: }
                   19395: 
                   19396: void dma_run(int c, int ch)
                   19397: {
                   19398:        UINT8 bit = 1 << ch;
                   19399:        
                   19400:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   19401:                // execute dma
                   19402:                while(dma[c].req & bit) {
                   19403:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   19404:                                // memory -> memory
                   19405:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   19406:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   19407:                                
                   19408:                                if(c == 0) {
                   19409:                                        dma[c].tmp = read_byte(saddr);
                   19410:                                        write_byte(daddr, dma[c].tmp);
                   19411:                                } else {
                   19412:                                        dma[c].tmp = read_word(saddr << 1);
                   19413:                                        write_word(daddr << 1, dma[c].tmp);
                   19414:                                }
                   19415:                                if(!(dma[c].cmd & 0x02)) {
                   19416:                                        if(dma[c].ch[0].mode & 0x20) {
                   19417:                                                dma[c].ch[0].areg.w--;
                   19418:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   19419:                                                        dma[c].ch[0].pagereg--;
                   19420:                                                }
                   19421:                                        } else {
                   19422:                                                dma[c].ch[0].areg.w++;
                   19423:                                                if(dma[c].ch[0].areg.w == 0) {
                   19424:                                                        dma[c].ch[0].pagereg++;
                   19425:                                                }
                   19426:                                        }
                   19427:                                }
                   19428:                                if(dma[c].ch[1].mode & 0x20) {
                   19429:                                        dma[c].ch[1].areg.w--;
                   19430:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   19431:                                                dma[c].ch[1].pagereg--;
                   19432:                                        }
                   19433:                                } else {
                   19434:                                        dma[c].ch[1].areg.w++;
                   19435:                                        if(dma[c].ch[1].areg.w == 0) {
                   19436:                                                dma[c].ch[1].pagereg++;
                   19437:                                        }
                   19438:                                }
                   19439:                                
                   19440:                                // check dma condition
                   19441:                                if(dma[c].ch[0].creg.w-- == 0) {
                   19442:                                        if(dma[c].ch[0].mode & 0x10) {
                   19443:                                                // self initialize
                   19444:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   19445:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   19446:                                        } else {
                   19447: //                                             dma[c].mask |= bit;
                   19448:                                        }
                   19449:                                }
                   19450:                                if(dma[c].ch[1].creg.w-- == 0) {
                   19451:                                        // terminal count
                   19452:                                        if(dma[c].ch[1].mode & 0x10) {
                   19453:                                                // self initialize
                   19454:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   19455:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   19456:                                        } else {
                   19457:                                                dma[c].mask |= bit;
                   19458:                                        }
                   19459:                                        dma[c].req &= ~bit;
                   19460:                                        dma[c].tc |= bit;
                   19461:                                }
                   19462:                        } else {
                   19463:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   19464:                                
                   19465:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   19466:                                        // verify
                   19467:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   19468:                                        // io -> memory
                   19469:                                        if(c == 0) {
                   19470:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   19471:                                                write_byte(addr, dma[c].tmp);
                   19472:                                        } else {
                   19473:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   19474:                                                write_word(addr << 1, dma[c].tmp);
                   19475:                                        }
                   19476:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   19477:                                        // memory -> io
                   19478:                                        if(c == 0) {
                   19479:                                                dma[c].tmp = read_byte(addr);
                   19480:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   19481:                                        } else {
                   19482:                                                dma[c].tmp = read_word(addr << 1);
                   19483:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   19484:                                        }
                   19485:                                }
                   19486:                                if(dma[c].ch[ch].mode & 0x20) {
                   19487:                                        dma[c].ch[ch].areg.w--;
                   19488:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   19489:                                                dma[c].ch[ch].pagereg--;
                   19490:                                        }
                   19491:                                } else {
                   19492:                                        dma[c].ch[ch].areg.w++;
                   19493:                                        if(dma[c].ch[ch].areg.w == 0) {
                   19494:                                                dma[c].ch[ch].pagereg++;
                   19495:                                        }
                   19496:                                }
                   19497:                                
                   19498:                                // check dma condition
                   19499:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   19500:                                        // terminal count
                   19501:                                        if(dma[c].ch[ch].mode & 0x10) {
                   19502:                                                // self initialize
                   19503:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   19504:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   19505:                                        } else {
                   19506:                                                dma[c].mask |= bit;
                   19507:                                        }
                   19508:                                        dma[c].req &= ~bit;
                   19509:                                        dma[c].tc |= bit;
                   19510:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   19511:                                        // single mode
                   19512:                                        break;
                   19513:                                }
                   19514:                        }
                   19515:                }
                   19516:        }
                   19517: }
                   19518: 
                   19519: // pic
                   19520: 
                   19521: void pic_init()
                   19522: {
                   19523:        memset(pic, 0, sizeof(pic));
                   19524:        pic[0].imr = pic[1].imr = 0xff;
                   19525:        
                   19526:        // from bochs bios
                   19527:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   19528:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   19529:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   19530:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   19531:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   19532:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   19533:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   19534:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   19535:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   19536: }
                   19537: 
                   19538: void pic_write(int c, UINT32 addr, UINT8 data)
                   19539: {
                   19540:        if(addr & 1) {
                   19541:                if(pic[c].icw2_r) {
                   19542:                        // icw2
                   19543:                        pic[c].icw2 = data;
                   19544:                        pic[c].icw2_r = 0;
                   19545:                } else if(pic[c].icw3_r) {
                   19546:                        // icw3
                   19547:                        pic[c].icw3 = data;
                   19548:                        pic[c].icw3_r = 0;
                   19549:                } else if(pic[c].icw4_r) {
                   19550:                        // icw4
                   19551:                        pic[c].icw4 = data;
                   19552:                        pic[c].icw4_r = 0;
                   19553:                } else {
                   19554:                        // ocw1
1.1       root     19555:                        pic[c].imr = data;
                   19556:                }
                   19557:        } else {
                   19558:                if(data & 0x10) {
                   19559:                        // icw1
                   19560:                        pic[c].icw1 = data;
                   19561:                        pic[c].icw2_r = 1;
                   19562:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   19563:                        pic[c].icw4_r = data & 1;
                   19564:                        pic[c].irr = 0;
                   19565:                        pic[c].isr = 0;
                   19566:                        pic[c].imr = 0;
                   19567:                        pic[c].prio = 0;
                   19568:                        if(!(pic[c].icw1 & 1)) {
                   19569:                                pic[c].icw4 = 0;
                   19570:                        }
                   19571:                        pic[c].ocw3 = 0;
                   19572:                } else if(data & 8) {
                   19573:                        // ocw3
                   19574:                        if(!(data & 2)) {
                   19575:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   19576:                        }
                   19577:                        if(!(data & 0x40)) {
                   19578:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   19579:                        }
                   19580:                        pic[c].ocw3 = data;
                   19581:                } else {
                   19582:                        // ocw2
                   19583:                        int level = 0;
                   19584:                        if(data & 0x40) {
                   19585:                                level = data & 7;
                   19586:                        } else {
                   19587:                                if(!pic[c].isr) {
                   19588:                                        return;
                   19589:                                }
                   19590:                                level = pic[c].prio;
                   19591:                                while(!(pic[c].isr & (1 << level))) {
                   19592:                                        level = (level + 1) & 7;
                   19593:                                }
                   19594:                        }
                   19595:                        if(data & 0x80) {
                   19596:                                pic[c].prio = (level + 1) & 7;
                   19597:                        }
                   19598:                        if(data & 0x20) {
                   19599:                                pic[c].isr &= ~(1 << level);
                   19600:                        }
                   19601:                }
                   19602:        }
                   19603:        pic_update();
                   19604: }
                   19605: 
                   19606: UINT8 pic_read(int c, UINT32 addr)
                   19607: {
                   19608:        if(addr & 1) {
                   19609:                return(pic[c].imr);
                   19610:        } else {
                   19611:                // polling mode is not supported...
                   19612:                //if(pic[c].ocw3 & 4) {
                   19613:                //      return ???;
                   19614:                //}
                   19615:                if(pic[c].ocw3 & 1) {
                   19616:                        return(pic[c].isr);
                   19617:                } else {
                   19618:                        return(pic[c].irr);
                   19619:                }
                   19620:        }
                   19621: }
                   19622: 
                   19623: void pic_req(int c, int level, int signal)
                   19624: {
                   19625:        if(signal) {
                   19626:                pic[c].irr |= (1 << level);
                   19627:        } else {
                   19628:                pic[c].irr &= ~(1 << level);
                   19629:        }
                   19630:        pic_update();
                   19631: }
                   19632: 
                   19633: int pic_ack()
                   19634: {
                   19635:        // ack (INTA=L)
                   19636:        pic[pic_req_chip].isr |= pic_req_bit;
                   19637:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   19638:        if(pic_req_chip > 0) {
                   19639:                // update isr and irr of master
                   19640:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   19641:                pic[pic_req_chip - 1].isr |= slave;
                   19642:                pic[pic_req_chip - 1].irr &= ~slave;
                   19643:        }
                   19644:        //if(pic[pic_req_chip].icw4 & 1) {
                   19645:                // 8086 mode
                   19646:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   19647:        //} else {
                   19648:        //      // 8080 mode
                   19649:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   19650:        //      if(pic[pic_req_chip].icw1 & 4) {
                   19651:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   19652:        //      } else {
                   19653:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   19654:        //      }
                   19655:        //      vector = 0xcd | (addr << 8);
                   19656:        //}
                   19657:        if(pic[pic_req_chip].icw4 & 2) {
                   19658:                // auto eoi
                   19659:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   19660:        }
                   19661:        return(vector);
                   19662: }
                   19663: 
                   19664: void pic_update()
                   19665: {
                   19666:        for(int c = 0; c < 2; c++) {
                   19667:                UINT8 irr = pic[c].irr;
                   19668:                if(c + 1 < 2) {
                   19669:                        // this is master
                   19670:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   19671:                                // request from slave
                   19672:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   19673:                        }
                   19674:                }
                   19675:                irr &= (~pic[c].imr);
                   19676:                if(!irr) {
                   19677:                        break;
                   19678:                }
                   19679:                if(!(pic[c].ocw3 & 0x20)) {
                   19680:                        irr |= pic[c].isr;
                   19681:                }
                   19682:                int level = pic[c].prio;
                   19683:                UINT8 bit = 1 << level;
                   19684:                while(!(irr & bit)) {
                   19685:                        level = (level + 1) & 7;
                   19686:                        bit = 1 << level;
                   19687:                }
                   19688:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   19689:                        // check slave
                   19690:                        continue;
                   19691:                }
                   19692:                if(pic[c].isr & bit) {
                   19693:                        break;
                   19694:                }
                   19695:                // interrupt request
                   19696:                pic_req_chip = c;
                   19697:                pic_req_level = level;
                   19698:                pic_req_bit = bit;
1.1.1.3   root     19699:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     19700:                return;
                   19701:        }
1.1.1.3   root     19702:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     19703: }
1.1       root     19704: 
1.1.1.25  root     19705: // pio
                   19706: 
                   19707: void pio_init()
                   19708: {
1.1.1.38  root     19709: //     bool conv_mode = (GetConsoleCP() == 932);
                   19710:        
1.1.1.26  root     19711:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     19712:        
1.1.1.25  root     19713:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     19714:                pio[c].stat = 0xdf;
1.1.1.25  root     19715:                pio[c].ctrl = 0x0c;
1.1.1.38  root     19716: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     19717:        }
                   19718: }
                   19719: 
1.1.1.37  root     19720: void pio_finish()
                   19721: {
                   19722:        pio_release();
                   19723: }
                   19724: 
                   19725: void pio_release()
                   19726: {
                   19727:        for(int c = 0; c < 2; c++) {
                   19728:                if(pio[c].fp != NULL) {
1.1.1.38  root     19729:                        if(pio[c].jis_mode) {
                   19730:                                fputc(0x1c, pio[c].fp);
                   19731:                                fputc(0x2e, pio[c].fp);
                   19732:                        }
1.1.1.37  root     19733:                        fclose(pio[c].fp);
                   19734:                        pio[c].fp = NULL;
                   19735:                }
                   19736:        }
                   19737: }
                   19738: 
1.1.1.25  root     19739: void pio_write(int c, UINT32 addr, UINT8 data)
                   19740: {
                   19741:        switch(addr & 3) {
                   19742:        case 0:
                   19743:                pio[c].data = data;
                   19744:                break;
                   19745:        case 2:
1.1.1.37  root     19746:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   19747:                        // strobe H -> L
                   19748:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   19749:                                // auto feed
                   19750:                                printer_out(c, 0x0d);
                   19751:                                printer_out(c, 0x0a);
                   19752:                        } else {
                   19753:                                printer_out(c, pio[c].data);
                   19754:                        }
                   19755:                        pio[c].stat &= ~0x40; // set ack
                   19756:                }
1.1.1.25  root     19757:                pio[c].ctrl = data;
                   19758:                break;
                   19759:        }
                   19760: }
                   19761: 
                   19762: UINT8 pio_read(int c, UINT32 addr)
                   19763: {
                   19764:        switch(addr & 3) {
                   19765:        case 0:
1.1.1.37  root     19766:                if(pio[c].ctrl & 0x20) {
                   19767:                        // input mode
                   19768:                        return(0xff);
                   19769:                }
1.1.1.25  root     19770:                return(pio[c].data);
                   19771:        case 1:
1.1.1.37  root     19772:                {
                   19773:                        UINT8 stat = pio[c].stat;
                   19774:                        pio[c].stat |= 0x40; // clear ack
                   19775:                        return(stat);
                   19776:                }
1.1.1.25  root     19777:        case 2:
                   19778:                return(pio[c].ctrl);
                   19779:        }
                   19780:        return(0xff);
                   19781: }
                   19782: 
1.1.1.37  root     19783: void printer_out(int c, UINT8 data)
                   19784: {
                   19785:        SYSTEMTIME time;
1.1.1.38  root     19786:        bool jis_mode = false;
1.1.1.37  root     19787:        
                   19788:        GetLocalTime(&time);
                   19789:        
                   19790:        if(pio[c].fp != NULL) {
                   19791:                // if at least 1000ms passed from last written, close the current file
                   19792:                FILETIME ftime1;
                   19793:                FILETIME ftime2;
                   19794:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   19795:                SystemTimeToFileTime(&time, &ftime2);
                   19796:                INT64 *time1 = (INT64 *)&ftime1;
                   19797:                INT64 *time2 = (INT64 *)&ftime2;
                   19798:                INT64 msec = (*time2 - *time1) / 10000;
                   19799:                
                   19800:                if(msec >= 1000) {
1.1.1.38  root     19801:                        if(pio[c].jis_mode) {
                   19802:                                fputc(0x1c, pio[c].fp);
                   19803:                                fputc(0x2e, pio[c].fp);
                   19804:                                jis_mode = true;
                   19805:                        }
1.1.1.37  root     19806:                        fclose(pio[c].fp);
                   19807:                        pio[c].fp = NULL;
                   19808:                }
                   19809:        }
                   19810:        if(pio[c].fp == NULL) {
                   19811:                // create a new file in the temp folder
                   19812:                char file_name[MAX_PATH];
                   19813:                
                   19814:                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     19815:                if(GetTempPathA(MAX_PATH, pio[c].path)) {
1.1.1.37  root     19816:                        strcat(pio[c].path, file_name);
                   19817:                } else {
                   19818:                        strcpy(pio[c].path, file_name);
                   19819:                }
1.1.1.38  root     19820:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     19821:        }
                   19822:        if(pio[c].fp != NULL) {
1.1.1.38  root     19823:                if(jis_mode) {
                   19824:                        fputc(0x1c, pio[c].fp);
                   19825:                        fputc(0x26, pio[c].fp);
                   19826:                }
1.1.1.37  root     19827:                fputc(data, pio[c].fp);
1.1.1.38  root     19828:                
                   19829:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   19830:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   19831:                        UINT8 buffer[4];
                   19832:                        fseek(pio[c].fp, 0, SEEK_SET);
                   19833:                        fread(buffer, 4, 1, pio[c].fp);
                   19834:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   19835:                                fclose(pio[c].fp);
                   19836:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   19837:                        }
                   19838:                }
1.1.1.37  root     19839:                pio[c].time = time;
                   19840:        }
                   19841: }
                   19842: 
1.1       root     19843: // pit
                   19844: 
1.1.1.22  root     19845: #define PIT_FREQ 1193182ULL
1.1       root     19846: #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)
                   19847: 
                   19848: void pit_init()
                   19849: {
1.1.1.8   root     19850:        memset(pit, 0, sizeof(pit));
1.1       root     19851:        for(int ch = 0; ch < 3; ch++) {
                   19852:                pit[ch].count = 0x10000;
                   19853:                pit[ch].ctrl_reg = 0x34;
                   19854:                pit[ch].mode = 3;
                   19855:        }
                   19856:        
                   19857:        // from bochs bios
                   19858:        pit_write(3, 0x34);
                   19859:        pit_write(0, 0x00);
                   19860:        pit_write(0, 0x00);
                   19861: }
                   19862: 
                   19863: void pit_write(int ch, UINT8 val)
                   19864: {
1.1.1.8   root     19865: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19866:        if(!pit_active) {
                   19867:                pit_active = 1;
                   19868:                pit_init();
                   19869:        }
1.1.1.8   root     19870: #endif
1.1       root     19871:        switch(ch) {
                   19872:        case 0:
                   19873:        case 1:
                   19874:        case 2:
                   19875:                // write count register
                   19876:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19877:                        if(pit[ch].ctrl_reg & 0x10) {
                   19878:                                pit[ch].low_write = 1;
                   19879:                        }
                   19880:                        if(pit[ch].ctrl_reg & 0x20) {
                   19881:                                pit[ch].high_write = 1;
                   19882:                        }
                   19883:                }
                   19884:                if(pit[ch].low_write) {
                   19885:                        pit[ch].count_reg = val;
                   19886:                        pit[ch].low_write = 0;
                   19887:                } else if(pit[ch].high_write) {
                   19888:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   19889:                                pit[ch].count_reg = val << 8;
                   19890:                        } else {
                   19891:                                pit[ch].count_reg |= val << 8;
                   19892:                        }
                   19893:                        pit[ch].high_write = 0;
                   19894:                }
                   19895:                // start count
1.1.1.8   root     19896:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   19897:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   19898:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   19899:                                pit[ch].prev_time = timeGetTime();
                   19900:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19901:                        }
                   19902:                }
                   19903:                break;
                   19904:        case 3: // ctrl reg
                   19905:                if((val & 0xc0) == 0xc0) {
                   19906:                        // i8254 read-back command
                   19907:                        for(ch = 0; ch < 3; ch++) {
                   19908:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   19909:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   19910:                                        pit[ch].status_latched = 1;
                   19911:                                }
                   19912:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   19913:                                        pit_latch_count(ch);
                   19914:                                }
                   19915:                        }
                   19916:                        break;
                   19917:                }
                   19918:                ch = (val >> 6) & 3;
                   19919:                if(val & 0x30) {
1.1.1.35  root     19920:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     19921:                        pit[ch].mode = modes[(val >> 1) & 7];
                   19922:                        pit[ch].count_latched = 0;
                   19923:                        pit[ch].low_read = pit[ch].high_read = 0;
                   19924:                        pit[ch].low_write = pit[ch].high_write = 0;
                   19925:                        pit[ch].ctrl_reg = val;
                   19926:                        // stop count
1.1.1.8   root     19927:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     19928:                        pit[ch].count_reg = 0;
                   19929:                } else if(!pit[ch].count_latched) {
                   19930:                        pit_latch_count(ch);
                   19931:                }
                   19932:                break;
                   19933:        }
                   19934: }
                   19935: 
                   19936: UINT8 pit_read(int ch)
                   19937: {
1.1.1.8   root     19938: #ifndef PIT_ALWAYS_RUNNING
1.1       root     19939:        if(!pit_active) {
                   19940:                pit_active = 1;
                   19941:                pit_init();
                   19942:        }
1.1.1.8   root     19943: #endif
1.1       root     19944:        switch(ch) {
                   19945:        case 0:
                   19946:        case 1:
                   19947:        case 2:
                   19948:                if(pit[ch].status_latched) {
                   19949:                        pit[ch].status_latched = 0;
                   19950:                        return(pit[ch].status);
                   19951:                }
                   19952:                // if not latched, through current count
                   19953:                if(!pit[ch].count_latched) {
                   19954:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   19955:                                pit_latch_count(ch);
                   19956:                        }
                   19957:                }
                   19958:                // return latched count
                   19959:                if(pit[ch].low_read) {
                   19960:                        pit[ch].low_read = 0;
                   19961:                        if(!pit[ch].high_read) {
                   19962:                                pit[ch].count_latched = 0;
                   19963:                        }
                   19964:                        return(pit[ch].latch & 0xff);
                   19965:                } else if(pit[ch].high_read) {
                   19966:                        pit[ch].high_read = 0;
                   19967:                        pit[ch].count_latched = 0;
                   19968:                        return((pit[ch].latch >> 8) & 0xff);
                   19969:                }
                   19970:        }
                   19971:        return(0xff);
                   19972: }
                   19973: 
1.1.1.8   root     19974: int pit_run(int ch, UINT32 cur_time)
1.1       root     19975: {
1.1.1.8   root     19976:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     19977:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     19978:                pit[ch].prev_time = pit[ch].expired_time;
                   19979:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   19980:                if(cur_time >= pit[ch].expired_time) {
                   19981:                        pit[ch].prev_time = cur_time;
                   19982:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     19983:                }
1.1.1.8   root     19984:                return(1);
1.1       root     19985:        }
1.1.1.8   root     19986:        return(0);
1.1       root     19987: }
                   19988: 
                   19989: void pit_latch_count(int ch)
                   19990: {
1.1.1.8   root     19991:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     19992:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     19993:                pit_run(ch, cur_time);
                   19994:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     19995:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   19996:                
                   19997:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   19998:                        // decrement counter in 1msec period
                   19999:                        if(pit[ch].next_latch == 0) {
                   20000:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   20001:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   20002:                        }
                   20003:                        if(pit[ch].latch > pit[ch].next_latch) {
                   20004:                                pit[ch].latch--;
                   20005:                        }
                   20006:                } else {
                   20007:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   20008:                        pit[ch].next_latch = 0;
                   20009:                }
1.1.1.8   root     20010:        } else {
                   20011:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     20012:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     20013:        }
                   20014:        pit[ch].count_latched = 1;
                   20015:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   20016:                // lower byte
                   20017:                pit[ch].low_read = 1;
                   20018:                pit[ch].high_read = 0;
                   20019:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   20020:                // upper byte
                   20021:                pit[ch].low_read = 0;
                   20022:                pit[ch].high_read = 1;
                   20023:        } else {
                   20024:                // lower -> upper
1.1.1.14  root     20025:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     20026:        }
                   20027: }
                   20028: 
1.1.1.8   root     20029: int pit_get_expired_time(int ch)
1.1       root     20030: {
1.1.1.22  root     20031:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   20032:        UINT64 val = pit[ch].accum >> 10;
                   20033:        pit[ch].accum -= val << 10;
                   20034:        return((val != 0) ? val : 1);
1.1.1.8   root     20035: }
                   20036: 
1.1.1.25  root     20037: // sio
                   20038: 
                   20039: void sio_init()
                   20040: {
1.1.1.26  root     20041:        memset(sio, 0, sizeof(sio));
                   20042:        memset(sio_mt, 0, sizeof(sio_mt));
                   20043:        
1.1.1.29  root     20044:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     20045:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   20046:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   20047:                
                   20048:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   20049:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     20050:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   20051:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     20052:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   20053:                sio[c].irq_identify = 0x01;     // no pending irq
                   20054:                
                   20055:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   20056:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   20057:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   20058:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   20059:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   20060:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   20061:                
1.1.1.26  root     20062:                if(sio_port_number[c] != 0) {
1.1.1.25  root     20063:                        sio[c].channel = c;
                   20064:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   20065:                }
                   20066:        }
                   20067: }
                   20068: 
                   20069: void sio_finish()
                   20070: {
1.1.1.29  root     20071:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     20072:                if(sio_mt[c].hThread != NULL) {
                   20073:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   20074:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     20075:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     20076:                }
                   20077:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   20078:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   20079:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   20080:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   20081:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   20082:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     20083:        }
                   20084:        sio_release();
                   20085: }
                   20086: 
                   20087: void sio_release()
                   20088: {
1.1.1.29  root     20089:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     20090:                // sio_thread() may access the resources :-(
1.1.1.32  root     20091:                bool running = (sio_mt[c].hThread != NULL);
                   20092:                
                   20093:                if(running) {
                   20094:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   20095:                }
                   20096:                if(sio[c].send_buffer != NULL) {
                   20097:                        sio[c].send_buffer->release();
                   20098:                        delete sio[c].send_buffer;
                   20099:                        sio[c].send_buffer = NULL;
                   20100:                }
                   20101:                if(running) {
                   20102:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   20103:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   20104:                }
                   20105:                if(sio[c].recv_buffer != NULL) {
                   20106:                        sio[c].recv_buffer->release();
                   20107:                        delete sio[c].recv_buffer;
                   20108:                        sio[c].recv_buffer = NULL;
                   20109:                }
                   20110:                if(running) {
                   20111:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     20112:                }
1.1.1.25  root     20113:        }
                   20114: }
                   20115: 
                   20116: void sio_write(int c, UINT32 addr, UINT8 data)
                   20117: {
                   20118:        switch(addr & 7) {
                   20119:        case 0:
                   20120:                if(sio[c].selector & 0x80) {
                   20121:                        if(sio[c].divisor.b.l != data) {
                   20122:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20123:                                sio[c].divisor.b.l = data;
                   20124:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20125:                        }
                   20126:                } else {
                   20127:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20128:                        if(sio[c].send_buffer != NULL) {
                   20129:                                sio[c].send_buffer->write(data);
                   20130:                        }
1.1.1.25  root     20131:                        // transmitter holding/shift registers are not empty
                   20132:                        sio[c].line_stat_buf &= ~0x60;
                   20133:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   20134:                        
                   20135:                        if(sio[c].irq_enable & 0x02) {
                   20136:                                sio_update_irq(c);
                   20137:                        }
                   20138:                }
                   20139:                break;
                   20140:        case 1:
                   20141:                if(sio[c].selector & 0x80) {
                   20142:                        if(sio[c].divisor.b.h != data) {
                   20143:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20144:                                sio[c].divisor.b.h = data;
                   20145:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20146:                        }
                   20147:                } else {
                   20148:                        if(sio[c].irq_enable != data) {
                   20149:                                sio[c].irq_enable = data;
                   20150:                                sio_update_irq(c);
                   20151:                        }
                   20152:                }
                   20153:                break;
                   20154:        case 3:
                   20155:                {
                   20156:                        UINT8 line_ctrl = data & 0x3f;
                   20157:                        bool set_brk = ((data & 0x40) != 0);
                   20158:                        
                   20159:                        if(sio[c].line_ctrl != line_ctrl) {
                   20160:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   20161:                                sio[c].line_ctrl = line_ctrl;
                   20162:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   20163:                        }
                   20164:                        if(sio[c].set_brk != set_brk) {
                   20165:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   20166:                                sio[c].set_brk = set_brk;
                   20167:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   20168:                        }
                   20169:                }
                   20170:                sio[c].selector = data;
                   20171:                break;
                   20172:        case 4:
                   20173:                {
                   20174:                        bool set_dtr = ((data & 0x01) != 0);
                   20175:                        bool set_rts = ((data & 0x02) != 0);
                   20176:                        
                   20177:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     20178: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     20179:                                sio[c].set_dtr = set_dtr;
                   20180:                                sio[c].set_rts = set_rts;
1.1.1.26  root     20181: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   20182:                                
                   20183:                                bool state_changed = false;
                   20184:                                
                   20185:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20186:                                if(set_dtr) {
                   20187:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   20188:                                } else {
                   20189:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   20190:                                }
                   20191:                                if(set_rts) {
                   20192:                                        sio[c].modem_stat |= 0x10;      // cts on
                   20193:                                } else {
                   20194:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   20195:                                }
                   20196:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   20197:                                        if(!(sio[c].modem_stat & 0x02)) {
                   20198:                                                if(sio[c].irq_enable & 0x08) {
                   20199:                                                        state_changed = true;
                   20200:                                                }
                   20201:                                                sio[c].modem_stat |= 0x02;
                   20202:                                        }
                   20203:                                }
                   20204:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   20205:                                        if(!(sio[c].modem_stat & 0x01)) {
                   20206:                                                if(sio[c].irq_enable & 0x08) {
                   20207:                                                        state_changed = true;
                   20208:                                                }
                   20209:                                                sio[c].modem_stat |= 0x01;
                   20210:                                        }
                   20211:                                }
                   20212:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   20213:                                
                   20214:                                if(state_changed) {
                   20215:                                        sio_update_irq(c);
                   20216:                                }
1.1.1.25  root     20217:                        }
                   20218:                }
                   20219:                sio[c].modem_ctrl = data;
                   20220:                break;
                   20221:        case 7:
                   20222:                sio[c].scratch = data;
                   20223:                break;
                   20224:        }
                   20225: }
                   20226: 
                   20227: UINT8 sio_read(int c, UINT32 addr)
                   20228: {
                   20229:        switch(addr & 7) {
                   20230:        case 0:
                   20231:                if(sio[c].selector & 0x80) {
                   20232:                        return(sio[c].divisor.b.l);
                   20233:                } else {
                   20234:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20235:                        UINT8 data = 0;
                   20236:                        if(sio[c].recv_buffer != NULL) {
                   20237:                                data = sio[c].recv_buffer->read();
                   20238:                        }
1.1.1.25  root     20239:                        // data is not ready
                   20240:                        sio[c].line_stat_buf &= ~0x01;
                   20241:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20242:                        
                   20243:                        if(sio[c].irq_enable & 0x01) {
                   20244:                                sio_update_irq(c);
                   20245:                        }
                   20246:                        return(data);
                   20247:                }
                   20248:        case 1:
                   20249:                if(sio[c].selector & 0x80) {
                   20250:                        return(sio[c].divisor.b.h);
                   20251:                } else {
                   20252:                        return(sio[c].irq_enable);
                   20253:                }
                   20254:        case 2:
                   20255:                return(sio[c].irq_identify);
                   20256:        case 3:
                   20257:                return(sio[c].selector);
                   20258:        case 4:
                   20259:                return(sio[c].modem_ctrl);
                   20260:        case 5:
                   20261:                {
                   20262:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   20263:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   20264:                        sio[c].line_stat_err = 0x00;
                   20265:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   20266:                        
                   20267:                        bool state_changed = false;
                   20268:                        
                   20269:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   20270:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20271:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     20272:                                        // transmitter holding register will be empty first
                   20273:                                        if(sio[c].irq_enable & 0x02) {
                   20274:                                                state_changed = true;
                   20275:                                        }
                   20276:                                        sio[c].line_stat_buf |= 0x20;
                   20277:                                }
                   20278:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   20279:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   20280:                                // transmitter shift register will be empty later
                   20281:                                sio[c].line_stat_buf |= 0x40;
                   20282:                        }
                   20283:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   20284:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20285:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     20286:                                        // data is ready
                   20287:                                        if(sio[c].irq_enable & 0x01) {
                   20288:                                                state_changed = true;
                   20289:                                        }
                   20290:                                        sio[c].line_stat_buf |= 0x01;
                   20291:                                }
                   20292:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20293:                        }
                   20294:                        if(state_changed) {
                   20295:                                sio_update_irq(c);
                   20296:                        }
                   20297:                        return(val);
                   20298:                }
                   20299:        case 6:
                   20300:                {
                   20301:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   20302:                        UINT8 val = sio[c].modem_stat;
                   20303:                        sio[c].modem_stat &= 0xf0;
                   20304:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   20305:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   20306:                        
                   20307:                        if(sio[c].modem_ctrl & 0x10) {
                   20308:                                // loop-back
                   20309:                                val &= 0x0f;
                   20310:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   20311:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   20312:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   20313:                        }
                   20314:                        return(val);
                   20315:                }
                   20316:        case 7:
                   20317:                return(sio[c].scratch);
                   20318:        }
                   20319:        return(0xff);
                   20320: }
                   20321: 
                   20322: void sio_update(int c)
                   20323: {
                   20324:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   20325:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     20326:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     20327:                        // transmitter holding/shift registers will be empty
                   20328:                        sio[c].line_stat_buf |= 0x60;
                   20329:                }
                   20330:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   20331:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   20332:                // transmitter shift register will be empty
                   20333:                sio[c].line_stat_buf |= 0x40;
                   20334:        }
                   20335:        if(!(sio[c].line_stat_buf & 0x01)) {
                   20336:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     20337:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     20338:                        // data is ready
                   20339:                        sio[c].line_stat_buf |= 0x01;
                   20340:                }
                   20341:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   20342:        }
                   20343:        sio_update_irq(c);
                   20344: }
                   20345: 
                   20346: void sio_update_irq(int c)
                   20347: {
                   20348:        int level = -1;
                   20349:        
                   20350:        if(sio[c].irq_enable & 0x08) {
                   20351:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20352:                if((sio[c].modem_stat & 0x0f) != 0) {
                   20353:                        level = 0;
                   20354:                }
                   20355:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   20356:        }
                   20357:        if(sio[c].irq_enable & 0x02) {
                   20358:                if(sio[c].line_stat_buf & 0x20) {
                   20359:                        level = 1;
                   20360:                }
                   20361:        }
                   20362:        if(sio[c].irq_enable & 0x01) {
                   20363:                if(sio[c].line_stat_buf & 0x01) {
                   20364:                        level = 2;
                   20365:                }
                   20366:        }
                   20367:        if(sio[c].irq_enable & 0x04) {
                   20368:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   20369:                if(sio[c].line_stat_err != 0) {
                   20370:                        level = 3;
                   20371:                }
                   20372:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   20373:        }
1.1.1.29  root     20374:        
                   20375:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     20376:        if(level != -1) {
                   20377:                sio[c].irq_identify = level << 1;
1.1.1.29  root     20378:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     20379:        } else {
                   20380:                sio[c].irq_identify = 1;
1.1.1.29  root     20381:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     20382:        }
                   20383: }
                   20384: 
                   20385: DWORD WINAPI sio_thread(void *lpx)
                   20386: {
                   20387:        volatile sio_t *p = (sio_t *)lpx;
                   20388:        sio_mt_t *q = &sio_mt[p->channel];
                   20389:        
                   20390:        char name[] = "COM1";
1.1.1.26  root     20391:        name[3] = '0' + sio_port_number[p->channel];
                   20392:        HANDLE hComm = NULL;
                   20393:        COMMPROP commProp;
                   20394:        DCB dcb;
                   20395:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   20396:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   20397:        
1.1.1.60  root     20398:        if((hComm = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
1.1.1.26  root     20399:                if(GetCommProperties(hComm, &commProp)) {
                   20400:                        dwSettableBaud = commProp.dwSettableBaud;
                   20401:                }
1.1.1.25  root     20402:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     20403: //             EscapeCommFunction(hComm, SETRTS);
                   20404: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     20405:                
1.1.1.54  root     20406:                while(!m_exit) {
1.1.1.25  root     20407:                        // setup comm port
                   20408:                        bool comm_state_changed = false;
                   20409:                        
                   20410:                        EnterCriticalSection(&q->csLineCtrl);
                   20411:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   20412:                                p->prev_divisor = p->divisor.w;
                   20413:                                p->prev_line_ctrl = p->line_ctrl;
                   20414:                                comm_state_changed = true;
                   20415:                        }
                   20416:                        LeaveCriticalSection(&q->csLineCtrl);
                   20417:                        
                   20418:                        if(comm_state_changed) {
1.1.1.26  root     20419:                                if(GetCommState(hComm, &dcb)) {
                   20420: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   20421:                                        DWORD baud = 115200 / p->prev_divisor;
                   20422:                                        dcb.BaudRate = 9600; // default
                   20423:                                        
                   20424:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   20425:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   20426:                                        // 134.5bps is not supported ???
                   20427: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   20428:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   20429:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   20430:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   20431:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   20432:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   20433:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   20434:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   20435:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   20436:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   20437: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   20438: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   20439: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   20440:                                        
                   20441:                                        switch(p->prev_line_ctrl & 0x03) {
                   20442:                                        case 0x00: dcb.ByteSize = 5; break;
                   20443:                                        case 0x01: dcb.ByteSize = 6; break;
                   20444:                                        case 0x02: dcb.ByteSize = 7; break;
                   20445:                                        case 0x03: dcb.ByteSize = 8; break;
                   20446:                                        }
                   20447:                                        switch(p->prev_line_ctrl & 0x04) {
                   20448:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   20449:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   20450:                                        }
                   20451:                                        switch(p->prev_line_ctrl & 0x38) {
                   20452:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   20453:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   20454:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   20455:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   20456:                                        default:   dcb.Parity = NOPARITY;    break;
                   20457:                                        }
                   20458:                                        dcb.fBinary = TRUE;
                   20459:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   20460:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   20461:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   20462:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   20463:                                        dcb.fTXContinueOnXoff = TRUE;
                   20464:                                        dcb.fOutX = dcb.fInX = FALSE;
                   20465:                                        dcb.fErrorChar = FALSE;
                   20466:                                        dcb.fNull = FALSE;
                   20467:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   20468:                                        dcb.fAbortOnError = FALSE;
                   20469:                                        
                   20470:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     20471:                                }
                   20472:                                
                   20473:                                // check again to apply all comm state changes
                   20474:                                Sleep(10);
                   20475:                                continue;
                   20476:                        }
                   20477:                        
                   20478:                        // set comm pins
                   20479:                        bool change_brk = false;
1.1.1.26  root     20480: //                     bool change_rts = false;
                   20481: //                     bool change_dtr = false;
1.1.1.25  root     20482:                        
                   20483:                        EnterCriticalSection(&q->csModemCtrl);
                   20484:                        if(p->prev_set_brk != p->set_brk) {
                   20485:                                p->prev_set_brk = p->set_brk;
                   20486:                                change_brk = true;
                   20487:                        }
1.1.1.26  root     20488: //                     if(p->prev_set_rts != p->set_rts) {
                   20489: //                             p->prev_set_rts = p->set_rts;
                   20490: //                             change_rts = true;
                   20491: //                     }
                   20492: //                     if(p->prev_set_dtr != p->set_dtr) {
                   20493: //                             p->prev_set_dtr = p->set_dtr;
                   20494: //                             change_dtr = true;
                   20495: //                     }
1.1.1.25  root     20496:                        LeaveCriticalSection(&q->csModemCtrl);
                   20497:                        
                   20498:                        if(change_brk) {
1.1.1.26  root     20499:                                static UINT32 clear_time = 0;
                   20500:                                if(p->prev_set_brk) {
                   20501:                                        EscapeCommFunction(hComm, SETBREAK);
                   20502:                                        clear_time = timeGetTime() + 200;
                   20503:                                } else {
                   20504:                                        // keep break for at least 200msec
                   20505:                                        UINT32 cur_time = timeGetTime();
                   20506:                                        if(clear_time > cur_time) {
                   20507:                                                Sleep(clear_time - cur_time);
                   20508:                                        }
                   20509:                                        EscapeCommFunction(hComm, CLRBREAK);
                   20510:                                }
1.1.1.25  root     20511:                        }
1.1.1.26  root     20512: //                     if(change_rts) {
                   20513: //                             if(p->prev_set_rts) {
                   20514: //                                     EscapeCommFunction(hComm, SETRTS);
                   20515: //                             } else {
                   20516: //                                     EscapeCommFunction(hComm, CLRRTS);
                   20517: //                             }
                   20518: //                     }
                   20519: //                     if(change_dtr) {
                   20520: //                             if(p->prev_set_dtr) {
                   20521: //                                     EscapeCommFunction(hComm, SETDTR);
                   20522: //                             } else {
                   20523: //                                     EscapeCommFunction(hComm, CLRDTR);
                   20524: //                             }
                   20525: //                     }
1.1.1.25  root     20526:                        
                   20527:                        // get comm pins
                   20528:                        DWORD dwModemStat = 0;
                   20529:                        
                   20530:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   20531:                                EnterCriticalSection(&q->csModemStat);
                   20532:                                if(dwModemStat & MS_RLSD_ON) {
                   20533:                                        p->modem_stat |= 0x80;
                   20534:                                } else {
                   20535:                                        p->modem_stat &= ~0x80;
                   20536:                                }
                   20537:                                if(dwModemStat & MS_RING_ON) {
                   20538:                                        p->modem_stat |= 0x40;
                   20539:                                } else {
                   20540:                                        p->modem_stat &= ~0x40;
                   20541:                                }
1.1.1.26  root     20542: //                             if(dwModemStat & MS_DSR_ON) {
                   20543: //                                     p->modem_stat |= 0x20;
                   20544: //                             } else {
                   20545: //                                     p->modem_stat &= ~0x20;
                   20546: //                             }
                   20547: //                             if(dwModemStat & MS_CTS_ON) {
                   20548: //                                     p->modem_stat |= 0x10;
                   20549: //                             } else {
                   20550: //                                     p->modem_stat &= ~0x10;
                   20551: //                             }
1.1.1.25  root     20552:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   20553:                                        p->modem_stat |= 0x08;
                   20554:                                }
                   20555:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   20556:                                        p->modem_stat |= 0x04;
                   20557:                                }
1.1.1.26  root     20558: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   20559: //                                     p->modem_stat |= 0x02;
                   20560: //                             }
                   20561: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   20562: //                                     p->modem_stat |= 0x01;
                   20563: //                             }
1.1.1.25  root     20564:                                LeaveCriticalSection(&q->csModemStat);
                   20565:                        }
                   20566:                        
                   20567:                        // send data
                   20568:                        DWORD dwSend = 0;
                   20569:                        
                   20570:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     20571:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     20572:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   20573:                        }
                   20574:                        LeaveCriticalSection(&q->csSendData);
                   20575:                        
                   20576:                        if(dwSend != 0) {
                   20577:                                DWORD dwWritten = 0;
                   20578:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   20579:                        }
                   20580:                        
                   20581:                        // get line status and recv data
                   20582:                        DWORD dwLineStat = 0;
                   20583:                        COMSTAT comStat;
                   20584:                        
                   20585:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   20586:                                EnterCriticalSection(&q->csLineStat);
                   20587:                                if(dwLineStat & CE_BREAK) {
                   20588:                                        p->line_stat_err |= 0x10;
                   20589:                                }
                   20590:                                if(dwLineStat & CE_FRAME) {
                   20591:                                        p->line_stat_err |= 0x08;
                   20592:                                }
                   20593:                                if(dwLineStat & CE_RXPARITY) {
                   20594:                                        p->line_stat_err |= 0x04;
                   20595:                                }
                   20596:                                if(dwLineStat & CE_OVERRUN) {
                   20597:                                        p->line_stat_err |= 0x02;
                   20598:                                }
                   20599:                                LeaveCriticalSection(&q->csLineStat);
                   20600:                                
                   20601:                                if(comStat.cbInQue != 0) {
                   20602:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20603:                                        DWORD dwRecv = 0;
                   20604:                                        if(p->recv_buffer != NULL) {
                   20605:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   20606:                                        }
1.1.1.25  root     20607:                                        LeaveCriticalSection(&q->csRecvData);
                   20608:                                        
                   20609:                                        if(dwRecv != 0) {
                   20610:                                                DWORD dwRead = 0;
                   20611:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   20612:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     20613:                                                        if(p->recv_buffer != NULL) {
                   20614:                                                                for(int i = 0; i < dwRead; i++) {
                   20615:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   20616:                                                                }
1.1.1.25  root     20617:                                                        }
                   20618:                                                        LeaveCriticalSection(&q->csRecvData);
                   20619:                                                }
                   20620:                                        }
                   20621:                                }
                   20622:                        }
                   20623:                        Sleep(10);
                   20624:                }
                   20625:                CloseHandle(hComm);
                   20626:        }
                   20627:        return 0;
                   20628: }
                   20629: 
1.1.1.8   root     20630: // cmos
                   20631: 
                   20632: void cmos_init()
                   20633: {
                   20634:        memset(cmos, 0, sizeof(cmos));
                   20635:        cmos_addr = 0;
1.1       root     20636:        
1.1.1.8   root     20637:        // from DOSBox
                   20638:        cmos_write(0x0a, 0x26);
                   20639:        cmos_write(0x0b, 0x02);
                   20640:        cmos_write(0x0d, 0x80);
1.1       root     20641: }
                   20642: 
1.1.1.8   root     20643: void cmos_write(int addr, UINT8 val)
1.1       root     20644: {
1.1.1.8   root     20645:        cmos[addr & 0x7f] = val;
                   20646: }
                   20647: 
                   20648: #define CMOS_GET_TIME() { \
                   20649:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   20650:        if(prev_sec != cur_sec) { \
                   20651:                GetLocalTime(&time); \
                   20652:                prev_sec = cur_sec; \
                   20653:        } \
1.1       root     20654: }
1.1.1.8   root     20655: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     20656: 
1.1.1.8   root     20657: UINT8 cmos_read(int addr)
1.1       root     20658: {
1.1.1.8   root     20659:        static SYSTEMTIME time;
                   20660:        static UINT32 prev_sec = 0;
1.1       root     20661:        
1.1.1.8   root     20662:        switch(addr & 0x7f) {
                   20663:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   20664:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   20665:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   20666:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   20667:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   20668:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   20669:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   20670: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   20671:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   20672:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   20673:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   20674:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20675:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20676:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   20677:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   20678:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     20679:        }
1.1.1.8   root     20680:        return(cmos[addr & 0x7f]);
1.1       root     20681: }
                   20682: 
1.1.1.7   root     20683: // kbd (a20)
                   20684: 
                   20685: void kbd_init()
                   20686: {
1.1.1.8   root     20687:        kbd_data = kbd_command = 0;
1.1.1.7   root     20688:        kbd_status = 0x18;
                   20689: }
                   20690: 
                   20691: UINT8 kbd_read_data()
                   20692: {
1.1.1.57  root     20693:        UINT8 data = kbd_data;
                   20694:        kbd_data = 0;
1.1.1.8   root     20695:        kbd_status &= ~1;
1.1.1.57  root     20696:        return(data);
1.1.1.7   root     20697: }
                   20698: 
                   20699: void kbd_write_data(UINT8 val)
                   20700: {
                   20701:        switch(kbd_command) {
                   20702:        case 0xd1:
                   20703:                i386_set_a20_line((val >> 1) & 1);
                   20704:                break;
                   20705:        }
                   20706:        kbd_command = 0;
1.1.1.8   root     20707:        kbd_status &= ~8;
1.1.1.7   root     20708: }
                   20709: 
                   20710: UINT8 kbd_read_status()
                   20711: {
                   20712:        return(kbd_status);
                   20713: }
                   20714: 
                   20715: void kbd_write_command(UINT8 val)
                   20716: {
                   20717:        switch(val) {
                   20718:        case 0xd0:
                   20719:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     20720:                kbd_status |= 1;
1.1.1.7   root     20721:                break;
                   20722:        case 0xdd:
                   20723:                i386_set_a20_line(0);
                   20724:                break;
                   20725:        case 0xdf:
                   20726:                i386_set_a20_line(1);
                   20727:                break;
1.1.1.26  root     20728:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   20729:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     20730:                if(!(val & 1)) {
1.1.1.8   root     20731:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     20732:                                // reset pic
                   20733:                                pic_init();
                   20734:                                pic[0].irr = pic[1].irr = 0x00;
                   20735:                                pic[0].imr = pic[1].imr = 0xff;
                   20736:                        }
                   20737:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     20738:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   20739:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   20740:                        i386_jmp_far(selector, address);
1.1.1.7   root     20741:                }
                   20742:                i386_set_a20_line((val >> 1) & 1);
                   20743:                break;
                   20744:        }
                   20745:        kbd_command = val;
1.1.1.8   root     20746:        kbd_status |= 8;
1.1.1.7   root     20747: }
                   20748: 
1.1.1.9   root     20749: // vga
                   20750: 
                   20751: UINT8 vga_read_status()
                   20752: {
                   20753:        // 60hz
                   20754:        static const int period[3] = {16, 17, 17};
                   20755:        static int index = 0;
                   20756:        UINT32 time = timeGetTime() % period[index];
                   20757:        
                   20758:        index = (index + 1) % 3;
1.1.1.14  root     20759:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     20760: }
                   20761: 
1.1       root     20762: // i/o bus
                   20763: 
1.1.1.29  root     20764: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   20765: //#define SW1US_PATCH
                   20766: 
1.1.1.25  root     20767: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     20768: #ifdef USE_DEBUGGER
1.1.1.25  root     20769: {
1.1.1.33  root     20770:        if(now_debugging) {
                   20771:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20772:                        if(in_break_point.table[i].status == 1) {
                   20773:                                if(addr == in_break_point.table[i].addr) {
                   20774:                                        in_break_point.hit = i + 1;
                   20775:                                        now_suspended = true;
                   20776:                                        break;
                   20777:                                }
                   20778:                        }
                   20779:                }
1.1.1.25  root     20780:        }
1.1.1.33  root     20781:        return(debugger_read_io_byte(addr));
1.1.1.25  root     20782: }
1.1.1.33  root     20783: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     20784: #endif
1.1       root     20785: {
1.1.1.33  root     20786:        UINT8 val = 0xff;
                   20787:        
1.1       root     20788:        switch(addr) {
1.1.1.29  root     20789: #ifdef SW1US_PATCH
                   20790:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20791:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     20792:                val = sio_read(0, addr - 1);
                   20793:                break;
1.1.1.29  root     20794: #else
1.1.1.25  root     20795:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20796:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     20797:                val = dma_read(0, addr);
                   20798:                break;
1.1.1.29  root     20799: #endif
1.1.1.25  root     20800:        case 0x20: case 0x21:
1.1.1.33  root     20801:                val = pic_read(0, addr);
                   20802:                break;
1.1.1.25  root     20803:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     20804:                val = pit_read(addr & 0x03);
                   20805:                break;
1.1.1.7   root     20806:        case 0x60:
1.1.1.33  root     20807:                val = kbd_read_data();
                   20808:                break;
1.1.1.9   root     20809:        case 0x61:
1.1.1.33  root     20810:                val = system_port;
                   20811:                break;
1.1.1.7   root     20812:        case 0x64:
1.1.1.33  root     20813:                val = kbd_read_status();
                   20814:                break;
1.1       root     20815:        case 0x71:
1.1.1.33  root     20816:                val = cmos_read(cmos_addr);
                   20817:                break;
1.1.1.25  root     20818:        case 0x81:
1.1.1.33  root     20819:                val = dma_page_read(0, 2);
                   20820:                break;
1.1.1.25  root     20821:        case 0x82:
1.1.1.33  root     20822:                val = dma_page_read(0, 3);
                   20823:                break;
1.1.1.25  root     20824:        case 0x83:
1.1.1.33  root     20825:                val = dma_page_read(0, 1);
                   20826:                break;
1.1.1.25  root     20827:        case 0x87:
1.1.1.33  root     20828:                val = dma_page_read(0, 0);
                   20829:                break;
1.1.1.25  root     20830:        case 0x89:
1.1.1.33  root     20831:                val = dma_page_read(1, 2);
                   20832:                break;
1.1.1.25  root     20833:        case 0x8a:
1.1.1.33  root     20834:                val = dma_page_read(1, 3);
                   20835:                break;
1.1.1.25  root     20836:        case 0x8b:
1.1.1.33  root     20837:                val = dma_page_read(1, 1);
                   20838:                break;
1.1.1.25  root     20839:        case 0x8f:
1.1.1.33  root     20840:                val = dma_page_read(1, 0);
                   20841:                break;
1.1       root     20842:        case 0x92:
1.1.1.33  root     20843:                val = (m_a20_mask >> 19) & 2;
                   20844:                break;
1.1.1.25  root     20845:        case 0xa0: case 0xa1:
1.1.1.33  root     20846:                val = pic_read(1, addr);
                   20847:                break;
1.1.1.25  root     20848:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   20849:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     20850:                val = dma_read(1, (addr - 0xc0) >> 1);
                   20851:                break;
1.1.1.37  root     20852:        case 0x278: case 0x279: case 0x27a:
                   20853:                val = pio_read(1, addr);
                   20854:                break;
1.1.1.29  root     20855:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     20856:                val = sio_read(3, addr);
                   20857:                break;
1.1.1.25  root     20858:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     20859:                val = sio_read(1, addr);
                   20860:                break;
1.1.1.25  root     20861:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     20862:                val = pio_read(0, addr);
                   20863:                break;
1.1.1.25  root     20864:        case 0x3ba: case 0x3da:
1.1.1.33  root     20865:                val = vga_read_status();
                   20866:                break;
1.1.1.37  root     20867:        case 0x3bc: case 0x3bd: case 0x3be:
                   20868:                val = pio_read(2, addr);
                   20869:                break;
1.1.1.58  root     20870:        case 0x3d5:
                   20871:                if(crtc_addr < 16) {
                   20872:                        val = crtc_regs[crtc_addr];
                   20873:                }
                   20874:                break;
1.1.1.29  root     20875:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     20876:                val = sio_read(2, addr);
                   20877:                break;
1.1.1.25  root     20878:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     20879:                val = sio_read(0, addr);
                   20880:                break;
1.1       root     20881:        default:
1.1.1.33  root     20882: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     20883:                break;
                   20884:        }
1.1.1.33  root     20885: #ifdef ENABLE_DEBUG_IOPORT
                   20886:        if(fp_debug_log != NULL) {
                   20887:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   20888:        }
                   20889: #endif
                   20890:        return(val);
1.1       root     20891: }
                   20892: 
                   20893: UINT16 read_io_word(offs_t addr)
                   20894: {
                   20895:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   20896: }
                   20897: 
1.1.1.33  root     20898: #ifdef USE_DEBUGGER
                   20899: UINT16 debugger_read_io_word(offs_t addr)
                   20900: {
                   20901:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   20902: }
                   20903: #endif
                   20904: 
1.1       root     20905: UINT32 read_io_dword(offs_t addr)
                   20906: {
                   20907:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   20908: }
                   20909: 
1.1.1.33  root     20910: #ifdef USE_DEBUGGER
                   20911: UINT32 debugger_read_io_dword(offs_t addr)
                   20912: {
                   20913:        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));
                   20914: }
                   20915: #endif
                   20916: 
1.1       root     20917: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     20918: #ifdef USE_DEBUGGER
                   20919: {
                   20920:        if(now_debugging) {
                   20921:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   20922:                        if(out_break_point.table[i].status == 1) {
                   20923:                                if(addr == out_break_point.table[i].addr) {
                   20924:                                        out_break_point.hit = i + 1;
                   20925:                                        now_suspended = true;
                   20926:                                        break;
                   20927:                                }
                   20928:                        }
                   20929:                }
                   20930:        }
                   20931:        debugger_write_io_byte(addr, val);
                   20932: }
                   20933: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   20934: #endif
1.1       root     20935: {
1.1.1.25  root     20936: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     20937:        if(fp_debug_log != NULL) {
1.1.1.43  root     20938: #ifdef USE_SERVICE_THREAD
                   20939:                if(addr != 0xf7)
                   20940: #endif
1.1.1.33  root     20941:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     20942:        }
                   20943: #endif
1.1       root     20944:        switch(addr) {
1.1.1.29  root     20945: #ifdef SW1US_PATCH
                   20946:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   20947:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   20948:                sio_write(0, addr - 1, val);
                   20949:                break;
                   20950: #else
1.1.1.25  root     20951:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   20952:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   20953:                dma_write(0, addr, val);
                   20954:                break;
1.1.1.29  root     20955: #endif
1.1.1.25  root     20956:        case 0x20: case 0x21:
1.1       root     20957:                pic_write(0, addr, val);
                   20958:                break;
1.1.1.25  root     20959:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     20960:                pit_write(addr & 0x03, val);
                   20961:                break;
1.1.1.7   root     20962:        case 0x60:
                   20963:                kbd_write_data(val);
                   20964:                break;
1.1.1.9   root     20965:        case 0x61:
                   20966:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   20967:                        // beep on
                   20968: //                     MessageBeep(-1);
                   20969:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   20970:                        // beep off
                   20971:                }
                   20972:                system_port = val;
                   20973:                break;
1.1       root     20974:        case 0x64:
1.1.1.7   root     20975:                kbd_write_command(val);
1.1       root     20976:                break;
                   20977:        case 0x70:
                   20978:                cmos_addr = val;
                   20979:                break;
                   20980:        case 0x71:
1.1.1.8   root     20981:                cmos_write(cmos_addr, val);
1.1       root     20982:                break;
1.1.1.25  root     20983:        case 0x81:
                   20984:                dma_page_write(0, 2, val);
                   20985:        case 0x82:
                   20986:                dma_page_write(0, 3, val);
                   20987:        case 0x83:
                   20988:                dma_page_write(0, 1, val);
                   20989:        case 0x87:
                   20990:                dma_page_write(0, 0, val);
                   20991:        case 0x89:
                   20992:                dma_page_write(1, 2, val);
                   20993:        case 0x8a:
                   20994:                dma_page_write(1, 3, val);
                   20995:        case 0x8b:
                   20996:                dma_page_write(1, 1, val);
                   20997:        case 0x8f:
                   20998:                dma_page_write(1, 0, val);
1.1       root     20999:        case 0x92:
1.1.1.7   root     21000:                i386_set_a20_line((val >> 1) & 1);
1.1       root     21001:                break;
1.1.1.25  root     21002:        case 0xa0: case 0xa1:
1.1       root     21003:                pic_write(1, addr, val);
                   21004:                break;
1.1.1.25  root     21005:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   21006:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     21007:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     21008:                break;
1.1.1.35  root     21009: #ifdef USE_SERVICE_THREAD
                   21010:        case 0xf7:
                   21011:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     21012:                if(in_service && cursor_moved) {
                   21013:                        // update cursor position before service is done
                   21014:                        pcbios_update_cursor_position();
                   21015:                        cursor_moved = false;
                   21016:                }
1.1.1.35  root     21017:                finish_service_loop();
                   21018:                break;
                   21019: #endif
1.1.1.37  root     21020:        case 0x278: case 0x279: case 0x27a:
                   21021:                pio_write(1, addr, val);
                   21022:                break;
1.1.1.29  root     21023:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   21024:                sio_write(3, addr, val);
                   21025:                break;
1.1.1.25  root     21026:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   21027:                sio_write(1, addr, val);
                   21028:                break;
                   21029:        case 0x378: case 0x379: case 0x37a:
                   21030:                pio_write(0, addr, val);
                   21031:                break;
1.1.1.37  root     21032:        case 0x3bc: case 0x3bd: case 0x3be:
                   21033:                pio_write(2, addr, val);
                   21034:                break;
1.1.1.58  root     21035:        case 0x3d4:
                   21036:                crtc_addr = val;
                   21037:                break;
                   21038:        case 0x3d5:
                   21039:                if(crtc_addr < 16) {
                   21040:                        if(crtc_regs[crtc_addr] != val) {
                   21041:                                crtc_regs[crtc_addr] = val;
                   21042:                                crtc_changed[crtc_addr] = 1;
                   21043:                        }
                   21044:                }
                   21045:                break;
1.1.1.29  root     21046:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   21047:                sio_write(2, addr, val);
                   21048:                break;
1.1.1.25  root     21049:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   21050:                sio_write(0, addr, val);
                   21051:                break;
1.1       root     21052:        default:
1.1.1.33  root     21053: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     21054:                break;
                   21055:        }
                   21056: }
                   21057: 
                   21058: void write_io_word(offs_t addr, UINT16 val)
                   21059: {
                   21060:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   21061:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   21062: }
                   21063: 
1.1.1.33  root     21064: #ifdef USE_DEBUGGER
                   21065: void debugger_write_io_word(offs_t addr, UINT16 val)
                   21066: {
                   21067:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   21068:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   21069: }
                   21070: #endif
                   21071: 
1.1       root     21072: void write_io_dword(offs_t addr, UINT32 val)
                   21073: {
                   21074:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   21075:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   21076:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   21077:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   21078: }
1.1.1.33  root     21079: 
                   21080: #ifdef USE_DEBUGGER
                   21081: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   21082: {
                   21083:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   21084:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   21085:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   21086:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   21087: }
                   21088: #endif

unix.superglobalmegacorp.com

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